🐛 persist live energy state across HA restarts #65
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "persist-energy-state"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Fix the
*_total_energysensors jumping backwards by thousands of Wh at every HA restart/reload.Problem
The legacy seed path (
state.total_wh += val, used whenlive_total_wh is Noneat restart/first-install) summed rawQUANTITY/HOURbuckets WITHOUT the kWh→Wh unit conversion PR #64 added to the reconciliation path, AND only knew the last 24h. Measured live at one restart: solar −24636 Wh, injection −3881 Wh, soutirage −8482 Wh. Sensors arestate_class: total_increasing, so each restart corrupted the HA Energy dashboard.Fix
live_total_wh+live_by_hour+last_bucket_ts+total_whper device viahass.storage.Store(keycomwatt.energy_state, version 1).async_setup_entryBEFOREasync_config_entry_first_refresh._server_bucket_to_wh(val, live_wh)to the legacy seed path too (unit-safe first-install seed: skips buckets with no live reference → 0.0 until the stream produces one, ≤ ~15s of energy lost; hourly reconciliation self-heals).After this, counters are forward-only across restarts; the server endpoint keeps its role as the hourly drift-correction source.
Tests
6 new (RED→GREEN): restore round-trip, poll-cycle persistence, schema versioning, ISO hour round-trip, unit-safe first-install, legacy-seed-skip. 85 total passing, ruff + mypy clean.
Scope
coordinator.py,__init__.py,tests/test_coordinator.py+tests/test_sensor.py(test that directly asserted the buggy raw-accumulation behavior). No changes to stream/sensor/switch/const/manifest.Follow-ups (non-blocking): swallowed-warning save wrapper, store
migrate_func, prune removed-device keys, stream-burst-after-restore test.WIP: fix: persist live energy state across HA restarts (Problem 1)to WIP: 🐛 fix: persist live energy state across HA restarts (Problem 1)WIP: 🐛 fix: persist live energy state across HA restarts (Problem 1)to WIP: 🐛 persist live energy state across HA restarts (Problem 1)WIP: 🐛 persist live energy state across HA restarts (Problem 1)to WIP: 🐛 persist live energy state across HA restartsWIP: 🐛 persist live energy state across HA restartsto 🐛 persist live energy state across HA restarts