Skip to content

Let TokenExpiredError propagate from _async_request (fixes #5) - #7

Open
YeomansIII wants to merge 4 commits into
mainfrom
fix/token-expired-passthrough
Open

YeomansIII wants to merge 4 commits into
mainfrom
fix/token-expired-passthrough

Conversation

@YeomansIII

@YeomansIII YeomansIII commented Aug 27, 2026 •

Copy link
Copy Markdown
Owner

Fixes #5.

1. TokenExpiredError rewrapped as CannotConnectError (#5)

_async_request() re-raised (InvalidAuthError, ApiError, RateLimitError), but omitted TokenExpiredError — the exception async_refresh_tokens() raises when the refresh endpoint returns 401.

Per exceptions.py, TokenExpiredError subclasses AuthenticationError, not ApiError. So it matched only the bare except Exception and was rewrapped as CannotConnectError. auth.py:173-179 guards this exception deliberately. client.py undid that one frame later.

Downstream, consumers could not tell "credentials expired, the user must re-authenticate" apart from "the network is down". In ha-dominion-energy, the coordinator raised UpdateFailed instead of ConfigEntryAuthFailed, so Home Assistant retried forever and never created a reauth flow. Same symptom as YeomansIII/ha-dominion-energy#16, fixed for the ApiError 401/403 path in v0.2.2 but not for this one.

Fix: catch the AuthenticationError base class, which covers InvalidAuthError, TokenExpiredError, BrowserAuthRequiredError, and anything added later.

Two tests. test_expired_refresh_token_propagates mocks the API returning 401, then the refresh endpoint returning 401, and asserts TokenExpiredError reaches the caller. On main it fails with the exact text from the issue report:

CannotConnectError: Failed to connect to API: Refresh token expired - browser authentication required

test_connection_failure_still_raises_cannot_connect confirms a real ClientConnectorError still surfaces as CannotConnectError, so the wider except does not swallow transport errors.

2. Pin ruff and mypy

The dev extra used open ranges, so CI installed whatever was newest. ruff 0.16 began formatting Python code blocks inside Markdown, which broke ruff format --check . on main and on every new PR with no repo change to cause it. Reproduced with uvx ruff@0.16.4 format --check . on a clean main worktree.

Pinned ruff==0.16.4 and mypy==2.3.1, and moved .pre-commit-config.yaml to matching tags so local hooks, uv.lock and CI agree. One commit applies that formatter to README.md; it is cosmetic.

This also unblocks #6, whose only CI failure is the same README check.

3. Fix and centralize the user agent

Dominion sits behind an Incapsula WAF (see _async_load_login_page), which fingerprints User-Agent. Both strings in the library were wrong in ways a real browser never is:

  • const.py sent AppleWebKit/537.00 and Safari/537.00. Every Chrome build reports 537.36. It also sent macOS 10_15_0, where Chrome always reports 10_15_7, and Chrome 142, ten releases behind stable.
  • gigya_auth.py sent a truncated string that stopped after AppleWebKit/537.36, with no Chrome or Safari token at all — on the request that fetches the WAF cookies.

Moved to a single USER_AGENT constant matching Chrome 152 on macOS, used in both places, with tests that lock the format and set a version floor.

This may relate to #4, where every endpoint returns 403 with an HTML error page despite a valid token. That is what a WAF block looks like. Not confirmed — the reporter has not said which state or utility they use.

Verification

44 tests pass. ruff check, ruff format --check and mypy are clean. CI green on 3.12 and 3.13.

Follow-up

ha-dominion-energy needs a dompower>=0.2.3 bump in its manifest once this is released. Its coordinator already handles TokenExpiredError correctly.

_async_request() re-raised (InvalidAuthError, ApiError, RateLimitError) but
omitted TokenExpiredError, which async_refresh_tokens() raises on a 401 from
the refresh endpoint. TokenExpiredError subclasses AuthenticationError, not
ApiError, so it matched only the bare `except Exception` and was rewrapped as
CannotConnectError.

auth.py already guards this exception deliberately; client.py undid that one
frame later. Downstream, ha-dominion-energy's coordinator raised UpdateFailed
instead of ConfigEntryAuthFailed, so Home Assistant retried forever and never
created a reauth flow.

Catch the AuthenticationError base class instead, so InvalidAuthError,
TokenExpiredError, BrowserAuthRequiredError, and any future auth error keep
their type.

Bump version to 0.2.3.
ruff is unpinned in the dev extra, so CI now installs 0.16.4, which formats
Python code blocks inside Markdown. This breaks 'ruff format --check .' on
main as well. Cosmetic only - no code or documented behavior changes.
@YeomansIII

Copy link
Copy Markdown
Owner Author

Note on the second commit (README formatting): the first CI run failed on ruff format --check . against README.md, which this PR does not touch. That failure is pre-existing on main — ruff is unpinned in the dev extra, so CI now installs 0.16.4, which formats Python code blocks inside Markdown. Reproduced with uvx ruff@0.16.4 format --check . on a clean main worktree: same single-file failure. The last green run on main was in March with an older ruff.

Commit 14d0343 just applies that formatter to the README so this PR can merge green. Cosmetic only.

Worth pinning ruff and mypy in the dev extra separately, so CI does not break again on the next release — CI also picked up mypy 2.3.1 here, while .pre-commit-config.yaml pins 1.19.1.

The dev extra used open ranges (ruff>=0.6.0, mypy>=1.11.0), so CI installed
whatever was newest at the time. ruff 0.16 began formatting Python code blocks
inside Markdown, which broke 'ruff format --check .' on main and on every new
PR, with no repo change to cause it.

Pin ruff==0.16.4 and mypy==2.3.1, and move .pre-commit-config.yaml to the same
versions so local hooks, uv.lock and CI agree. Both are the versions CI has
already run green.
Dominion sits behind an Incapsula WAF, which fingerprints the User-Agent
header. Both strings in the library were wrong in ways a real browser never
is:

  const.py sent AppleWebKit/537.00 and Safari/537.00. Every Chrome build
  reports 537.36. It also sent macOS 10_15_0, where Chrome always reports
  10_15_7, and Chrome 142, which is ten releases behind stable.

  gigya_auth.py sent a truncated string that stopped after
  AppleWebKit/537.36, with no Chrome or Safari token at all. This is the
  header on the request that fetches the WAF cookies.

Move the string to a single USER_AGENT constant, correct it to match Chrome
152 on macOS, and use it in both places. Add tests that lock the format and
set a version floor.

This may relate to #4, where every endpoint returns 403 with an HTML error
page and a valid token. That is what a WAF block looks like. Not confirmed -
the reporter has not said which state or utility they use.
@dertbv

dertbv commented Oct 24, 2026

Copy link
Copy Markdown

Hit this in production: an expired refresh token showed up as CannotConnectError, so the integration's TokenExpiredError → auto-reauth branch never ran and the entry sat in setup_retry for 3 days. A test where both the request and the refresh return 401 fails on main and passes with this change. Any chance of a merge and a 0.2.x release, plus the dompower pin bump in ha-dominion-energy?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TokenExpiredError rewrapped as CannotConnectError in client.py, breaking downstream reauth

2 participants