Skip to content

Retry setup when vehicle base data is incomplete - #394

Open
ML19821 wants to merge 1 commit into
CJNE:mainfrom
ML19821:fix/retry-on-incomplete-vehicle-data
Open

Retry setup when vehicle base data is incomplete#394
ML19821 wants to merge 1 commit into
CJNE:mainfrom
ML19821:fix/retry-on-incomplete-vehicle-data

Conversation

@ML19821

@ML19821 ML19821 commented Jul 26, 2026

Copy link
Copy Markdown

Fixes #393.

get_stored_overview() in pyporscheconnectapi catches PorscheExceptionError internally and returns normally, so a rate limited API leaves vehicle.data without the base data it would have merged in — including name. No exception reaches the coordinator, so the refresh counts as successful, and entity setup then raises KeyError: 'name' in every platform. Home Assistant does not retry platform setup after that, so the integration ends up with a loaded config entry and zero entities until someone reloads it by hand.

This detects the incomplete data in the coordinator and fails the refresh instead. During setup async_config_entry_first_refresh() converts UpdateFailed into ConfigEntryNotReady, so Home Assistant retries with backoff and the integration recovers on its own once the API responds again.

self.vehicles is reset so the retry refetches, rather than falling into the else branch below, which would never call get_picture_locations() again.

Why not a fallback for vehicle.data["name"]

That was my first instinct, but sensor.py builds unique_id from the same value. A fallback would mint different unique IDs whenever the data is incomplete and leave duplicate entities behind once the API recovers. Not creating entities from incomplete data at all avoids the problem instead of trading it for a subtler one.

Testing

Isolated tests of the guard (complete data, missing base data, two vehicles with one incomplete, both incomplete, empty vehicle list, empty-string name) all behave as intended.

End-to-end on a live installation, simulating the swallowed failure with vehicle.data.pop("name", None) after the overview call:

  • before: KeyError: 'name' in sensor, binary_sensor, device_tracker, lock and image; config entry loaded, no entities, no recovery
  • after: config entry setup_retry, reason Incomplete data for vehicle(s) <vin>, will retry, automatic retries

With the simulation removed and normal API responses, all entities come back unchanged — no change to unique IDs or entity IDs.

Note

Arguably get_stored_overview() should let the exception propagate rather than swallow it; the coordinator already handles PorscheExceptionError correctly. That is the more complete fix but touches pyporscheconnectapi, so it is not part of this PR.

get_stored_overview() in pyporscheconnectapi catches PorscheExceptionError
internally and returns normally, so a rate limited API leaves vehicle.data
without the base data it would have merged in - including "name".

Entity setup indexes vehicle.data["name"] directly, so this raised KeyError
in every platform. Home Assistant does not retry platform setup after that,
which turned a transient API hiccup into a permanent outage until someone
noticed and reloaded the entry manually.

Detect the incomplete data in the coordinator and fail the refresh instead.
During setup async_config_entry_first_refresh() converts UpdateFailed into
ConfigEntryNotReady, so Home Assistant retries with backoff and recovers on
its own once the API responds again.

Verified against a live installation: with the base data missing the config
entry now goes to setup_retry with the reason "Incomplete data for
vehicle(s) <vin>, will retry" instead of raising KeyError in sensor,
binary_sensor, device_tracker, lock and image.
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.

Transient API failure during startup permanently breaks the integration (KeyError: 'name')

1 participant