Lines
76.47%
13 / 17
Functions and Methods
60.00%
6 / 10
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| BrokerHaEvents | 76.47% | 13 / 17 | 60.00% | 6 / 10 | 11.30 | 0.00% | 0 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| stateUpdated | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| eventFired | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | |||||
| triggerFired | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | |||||
| triggerRejected | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | |||||
| exposedEntitySynced | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | |||||
| exposedEntityCommanded | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | |||||
| connectionLost | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 1 | |||||
| reconnected | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 1 | |||||
| connectionFailed | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Runtime\Broker; | |
| 6 | ||
| 7 | use Stewart\Contracts\Event\HaEvent; | |
| 8 | use Stewart\Contracts\Time\Duration; | |
| 9 | use Stewart\Contracts\Time\Instant; | |
| 10 | use Stewart\Contracts\Trigger\TriggerEvent; | |
| 11 | use Stewart\Runtime\Broker\Exposure\ExposedEntityCommand; | |
| 12 | use Stewart\Runtime\Broker\Exposure\ExposedEntitySync; | |
| 13 | use Stewart\Runtime\Broker\Exposure\ExposureCommandRouter; | |
| 14 | use Stewart\Runtime\Broker\Trigger\TriggerRejections; | |
| 15 | use Stewart\Runtime\Ipc\Message\ExposedEntitySynced; | |
| 16 | use Stewart\Runtime\Ipc\Message\HaConnectionLost; | |
| 17 | use Stewart\Runtime\Ipc\Message\StateResynced; | |
| 18 | use Stewart\Runtime\Model\ResourceScope; | |
| 19 | use Stewart\Support\State\StateUpdate; | |
| 20 | use Stewart\Support\Wire\TriggerSpec; | |
| 21 | use Throwable; | |
| 22 | ||
| 23 | final readonly class BrokerHaEvents implements HaSessionListener | |
| 24 | { | |
| 25 | public function __construct( | |
| 26 | private HaSession $session, | |
| 27 | private EventRouter $router, | |
| 28 | private WorkerSlotRegistry $slots, | |
| 29 | private ConnectionTracker $connection, | |
| 30 | private BrokerRun $run, | |
| 31 | private TriggerRejections $triggerRejections, | |
| 32 | private RegistryRefresher $registryRefresher, | |
| 33 | private ExposureCommandRouter $commandRouter, | |
| 34 | private GeneratedCodeDrift $drift, | |
| 35 | ) {} | |
| 36 | ||
| 37 | public function stateUpdated(StateUpdate $update): void | |
| 38 | { | |
| 39 | $this->router->broadcastStateUpdate($update); | |
| 40 | } | |
| 41 | ||
| 42 | public function eventFired(HaEvent $event): void | |
| 43 | { | |
| 44 | $this->registryRefresher->scheduleRefreshFor($event); | |
| 45 | $this->router->routeEvent($event); | |
| 46 | } | |
| 47 | ||
| 48 | public function triggerFired(TriggerSpec $spec, TriggerEvent $event): void | |
| 49 | { | |
| 50 | $this->router->routeTrigger($spec, $event); | |
| 51 | } | |
| 52 | ||
| 53 | public function triggerRejected(TriggerSpec $spec, string $reason): void | |
| 54 | { | |
| 55 | $this->triggerRejections->refuseSubscribers($spec, $reason); | |
| 56 | } | |
| 57 | ||
| 58 | public function exposedEntitySynced(ExposedEntitySync $sync): void | |
| 59 | { | |
| 60 | $this->slots->findHandleForWorker($sync->owner)?->send(new ExposedEntitySynced(ResourceScope::forApp($sync->appId), $sync->key, $sync->snapshot)); | |
| 61 | } | |
| 62 | ||
| 63 | public function exposedEntityCommanded(ExposedEntityCommand $command): void | |
| 64 | { | |
| 65 | $this->commandRouter->routeCommand($command); | |
| 66 | } | |
| 67 | ||
| 68 | public function connectionLost(string $reason, Instant $lostAt): void | |
| 69 | { | |
| 70 | $lost = new HaConnectionLost($lostAt, $reason); | |
| 71 | $this->connection->markReconnecting($lost); | |
| 72 | $this->slots->broadcast($lost); | |
| 73 | } | |
| 74 | ||
| 75 | public function reconnected(Duration $outage): void | |
| 76 | { | |
| 77 | $snapshot = $this->session->snapshotStateCache(); | |
| 78 | $this->connection->markRestored($outage); | |
| 79 | ||
| 80 | // The registry goes first so a resynced change is matched against the current areas and labels. | |
| 81 | $this->slots->broadcast($this->session->snapshotRegistry()->toRegistrySnapshot()); | |
| 82 | $this->slots->broadcast(new StateResynced($snapshot->states, $snapshot->revision, $outage)); | |
| 83 | $this->drift->recheckGeneratedCode(); | |
| 84 | } | |
| 85 | ||
| 86 | public function connectionFailed(Throwable $error): void | |
| 87 | { | |
| 88 | $this->run->stopWithError('Home Assistant rejected the connection', $error); | |
| 89 | } | |
| 90 | } |