100100ATTR_TIMESTAMP = "timestamp"
101101
102102DEFAULT_SCAN_INTERVAL = timedelta (seconds = 30 )
103- DEFAULT_SOCKET_MIN_RETRY = 15
104103
104+ WEBSOCKET_RECONNECT_RETRIES = 3
105+ WEBSOCKET_RETRY_DELAY = 2
105106
106107EVENT_SIMPLISAFE_EVENT = "SIMPLISAFE_EVENT"
107108EVENT_SIMPLISAFE_NOTIFICATION = "SIMPLISAFE_NOTIFICATION"
@@ -419,6 +420,7 @@ def __init__(self, hass: HomeAssistant, entry: ConfigEntry, api: API) -> None:
419420 self ._api = api
420421 self ._hass = hass
421422 self ._system_notifications : dict [int , set [SystemNotification ]] = {}
423+ self ._websocket_reconnect_retries : int = 0
422424 self ._websocket_reconnect_task : asyncio .Task | None = None
423425 self .entry = entry
424426 self .initial_event_to_use : dict [int , dict [str , Any ]] = {}
@@ -469,6 +471,8 @@ async def _async_start_websocket_loop(self) -> None:
469471 """Start a websocket reconnection loop."""
470472 assert self ._api .websocket
471473
474+ self ._websocket_reconnect_retries += 1
475+
472476 try :
473477 await self ._api .websocket .async_connect ()
474478 await self ._api .websocket .async_listen ()
@@ -479,9 +483,21 @@ async def _async_start_websocket_loop(self) -> None:
479483 LOGGER .error ("Failed to connect to websocket: %s" , err )
480484 except Exception as err : # noqa: BLE001
481485 LOGGER .error ("Unknown exception while connecting to websocket: %s" , err )
486+ else :
487+ self ._websocket_reconnect_retries = 0
482488
483- LOGGER .debug ("Reconnecting to websocket" )
484- await self ._async_cancel_websocket_loop ()
489+ if self ._websocket_reconnect_retries >= WEBSOCKET_RECONNECT_RETRIES :
490+ LOGGER .error ("Max websocket connection retries exceeded" )
491+ return
492+
493+ delay = WEBSOCKET_RETRY_DELAY * (2 ** (self ._websocket_reconnect_retries - 1 ))
494+ LOGGER .info (
495+ "Retrying websocket connection in %s seconds (attempt %s/%s)" ,
496+ delay ,
497+ self ._websocket_reconnect_retries ,
498+ WEBSOCKET_RECONNECT_RETRIES ,
499+ )
500+ await asyncio .sleep (delay )
485501 self ._websocket_reconnect_task = self ._hass .async_create_task (
486502 self ._async_start_websocket_loop ()
487503 )
0 commit comments