From 0a7f4104c0c0307c9a849486c3eae14dbc40d2ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joris=20Pelgr=C3=B6m?= Date: Fri, 1 Aug 2025 15:43:10 +0200 Subject: [PATCH] Adjust connection failure backoff and logging - When the connection fails, immediately retry once and only backoff starting with the second attempt - Adjust logging for recoverable connection failures: log as info for error 1/reconnected, debug for error >1 --- letpot/deviceclient.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/letpot/deviceclient.py b/letpot/deviceclient.py index c6b09af..2f3afa2 100644 --- a/letpot/deviceclient.py +++ b/letpot/deviceclient.py @@ -212,6 +212,9 @@ async def _connect(self) -> None: tls_insecure=False, websocket_path="/mqttwss", ) as client: + if connection_attempts >= 1: + _LOGGER.info("Reconnected to MQTT broker") + self._client = client self._message_id = 0 connection_attempts = 0 @@ -239,14 +242,16 @@ async def _connect(self) -> None: raise auth_exception from err connection_attempts += 1 - reconnect_interval = min(connection_attempts * 15, 600) - _LOGGER.error( - "MQTT error, reconnecting in %i seconds: %s", - reconnect_interval, - err, - ) - - await asyncio.sleep(reconnect_interval) + if connection_attempts == 1: + _LOGGER.info("MQTT connection error, reconnecting...: %s", err) + else: + reconnect_interval = min((connection_attempts - 1) * 15, 600) + _LOGGER.debug( + "MQTT connection error, retrying in %i seconds: %s", + reconnect_interval, + err, + ) + await asyncio.sleep(reconnect_interval) finally: self._client = None if self._connected is not None: