Repository navigation
fix: reconnect MQTT subscription with backoff after a dropped connection - #49
Open
jasperchan wants to merge 1 commit into
Open
jasperchan wants to merge 1 commit into
jasperchan wants to merge 1 commit into
Conversation
subscribe_mqtt ran a single aiomqtt session: any MqttError (timeout on connect, "Disconnected during message iteration") was logged and the task ended. Nothing re-established the subscription until the scheduled Cognito token refresh (~2.9 h later) or a manual config-entry reload, so Home Assistant silently stopped receiving activity_state pushes and every command failed after the 30 s wait in send_command. Fixes Lash-L#47. - subscribe_mqtt now loops: each attempt runs one session via _run_mqtt_session and a failure is retried with exponential backoff (1 s .. 300 s), reset once a connection has stayed up for 60 s. - Each attempt reads the current id token, so a refreshed token is used on the next connect. - Cancellation (disconnect / schedule_reauthorization) still ends the task. - A failing callback or unparsable payload is logged instead of dropping the connection. - Log through the module logger instead of the root logger, so `python_snoo` debug logging shows the connection lifecycle. - Add pytest coverage for connect failure, mid-stream disconnect, _client_map bookkeeping, cancellation and token pick-up. Claude-Session: https://claude.ai/code/session_01Kjt2zjjJbbcYXsbXpzBUQk
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #47 (the "subscription never retries" half; the logger half overlaps with #48, happy to rebase on it if that lands first).
Problem
subscribe_mqttruns a singleaiomqtt.Clientsession. AnyMqttErroris logged and the task simply ends, and nothing re-establishes the subscription untilschedule_reauthorizationfires (~2 h 55 min later) or the user reloads the config entry. In the meantime Home Assistant silently stops receivingactivity_statepushes (entities look available but freeze) and every command raises after the 30 s wait insend_command.Seen today on HA 2026.10.4 / python-snoo 0.8.3: one
MQTT connection for <serial> failed: Disconnected during message iterationat 13:31 and then total silence until a manual reload at 13:44. The code path is unchanged in 0.11.0.Change
subscribe_mqttnow loops. Each attempt runs one session (_run_mqtt_session); a connect failure or a dropped connection is retried with exponential backoff (1 s → 300 s), and the backoff resets once a connection has stayed up for 60 s.tokens.aws_id, so a token refreshed byschedule_reauthorizationis used on the next connect. Cancellation (fromdisconnect/ the refresh path) still ends the task as before._client_mapbookkeeping is unchanged in spirit: the entry is removed whenever the session ends and re-added on reconnect, sosend_commandwaits for a live client instead of publishing into a dead one.python_snoo.snoo) instead of the root logger, sologger: python_snoo: debugin HA actually shows it.Tests
Added
tests/test_mqtt_reconnect.pywith a scripted fakeaiomqtt.Clientcovering: connect failure then success, mid-stream disconnect,_client_mapstate across a drop, cancellation stopping the loop, and a refreshed token being picked up by the next attempt.pytestandpre-commit run --all-filespass locally.https://claude.ai/code/session_01Kjt2zjjJbbcYXsbXpzBUQk