Skip to content

MySkoda: Add SoC timestamp and option to calculate SoC - #4328

Open
seaspotter wants to merge 3 commits into
openWB:masterfrom
seaspotter:vehicle-myskoda-calc-soc
Open

seaspotter wants to merge 3 commits into
openWB:masterfrom
seaspotter:vehicle-myskoda-calc-soc

Conversation

@seaspotter

@seaspotter seaspotter commented Nov 14, 2026 •

Copy link
Copy Markdown
Collaborator

UI: openWB/openwb-ui-settings#1172

#3413 (comment)

Summary by Sourcery

Support reliable MySkoda state-of-charge freshness tracking and optionally calculate state of charge while charging.

New Features:

  • Add an option to calculate MySkoda state of charge during charging.
  • Record the vehicle-reported timestamp associated with MySkoda state-of-charge data.

Bug Fixes:

  • Prevent stale cached MySkoda charging data from being treated as current by exposing its capture timestamp.

Tests:

  • Add coverage for parsing valid, missing, malformed, and non-string MySkoda capture timestamps.

Chores:

  • Migrate existing MySkoda configurations to include the new state-of-charge calculation option with its default behavior.

@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 5 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="packages/modules/vehicles/myskoda/soc.py" line_range="33" />
<code_context>
+    soc_timestamp = api.extract_captured_timestamp(data)

-    return CarState(soc=soc, range=range_km, odometer=odometer_km, warning=warning)
+    return CarState(soc=soc, range=range_km, soc_timestamp=soc_timestamp, odometer=odometer_km, warning=warning)


</code_context>
<issue_to_address>
**Stale SoC appears fresh**

When the API returns cached charging data without a valid `carCapturedTimestamp`, `extract_captured_timestamp` returns `None`, and `CarState` replaces it with the request time; `ConfigurableVehicle` then treats the stale SoC as fresh and accepts it instead of detecting its age.

Preserve a missing or invalid capture time as unknown or stale so `CarState` cannot substitute the request time and make cached SoC appear fresh.

Also at `packages/modules/vehicles/myskoda/soc.py:31`, `packages/modules/vehicles/myskoda/api.py:105`, `packages/modules/vehicles/myskoda/api.py:112-117`.
</issue_to_address>

### Comment 2
<location path="packages/modules/vehicles/myskoda/api.py" line_range="114" />
<code_context>
+    if not timestamp:
+        return None
+    try:
+        return datetime.fromisoformat(timestamp.replace("Z", "+00:00")).timestamp()
+    except ValueError:
+        log.debug(f"Konnte carCapturedTimestamp nicht parsen: {timestamp}")
</code_context>
<issue_to_address>
**Invalid timestamp aborts fetch**

When the API returns a truthy, non-string `carCapturedTimestamp`, `timestamp.replace(...)` raises `AttributeError`, which the `ValueError` handler does not catch; `fetch` fails instead of returning the vehicle’s otherwise valid SoC.

Check that `timestamp` is a string before calling `replace`, and handle invalid values without aborting the fetch.
</issue_to_address>

### Comment 3
<location path="packages/modules/vehicles/myskoda/config.py" line_range="8" />
<code_context>
 class MyskodaConfiguration:
     api_key: str = ""
     vin: str = ""
+    calculate_soc: bool = False
     # wird vom Backend nach jedem erfolgreichen Abruf aus dem API-Response-Header
     # zurückgeschrieben (ISO-8601), nicht vom Nutzer editierbar - siehe soc.py
</code_context>
<issue_to_address>
**User setting is overwritten**

When a UI change to `calculate_soc` races with a fetch that publishes a changed API-key expiry, `fetch` publishes its stale full `vehicle_config`, restoring the old `calculate_soc` value and causing the next vehicle instance to use the wrong charging-calculation setting.

Publish the updated API-key expiry without overwriting newer UI changes to `calculate_soc`.
</issue_to_address>

### Comment 4
<location path="packages/modules/vehicles/myskoda/api.py" line_range="116" />
<code_context>
+    try:
+        return datetime.fromisoformat(timestamp.replace("Z", "+00:00")).timestamp()
+    except ValueError:
+        log.debug(f"Konnte carCapturedTimestamp nicht parsen: {timestamp}")
+        return None
+
</code_context>
<issue_to_address>
**Forged entries appear in logs**

When an attacker able to influence the MySkoda API response supplies an invalid `carCapturedTimestamp` containing line breaks, the debug message writes the value verbatim, so embedded CR/LF characters create attacker-controlled entries in the application logs.

Escape or remove control characters from the timestamp before logging it.
</issue_to_address>

### Comment 5
<location path="packages/modules/vehicles/myskoda/soc.py" line_range="31" />
<code_context>
     range_km = api.extract_range(data)
     odometer_km = api.extract_odometer(data)
     warning = api.extract_warning(data)
+    soc_timestamp = api.extract_captured_timestamp(data)

-    return CarState(soc=soc, range=range_km, odometer=odometer_km, warning=warning)
</code_context>
<issue_to_address>
**Timestamp contract is misleading**

When a caller relies on the shared `CarState` timestamp contract for MySkoda data, `CarState` documents `soc_timestamp` as the time of the last request, but MySkoda now supplies the vehicle’s capture time. Callers following the documented contract interpret stale vehicle data as request-fresh and misunderstand the freshness checks.

Update the shared `CarState` documentation to define `soc_timestamp` as the timestamp of the SoC data, which may differ from the request time.
</issue_to_address>

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

@seaspotter

Copy link
Copy Markdown
Collaborator Author

@sourcery-ai review

@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 2 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="packages/modules/vehicles/myskoda/soc.py" line_range="31" />
<code_context>
     range_km = api.extract_range(data)
     odometer_km = api.extract_odometer(data)
     warning = api.extract_warning(data)
+    soc_timestamp = api.extract_captured_timestamp(data)

-    return CarState(soc=soc, range=range_km, odometer=odometer_km, warning=warning)
</code_context>
<issue_to_address>
**Stale SoC is treated as current**

When the API returns a stale cached SoC with a missing or invalid `carCapturedTimestamp`, `CarState` replaces the missing timestamp with the request time, so `ConfigurableVehicle` treats the SoC as fresh and skips its stale-data fallback; callers see an old SoC as current.

Preserve a missing capture time through `CarState` so `ConfigurableVehicle` can run its stale-data fallback.

Also at `packages/modules/vehicles/myskoda/soc.py:33`, `packages/modules/vehicles/myskoda/api.py:112`.
</issue_to_address>

### Comment 2
<location path="packages/modules/vehicles/myskoda/config.py" line_range="8" />
<code_context>
 class MyskodaConfiguration:
     api_key: str = ""
     vin: str = ""
+    calculate_soc: bool = False
     # wird vom Backend nach jedem erfolgreichen Abruf aus dem API-Response-Header
     # zurückgeschrieben (ISO-8601), nicht vom Nutzer editierbar - siehe soc.py
</code_context>
<issue_to_address>
**Saved calculation option gets reverted**

When a user changes `calculate_soc` while a fetch is in flight and the fetch publishes a key-expiry update, `fetch` publishes its older full configuration and overwrites the newer `calculate_soc` value, so the retained config restores the old setting and charging continues with it.

Apply the key-expiry update to the latest configuration instead of publishing the in-flight fetch’s stale configuration.
</issue_to_address>

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

@seaspotter
seaspotter force-pushed the vehicle-myskoda-calc-soc branch from a17c254 to ddcc87b Compare November 16, 2026 14:22
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