Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion custom_components/panasonic_cc/aquarea/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
5 changes: 4 additions & 1 deletion custom_components/panasonic_cc/hws/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
15 changes: 10 additions & 5 deletions custom_components/panasonic_cc/panasonic/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
34 changes: 23 additions & 11 deletions tests/test_coordinators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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),
Expand Down Expand Up @@ -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 -----------------------------------------------


Expand Down
Loading