Lines 76.47% 13 / 17
Methods 60.00% 6 / 10
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 __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
23final 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}