Compare commits

...

2 Commits

Author SHA1 Message Date
Stefan Agner
8e026bd945 Add log message when connecting to Core WebSocket 2025-02-14 12:34:23 +00:00
Stefan Agner
3d74d2bb7c Avoid exception when handling closed WebSocket connection
When delivering multiple messages to Core, and the first fails with a
connection error, the second message will also fail with the same error.
But at this point the connection is already clsoed, which leads to an
exception in the exception handler. Avoid this compunding error by
checking if the connection is still exists before trying to close.

Fixes: #5629
2025-02-14 11:20:08 +00:00

View File

@@ -98,6 +98,7 @@ class WSClient:
async def start_listener(self) -> None:
"""Start listening to the websocket."""
_LOGGER.debug("Starting WebSocket listener")
if not self.connected:
raise HomeAssistantWSConnectionError("Not connected when start listening")
@@ -202,6 +203,7 @@ class HomeAssistantWebSocket(CoreSysAttributes):
return self._client
await self.sys_homeassistant.api.ensure_access_token()
_LOGGER.debug("Connecting via WebSocket to Home Assistant Core")
client = await WSClient.connect_with_auth(
self.sys_websession,
self.sys_loop,
@@ -266,7 +268,8 @@ class HomeAssistantWebSocket(CoreSysAttributes):
try:
await self._client.async_send_command(message)
except HomeAssistantWSConnectionError:
await self._client.close()
if self._client:
await self._client.close()
self._client = None
async def async_send_command(self, message: dict[str, Any]) -> dict[str, Any]:
@@ -277,7 +280,8 @@ class HomeAssistantWebSocket(CoreSysAttributes):
try:
return await self._client.async_send_command(message)
except HomeAssistantWSConnectionError:
await self._client.close()
if self._client:
await self._client.close()
self._client = None
raise