back to 100% coverage

This commit is contained in:
J. Nick Koston
2020-07-20 22:41:17 +00:00
parent 92042ee23e
commit 558e8add39
3 changed files with 26 additions and 28 deletions

View File

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

View File

@@ -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_),

View File

@@ -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()