fix(costs): clear _last_soc_pct on midnight reset; wire reset_daily_accumulators (#899)#899
Open
jackmcintyre wants to merge 1 commit into
Open
fix(costs): clear _last_soc_pct on midnight reset; wire reset_daily_accumulators (#899)#899jackmcintyre wants to merge 1 commit into
jackmcintyre wants to merge 1 commit into
Conversation
…ccumulators (#899) Two related issues: 1. CostTracker.reset_daily_accumulators was dead code (zero callers in custom_components/ — only tests). The midnight-reset path inlined the same field-zeroing but did NOT clear _last_soc_pct. Result: a grid charge spanning midnight left _last_soc_pct at the pre-midnight SOC, so the first post-midnight accumulate_costs attributed the entire overnight SOC delta to the new day's soc_gain_during_grid_charge_kwh_today — corrupting grid_charge_efficiency. 2. reset_daily_accumulators now clears _last_soc_pct = None, and handle_midnight_reset delegates to it (with a defensive inline fallback if cost_tracker is somehow None). TDD: 2 failing tests (assert _last_soc_pct is None after reset; end-to-end no phantom SOC gain). 3216 pass, coverage 95%.
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.
Summary
Two related issues:
CostTracker.reset_daily_accumulatorswas dead code (zero callers incustom_components/— only tests exercised it). The midnight-reset path (tick_scheduler.handle_midnight_reset) inlined the same field-zeroing logic but did NOT clear_last_soc_pct.The omission corrupts
grid_charge_efficiency: a grid charge spanning midnight leaves_last_soc_pctat the pre-midnight SOC. The firstaccumulate_costsafter midnight computessoc_delta = current_soc - _last_soc_pctand attributes the entire overnight SOC gain to the new day'ssoc_gain_during_grid_charge_kwh_today, inflating the ratio.Changes
utils/costs.py:reset_daily_accumulators: now also clearsself._last_soc_pct = Noneso the first post-midnight grid-charge sample establishes a fresh baseline.coordinator/tick_scheduler.py:handle_midnight_reset: delegates tocost_tracker.reset_daily_accumulators(data)(with a defensive inline fallback ifcost_trackeris somehowNone). Replaces the inline field-zeroing that was duplicatingCostTracker's logic and missing the SOC baseline clear.Validation
TestAccumulateEnergyKwh:test_reset_daily_accumulators_clears_last_soc_pct—_last_soc_pctmust beNoneafter reset (was40.0).test_no_phantom_soc_gain_after_midnight_reset— end-to-end: pre-midnight charge to SOC 50, reset, post-midnight sample at 81 must NOT attribute the 31-point delta (was50.0, notNone).uv run ruff checkclean.