From 410e0f52a304c00f24006de498cae160a31fc911 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Fri, 15 Apr 2022 18:31:02 +0200 Subject: [PATCH] Limit Supervisor refresh updates (#70075) --- homeassistant/components/hassio/__init__.py | 16 ++++++- tests/components/hassio/test_init.py | 46 ++++++++++++++++++--- 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/hassio/__init__.py b/homeassistant/components/hassio/__init__.py index f3f6887570f788..278f8a50ebcee7 100644 --- a/homeassistant/components/hassio/__init__.py +++ b/homeassistant/components/hassio/__init__.py @@ -917,7 +917,6 @@ async def force_info_update_supervisor(self) -> None: async def force_data_refresh(self) -> None: """Force update of the addon info.""" - await self.hassio.refresh_updates() ( self.hass.data[DATA_INFO], self.hass.data[DATA_CORE_INFO], @@ -976,3 +975,18 @@ async def _update_addon_info(self, slug): except HassioAPIError as err: _LOGGER.warning("Could not fetch info for %s: %s", slug, err) return (slug, None) + + async def _async_refresh( + self, + log_failures: bool = True, + raise_on_auth_failed: bool = False, + scheduled: bool = False, + ) -> None: + """Refresh data.""" + if not scheduled: + # Force refreshing updates for non-scheduled updates + try: + await self.hassio.refresh_updates() + except HassioAPIError as err: + _LOGGER.warning("Error on Supervisor API: %s", err) + await super()._async_refresh(log_failures, raise_on_auth_failed, scheduled) diff --git a/tests/components/hassio/test_init.py b/tests/components/hassio/test_init.py index 2e7bea90f68e52..6f4b9a39a9febf 100644 --- a/tests/components/hassio/test_init.py +++ b/tests/components/hassio/test_init.py @@ -646,7 +646,8 @@ async def test_device_registry_calls(hass): async def test_coordinator_updates(hass, caplog): - """Test coordinator.""" + """Test coordinator updates.""" + await async_setup_component(hass, "homeassistant", {}) with patch.dict(os.environ, MOCK_ENVIRON), patch( "homeassistant.components.hassio.HassIO.refresh_updates" ) as refresh_updates_mock: @@ -658,12 +659,45 @@ async def test_coordinator_updates(hass, caplog): with patch( "homeassistant.components.hassio.HassIO.refresh_updates", - side_effect=HassioAPIError("Unknown"), ) as refresh_updates_mock: - async_fire_time_changed(hass, dt_util.now() + timedelta(minutes=5)) + async_fire_time_changed(hass, dt_util.now() + timedelta(minutes=20)) await hass.async_block_till_done() + assert refresh_updates_mock.call_count == 0 + + with patch( + "homeassistant.components.hassio.HassIO.refresh_updates", + ) as refresh_updates_mock: + await hass.services.async_call( + "homeassistant", + "update_entity", + { + "entity_id": [ + "update.home_assistant_core_update", + "update.home_assistant_supervisor_update", + ] + }, + blocking=True, + ) assert refresh_updates_mock.call_count == 1 - assert ( - "Error fetching hassio data: Error on Supervisor API: Unknown" - in caplog.text + + # There is a 10s cooldown on the debouncer + async_fire_time_changed(hass, dt_util.now() + timedelta(seconds=10)) + await hass.async_block_till_done() + + with patch( + "homeassistant.components.hassio.HassIO.refresh_updates", + side_effect=HassioAPIError("Unknown"), + ) as refresh_updates_mock: + await hass.services.async_call( + "homeassistant", + "update_entity", + { + "entity_id": [ + "update.home_assistant_core_update", + "update.home_assistant_supervisor_update", + ] + }, + blocking=True, ) + assert refresh_updates_mock.call_count == 1 + assert "Error on Supervisor API: Unknown" in caplog.text