1
mirror of https://github.com/home-assistant/core synced 2024-07-12 07:21:24 +02:00

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.
This commit is contained in:
Eugenio Panadero 2020-03-22 00:57:12 +01:00 committed by GitHub
parent 6d4fa76107
commit d5e606640c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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: