Fix soc_max_calculated 7-day history being wiped on every Home Assistant restart#4259
Merged
Merged
Conversation
Copilot
AI
changed the title
[WIP] Fix battery scaling auto history wipe on HA restart
Fix soc_max_calculated 7-day history being wiped on every Home Assistant restart
Jul 16, 2026
springfall2008
marked this pull request as ready for review
July 17, 2026 08:15
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses a Home Assistant restart edge case where sensor.*_soc_max_calculated’s 7‑day rolling history attribute was effectively treated as empty after restart, causing the trimmed-mean smoothing (and thus battery_scaling_auto / soc_max) to jump based on a single day.
Changes:
- Switch
Inverter.battery_size_tracking()andInverter.update_soc_max_calculated_sensor()to read thehistoryattribute viaload_previous_value_from_ha()so recorder history is used when the live entity is missing. - Fix
TestHAInterface.get_state()to honordefaultfor missing entities even when anattributeis requested. - Add a regression test simulating “entity missing after restart but recorder history present” to ensure trimmed-mean behavior is preserved.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| apps/predbat/inverter.py | Uses recorder-backed reads for soc_max_calculated history to preserve the rolling window across HA restarts. |
| apps/predbat/tests/test_infra.py | Aligns test HA interface behavior with real HAInterface for missing-entity + attribute reads. |
| apps/predbat/tests/test_find_battery_size.py | Adds regression coverage for history recovery after simulated HA restart. |
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.
sensor.predbat_soc_max_calculatedstores a 7-day rolling history whose trimmed mean feedsbattery_scaling_auto/soc_max. That history lived only in the live entity'shistoryattribute, which is ephemeral and not restored after a Home Assistant restart. Both read sites used the live-state-only accessor, so after any HA restart the history was silently reset to a single day, defeating the smoothing and causingsoc_max/degradation readings to jump on noise alone.Core fix
Inverter.battery_size_tracking()andInverter.update_soc_max_calculated_sensor()(inverter.py) now read thehistoryattribute viaload_previous_value_from_ha()instead ofget_state_wrapper(), so the recorder is used as a fallback when the live entity is missing:This mirrors the pattern already used for
savings_total_predbatandinverter_register_writes.Test infrastructure
TestHAInterface.get_statehardcoded""for missing entities when anattributewas requested, ignoring thedefaultargument. This diverged from the realHAInterface.get_state(which correctly returnsdefault) and made the "entity gone after restart, recorder still has it" scenario impossible to simulate, masking this exact bug class. Corrected to honordefault.test_battery_scaling_auto_history_survives_ha_restartintest_find_battery_size.py, which removes the live entity, mocks recorder history viaget_history, and asserts the trimmed mean/soc_maxare computed as if the history had survived the restart.