Lines 79.10% 106 / 134
Methods 68.96% 20 / 29
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 __construct 100.00% 3 / 3 100.00% 1 / 1 1
 onEntitySynced 100.00% 1 / 1 100.00% 1 / 1 1
 onEntityCommanded 100.00% 1 / 1 100.00% 1 / 1 1
 onSessionLost 0.00% 0 / 1 0.00% 0 / 1 2
 receiveCommand 100.00% 11 / 11 100.00% 1 / 1 3
 acceptCommand 100.00% 4 / 4 100.00% 1 / 1 2
 rejectCommand 100.00% 1 / 1 100.00% 1 / 1 1
 exposeEntity 100.00% 9 / 9 100.00% 1 / 1 3
 reconfigureEntity 100.00% 10 / 10 100.00% 1 / 1 3
 updateEntity 88.88% 8 / 9 0.00% 0 / 1 3.01
 removeEntity 36.36% 4 / 11 0.00% 0 / 1 5.32
 orphanExposuresOfApp 100.00% 1 / 1 100.00% 1 / 1 1
 orphanExposuresOfWorker 100.00% 1 / 1 100.00% 1 / 1 1
 forgetExposuresOfApp 100.00% 1 / 1 100.00% 1 / 1 1
 countExposuresOfApp 100.00% 1 / 1 100.00% 1 / 1 1
 findAddressByEntityId 100.00% 2 / 2 100.00% 1 / 1 1
 listKeptAddresses 100.00% 1 / 1 100.00% 1 / 1 1
 replayAll 55.55% 5 / 9 0.00% 0 / 1 9.16
 assertComponentUsable 100.00% 5 / 5 100.00% 1 / 1 5
 sendUpsert 81.81% 9 / 11 0.00% 0 / 1 3.05
 throwIfAppCaused 75.00% 3 / 4 0.00% 0 / 1 4.25
 handleUpdateFailure 66.66% 6 / 9 0.00% 0 / 1 3.33
 reportIfSessionLost 100.00% 2 / 2 100.00% 1 / 1 2
 orphanExposures 100.00% 7 / 7 100.00% 1 / 1 6
 sendUnavailable 33.33% 3 / 9 0.00% 0 / 1 8.74
 sendCommandAnswer 50.00% 3 / 6 0.00% 0 / 1 4.12
 syncOwner 100.00% 2 / 2 100.00% 1 / 1 2
 findLive 100.00% 1 / 1 100.00% 1 / 1 1
 createAddress 100.00% 1 / 1 100.00% 1 / 1 1
