fix: always persist calibration after sampling, not only when correction moves - #198
Merged
Merged
Conversation
…ion moves When the efficiency correction is at CALIBRATION_APPLY_MAX (1.05) every new sample is added to the rolling window but record() returns moved=False, so async_save() is never called. The file on disk then keeps the old raw_ratio schema indefinitely and is re-migrated on every HA restart, resetting to correction=1.05 each time. The sensor appears permanently stuck. Fix: save after every successful sample (CALIBRATION_SAMPLED), not only when the correction shifts by more than CALIBRATION_SIGNIFICANT_CHANGE. This keeps the file in the current efficiency_factor schema and the sample count current, so a reset is not needed to unblock the sensor.
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.
Root cause
When the efficiency correction hits
CALIBRATION_APPLY_MAX(1.05) — which happens when the battery consistently outperforms its configured curve — every new sample is recorded in the in-memory rolling window butrecord()returnsmoved=False. Becauseasync_save()was only called whenmoved=True, the.storagefile was never updated.On every HA restart the stale file (still in the old
raw_ratioschema) was loaded,_migrate_ratios()ran again, and the correction snapped back to 1.05. New samples kept coming in and hitting the cap, the file was never written, and the sensor value never changed — until a manual reset via the service call.Fix
Save after every successful sample (
CALIBRATION_SAMPLED), not only when the correction moves by more thanCALIBRATION_SIGNIFICANT_CHANGE. This ensures:efficiency_factorschema on the first sample after startup, so subsequent restarts load clean dataTest plan
python -m pytest tests/ -v— all 1052 tests passlast_resultshowssampledafter a charge/discharge step instead of staying atdirection_not_planned.storagefile is updated after each run (schema becomesefficiency_factor, sample count increments)