Skip to content

Commit 890d06c

Browse files
sebrclaude
andauthored
Fix websocket reconnecting after shutting down Home Assistant (#376)
When Home Assistant shuts down, the websocket stop() method sets state to STOPPED, but the running() coroutine's else block unconditionally calls retry(), causing an unwanted reconnection attempt. Add a _closing flag to distinguish intentional stops from unexpected disconnections, so only the latter trigger automatic reconnection. Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent dff7c84 commit 890d06c

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

custom_components/bhyve/pybhyve/websocket.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def __init__(
4747
self._heartbeat_cb = None
4848
self._heartbeat: int = 25
4949
self._ws: ClientWebSocketResponse | None = None
50+
self._closing: bool = False
5051

5152
def _cancel_heartbeat(self) -> None:
5253
if self._heartbeat_cb is not None:
@@ -172,12 +173,16 @@ async def running(self) -> None: # noqa: PLR0912, PLR0915
172173
self.retry()
173174

174175
else:
175-
_LOGGER.info("Reconnecting websocket; state: %s", self.state)
176-
self.retry()
176+
if self._closing:
177+
_LOGGER.info("Websocket closed intentionally, not reconnecting")
178+
else:
179+
_LOGGER.info("Reconnecting websocket; state: %s", self.state)
180+
self.retry()
177181

178182
async def stop(self) -> None:
179183
"""Close websocket connection."""
180184
_LOGGER.info("Closing websocket connection; state: %s --> STOPPED", self.state)
185+
self._closing = True
181186
self.state = STATE_STOPPED
182187
self._cancel_heartbeat()
183188
if self._ws is not None:

0 commit comments

Comments
 (0)