Add watchdog to Ambient PWS (#21507)
* Add watchdog to Ambient PWS * Better labeling * Owner commentspull/21712/head
parent
9e140d27bf
commit
aa546b5a1f
|
@ -27,6 +27,7 @@ _LOGGER = logging.getLogger(__name__)
|
|||
DATA_CONFIG = 'config'
|
||||
|
||||
DEFAULT_SOCKET_MIN_RETRY = 15
|
||||
DEFAULT_WATCHDOG_SECONDS = 5 * 60
|
||||
|
||||
TYPE_24HOURRAININ = '24hourrainin'
|
||||
TYPE_BAROMABSIN = 'baromabsin'
|
||||
|
@ -296,6 +297,7 @@ class AmbientStation:
|
|||
"""Initialize."""
|
||||
self._config_entry = config_entry
|
||||
self._hass = hass
|
||||
self._watchdog_listener = None
|
||||
self._ws_reconnect_delay = DEFAULT_SOCKET_MIN_RETRY
|
||||
self.client = client
|
||||
self.monitored_conditions = monitored_conditions
|
||||
|
@ -305,9 +307,18 @@ class AmbientStation:
|
|||
"""Register handlers and connect to the websocket."""
|
||||
from aioambient.errors import WebsocketError
|
||||
|
||||
async def _ws_reconnect(event_time):
|
||||
"""Forcibly disconnect from and reconnect to the websocket."""
|
||||
_LOGGER.debug('Watchdog expired; forcing socket reconnection')
|
||||
await self.client.websocket.disconnect()
|
||||
await self.client.websocket.connect()
|
||||
|
||||
def on_connect():
|
||||
"""Define a handler to fire when the websocket is connected."""
|
||||
_LOGGER.info('Connected to websocket')
|
||||
_LOGGER.debug('Watchdog starting')
|
||||
self._watchdog_listener = async_call_later(
|
||||
self._hass, DEFAULT_WATCHDOG_SECONDS, _ws_reconnect)
|
||||
|
||||
def on_data(data):
|
||||
"""Define a handler to fire when the data is received."""
|
||||
|
@ -317,6 +328,11 @@ class AmbientStation:
|
|||
self.stations[mac_address][ATTR_LAST_DATA] = data
|
||||
async_dispatcher_send(self._hass, TOPIC_UPDATE)
|
||||
|
||||
_LOGGER.debug('Resetting watchdog')
|
||||
self._watchdog_listener()
|
||||
self._watchdog_listener = async_call_later(
|
||||
self._hass, DEFAULT_WATCHDOG_SECONDS, _ws_reconnect)
|
||||
|
||||
def on_disconnect():
|
||||
"""Define a handler to fire when the websocket is disconnected."""
|
||||
_LOGGER.info('Disconnected from websocket')
|
||||
|
|
Loading…
Reference in New Issue