From 558e8add39a2620a1346315b144cefd14203cad5 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 20 Jul 2020 22:41:17 +0000 Subject: [PATCH] back to 100% coverage --- homeassistant/helpers/event.py | 8 +++---- tests/common.py | 4 ++-- tests/helpers/test_event.py | 42 +++++++++++++++++----------------- 3 files changed, 26 insertions(+), 28 deletions(-) diff --git a/homeassistant/helpers/event.py b/homeassistant/helpers/event.py index dd386edfa3d..74fca19495a 100644 --- a/homeassistant/helpers/event.py +++ b/homeassistant/helpers/event.py @@ -617,14 +617,12 @@ def async_track_utc_time_change( nonlocal next_time, last_now, cancel_callback now = pattern_utc_now() - - if now < last_now: - # Time rolled back - calculate_next(now) + hass.async_run_job(action, dt_util.as_local(now) if local else now) if next_time <= now: - hass.async_run_job(action, dt_util.as_local(now) if local else now) calculate_next(now + timedelta(seconds=1)) + else: + calculate_next(now) last_now = now diff --git a/tests/common.py b/tests/common.py index b4bf130d114..bcb66428f6b 100644 --- a/tests/common.py +++ b/tests/common.py @@ -285,7 +285,7 @@ fire_mqtt_message = threadsafe_callback_factory(async_fire_mqtt_message) @ha.callback -def async_fire_time_changed(hass, datetime_): +def async_fire_time_changed(hass, datetime_, fire_all=False): """Fire a time changes event.""" hass.bus.async_fire(EVENT_TIME_CHANGED, {"now": date_util.as_utc(datetime_)}) @@ -298,7 +298,7 @@ def async_fire_time_changed(hass, datetime_): future_seconds = task.when() - hass.loop.time() mock_seconds_into_future = datetime_.timestamp() - time.time() - if mock_seconds_into_future >= future_seconds: + if fire_all or mock_seconds_into_future >= future_seconds: with patch( "homeassistant.helpers.event.pattern_utc_now", return_value=date_util.as_utc(datetime_), diff --git a/tests/helpers/test_event.py b/tests/helpers/test_event.py index e84f8cfc9ff..e78213194db 100644 --- a/tests/helpers/test_event.py +++ b/tests/helpers/test_event.py @@ -879,25 +879,25 @@ async def test_periodic_task_clock_rollback(hass): await hass.async_block_till_done() assert len(specific_runs) == 1 - # The event loop uses monotonic clocks to track time so it can't - # move backwards, only utcnow can - async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 22, 0, 0)) - await hass.async_block_till_done() - assert len(specific_runs) == 1 - - async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 0, 0, 0)) - await hass.async_block_till_done() - assert len(specific_runs) == 1 - - async_fire_time_changed(hass, datetime(now.year + 1, 5, 25, 2, 0, 0)) + async_fire_time_changed( + hass, datetime(now.year + 1, 5, 24, 22, 0, 0), fire_all=True + ) await hass.async_block_till_done() assert len(specific_runs) == 2 + async_fire_time_changed(hass, datetime(now.year + 1, 5, 24, 0, 0, 0), fire_all=True) + await hass.async_block_till_done() + assert len(specific_runs) == 3 + + async_fire_time_changed(hass, datetime(now.year + 1, 5, 25, 2, 0, 0), fire_all=True) + await hass.async_block_till_done() + assert len(specific_runs) == 4 + unsub() - async_fire_time_changed(hass, datetime(now.year + 1, 5, 25, 2, 0, 0)) + async_fire_time_changed(hass, datetime(now.year + 1, 5, 25, 2, 0, 0), fire_all=True) await hass.async_block_till_done() - assert len(specific_runs) == 2 + assert len(specific_runs) == 4 async def test_periodic_task_duplicate_time(hass): @@ -1003,13 +1003,7 @@ async def test_periodic_task_leaving_dst(hass): assert len(specific_runs) == 1 async_fire_time_changed( - hass, timezone.localize(datetime(now.year + 2, 10, 28, 1, 5, 0), is_dst=True) - ) - await hass.async_block_till_done() - assert len(specific_runs) == 2 - - async_fire_time_changed( - hass, timezone.localize(datetime(now.year + 2, 10, 28, 1, 55, 0), is_dst=True) + hass, timezone.localize(datetime(now.year + 2, 10, 28, 2, 45, 0), is_dst=True) ) await hass.async_block_till_done() assert len(specific_runs) == 2 @@ -1018,7 +1012,13 @@ async def test_periodic_task_leaving_dst(hass): hass, timezone.localize(datetime(now.year + 2, 10, 28, 2, 55, 0), is_dst=True) ) await hass.async_block_till_done() - assert len(specific_runs) == 3 + assert len(specific_runs) == 2 + + async_fire_time_changed( + hass, timezone.localize(datetime(now.year + 2, 10, 28, 2, 55, 0), is_dst=True) + ) + await hass.async_block_till_done() + assert len(specific_runs) == 2 unsub()