32final class ExposureLink
33{
34    /** @var array<string, LiveExposure> */
35    private array $exposuresByAddress = [];
36
37    /** @var Closure(ExposedEntitySync): void */
38    private Closure $onSynced;
39
40    /** @var Closure(ExposedEntityCommand): void */
41    private Closure $onCommanded;
42
43    /** @var Closure(): void */
44    private Closure $onSessionLost;
45
46    public function __construct(
47        private readonly HaClient $client,
48        private readonly LoggerInterface $logger,
49        private readonly ComponentTracker $tracker,
50        private readonly ExposeConfig $expose,
51    ) {
52        $this->onSynced = static function (): void {};
53        $this->onCommanded = static function (): void {};
54        $this->onSessionLost = static function (): void {};
55    }
56
57    /** @param Closure(ExposedEntitySync): void $handler */
58    public function onEntitySynced(Closure $handler): void
59    {
60        $this->onSynced = $handler;
61    }
62
63    /** @param Closure(ExposedEntityCommand): void $handler */
64    public function onEntityCommanded(Closure $handler): void
65    {
66        $this->onCommanded = $handler;
67    }
68
69    /** @param Closure(): void $handler */
70    public function onSessionLost(Closure $handler): void
71    {
72        $this->onSessionLost = $handler;
73    }
74
75    public function receiveCommand(ComponentCommand $command): void
76    {
77        $live = $this->findLive($command->appId, $command->key);
78
79        if ($live === null) {
80            $reason = \sprintf('Entity %s of app %s is not exposed by a running app.', $command->key, $command->appId);
81            $this->sendCommandAnswer(ComponentCommandAnswer::reject($command->commandId, $reason));
82
83            return;
84        }
85
86        $exposedCommand = $command->readExposedCommand($live->definition->platform);
87
88        if ($exposedCommand === null) {
89            $reason = \sprintf('Entity %s of app %s does not take %s commands.', $command->key, $command->appId, $command->action->value);
90            $this->sendCommandAnswer(ComponentCommandAnswer::reject($command->commandId, $reason));
91
92            return;
93        }
94
95        ($this->onCommanded)(new ExposedEntityCommand($command->commandId, $live->owner, $command->appId, $command->key, $exposedCommand));
96    }
97
98    public function acceptCommand(ExposedEntityCommand $command): void
99    {
100        $requestedState = $command->command->getRequestedState();
101
102        if ($requestedState !== null) {
103            $this->findLive($command->appId, $command->key)?->recordChange(new ExposedStateChange($requestedState));
104        }
105
106        $this->sendCommandAnswer(ComponentCommandAnswer::accept($command->commandId));
107    }
108
109    public function rejectCommand(ExposedEntityCommand $command, string $reason): void
110    {
111        $this->sendCommandAnswer(ComponentCommandAnswer::reject($command->commandId, $reason));
112    }
113
114    /** @throws ExposureException */
115    public function exposeEntity(
116        WorkerId $owner,
117        AppId $appId,
118        ExposedEntityKey $key,
119        ExposedEntityDefinition $definition,
120        ExposedStateChange $change,
121    ): ?ExposedEntitySnapshot {
122        $this->assertComponentUsable($key);
123
124        $live = new LiveExposure($owner, $this->createAddress($appId, $key), $definition, new ExposedStateChange(available: true)->withLaterChange($change));
125        $this->exposuresByAddress[$live->address->describe()] = $live;
126
127        if (!$this->tracker->isActive()) {
128            return null;
129        }
130
131        try {
132            return $this->sendUpsert($live, $live->definition, $live->buildUpsertChange());
133        } catch (ExposureException $e) {
134            unset($this->exposuresByAddress[$live->address->describe()]);
135
136            throw $e;
137        }
138    }
139
140    /** @throws ExposureException */
141    public function reconfigureEntity(AppId $appId, ExposedEntityKey $key, ExposedEntityDefinition $definition): ?ExposedEntitySnapshot
142    {
143        $live = $this->findLive($appId, $key) ?? throw ExposureException::removed($key);
144        $definition = $live->definition->withConfigOf($definition);
145
146        if (!$this->tracker->isActive()) {
147            $live->recordDefinition($definition);
148
149            return null;
150        }
151
152        $snapshot = $this->sendUpsert($live, $definition, new ExposedStateChange());
153        $live->recordDefinition($definition);
154
155        if ($snapshot !== null) {
156            $live->recordChange(new ExposedStateChange($snapshot->state));
157        }
158
159        return $snapshot;
160    }
161
162    /** @throws ExposureException */
163    public function updateEntity(AppId $appId, ExposedEntityKey $key, ExposedStateChange $change): void
164    {
165        $live = $this->findLive($appId, $key) ?? throw ExposureException::removed($key);
166
167        if (!$this->tracker->isActive()) {
168            $live->recordChange($change);
169
170            return;
171        }
172
173        try {
174            $this->client->updateExposedEntityState($live->address, $change);
175        } catch (HaClientException $e) {
176            $this->handleUpdateFailure($live, $change, $e);
177
178            return;
179        }
180
181        $live->recordChange($change);
182    }
183
184    public function removeEntity(AppId $appId, ExposedEntityKey $key): void
185    {
186        $live = $this->findLive($appId, $key);
187
188        if ($live === null) {
189            return;
190        }
191
192        unset($this->exposuresByAddress[$live->address->describe()]);
193
194        try {
195            $this->client->removeExposedEntity($live->address);
196        } catch (HaClientException $e) {
197            $this->logger->warning('Could not remove an exposed entity from Home Assistant; it stays there until removed by hand', [
198                'entity' => $live->address->describe(),
199                'exception' => $e,
200            ]);
201            $this->reportIfSessionLost($e);
202        }
203    }
204
205    public function orphanExposuresOfApp(AppId $appId): void
206    {
207        $this->orphanExposures(static fn(LiveExposure $live): bool => $live->address->appId->equals($appId));
208    }
209
210    public function orphanExposuresOfWorker(WorkerId $workerId): void
211    {
212        $this->orphanExposures(static fn(LiveExposure $live): bool => $live->isOwnedBy($workerId));
213    }
214
215    // A removed app's entities are left for the reconciler to prune.
216    public function forgetExposuresOfApp(AppId $appId): void
217    {
218        $this->exposuresByAddress = array_filter($this->exposuresByAddress, static fn(LiveExposure $live): bool => !$live->address->appId->equals($appId));
219    }
220
221    public function countExposuresOfApp(AppId $appId): int
222    {
223        return \count(array_filter($this->exposuresByAddress, static fn(LiveExposure $live): bool => $live->address->appId->equals($appId)));
224    }
225
226    public function findAddressByEntityId(EntityId $entityId): ?ExposedEntityAddress
227    {
228        $live = array_find($this->exposuresByAddress, static fn(LiveExposure $live): bool => $live->entityId?->equals($entityId) === true);
229
230        return $live?->address;
231    }
232
233    public function listKeptAddresses(): ExposedEntityAddressCollection
234    {
235        return ExposedEntityAddressCollection::fromList(array_map(static fn(LiveExposure $live) => $live->address, array_values($this->exposuresByAddress)));
236    }
237
238    /** @throws HaClientException */
239    public function replayAll(): void
240    {
241        if (!$this->tracker->isActive()) {
242            return;
243        }
244
245        foreach ($this->exposuresByAddress as $live) {
246            try {
247                $snapshot = $this->sendUpsert($live, $live->definition, $live->buildUpsertChange());
248            } catch (ExposureException $e) {
249                $this->logger->error('Home Assistant refused an exposed entity again', ['entity' => $live->address->describe(), 'exception' => $e]);
250
251                continue;
252            }
253
254            if ($snapshot !== null && $this->findLive($live->address->appId, $live->address->key) === $live) {
255                $this->syncOwner($live, $snapshot);
256            }
257        }
258    }
259
260    /** @throws ExposureException */
261    private function assertComponentUsable(ExposedEntityKey $key): void
262    {
263        match ($this->tracker->detection->state) {
264            ComponentState::ProtocolMismatch => throw ExposureException::createForReason(ExposureError::ProtocolMismatch, key: $key->value),
265            ComponentState::Refused => throw ExposureException::createForReason(ExposureError::ComponentRefused, key: $key->value),
266            ComponentState::Disabled => throw ExposureException::createForReason(ExposureError::Disabled, key: $key->value),
267            ComponentState::Unchecked, ComponentState::Missing, ComponentState::Replaced, ComponentState::Active => null,
268        };
269    }
270
271    /** @throws ExposureException */
272    private function sendUpsert(LiveExposure $live, ExposedEntityDefinition $definition, ExposedStateChange $change): ?ExposedEntitySnapshot
273    {
274        try {
275            if ($definition->isAction()) {
276                $this->client->upsertExposedAction($live->address, $definition);
277
278                return null;
279            }
280
281            $snapshot = $this->client->upsertExposedEntity($live->address, $definition, $change);
282        } catch (HaClientException $e) {
283            $this->throwIfAppCaused($e);
284            $this->logger->debug('Exposed entity waits for the next stewart integration session', ['entity' => $live->address->describe(), 'exception' => $e]);
285            $this->reportIfSessionLost($e);
286
287            return null;
288        }
289
290        $live->recordEntityId($snapshot->entityId);
291
292        return $snapshot;
293    }
294
295    /** @throws ExposureException */
296    private function throwIfAppCaused(HaClientException $e): void
297    {
298        match (ComponentErrorCode::tryFromException($e)) {
299            ComponentErrorCode::InvalidConfig, ComponentErrorCode::ActionTaken => throw ExposureException::configInvalid($e->findDetail() ?? $e->getMessage()),
300            ComponentErrorCode::InvalidState => throw ExposureException::stateInvalid($e->findDetail() ?? $e->getMessage()),
301            default => null,
302        };
303    }
304
305    /** @throws ExposureException */
306    private function handleUpdateFailure(LiveExposure $live, ExposedStateChange $change, HaClientException $e): void
307    {
308        $this->throwIfAppCaused($e);
309        $live->recordChange($change);
310
311        if (ComponentErrorCode::tryFromException($e) !== ComponentErrorCode::NotFound) {
312            $this->logger->debug('Exposed entity state waits for the next stewart integration session', ['entity' => $live->address->describe(), 'exception' => $e]);
313            $this->reportIfSessionLost($e);
314
315            return;
316        }
317
318        $snapshot = $this->sendUpsert($live, $live->definition, $live->buildUpsertChange());
319
320        if ($snapshot !== null) {
321            $this->syncOwner($live, $snapshot);
322        }
323    }
324
325    private function reportIfSessionLost(HaClientException $e): void
326    {
327        if (ComponentErrorCode::tryFromException($e) === ComponentErrorCode::NoSession) {
328            ($this->onSessionLost)();
329        }
330    }
331
332    /** @param Closure(LiveExposure): bool $belongs */
333    private function orphanExposures(Closure $belongs): void
334    {
335        $orphaned = [];
336
337        foreach ($this->exposuresByAddress as $live) {
338            if (!$live->isOrphaned() && $belongs($live)) {
339                $live->markOrphaned();
340                $orphaned[] = $live;
341            }
342        }
343
344        if ($orphaned !== [] && $this->tracker->isActive()) {
345            async($this->sendUnavailable(...), $orphaned);
346        }
347    }
348
349    /** @param list<LiveExposure> $orphaned */
350    private function sendUnavailable(array $orphaned): void
351    {
352        foreach ($orphaned as $live) {
353            // An action stays registered in Home Assistant; the broker refuses its calls while it is orphaned.
354            if ($live->definition->isAction()) {
355                continue;
356            }
357
358            try {
359                $this->client->updateExposedEntityState($live->address, new ExposedStateChange(available: false));
360            } catch (HaClientException $e) {
361                $this->logger->debug('Orphaned exposed entity turns unavailable with the next stewart integration session', [
362                    'entity' => $live->address->describe(),
363                    'exception' => $e,
364                ]);
365                $this->reportIfSessionLost($e);
366            }
367        }
368    }
369
370    private function sendCommandAnswer(ComponentCommandAnswer $answer): void
371    {
372        async(function () use ($answer): void {
373            try {
374                if (!$this->client->answerComponentCommand($answer)) {
375                    $this->logger->debug('Home Assistant stopped waiting for an exposed entity command before it was answered', ['command' => $answer->commandId]);
376                }
377            } catch (HaClientException $e) {
378                $this->logger->debug('Could not answer an exposed entity command; Home Assistant times it out', ['command' => $answer->commandId, 'exception' => $e]);
379            }
380        })->ignore();
381    }
382
383    private function syncOwner(LiveExposure $live, ExposedEntitySnapshot $snapshot): void
384    {
385        if ($live->owner !== null) {
386            ($this->onSynced)(new ExposedEntitySync($live->owner, $live->address->appId, $live->address->key, $snapshot));
387        }
388    }
389
390    private function findLive(AppId $appId, ExposedEntityKey $key): ?LiveExposure
391    {
392        return $this->exposuresByAddress[$this->createAddress($appId, $key)->describe()] ?? null;
393    }
394
395    private function createAddress(AppId $appId, ExposedEntityKey $key): ExposedEntityAddress
396    {
397        return new ExposedEntityAddress($this->expose->instance, $appId, $key);
398    }
399}