✨ feat: auto-migrate stale Wh statistics on site power sensors #72
No reviewers
Labels
No labels
v1.0 · bloquant
v1.0 · majeur
v1.0 · mineur
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
mat/homeassistant-comwatt!72
Loading…
Reference in a new issue
No description provided.
Delete branch "migrate-site-stats"
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?
Stacked on #70. Makes the unit correction of #70 transparent for users: no manual step in Developer Tools.
Why
#70 corrects the 6 site sensors from
WhtoW(they were instantaneous watts mislabelled as watt-hours). On HA ≥ 2025.12 that leaves stale Wh long-term statistics behind, which raise aunits_changedrepair per entity and suppress statistics compilation until the old statistics are deleted by hand. No conversion is possible (watts and watt-hours are different physical dimensions — and the stored values were wrong anyway), so they must be deleted.What it does
On config entry setup, best-effort and idempotent:
recordercomponent is not loaded;entity_ids through the entity registry (site_{site_id}_{key}), so user renames are handled and nothing else can ever be targeted;async_list_statistic_ids;get_instance(hass).async_clear_statistics(...).Same pattern as
duke_energyandista_ecotrendin HA core. Raw history states are kept; only long-term statistics restart. Any failure is logged and never blocks setup.Safety: which statistics can be deleted
Only those whose recorded unit is not a power unit at all:
Wh(the stale ones)WkW,MW,mW,GW,TW(user display override)This matters: a naive
!= "W"comparison would have deleted the valid statistics of any user who overrode the display unit to kW — and would have done so on every restart, because the recompiled statistics would again be in kW. Verified in the HA source:_normalize_statessetsstatistics_unit = state_unitfor a first-seen sensor, andSensorEntity.unit_of_measurementgives highest priority to the user's chosen unit. A dedicated regression test covers the six power units.The device
*_total_energyentities (legitimately in Wh) have different unique_ids and can never be touched.Also
Restores
"after_dependencies": ["recorder"]in the manifest — introduced by #53, silently lost during the manifest rewrite in81f6fcc, and required again now that__init__.pyimports the recorder.Tests
101 total (95 baseline + 6 new): stale Wh cleared, all-W idle, user-overridden power units kept, no recorder, failure does not break setup, fresh install no-op. ruff + mypy clean.
Merge order
Merge #70 first, then this PR (its base should be retargeted to
mainonce #70 is merged). The migration note in #70's description can then be replaced by "handled automatically".WIP: feat: auto-migrate stale Wh statistics on site power sensorsto ✨ feat: auto-migrate stale Wh statistics on site power sensors