mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Handle possible exception during Ambient PWS reconnect (#22134)
* Handle possible exception during Ambient PWS reconnect * Hound
This commit is contained in:
parent
8529ad3ba1
commit
c020b7c47d
@ -304,15 +304,26 @@ class AmbientStation:
|
|||||||
self.monitored_conditions = monitored_conditions
|
self.monitored_conditions = monitored_conditions
|
||||||
self.stations = {}
|
self.stations = {}
|
||||||
|
|
||||||
async def ws_connect(self):
|
async def _attempt_connect(self):
|
||||||
"""Register handlers and connect to the websocket."""
|
"""Attempt to connect to the socket (retrying later on fail)."""
|
||||||
from aioambient.errors import WebsocketError
|
from aioambient.errors import WebsocketError
|
||||||
|
|
||||||
|
try:
|
||||||
|
await self.client.websocket.connect()
|
||||||
|
except WebsocketError as err:
|
||||||
|
_LOGGER.error("Error with the websocket connection: %s", err)
|
||||||
|
self._ws_reconnect_delay = min(
|
||||||
|
2 * self._ws_reconnect_delay, 480)
|
||||||
|
async_call_later(
|
||||||
|
self._hass, self._ws_reconnect_delay, self.ws_connect)
|
||||||
|
|
||||||
|
async def ws_connect(self):
|
||||||
|
"""Register handlers and connect to the websocket."""
|
||||||
async def _ws_reconnect(event_time):
|
async def _ws_reconnect(event_time):
|
||||||
"""Forcibly disconnect from and reconnect to the websocket."""
|
"""Forcibly disconnect from and reconnect to the websocket."""
|
||||||
_LOGGER.debug('Watchdog expired; forcing socket reconnection')
|
_LOGGER.debug('Watchdog expired; forcing socket reconnection')
|
||||||
await self.client.websocket.disconnect()
|
await self.client.websocket.disconnect()
|
||||||
await self.client.websocket.connect()
|
await self._attempt_connect()
|
||||||
|
|
||||||
def on_connect():
|
def on_connect():
|
||||||
"""Define a handler to fire when the websocket is connected."""
|
"""Define a handler to fire when the websocket is connected."""
|
||||||
@ -381,15 +392,7 @@ class AmbientStation:
|
|||||||
self.client.websocket.on_disconnect(on_disconnect)
|
self.client.websocket.on_disconnect(on_disconnect)
|
||||||
self.client.websocket.on_subscribed(on_subscribed)
|
self.client.websocket.on_subscribed(on_subscribed)
|
||||||
|
|
||||||
try:
|
await self._attempt_connect()
|
||||||
await self.client.websocket.connect()
|
|
||||||
except WebsocketError as err:
|
|
||||||
_LOGGER.error("Error with the websocket connection: %s", err)
|
|
||||||
|
|
||||||
self._ws_reconnect_delay = min(2 * self._ws_reconnect_delay, 480)
|
|
||||||
|
|
||||||
async_call_later(
|
|
||||||
self._hass, self._ws_reconnect_delay, self.ws_connect)
|
|
||||||
|
|
||||||
async def ws_disconnect(self):
|
async def ws_disconnect(self):
|
||||||
"""Disconnect from the websocket."""
|
"""Disconnect from the websocket."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user