From 3b13714ef489b5c5661ae0254b12f137d047fc26 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 20 Jul 2020 22:52:46 +0000 Subject: [PATCH] simplify since the event loop time cannot move backwards --- homeassistant/helpers/event.py | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/homeassistant/helpers/event.py b/homeassistant/helpers/event.py index 74fca19495a..aa2df4b0ace 100644 --- a/homeassistant/helpers/event.py +++ b/homeassistant/helpers/event.py @@ -594,37 +594,31 @@ def async_track_utc_time_change( matching_minutes = dt_util.parse_time_expression(minute, 0, 59) matching_hours = dt_util.parse_time_expression(hour, 0, 23) - last_now: datetime = dt_util.utcnow() - next_time: datetime = last_now - - def calculate_next(now: datetime) -> None: + def calculate_next(now: datetime) -> datetime: """Calculate and set the next time the trigger should fire.""" - nonlocal next_time localized_now = dt_util.as_local(now) if local else now - next_time = dt_util.find_next_time_expression_time( + return dt_util.find_next_time_expression_time( localized_now, matching_seconds, matching_minutes, matching_hours ) # Make sure rolling back the clock doesn't prevent the timer from # triggering. cancel_callback: Optional[asyncio.TimerHandle] = None - calculate_next(last_now) + next_time: datetime = calculate_next(dt_util.utcnow()) @callback def pattern_time_change_listener() -> None: """Listen for matching time_changed events.""" - nonlocal next_time, last_now, cancel_callback + nonlocal next_time, cancel_callback now = pattern_utc_now() hass.async_run_job(action, dt_util.as_local(now) if local else now) if next_time <= now: - calculate_next(now + timedelta(seconds=1)) + next_time = calculate_next(now + timedelta(seconds=1)) else: - calculate_next(now) - - last_now = now + next_time = calculate_next(now) cancel_callback = hass.loop.call_at( hass.loop.time() + next_time.timestamp() - time.time(),