Fix event re-firing, spot price timezone/staleness, reauth and token handling#2
Merged
Merged
Conversation
…handling - Event entities: skip processing on failed coordinator refreshes so stale signals (CM10 status change, logbook/news entries) don't fire duplicate events on every failed cycle. - Spot price: compare slots against Europe/Stockholm local time instead of the host's local time, and re-select the current 15-min slot every coordinator cycle instead of only at the hourly fetch. - Use Sweden's calendar date for the spot price fetch range. - Reauth flow: reject credentials for a different account instead of silently rebinding the entry; pre-fill the existing email. - API client: invalidate the cached JWT when the server returns 401 so the next cycle re-authenticates instead of looping until local expiry. - Monthly revenue: tolerate explicit null NetRevenue values. - Raise minimum Home Assistant version to 2024.11 (ConfigFlowResult, _get_reauth_entry and data_updates are newer than the claimed 2024.1). https://claude.ai/code/session_01UKMpv4yQmMeG86GiRnvfkU
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 the seven correctness/compatibility issues found in the code review:
Event entities re-fired duplicate events after a failed refresh —
DataUpdateCoordinatornotifies listeners on every refresh, including failed ones wherecoordinator.datastill holds the previous payload. Event entities now skip processing whenlast_update_successis false, so a stalecm10_status_changed/new_logbook_entries/new_news_itemssignal can't trigger the same events once per failed cycle during an API outage.Spot price used the host's local timezone — slot timestamps from
/ems/spotpriceare naive Swedish local time, but they were compared against the server's local clock. On any HA host outside Europe/Stockholm (e.g. a UTC cloud VM) the wrong slot was selected. Comparison now uses an explicitZoneInfo("Europe/Stockholm")clock, and the fetch date range uses Sweden's "today" as well.Spot price sensor lagged up to an hour — the current slot was only selected at fetch time (every 60 min) even though slots are 15 min. The day's price list is now cached and the current slot is re-selected on every 60-second coordinator cycle (
_select_spot_price, covered by new unit tests).Declared minimum HA version was wrong — the code uses
ConfigFlowResult(2024.2),data_updates(2024.4) and_get_reauth_entry(2024.11), buthacs.jsonclaimed 2024.1.0. Bumped to 2024.11.0 (README updated to match).Reauth could silently rebind the entry to a different account — the reauth form accepted any valid credentials, leaving the entry's unique_id and device pointing at the old account. The flow now rejects a username that doesn't match the entry's unique_id (new
account_mismatcherror string in en/sv) and pre-fills the existing email.A server-side 401 never invalidated the cached JWT — a revoked/rejected token caused repeated failures until its local expiry (up to 24 h in the fallback path).
_get/_get_textnow drop the cached JWT on 401 so the next cycle re-authenticates.Monthly revenue broke on explicit
nullNetRevenue —r.get("NetRevenue", 0)returnsNonefor JSON nulls, makingsum()raise and leaving the sensor silently stale. Now coalesced withor 0.Tests: 49 passed (6 new for spot price slot selection), ruff check/format clean.
https://claude.ai/code/session_01UKMpv4yQmMeG86GiRnvfkU
Generated by Claude Code