Fix InvalidSessionException treated as permanent auth failure - #520
Merged
iMicknl merged 1 commit intoAug 2, 2026
Merged
Conversation
Two changes: 1. Change InvalidSessionException to inherit from BaseSagemcomException instead of UnauthorizedException. An invalid session is a transient error (e.g., router invalidated the session due to a concurrent login), not a permanent authorization failure. Consumers that catch UnauthorizedException to signal permanent auth failures (like Home Assistant's ConfigEntryAuthFailed) should not catch transient session errors. 2. Increase max_tries from 1 to 2 on all backoff-decorated API methods. With max_tries=1, the on_backoff=retry_login handler fires but no retry actually occurs. With max_tries=2, after an InvalidSessionException the client will re-login and retry the operation once before giving up.
iMicknl
approved these changes
Aug 2, 2026
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.
Summary
InvalidSessionExceptioninherits fromUnauthorizedException, which causes consumers (like the ha-sagemcom-fast integration) to treat transient session errors as permanent authorization failures.Problem
When the router invalidates a session (e.g., due to a concurrent login from the web interface), the API raises
InvalidSessionException. Because it inherits fromUnauthorizedException, consumers that catchUnauthorizedExceptionto detect permanent auth failures (like Home Assistant raisingConfigEntryAuthFailed) incorrectly treat this transient condition as "bad credentials", permanently disabling the integration.Additionally,
max_tries=1on the backoff decorators means theon_backoff=retry_loginhandler fires but no actual retry occurs — the function is only called once.Fix
Change exception hierarchy:
InvalidSessionExceptionnow inherits fromBaseSagemcomExceptioninstead ofUnauthorizedException. This is a transient error, not a permanent auth failure.Increase
max_triesto 2: Withmax_tries=2, when anInvalidSessionExceptionoccurs, the backoff handler callsretry_login()and then retries the operation once before giving up.Breaking Change Note
This is a minor breaking change for consumers that catch
UnauthorizedExceptionand expectInvalidSessionExceptionto be included. Consumers should update to catchInvalidSessionExceptionseparately if they need to handle it. The ha-sagemcom-fast integration already has a PR to handle this.Reproduction
get_hosts()raisesInvalidSessionExceptionUnauthorizedException→ permanent failureAfter this fix, the library retries the login and operation automatically.