♻️ Skip redundant energy fetches and emit statistics at real timestamps #18
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "fix/5-energy-hour-offset"
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?
Summary
Closes MateoGreil/homeassistant-comwatt#3, MateoGreil/homeassistant-comwatt#5, MateoGreil/homeassistant-comwatt#42.
Two related fixes to
ComwattEnergySensorbundled because they live in the same code path:Skip the call when the bucket can't have changed (#3)
The QUANTITY/HOUR endpoint only publishes a new bucket once per hour, so polling every 2 min was 29 wasted calls per hour per device. The coordinator now tracks the last successful fetch time per device and skips the call while the cached result is < 55 min old. Power fetches are unchanged.
Emit statistics at the real bucket timestamp (#5, #42)
Previously the energy value was published under
noweven though the API bucket represents the previous hour, so the Energy dashboard attributed each reading to the wrong hour. The coordinator now forwards each new hourly bucket toasync_add_external_statisticswith the real bucket timestamp and a running cumulative sum, under an external statistic idcomwatt:<device_id>_total_energy.sensor.<device>_total_energyentity unchanged — users whose Energy dashboard points at it keep working.comwatt:*_total_energyattributes energy to the correct hour; users can opt into it by adding it to the dashboard.Guarded on
"recorder" in hass.config.componentsso the coordinator stays usable when recorder is disabled.Test plan
test_energy_fetch_is_skipped_within_interval— second refresh within the window does not re-call QUANTITY; advancing the cached timestamp triggers the call again.test_new_energy_buckets_are_pushed_as_external_statistics— two new buckets produce oneasync_add_external_statisticscall with the correct statistic id, cumulative sum, unit, and top-of-hour timestamp.pytest tests/— 26 passed;ruff+mypyclean.comwatt:<device>_total_energyappears in the statistic picker. Compare the hour alignment to the existingsensor.*_total_energyentity.4a14c4d4251b23292466View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.