From 1fc569c86d1358c04e2b36e9ed248448dd100e65 Mon Sep 17 00:00:00 2001 From: Robert Chmielowiec Date: Tue, 1 Dec 2026 14:33:20 +0100 Subject: [PATCH] Cache failed Portainer image checks until the next watcher run (#195796) --- .../components/portainer/coordinator.py | 5 +++-- tests/components/portainer/test_update.py | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/portainer/coordinator.py b/homeassistant/components/portainer/coordinator.py index a2925572e9db..f0037ebd075f 100644 --- a/homeassistant/components/portainer/coordinator.py +++ b/homeassistant/components/portainer/coordinator.py @@ -265,7 +265,7 @@ class PortainerCoordinator( tuple[int, str], tuple[float, LocalImageInformation] ] = {} self._image_status_cache: dict[ - tuple[int, str], tuple[float, PortainerImageUpdateStatus] + tuple[int, str], tuple[float, PortainerImageUpdateStatus | None] ] = {} self._event_listeners: dict[int, PortainerEventListener] = {} self._event_listeners_enabled = False @@ -721,7 +721,8 @@ class PortainerCoordinator( endpoint_id, err, ) - return None + # Cache the failure too, so it is retried with the watcher, not every poll + image_status = None self._image_status_cache[cache_key] = (time.time(), image_status) return image_status diff --git a/tests/components/portainer/test_update.py b/tests/components/portainer/test_update.py index 9968b407ea6c..cf4a5c6e2972 100644 --- a/tests/components/portainer/test_update.py +++ b/tests/components/portainer/test_update.py @@ -650,6 +650,25 @@ async def test_update_recreated_container_check_fails( assert state is not None assert state.state == STATE_UNKNOWN + # Next scan interval to check if the cached failure is still in effect + freezer.tick(DEFAULT_SCAN_INTERVAL) + async_fire_time_changed(hass, dt_util.utcnow()) + await hass.async_block_till_done(wait_background_tasks=True) + + mock_portainer_client.container_image_status.assert_called_once() + + # A new watcher run invalidates the cached failure + mock_portainer_watcher.last_check = dt_util.utcnow().timestamp() + + freezer.tick(DEFAULT_SCAN_INTERVAL) + async_fire_time_changed(hass, dt_util.utcnow()) + await hass.async_block_till_done(wait_background_tasks=True) + + assert mock_portainer_client.container_image_status.call_count == 2 + state = hass.states.get(ENTITY_ID) + assert state is not None + assert state.state == STATE_UNKNOWN + async def test_server_update_up_to_date( hass: HomeAssistant,