Skip to content

Integration blocking Home Assistant Core from writing to core.restore_state #178

@mgsection9

Description

@mgsection9

Describe the bug
On Home Assistant shutdown, the eeveemobility custom integration raises a KeyError while HA is dumping restore state data. This aborts restore_state.async_dump_states() and prevents all entities’ states from being saved. On subsequent restarts, HA restores stale checkpoints (e.g., integral/energy sensors reset to old values), causing history/statistics regressions.

To Reproduce

  1. Install and configure the eeveemobility custom integration (HACS) and let it run normally.
  2. Ensure entities from this integration are loaded
  3. Restart or shut down Home Assistant via Settings → System → Restart.
  4. Observe Home Assistant logs during shutdown: a KeyError: 'user' is thrown from eeveemobility → HA fails to dump restore states → after reboot, other entities (e.g., integration/riemann sum sensors) revert to older values.

Expected behavior

  • eeveemobility should never raise during extra_restore_state_data access (native_value, item, etc.).
  • Missing keys in coordinator.data should be handled gracefully (e.g., return None, mark entity unavailable) without exceptions.
  • Home Assistant should be able to complete restore_state dump cleanly so entities resume with fresh checkpoints on next boot.

Screenshots
N/A (log-only issue). If needed, I can add screenshots of history graphs showing integral sensors reverting after each reboot.

Log information

2025-10-20 20:37:23.205 ERROR (SyncWorker_13) [homeassistant] Error doing job: Task exception was never retrieved (None)
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/restore_state.py", line 219, in _async_dump_states
    await self.async_dump_states()
  File "/usr/src/homeassistant/homeassistant/helpers/restore_state.py", line 208, in async_dump_states
    for stored_state in self.async_get_stored_states()
                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/src/homeassistant/homeassistant/helpers/restore_state.py", line 178, in async_get_stored_states
    entity.extra_restore_state_data,
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 981, in extra_restore_state_data
    return SensorExtraStoredData(self.native_value, self.native_unit_of_measurement)
                                 ^^^^^^^^^^^^^^^^^
  File "/config/custom_components/eeveemobility/sensor.py", line 325, in native_value
    return self.entity_description.value_fn(self.item)
                                            ^^^^^^^^^
  File "/config/custom_components/eeveemobility/entity.py", line 83, in item
    return self.coordinator.data[self.entity_description.key]
           ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: 'user'

Additional context
The exception originates from accessing self.coordinator.data[self.entity_description.key] when the key (e.g., 'user') is missing during shutdown or periodic dump states (every 15 min).

Because this propagates up through native_value → extra_restore_state_data → restore_state, the entire restore-state dump fails, impacting unrelated entities.

Suggested fix direction:

  • Use safe access in item() (e.g., data.get(self.entity_description.key)) and return None if missing.
  • Guard native_value with try/except and log a warning instead of raising (return None when data isn’t ready).
  • Optionally implement available to reflect coordinator readiness: only True when the expected key exists.

Environment (please adjust if maintainers need specifics):

  • Home Assistant Core: (e.g.) 2025.10.x
  • eeveemobility integration: version from HACS at time of error (v1.0.0)
  • Install method: HACS
  • Python / OS / Container: HAOS / containerized

Metadata

Metadata

Assignees

Labels

fixA bug fix

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions