fix: force re-login on every update cycle instead of reusing expired token#34
Conversation
…token Previously the coordinator only called login() when access_token was None. On auto-refresh, the existing API client retains the old token/session, which may have expired. Expired tokens cause API calls to silently fail (returning empty data or error state with HTTP 200 instead of 401), so no data gets saved to SQLite and sensors show stale values. Now login() is called on every update cycle to ensure a fresh token/session before fetching data, matching the behavior of a manual reload. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughThe coordinator's data update now always closes the API session, calls ChangesAuthentication enforcement in coordinator
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning Billing warning: we have not been able to collect payment for this subscription for more than 72 hours. Please update the payment method or pay any pending invoices in Billing to avoid service interruption. Comment |
There was a problem hiding this comment.
Pull request overview
This PR updates the Home Assistant EVN VN coordinator to re-authenticate on every scheduled refresh to avoid stale sensor data caused by expired tokens/sessions being reused across update cycles.
Changes:
- Always call
EVNAPI.login()at the start of each_async_update_data()execution. - Fail the update cycle immediately if login fails (via
UpdateFailed("Failed to login")).
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| # Always re-login to ensure fresh token/session before fetching data | ||
| if not await self.api.login(): | ||
| raise UpdateFailed("Failed to login") | ||
|
|
…nnections The root cause of auto-update not working despite 401 retry correctly re-logging in: the cached aiohttp ClientSession retains stale TCP connections that the server may have closed during idle time between update cycles (10 min). When requests fail due to dead connections, data is not saved to SQLite and sensors show old values. On reload, a new EVNAPI creates a fresh ClientSession, which is why reload works but auto-refresh doesn't. Also always call login() to ensure fresh token, matching reload behavior. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Summary
Fix auto-update not working - sensors showing stale data despite coordinator running every 10 minutes.
Root Cause
The coordinator only called
login()whenself.api.access_tokenwasNone:On auto-refresh, the existing API client retains the old token/session, which may have expired. Expired tokens cause API calls to silently fail (returning empty data or error state with HTTP 200 instead of 401), so no data gets saved to SQLite and sensors show stale values.
On manual reload, a fresh API client is created (
access_token = None), sologin()is called and gets a fresh token - which is why reload works.Fix
Always call
login()before each update cycle to ensure fresh token/session:This matches the behavior of a manual reload.
Test Plan
lan_cap_nhat_cuoiupdates every 10 minutes as before🤖 Generated with Claude Code
Summary by CodeRabbit