From 4588e9da8d2dae0b06e40171e0ddea10c380776b Mon Sep 17 00:00:00 2001 From: Aaron Bach Date: Wed, 8 Oct 2025 08:32:17 -0600 Subject: [PATCH] Limit SimpliSafe websocket connection attempts during startup (#153853) Co-authored-by: Joost Lekkerkerker --- .../components/simplisafe/__init__.py | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/simplisafe/__init__.py b/homeassistant/components/simplisafe/__init__.py index f2ef3ce9063..8e964e0c776 100644 --- a/homeassistant/components/simplisafe/__init__.py +++ b/homeassistant/components/simplisafe/__init__.py @@ -100,8 +100,9 @@ ATTR_PIN_VALUE = "pin" ATTR_TIMESTAMP = "timestamp" DEFAULT_SCAN_INTERVAL = timedelta(seconds=30) -DEFAULT_SOCKET_MIN_RETRY = 15 +WEBSOCKET_RECONNECT_RETRIES = 3 +WEBSOCKET_RETRY_DELAY = 2 EVENT_SIMPLISAFE_EVENT = "SIMPLISAFE_EVENT" EVENT_SIMPLISAFE_NOTIFICATION = "SIMPLISAFE_NOTIFICATION" @@ -419,6 +420,7 @@ class SimpliSafe: self._api = api self._hass = hass self._system_notifications: dict[int, set[SystemNotification]] = {} + self._websocket_reconnect_retries: int = 0 self._websocket_reconnect_task: asyncio.Task | None = None self.entry = entry self.initial_event_to_use: dict[int, dict[str, Any]] = {} @@ -469,6 +471,8 @@ class SimpliSafe: """Start a websocket reconnection loop.""" assert self._api.websocket + self._websocket_reconnect_retries += 1 + try: await self._api.websocket.async_connect() await self._api.websocket.async_listen() @@ -479,9 +483,21 @@ class SimpliSafe: LOGGER.error("Failed to connect to websocket: %s", err) except Exception as err: # noqa: BLE001 LOGGER.error("Unknown exception while connecting to websocket: %s", err) + else: + self._websocket_reconnect_retries = 0 - LOGGER.debug("Reconnecting to websocket") - await self._async_cancel_websocket_loop() + if self._websocket_reconnect_retries >= WEBSOCKET_RECONNECT_RETRIES: + LOGGER.error("Max websocket connection retries exceeded") + return + + delay = WEBSOCKET_RETRY_DELAY * (2 ** (self._websocket_reconnect_retries - 1)) + LOGGER.info( + "Retrying websocket connection in %s seconds (attempt %s/%s)", + delay, + self._websocket_reconnect_retries, + WEBSOCKET_RECONNECT_RETRIES, + ) + await asyncio.sleep(delay) self._websocket_reconnect_task = self._hass.async_create_task( self._async_start_websocket_loop() )