Lines
94.73%
36 / 38
Functions and Methods
87.50%
7 / 8
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| ComponentSession | 94.73% | 36 / 38 | 87.50% | 7 / 8 | 21.06 | 0.00% | 0 / 1 | |||
| __construct | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 1 | |||||
| openSession | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 1 | |||||
| relinkAfterSessionEnded | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 2 | |||||
| startRelinkingUnlessSettled | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 3 | |||||
| markLinkLost | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 1 | |||||
| startRelinking | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 2 | |||||
| relinkUntilSettled | 83.33% | 10 / 12 | 0.00% | 0 / 1 | 8.30 | |||||
| hasSettled | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 3 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Runtime\Broker\Component; | |
| 6 | ||
| 7 | use Amp\Cancellation; | |
| 8 | use Amp\CancelledException; | |
| 9 | use Amp\DeferredCancellation; | |
| 10 | use Psr\Log\LoggerInterface; | |
| 11 | use Stewart\Client\Exception\HaClientError; | |
| 12 | use Stewart\Client\Exception\HaClientException; | |
| 13 | use Stewart\Runtime\Broker\Exposure\ExposureLink; | |
| 14 | use Stewart\Runtime\Broker\Exposure\ExposureReconciler; | |
| 15 | use Stewart\Runtime\Config\StewartConfig; | |
| 16 | use Stewart\Runtime\Lifecycle\ComponentState; | |
| 17 | use Stewart\Support\Time\Deadlines; | |
| 18 | ||
| 19 | use function Amp\async; | |
| 20 | ||
| 21 | final class ComponentSession | |
| 22 | { | |
| 23 | private ?DeferredCancellation $relinking = null; | |
| 24 | ||
| 25 | public function __construct( | |
| 26 | private readonly ComponentLink $link, | |
| 27 | private readonly ExposureLink $exposures, | |
| 28 | private readonly ExposureReconciler $reconciler, | |
| 29 | private readonly ComponentTracker $tracker, | |
| 30 | private readonly StewartConfig $config, | |
| 31 | private readonly Deadlines $deadlines, | |
| 32 | private readonly LoggerInterface $logger, | |
| 33 | ) { | |
| 34 | $link->onCommand($exposures->receiveCommand(...)); | |
| 35 | $link->onSessionEnded($this->relinkAfterSessionEnded(...)); | |
| 36 | $exposures->onSessionLost($this->relinkAfterSessionEnded(...)); | |
| 37 | } | |
| 38 | ||
| 39 | /** @throws HaClientException */ | |
| 40 | public function openSession(): void | |
| 41 | { | |
| 42 | $this->link->establishLink(); | |
| 43 | $this->exposures->replayAll(); | |
| 44 | $this->reconciler->reconcileIfDue(); | |
| 45 | } | |
| 46 | ||
| 47 | public function relinkAfterSessionEnded(): void | |
| 48 | { | |
| 49 | // Also when a relink runs, so a session lost during its replay keeps it going. | |
| 50 | $this->link->markLinkLost(); | |
| 51 | ||
| 52 | if ($this->relinking !== null) { | |
| 53 | return; | |
| 54 | } | |
| 55 | ||
| 56 | $this->logger->info('The stewart Home Assistant integration session ended; opening a new one'); | |
| 57 | $this->startRelinking(0); | |
| 58 | } | |
| 59 | ||
| 60 | // Home Assistant answers before it has loaded the integration, so a connect can find it missing. | |
| 61 | public function startRelinkingUnlessSettled(): void | |
| 62 | { | |
| 63 | if ($this->relinking !== null || $this->hasSettled()) { | |
| 64 | return; | |
| 65 | } | |
| 66 | ||
| 67 | $this->startRelinking(1); | |
| 68 | } | |
| 69 | ||
| 70 | public function markLinkLost(): void | |
| 71 | { | |
| 72 | $this->relinking?->cancel(); | |
| 73 | $this->relinking = null; | |
| 74 | $this->link->markLinkLost(); | |
| 75 | } | |
| 76 | ||
| 77 | private function startRelinking(int $attemptsMade): void | |
| 78 | { | |
| 79 | $relinking = $this->relinking = new DeferredCancellation(); | |
| 80 | ||
| 81 | async(function () use ($relinking, $attemptsMade): void { | |
| 82 | try { | |
| 83 | $this->relinkUntilSettled($attemptsMade, $relinking->getCancellation()); | |
| 84 | } finally { | |
| 85 | if ($this->relinking === $relinking) { | |
| 86 | $this->relinking = null; | |
| 87 | } | |
| 88 | } | |
| 89 | })->ignore(); | |
| 90 | } | |
| 91 | ||
| 92 | private function relinkUntilSettled(int $attemptsMade, Cancellation $stop): void | |
| 93 | { | |
| 94 | for ($attempt = $attemptsMade + 1; ; ++$attempt) { | |
| 95 | if ($attempt > 1) { | |
| 96 | try { | |
| 97 | $this->deadlines->delay($this->config->reconnectBackoff->delayFor($attempt - 1), $stop); | |
| 98 | } catch (CancelledException) { | |
| 99 | return; | |
| 100 | } | |
| 101 | } | |
| 102 | ||
| 103 | try { | |
| 104 | $this->openSession(); | |
| 105 | } catch (HaClientException $e) { | |
| 106 | // Any other failure means the connection is gone; the reconnect relinks. | |
| 107 | if ($e->reason !== HaClientError::CommandTimedOut) { | |
| 108 | return; | |
| 109 | } | |
| 110 | ||
| 111 | $this->logger->debug('Could not open the stewart integration session yet', ['exception' => $e, 'attempt' => $attempt]); | |
| 112 | } | |
| 113 | ||
| 114 | if ($stop->isRequested() || $this->hasSettled()) { | |
| 115 | return; | |
| 116 | } | |
| 117 | } | |
| 118 | } | |
| 119 | ||
| 120 | // A missing component is usually one that Home Assistant is reloading. | |
| 121 | private function hasSettled(): bool | |
| 122 | { | |
| 123 | return match ($this->tracker->detection->state) { | |
| 124 | ComponentState::Unchecked, ComponentState::Missing => false, | |
| 125 | ComponentState::ProtocolMismatch, ComponentState::Refused, ComponentState::Replaced, ComponentState::Active, ComponentState::Disabled => true, | |
| 126 | }; | |
| 127 | } | |
| 128 | } |