diff --git a/custom_components/panasonic_cc/aquarea/coordinator.py b/custom_components/panasonic_cc/aquarea/coordinator.py index 44ca794..4ee2689 100644 --- a/custom_components/panasonic_cc/aquarea/coordinator.py +++ b/custom_components/panasonic_cc/aquarea/coordinator.py @@ -260,7 +260,10 @@ async def _delayed_refresh() -> None: except asyncio.CancelledError: pass finally: - self._refresh_task = None + # A cancelled task finishes after its replacement was + # stored; only clear the reference if it's still ours. + if self._refresh_task is asyncio.current_task(): + self._refresh_task = None self._refresh_task = self.hass.async_create_task(_delayed_refresh()) diff --git a/custom_components/panasonic_cc/hws/coordinator.py b/custom_components/panasonic_cc/hws/coordinator.py index d527a9a..d8d2c62 100644 --- a/custom_components/panasonic_cc/hws/coordinator.py +++ b/custom_components/panasonic_cc/hws/coordinator.py @@ -249,7 +249,10 @@ async def _delayed_refresh() -> None: except asyncio.CancelledError: pass finally: - self._refresh_task = None + # A cancelled task finishes after its replacement was + # stored; only clear the reference if it's still ours. + if self._refresh_task is asyncio.current_task(): + self._refresh_task = None self._refresh_task = self.hass.async_create_task(_delayed_refresh()) diff --git a/custom_components/panasonic_cc/panasonic/coordinator.py b/custom_components/panasonic_cc/panasonic/coordinator.py index 5b01282..f73de75 100644 --- a/custom_components/panasonic_cc/panasonic/coordinator.py +++ b/custom_components/panasonic_cc/panasonic/coordinator.py @@ -176,7 +176,10 @@ async def _delayed_refresh() -> None: except asyncio.CancelledError: pass finally: - self._refresh_task = None + # A cancelled task finishes after its replacement was + # stored; only clear the reference if it's still ours. + if self._refresh_task is asyncio.current_task(): + self._refresh_task = None self._refresh_task = self.hass.async_create_task(_delayed_refresh()) @@ -209,8 +212,9 @@ async def _async_update_data(self) -> int: return self._update_id if await self._api_client.try_update_device(self._device): self._update_id += 1 - self._reset_backoff() - return self._update_id + # Any successful poll means the API is reachable again, + # even when nothing on the device changed. + self._reset_backoff() except Exception as err: if _is_auth_error(err): self._auth_failed = True @@ -350,8 +354,9 @@ async def _async_update_data(self) -> int: return self._update_id if await self._api_client.async_try_update_energy(self._energy): self._update_id += 1 - self._reset_backoff() - return self._update_id + # Any successful poll means the API is reachable again, + # even when nothing on the device changed. + self._reset_backoff() except Exception as err: if _is_auth_error(err): self._auth_failed = True diff --git a/tests/test_coordinators.py b/tests/test_coordinators.py index 16f5eee..8c82267 100644 --- a/tests/test_coordinators.py +++ b/tests/test_coordinators.py @@ -176,11 +176,6 @@ async def test_recovery_with_changes_resets_backoff( assert coordinator.last_error is None -@pytest.mark.xfail( - strict=True, - reason="Backoff is only reset when the poll returns changed data; " - "Aquarea and HWS reset it on any successful poll.", -) async def test_recovery_without_changes_resets_backoff( coordinator: PanasonicDeviceCoordinator, api: MagicMock ) -> None: @@ -242,11 +237,6 @@ async def test_apply_changes_failure_is_tracked( listener.assert_called() -@pytest.mark.xfail( - strict=True, - reason="A cancelled refresh task's `finally` clears _refresh_task, dropping " - "the reference to the newer task, so the next call can't cancel it.", -) async def test_schedule_refresh_is_debounced( hass: HomeAssistant, coordinator: PanasonicDeviceCoordinator ) -> None: @@ -256,7 +246,11 @@ async def test_schedule_refresh_is_debounced( async def fake_sleep(_seconds: float) -> None: await delay_over.wait() - fake_asyncio = SimpleNamespace(sleep=fake_sleep, CancelledError=asyncio.CancelledError) + fake_asyncio = SimpleNamespace( + sleep=fake_sleep, + CancelledError=asyncio.CancelledError, + current_task=asyncio.current_task, + ) with ( patch.object(coordinator, "async_request_refresh", AsyncMock()) as refresh, patch("custom_components.panasonic_cc.panasonic.coordinator.asyncio", fake_asyncio), @@ -304,6 +298,24 @@ async def test_energy_coordinator( notify.assert_called_once() +async def test_energy_recovery_without_changes_resets_backoff( + hass: HomeAssistant, api: MagicMock, device_info: SimpleNamespace +) -> None: + """Energy polling returns to normal even if the numbers didn't change.""" + coord = PanasonicDeviceEnergyCoordinator(hass, CONFIG, api, device_info) + await coord.async_refresh() + api.async_try_update_energy.side_effect = NETWORK_ERROR + await coord.async_refresh() + + api.async_try_update_energy.side_effect = None + api.async_try_update_energy.return_value = False + await coord.async_refresh() + + assert coord.connection_status == "connected" + assert interval(coord) == 300 + assert coord.data == 1 + + # --- Weekly timer coordinator -----------------------------------------------