Auto re-authenticate and retry once on 401 #15

Merged
mat merged 2 commits from feat/auto-reauth into main 2026-07-04 09:10:42 +00:00
Owner

Slice 2 of 2 — point #8 (session-expiry handling)

Stacked on #14 (base branch refactor/central-request-helper). Review/merge #14 first; this diff shows only the re-auth changes.

Transparent auto re-authentication: on a 401 the client silently re-logs-in once
(reusing credentials captured at authenticate() time) and retries the request once.
This is exactly what the Home Assistant integration needs to stop falling into
ConfigEntryAuthFailed every time the cwt_session cookie expires.

Design (agreed with maintainer):

  • ComwattClient(auto_reauth=True) — new flag, on by default; auto_reauth=False disables.
  • Hash-only credential retention — stores username + the sha256 hash, never the raw password.
  • Re-auth + retry logic lives only in the central _request helper; one retry, no loop.
  • Only 401 triggers it. 403 keeps raising ComwattAPIError.
  • authenticate() and is_authenticated() never auto-retry (the auth probe still returns True/False).

Tests: +6 (tests/test_reauth.py) covering happy path, auto_reauth=False, no-stored-creds,
re-auth-then-still-401 (single retry), is_authenticated unaffected, and no-raw-password-retained.
Full suite green (38 passed).

Draft (WIP) — remove the WIP prefix to mark ready for review.

## Slice 2 of 2 — point #8 (session-expiry handling) **Stacked on #14** (base branch `refactor/central-request-helper`). Review/merge #14 first; this diff shows only the re-auth changes. Transparent auto re-authentication: on a **401** the client silently re-logs-in once (reusing credentials captured at `authenticate()` time) and retries the request once. This is exactly what the Home Assistant integration needs to stop falling into `ConfigEntryAuthFailed` every time the `cwt_session` cookie expires. Design (agreed with maintainer): - `ComwattClient(auto_reauth=True)` — new flag, on by default; `auto_reauth=False` disables. - **Hash-only credential retention** — stores `username` + the sha256 hash, never the raw password. - Re-auth + retry logic lives only in the central `_request` helper; **one** retry, no loop. - **Only 401** triggers it. **403** keeps raising `ComwattAPIError`. - `authenticate()` and `is_authenticated()` never auto-retry (the auth probe still returns `True`/`False`). **Tests:** +6 (`tests/test_reauth.py`) covering happy path, `auto_reauth=False`, no-stored-creds, re-auth-then-still-401 (single retry), `is_authenticated` unaffected, and no-raw-password-retained. Full suite green (38 passed). _Draft (WIP) — remove the WIP prefix to mark ready for review._
Auto re-authenticate and retry once on 401
All checks were successful
Tests / forgejo-pytest (push) Successful in 37s
Tests / forgejo-pytest (pull_request) Successful in 37s
1b0de0feba
mat changed target branch from refactor/central-request-helper to main 2026-07-04 09:08:04 +00:00
mat changed title from WIP: Auto re-authenticate and retry once on 401 to Auto re-authenticate and retry once on 401 2026-07-04 09:10:04 +00:00
mat force-pushed feat/auto-reauth from 1b0de0feba
All checks were successful
Tests / forgejo-pytest (push) Successful in 37s
Tests / forgejo-pytest (pull_request) Successful in 37s
to c826981f6f
All checks were successful
Tests / forgejo-pytest (push) Successful in 37s
Tests / forgejo-pytest (pull_request) Successful in 37s
2026-07-04 09:10:29 +00:00
Compare
mat merged commit 59351ec0a9 into main 2026-07-04 09:10:42 +00:00
mat deleted branch feat/auto-reauth 2026-07-04 09:10:42 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
mat/python-comwatt-client!15
No description provided.