From 4ba706144d31415cbff5d7f2e176015c07b3e8a2 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 7 Mar 2024 08:57:06 -1000 Subject: [PATCH] Schedule periodic coordinator updates as background tasks. Currently, the coordinator's periodic refreshes delay startup because they are not scheduled as background tasks. We will wait if the startup takes long enough for the first planned refresh. Another coordinator's scheduled refresh will be fired on busy systems, further delaying the startup. This chain of events results in the startup taking a long time and hitting the safety timeout because too many coordinators are refreshing. This case can also happen with scheduled entity refreshes, but it's less common. A future PR will address that case. --- homeassistant/helpers/update_coordinator.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/homeassistant/helpers/update_coordinator.py b/homeassistant/helpers/update_coordinator.py index 018ba1d13e47..4907d0ae0304 100644 --- a/homeassistant/helpers/update_coordinator.py +++ b/homeassistant/helpers/update_coordinator.py @@ -253,7 +253,19 @@ class DataUpdateCoordinator(BaseDataUpdateCoordinatorProtocol, Generic[_DataT]): @callback def __wrap_handle_refresh_interval(self) -> None: """Handle a refresh interval occurrence.""" - self.hass.async_create_task(self._handle_refresh_interval(), eager_start=True) + if self.config_entry: + self.config_entry.async_create_background_task( + self.hass, + self._handle_refresh_interval(), + name=f"{self.name} - {self.config_entry.title} - refresh", + eager_start=True, + ) + else: + self.hass.async_create_background_task( + self._handle_refresh_interval(), + name=f"{self.name} - refresh", + eager_start=True, + ) async def _handle_refresh_interval(self, _now: datetime | None = None) -> None: """Handle a refresh interval occurrence."""