Skip to content

BMW CarData: pass SoC timestamp to CarState - #4345

Merged
LKuemmel merged 2 commits into
openWB:masterfrom
seaspotter:bmw-cardata-soc-timestamp
Nov 17, 2026
Merged

LKuemmel merged 2 commits into
openWB:masterfrom
seaspotter:bmw-cardata-soc-timestamp

Conversation

@seaspotter

@seaspotter seaspotter commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

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:

  • Pass the BMW CarData SoC measurement timestamp through to CarState so stale vehicle data can be detected and charging-time SoC calculations can use the measurement time.

Bug Fixes:

  • Preserve existing behavior by falling back to the current time when the SoC timestamp is missing or invalid.

Enhancements:

  • Document that CarState timestamps represent the vehicle measurement time when available, otherwise the request time.

Tests:

  • Add coverage for UTC and offset timestamps, missing or invalid timestamps, and ensuring timestamps from other data points are ignored.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

@LKuemmel
LKuemmel merged commit 06562ac into openWB:master Nov 17, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants