Fix review findings: first-refresh handling, stale runtime record, P3s
P1: the first-refresh error handling never fired against real Home Assistant. Real DataUpdateCoordinator.async_config_entry_first_refresh converts ANY refresh failure into ConfigEntryNotReady (the original exception survives only as __cause__), so `except NestQuestApiError` could never match: the failure fell through to the outer except block, unwound listeners, closed the DB and re-raised, and async_forward_entry_setups never ran — no entities at all. Two-sided fix: _async_update_data now converts every typed NestQuestApiError (unconfigured stub, transport failure, non-2xx) into homeassistant.helpers.update_coordinator.UpdateFailed — the exception HA's coordinator machinery records as a failed pass (last_update_success=False, data=None) — and setup catches ConfigEntryNotReady around the first refresh instead, so an unconfigured API or failed fetch keeps setup alive with unavailable entities while everything else still unwinds. The conftest DataUpdateCoordinator stand-in now reproduces HA's real contract (async_refresh records a failure instead of raising; first_refresh wraps it in ConfigEntryNotReady) and gains an UpdateFailed stand-in, so this class of bug is caught by the suite — verified by reverting the catch, which fails both unconfigured-API and failed-fetch tests. P1: a stale hass.data[DOMAIN][entry.entry_id] survived a failed setup. The runtime record registers in hass.data before the first refresh and platform forward, but the except cleanup only cleared entry.runtime_data; after any failure in that window a later unload picked up the stale, already-torn-down record and re-invoked the already-consumed update-listener remover (ValueError). The except block now also pops the hass.data entry, and test_setup_failure_after_registration_shuts_coordinator_down (reworked from the fiction-based first-refresh trigger: a failed first refresh is tolerated setup, not an unwind) proves a failed setup followed by an unload does not raise and leaves no residue. P3: dropped the function-local re-import of NestQuestApiClient in coordinator_client_from_entry (module-level import already covers it), and the unused coordinator_client_from_entry alias in the conftest fixture. P3: test_due_today_payload_omits_missed_admin_equals_panel dropped the missed-instance count assertions in the Feature 18 rewrite; restored quests_completed_today == 0 / quests_remaining_today == 1 so the missed-derivation + counts pairing stays verified (the counts arrive precomputed and still roll the full day).
This commit is contained in:
+46
-9
@@ -367,6 +367,15 @@ class BinarySensorEntity(Entity):
|
||||
return self._attr_device_class
|
||||
|
||||
|
||||
class UpdateFailed(HomeAssistantError):
|
||||
"""Stand-in mirroring homeassistant.helpers.update_coordinator.UpdateFailed.
|
||||
|
||||
The exception an update pass raises to report a failed fetch: the
|
||||
coordinator records ``last_update_success = False`` instead of
|
||||
propagating it out of the refresh.
|
||||
"""
|
||||
|
||||
|
||||
class DataUpdateCoordinator:
|
||||
"""Functional stand-in for HA's DataUpdateCoordinator.
|
||||
|
||||
@@ -375,8 +384,20 @@ class DataUpdateCoordinator:
|
||||
``async_add_listener``/``async_update_listeners`` for entity
|
||||
subscription, ``async_refresh``/``async_config_entry_first_refresh``
|
||||
driving the subclass's ``_async_update_data``, ``last_update_success``,
|
||||
and ``data``. Interval scheduling is NOT simulated (no real clock
|
||||
ticks in tests) — tests drive refreshes directly.
|
||||
``last_exception``, and ``data``. Interval scheduling is NOT
|
||||
simulated (no real clock ticks in tests) — tests drive refreshes
|
||||
directly.
|
||||
|
||||
The failure contract mirrors real HA exactly (it is the trap this
|
||||
harness once hid): an update failure NEVER propagates out of
|
||||
``async_refresh`` — the coordinator records
|
||||
``last_update_success = False``, keeps its last good ``data``, and
|
||||
still notifies listeners so entities re-render unavailable — and
|
||||
``async_config_entry_first_refresh`` wraps ANY failed pass in
|
||||
:class:`ConfigEntryNotReady`, with the original error only
|
||||
surviving as ``__cause__``. A stand-in that re-raised the raw
|
||||
update error would let setup code key on the original exception
|
||||
type — a clause real HA can never reach.
|
||||
"""
|
||||
|
||||
def __init__(self, hass, logger, *, name=None, update_interval=None):
|
||||
@@ -386,6 +407,7 @@ class DataUpdateCoordinator:
|
||||
self.update_interval = update_interval
|
||||
self.data = None
|
||||
self.last_update_success = False
|
||||
self.last_exception = None
|
||||
self._listeners = []
|
||||
|
||||
def async_add_listener(self, update_callback, context=None):
|
||||
@@ -405,18 +427,35 @@ class DataUpdateCoordinator:
|
||||
callback()
|
||||
|
||||
async def async_refresh(self):
|
||||
"""Run one update pass and notify listeners."""
|
||||
"""Run one update pass and notify listeners.
|
||||
|
||||
Mirrors HA: a failed update pass is RECORDED, never raised —
|
||||
``last_update_success`` goes False, the exception lands on
|
||||
``last_exception``, and listeners still fire; only cancellation
|
||||
propagates.
|
||||
"""
|
||||
try:
|
||||
self.data = await self._async_update_data()
|
||||
self.last_update_success = True
|
||||
except BaseException:
|
||||
except Exception as err:
|
||||
self.last_update_success = False
|
||||
raise
|
||||
self.last_exception = err
|
||||
self.async_update_listeners()
|
||||
|
||||
async def async_config_entry_first_refresh(self):
|
||||
"""First refresh at config-entry setup (alias of refresh)."""
|
||||
"""First refresh at config-entry setup, per HA's real contract.
|
||||
|
||||
HA's real ``async_config_entry_first_refresh`` wraps ANY failed
|
||||
update pass in ``ConfigEntryNotReady`` — the original error
|
||||
survives only as ``__cause__`` — so setup code that must
|
||||
tolerate a failed first refresh keys on ConfigEntryNotReady,
|
||||
never on the original exception type.
|
||||
"""
|
||||
await self.async_refresh()
|
||||
if not self.last_update_success:
|
||||
raise ConfigEntryNotReady(
|
||||
f"error fetching {self.name} data: {self.last_exception}"
|
||||
) from self.last_exception
|
||||
|
||||
async def async_shutdown(self):
|
||||
"""Release listeners on teardown."""
|
||||
@@ -426,6 +465,7 @@ class DataUpdateCoordinator:
|
||||
_update_coordinator_mock = _ha_mock("homeassistant.helpers.update_coordinator")
|
||||
_update_coordinator_mock.DataUpdateCoordinator = DataUpdateCoordinator
|
||||
_update_coordinator_mock.CoordinatorEntity = CoordinatorEntity
|
||||
_update_coordinator_mock.UpdateFailed = UpdateFailed
|
||||
|
||||
_entity_mock = _ha_mock("homeassistant.helpers.entity")
|
||||
_entity_mock.DeviceInfo = DeviceInfo
|
||||
@@ -787,9 +827,6 @@ def coordinator_serves_local_snapshot():
|
||||
"""
|
||||
import custom_components.nestquest as nq_mod
|
||||
import custom_components.nestquest.coordinator as coordinator_module
|
||||
from custom_components.nestquest.coordinator import (
|
||||
coordinator_client_from_entry as production_factory,
|
||||
)
|
||||
|
||||
_COORDINATOR_CLIENT_OVERRIDES = (
|
||||
coordinator_module._COORDINATOR_CLIENT_OVERRIDES
|
||||
|
||||
Reference in New Issue
Block a user