Repository navigation
BMW CarData: pass SoC timestamp to CarState - #4345
Merged
Merged
Conversation
There was a problem hiding this comment.
Hey - I've found 4 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="packages/modules/vehicles/bmw_cardata/soc_test.py" line_range="129" />
<code_context>
+
+ if expected is None:
+ # CarState nimmt ohne Zeitstempel "jetzt" an
+ assert result.soc_timestamp == timecheck.create_timestamp()
+ else:
+ assert result.soc_timestamp == expected
</code_context>
<issue_to_address>
**Timestamp fallback tests fail**
When the missing or invalid timestamp test cases run, the assertion calls `timecheck.create_timestamp()` after `CarState` has already created the fallback timestamp; the microsecond-resolution values differ, so the tests fail.
Capture or mock the fallback timestamp once and compare against that same value.
</issue_to_address>
### Comment 2
<location path="packages/modules/vehicles/bmw_cardata/soc.py" line_range="305" />
<code_context>
- soc_raw = _extract_first_value(td, FIELD_SOC_CANDIDATES)
+ soc_entry = _extract_first_entry(td, FIELD_SOC_CANDIDATES) or {}
+ soc_raw = soc_entry.get("value")
+ soc_timestamp = _parse_timestamp(soc_entry.get("timestamp"))
range_raw = _extract_first_value(td, FIELD_RANGE_CANDIDATES)
status = _extract_value(td, FIELD_STATUS)
</code_context>
<issue_to_address>
**Future timestamp blocks later readings**
When BMW CarData supplies a valid timestamp more than 60 seconds ahead of the system time, `fetch_soc` passes a syntactically valid future timestamp through to `CarState`; after that reading is stored, `ConfigurableVehicle.update` rejects subsequent readings whose timestamps are more than 60 seconds older, leaving the BMW SoC stale until time catches up.
Reject or bound timestamps that are implausibly ahead of the current time before passing them to `CarState`.
</issue_to_address>
### Comment 3
<location path="packages/modules/vehicles/bmw_cardata/soc.py" line_range="337" />
<code_context>
odometer,
)
- return CarState(soc=soc, range=vehicle_range, odometer=odometer, warning=warning)
+ return CarState(soc=soc, range=vehicle_range, soc_timestamp=soc_timestamp, odometer=odometer, warning=warning)
</code_context>
<issue_to_address>
**Older overlapping reading overwrites newer SoC**
When two BMW polls overlap, the newer reading is stored first, and the older reading is timestamped less than 60 seconds before it, `ConfigurableVehicle.update` accepts an older response when its timestamp is within 60 seconds of the stored timestamp, so an overlapping BMW poll that completes late can replace a newer SoC with an older reading.
Reject any response whose measurement timestamp is older than the currently stored SoC timestamp.
</issue_to_address>
### Comment 4
<location path="packages/modules/vehicles/bmw_cardata/soc.py" line_range="337" />
<code_context>
odometer,
)
- return CarState(soc=soc, range=vehicle_range, odometer=odometer, warning=warning)
+ return CarState(soc=soc, range=vehicle_range, soc_timestamp=soc_timestamp, odometer=odometer, warning=warning)
</code_context>
<issue_to_address>
**SoC timestamp documentation is stale**
When a BMW CarData SoC entry has a valid timestamp, `fetch_soc` passes the measurement time to `CarState`, while `CarState.__init__` documents `soc_timestamp` as the time of the last request, so consumers following that contract interpret the published BMW timestamp as a fetch time.
Update the `CarState` argument documentation to describe a measurement timestamp when available and the request-time fallback otherwise.
</issue_to_address>
LKuemmel
approved these changes
Nov 17, 2026
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.
Die CarData-Antwort liefert zu jedem Datenpunkt einen Zeitstempel. Dieser wird jetzt vom SoC-Datenpunkt an CarState übergeben, damit openWB veraltete Werte erkennt und den SoC während der Ladung berechnen kann.
Fehlt der Zeitstempel oder ist er ungültig, verhält sich das Modul wie bisher.
Summary by Sourcery
Propagate and validate the BMW CarData timestamp associated with the SoC measurement.
New Features:
Bug Fixes:
Enhancements:
Tests: