🐛 Keep device energy totals monotonic #73

Merged
mat merged 3 commits from fix-monotonic-energy into main 2026-08-24 14:37:16 +00:00
Owner

Context

Fixes the device energy regression described in GitHub issue #50:
https://github.com/MateoGreil/homeassistant-comwatt/issues/50

The WebSocket power integral can temporarily exceed Comwatt's official hourly bucket. Reconciliation then applies a negative correction to a sensor exposed as total_increasing, causing backward state transitions and negative Recorder deltas.

Impact

  • Device *_total_energy sensors never decrease in Home Assistant.
  • Internal reconciliation remains active, so the corrected value can catch up normally.
  • Positive corrections and live accumulation are published immediately.
  • The published high-water mark survives Home Assistant restarts, unloads, and final writes.
  • Existing stored state migrates without changing Recorder history.

Implementation notes

  • Adds a persisted published high-water value separate from the internal live total.
  • Serializes executor refreshes and WebSocket energy publication to prevent stale snapshots from overwriting newer values.
  • Site energy totals and unrelated integrations are unchanged.

Verification

  • 119 tests passed
  • compileall passed
  • git diff --check passed

This PR is intentionally opened as a draft for review.

## Context Fixes the device energy regression described in GitHub issue #50: https://github.com/MateoGreil/homeassistant-comwatt/issues/50 The WebSocket power integral can temporarily exceed Comwatt's official hourly bucket. Reconciliation then applies a negative correction to a sensor exposed as `total_increasing`, causing backward state transitions and negative Recorder deltas. ## Impact - Device `*_total_energy` sensors never decrease in Home Assistant. - Internal reconciliation remains active, so the corrected value can catch up normally. - Positive corrections and live accumulation are published immediately. - The published high-water mark survives Home Assistant restarts, unloads, and final writes. - Existing stored state migrates without changing Recorder history. ## Implementation notes - Adds a persisted published high-water value separate from the internal live total. - Serializes executor refreshes and WebSocket energy publication to prevent stale snapshots from overwriting newer values. - Site energy totals and unrelated integrations are unchanged. ## Verification - 119 tests passed - `compileall` passed - `git diff --check` passed This PR is intentionally opened as a draft for review.
🐛 serialize monotonic energy publication
All checks were successful
Validate / lint-ruff (push) Successful in 7s
Validate / test-pytest (push) Successful in 3m15s
Validate / type-check-mypy (push) Successful in 3m15s
Validate / lint-ruff (pull_request) Successful in 7s
Validate / test-pytest (pull_request) Successful in 3m14s
Validate / type-check-mypy (pull_request) Successful in 3m16s
845362b580
mat changed title from WIP: 🐛 Keep device energy totals monotonic to 🐛 Keep device energy totals monotonic 2026-08-24 14:37:04 +00:00
mat merged commit 4a9d94691a into main 2026-08-24 14:37:16 +00:00
Sign in to join this conversation.
No reviewers
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/homeassistant-comwatt!73
No description provided.