From d5e606640cbee87a8c6a282423f9657b602e57a3 Mon Sep 17 00:00:00 2001 From: Eugenio Panadero Date: Sun, 22 Mar 2020 00:57:12 +0100 Subject: [PATCH] Fix scheduled update time-drift in data update coordinator (#32974) * Fix scheduled update time-drift in data update coordinator As next schedule is calculated **after** the update is done, setting now + update_interval makes 1 second drift in practice, as the tick is 1Hz. * Floor the utcnow timestamp to remove sub-second error precision, and generate constant frequency updates as long as the update takes < 1s. --- homeassistant/helpers/update_coordinator.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/homeassistant/helpers/update_coordinator.py b/homeassistant/helpers/update_coordinator.py index 85db657c441c..b2b048166168 100644 --- a/homeassistant/helpers/update_coordinator.py +++ b/homeassistant/helpers/update_coordinator.py @@ -88,8 +88,14 @@ class DataUpdateCoordinator: self._unsub_refresh() self._unsub_refresh = None + # We _floor_ utcnow to create a schedule on a rounded second, + # minimizing the time between the point and the real activation. + # That way we obtain a constant update frequency, + # as long as the update process takes less than a second self._unsub_refresh = async_track_point_in_utc_time( - self.hass, self._handle_refresh_interval, utcnow() + self.update_interval + self.hass, + self._handle_refresh_interval, + utcnow().replace(microsecond=0) + self.update_interval, ) async def _handle_refresh_interval(self, _now: datetime) -> None: