From 9678cbdde20e9f879732836f3e9a8a2dd2a136a2 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 26 Aug 2020 10:50:25 -0500 Subject: [PATCH] Fix time pattern listener firing a few microseconds early (#39281) --- homeassistant/helpers/event.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/homeassistant/helpers/event.py b/homeassistant/helpers/event.py index 3f0c2db3b2f..be002a1b204 100644 --- a/homeassistant/helpers/event.py +++ b/homeassistant/helpers/event.py @@ -620,13 +620,19 @@ def async_track_utc_time_change( calculate_next(now + timedelta(seconds=1)) + # We always get time.time() first to avoid time.time() + # ticking forward after fetching hass.loop.time() + # and callback being scheduled a few microseconds early cancel_callback = hass.loop.call_at( - hass.loop.time() + next_time.timestamp() - time.time(), + -time.time() + hass.loop.time() + next_time.timestamp(), pattern_time_change_listener, ) + # We always get time.time() first to avoid time.time() + # ticking forward after fetching hass.loop.time() + # and callback being scheduled a few microseconds early cancel_callback = hass.loop.call_at( - hass.loop.time() + next_time.timestamp() - time.time(), + -time.time() + hass.loop.time() + next_time.timestamp(), pattern_time_change_listener, )