Skip to content

Commit f71f41d

Browse files
committed
Preserve sub-integer precision at trip end and BMW SOC fallback
Continuity check: also keep prediction when BMW is within rounding (abs < 0.5), not just when BMW >= prediction. Prevents jump from 54.7% to 54% at trip end. BMW SOC fallback: if existing last_magic_soc is within rounding of BMW integer, keep the sub-integer value instead of overwriting. Prevents jump after continuity window expires.
1 parent 4d862e8 commit f71f41d

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

custom_components/cardata/magic_soc.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,14 +605,19 @@ def get_magic_soc(self, vin: str, bmw_soc: float | None, mileage: float | None)
605605
if last_driving is not None:
606606
predicted_soc, saved_at = last_driving
607607
if (time.time() - saved_at) < DRIVING_SOC_CONTINUITY_SECONDS:
608-
# Use last prediction if BMW SOC appears stale (higher or equal)
609-
if bmw_soc is None or bmw_soc >= predicted_soc:
608+
# Use last prediction if BMW SOC is stale (higher/equal) or
609+
# within rounding of our sub-integer prediction
610+
if bmw_soc is None or bmw_soc >= predicted_soc or abs(bmw_soc - predicted_soc) < 0.5:
610611
self._last_magic_soc[vin] = predicted_soc
611612
return predicted_soc
612613
# Expired or BMW sent fresh lower SOC — discard
613614
del self._last_driving_predicted_soc[vin]
614615

615616
if bmw_soc is not None:
617+
# Keep existing sub-integer prediction if BMW agrees within rounding
618+
existing = self._last_magic_soc.get(vin)
619+
if existing is not None and abs(bmw_soc - existing) < 0.5:
620+
return existing
616621
self._last_magic_soc[vin] = bmw_soc
617622
return bmw_soc
618623
return self._last_magic_soc.get(vin)

0 commit comments

Comments
 (0)