Skip to content

Drop Home Assistant per-entity staleness check (delegate to integration)#365

Merged
tomquist merged 3 commits into
developfrom
claude/simplify-ha-staleness-363
May 16, 2026
Merged

Drop Home Assistant per-entity staleness check (delegate to integration)#365
tomquist merged 3 commits into
developfrom
claude/simplify-ha-staleness-363

Conversation

@tomquist
Copy link
Copy Markdown
Owner

Summary

Follow-up to #364 for #363. The 60 s per-entity timer and the REST last_reported fallback merged in #364 close the ESPHome/Slimmelezer case but not the push-based case @DerDaehne flagged: when a smart meter (e.g. ha-tibber-pulse-local) only pushes on value changes, HA's last_reported stays old too, so the REST fallback bails and re-raises the same false stale error.

HA integrations already own this signal. They set sensors to unavailable/unknown when their upstream source dies (ESPHome device offline, tibber-pulse websocket dead, etc.), and _update_entity_value already converts those to None, on which _get_entity_value already raises. A constant numeric value is legitimate steady-state behaviour — there's nothing for us to check on top.

This PR removes the timer, the REST fallback, the wall-clock parsing of last_reported, the injectable clock, and max_state_age_seconds. Net ~600 lines deleted.

aiohttp's WebSocket heartbeat (30 s) still catches dead TCP and triggers reconnect; the reconnect block now invalidates cached values to None so callers can't serve them across an extended outage.

Test plan

https://claude.ai/code/session_01LwA9fBRZcLjRi7fEU4fHyw


Generated by Claude Code

The 60 s per-entity timer and its REST-based last_reported fallback were
chasing a problem the integration layer already owns. HA integrations
set their sensors to unavailable/unknown when their upstream source goes
silent (ESPHome device offline, ha-tibber-pulse-local websocket dead,
etc.); _update_entity_value already converts those to None, which
_get_entity_value already raises on. Constant numeric values are
legitimate steady-state behaviour and must not be flagged.

This also covers the push-based integration case (issue #363 follow-up):
when a smart meter pushes only on value changes, HA's last_reported
stays old too, so the merged REST fallback bailed and re-raised the
same false stale error. Trusting unavailable/unknown is the only
correct freshness signal here.

aiohttp's websocket heartbeat (30 s) still catches dead TCP and
triggers reconnect; the reconnect block now invalidates cached values
so callers can't serve them across an extended outage.

Net: ~600 lines removed (staleness machinery + REST fallback + tests).
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 16, 2026

Warning

Rate limit exceeded

@tomquist has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 9 minutes and 2 seconds before requesting another review.

You’ve run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 712765c9-339d-48e5-86a3-cef0a74c68b9

📥 Commits

Reviewing files that changed from the base of the PR and between 847d9cd and f1fc507.

📒 Files selected for processing (3)
  • CHANGELOG.md
  • src/astrameter/powermeter/homeassistant.py
  • src/astrameter/powermeter/homeassistant_test.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/simplify-ha-staleness-363

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

claude added 2 commits May 16, 2026 09:36
- Extract _ws_loop reset block into _reset_for_reconnect so the test
  drives the real method rather than open-coding the same four
  statements: a regression in the production reset is now caught.
- Parametrize state_reported wake test over both lu and lc, and assert
  the cached numeric value is preserved (keepalive carries no s).
- Add test_unavailable_blocks_wait_for_message covering the consequence
  of a sensor going unavailable mid-stream: wait_for_message must block
  again instead of returning the cached "ready" flag.
- Move the power_input_alias / power_output_alias length-mismatch
  ValueError from get_powermeter_watts into __init__. The aliases are
  a static config invariant; fail fast at construction instead of on
  every call.
- Reword the CHANGELOG bullet to make the goal explicit: a sensor is
  treated as stale only when HA marks it unavailable/unknown or the
  websocket is lost. Mentions push-based integrations alongside the
  constant-value case so readers don't think the fix is only about
  rare edge cases.
@tomquist tomquist merged commit 3473793 into develop May 16, 2026
13 checks passed
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.

2 participants