Fix transient resets in cloud power statistics - #2900
Open
Wuty-zju wants to merge 3 commits into
Open
Conversation
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
There was a problem hiding this comment.
Pull request overview
This PR hardens cloud-backed daily/monthly energy (“power cost”) statistics updates to prevent transient missing/invalid cloud responses from being emitted as 0, which Home Assistant interprets as a reset for total_increasing sensors and can lead to double-counted long-term statistics.
Changes:
- Update the
micloud_statistics_power_costtemplate to distinguish missing data (None) from a measured zero and to ignore invalid/negative record values. - Add device-side validation and filtering for power statistics responses, including monotonicity enforcement within the current local day/month and independent handling of suffixed keys (
_2,_3, …). - Add focused tests covering missing/malformed responses, explicit zeros vs missing records, monotonicity rules, suffix handling, and DST fallback.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| tests/test_power_cost_statistics.py | Adds regression/unit tests for missing data handling, monotonicity, suffix independence, and failure modes. |
| custom_components/xiaomi_miot/core/templates.py | Changes the cloud power-cost template to output None for missing periods and ignore invalid record values. |
| custom_components/xiaomi_miot/core/device.py | Adds response validation, template error handling for power-cost stats, and an attribute filter to reject invalid/decreasing same-period updates. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Wuty-zju
force-pushed
the
fix/cloud-power-statistics-reset
branch
from
July 29, 2026 08:31
412f5e8 to
4097393
Compare
Wuty-zju
force-pushed
the
fix/cloud-power-statistics-reset
branch
from
July 29, 2026 08:41
4097393 to
462797b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Cloud-backed daily and monthly energy entities can transiently report resets or stale values:
Missing current-period data can also leave the previous period's final value visible after a day/month boundary. In addition, restored native values could be passed through
value_ratioagain for models that use scaled cloud statistics.Root cause
micloud_statistics_power_costinitialized missing daily/monthly values to0.Noneor decreasing values reachedself.propsbefore entity-level validation.value_ratiotwice.Invariants
Solution
core/templates.pypreserves missing current-day/current-month values asNone, while retaining an explicit numeric zero.core/device.pyfilters missing, invalid, and same-period decreasing power statistics beforeself.props.update()and dispatch. Other cloud statistics remain unchanged.core/utils.pycontains the shared power-cost attribute matching, period calculation, and finite non-negative numeric validation used by both layers.sensor.pyretains the entity boundary as a second guard, performs local day/month rollover, discards stale cross-period restores, and directly restores the already-normalized HA native value without reapplyingvalue_ratio.power_cost_today_2are tracked independently.The device and entity guards are intentionally separate: the device guard protects the shared property store, while the entity guard covers HA restore state and direct entity updates.
Scope
This PR does not change:
Historical raw states are not rewritten automatically. Permanent long-term statistic errors should be reviewed through Home Assistant's supported statistics tools.
Validation
Focused replay passed for:
1750 * 0.01 = 17.5, restored as17.5, not0.175).Repository validation:
python -m compileall -q custom_components/xiaomi_miot: passed.git diff --check: passed.Examples