Repository navigation
Conversation
When Wyze rejects the stored refresh token with code 2002 ("refresh token
is error"), refresh() raised a generic UnknownApiError and left the token
as it was. Every device poll goes through refresh_if_should(), so each
poll tried the same dead token again, about one request every 3 seconds
across a typical device list. After a few hours Wyze rate-limits the IP,
and logins from it get HTTP 429 too, so the user can't sign in again
until the block lifts.
refresh() now raises RefreshTokenError on 2002 and remembers the rejected
refresh token. refresh_if_should() and refresh() raise RefreshTokenError
straight away, without calling Wyze, for as long as the current token
still carries it. A new login replaces the token and clears the state.
RefreshTokenError subclasses AccessTokenError, so callers that already
start a re-login on AccessTokenError (ha-wyzeapi's setup and
token_exception_handler) handle it with no changes.
Refs SecKatie/ha-wyzeapi#999
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9c02de44f1
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| ) | ||
| response_json = await response.json() | ||
| if str(response_json.get("code")) == ResponseCodes.REFRESH_TOKEN_ERROR.value: | ||
| self._rejected_refresh_token = self.token.refresh_token |
There was a problem hiding this comment.
Cache the refresh token that was actually rejected
If overlapping refreshes produce a successful response followed by a delayed 2002 response, this assignment records the newly issued, valid refresh token as rejected instead of the token sent in the failed request. This overlap is possible through BaseService._monitoring_profile_state_status() (src/wyzeapy/services/base_service.py:730–731), which calls refresh() without taking refresh_lock. Every subsequent refresh_if_should() then fails locally despite the successful authentication, disabling device updates until reauthentication or restart. Associate the rejection with payload['refresh_token'] rather than reading mutable token state after the awaits, and cover this response ordering in a regression test.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Good catch, fixed in the latest commit: the rejection is now recorded from payload["refresh_token"] (the token this request actually sent), so a late 2002 can no longer mark a newer, valid token as rejected. Added test_late_rejection_does_not_poison_a_newer_token, which reproduces the overlap (a valid token lands while the 2002 is in flight). It fails on the old line and passes now; full suite 282 passed.
An overlapping refresh (BaseService._monitoring_profile_state_status calls refresh() without refresh_lock) can replace self.token with a valid token while a 2002 response is still in flight. Recording self.token then marked the new, valid token as rejected and blocked every later refresh. Record payload["refresh_token"] instead, and cover that ordering in a test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Fixes the retry loop in SecKatie/ha-wyzeapi#999.
The problem
When Wyze rejects the stored refresh token with code 2002 ("refresh token is error"),
refresh()raises a genericUnknownApiErrorand keeps the token. Every device poll goes throughrefresh_if_should(), andUpdateManagerlogs and swallows the error, so each poll tries the same dead token again. That comes to about one request every 3 seconds across a normal device list.On our Home Assistant install this ran for about three days before anyone noticed: 88,989 failed refreshes. Then Wyze rate-limited the home IP. Logins from that IP got HTTP 429 even with made-up credentials, so nobody could sign in again until the block lifted. #999 reports the same thing (about 17,500 attempts in 16 hours).
The fix
ResponseCodes.REFRESH_TOKEN_ERROR = "2002"and a newRefreshTokenError.refresh()remembers which refresh token Wyze rejected and raisesRefreshTokenError.refresh_if_should()andrefresh()raiseRefreshTokenErrorstraight away without calling Wyze. Polling keeps running but sends nothing to Wyze, so the IP doesn't get rate-limited.get_token_with_username_password,get_token_with_2fa, or a caller setting a newToken) replaces the refresh token, which clears the state with no extra bookkeeping.RefreshTokenErrorsubclassesAccessTokenError. ha-wyzeapi's setup (except AccessTokenError→ConfigEntryAuthFailed) and itstoken_exception_handleralready start a re-login onAccessTokenError, so they handle this with no changes. ha-wyzeapi pinswyzeapy>=0.8.1,<0.9.0, so a patch release reaches users without an integration release.Not in this PR
UpdateManagerstill logs a traceback each time a device poll fails. Those are now local log lines only, with no network calls, but quieter logging there would be a reasonable follow-up.UpdateManagerdoesn't raise into Home Assistant, so the re-login prompt shows up the next time something calls through atoken_exception_handlerpath, or at the next restart (setup). Starting re-login straight from the poller would need a hook from wyzeapy to the integration. That's a larger change and I've left it out here.Tests
Three new tests in
tests/test_wyze_auth_lib.py:RefreshTokenError, which is also anAccessTokenError;refresh_if_should()andrefresh()calls raise without a second request to Wyze (the mockedpostis called exactly once);pytest: 281 passed.ruff checkandruff format --checkare clean.🤖 Generated with Claude Code