Add token refresh and 401 retry for expired auth#558
Open
JeffSteinbok wants to merge 1 commit intomainfrom
Open
Add token refresh and 401 retry for expired auth#558JeffSteinbok wants to merge 1 commit intomainfrom
JeffSteinbok wants to merge 1 commit intomainfrom
Conversation
Tokens were fetched once at login with no refresh mechanism. If the token expired during runtime, all REST API calls and WebSocket reconnections would fail silently until a full HA restart. Changes: - helpers.py: Return actual HTTP status code on non-200 responses - pydreo/__init__.py: Add _re_login() method that re-authenticates and updates the WebSocket transport token. call_dreo_api() now detects 401 responses and automatically retries once after re-login (login API itself is excluded to prevent infinite loops). - commandtransport.py: Add update_token() method. Restructure _start_websocket() to rebuild the WebSocket URL on each reconnect attempt, picking up any token updates from re-login. Includes 10 new tests covering 401 retry, re-login success/failure, transport token update, and WebSocket reconnect with fresh tokens. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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 High #2 from code review: Token refresh / auth retry
Tokens were fetched once at login with no refresh mechanism. If the token expired during runtime, all REST API calls and WebSocket reconnections would fail silently until a full HA restart.
Changes
helpers.pyNonefor error codes)pydreo/__init__.py_re_login()— Re-authenticates and updates the WebSocket transport tokencall_dreo_api()— Detects 401 responses and automatically retries once after re-login (login API itself is excluded to prevent infinite loops)commandtransport.pyupdate_token()— Allows the PyDreo manager to inject a refreshed token_start_websocket()— Restructured to rebuild the WebSocket URL on each reconnect attempt, picking up any token updates. Previously the URL was built once and reused for all reconnection attempts.Tests
10 new tests:
_re_loginupdates transport token_re_loginfailure returns False_re_loginskips transport update in debug modeupdate_tokenupdates stored token