Migrate non-component tests to use freezegun/freezer (#105142)

This commit is contained in:
Jan-Philipp Benecke 2023-12-07 22:58:09 +01:00 committed by GitHub
parent 00e87b9dff
commit d1aa690c24
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 247 additions and 236 deletions

View file

@ -3,6 +3,7 @@ from datetime import datetime, timedelta
from typing import Any
from unittest.mock import AsyncMock, patch
from freezegun import freeze_time
import pytest
import voluptuous as vol
@ -1137,7 +1138,7 @@ async def test_state_for(hass: HomeAssistant) -> None:
assert not test(hass)
now = dt_util.utcnow() + timedelta(seconds=5)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
assert test(hass)
@ -1163,7 +1164,7 @@ async def test_state_for_template(hass: HomeAssistant) -> None:
assert not test(hass)
now = dt_util.utcnow() + timedelta(seconds=5)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
assert test(hass)
@ -2235,7 +2236,7 @@ async def test_if_action_before_sunrise_no_offset(
# sunrise: 2015-09-16 13:33:18 UTC, sunset: 2015-09-17 01:53:45 UTC
# now = sunrise + 1s -> 'before sunrise' not true
now = datetime(2015, 9, 16, 13, 33, 19, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 0
@ -2247,7 +2248,7 @@ async def test_if_action_before_sunrise_no_offset(
# now = sunrise -> 'before sunrise' true
now = datetime(2015, 9, 16, 13, 33, 18, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 1
@ -2259,7 +2260,7 @@ async def test_if_action_before_sunrise_no_offset(
# now = local midnight -> 'before sunrise' true
now = datetime(2015, 9, 16, 7, 0, 0, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 2
@ -2271,7 +2272,7 @@ async def test_if_action_before_sunrise_no_offset(
# now = local midnight - 1s -> 'before sunrise' not true
now = datetime(2015, 9, 17, 6, 59, 59, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 2
@ -2306,7 +2307,7 @@ async def test_if_action_after_sunrise_no_offset(
# sunrise: 2015-09-16 13:33:18 UTC, sunset: 2015-09-17 01:53:45 UTC
# now = sunrise - 1s -> 'after sunrise' not true
now = datetime(2015, 9, 16, 13, 33, 17, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 0
@ -2318,7 +2319,7 @@ async def test_if_action_after_sunrise_no_offset(
# now = sunrise + 1s -> 'after sunrise' true
now = datetime(2015, 9, 16, 13, 33, 19, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 1
@ -2330,7 +2331,7 @@ async def test_if_action_after_sunrise_no_offset(
# now = local midnight -> 'after sunrise' not true
now = datetime(2015, 9, 16, 7, 0, 0, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 1
@ -2342,7 +2343,7 @@ async def test_if_action_after_sunrise_no_offset(
# now = local midnight - 1s -> 'after sunrise' true
now = datetime(2015, 9, 17, 6, 59, 59, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 2
@ -2381,7 +2382,7 @@ async def test_if_action_before_sunrise_with_offset(
# sunrise: 2015-09-16 13:33:18 UTC, sunset: 2015-09-17 01:53:45 UTC
# now = sunrise + 1s + 1h -> 'before sunrise' with offset +1h not true
now = datetime(2015, 9, 16, 14, 33, 19, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 0
@ -2393,7 +2394,7 @@ async def test_if_action_before_sunrise_with_offset(
# now = sunrise + 1h -> 'before sunrise' with offset +1h true
now = datetime(2015, 9, 16, 14, 33, 18, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 1
@ -2405,7 +2406,7 @@ async def test_if_action_before_sunrise_with_offset(
# now = UTC midnight -> 'before sunrise' with offset +1h not true
now = datetime(2015, 9, 17, 0, 0, 0, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 1
@ -2417,7 +2418,7 @@ async def test_if_action_before_sunrise_with_offset(
# now = UTC midnight - 1s -> 'before sunrise' with offset +1h not true
now = datetime(2015, 9, 16, 23, 59, 59, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 1
@ -2429,7 +2430,7 @@ async def test_if_action_before_sunrise_with_offset(
# now = local midnight -> 'before sunrise' with offset +1h true
now = datetime(2015, 9, 16, 7, 0, 0, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 2
@ -2441,7 +2442,7 @@ async def test_if_action_before_sunrise_with_offset(
# now = local midnight - 1s -> 'before sunrise' with offset +1h not true
now = datetime(2015, 9, 17, 6, 59, 59, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 2
@ -2453,7 +2454,7 @@ async def test_if_action_before_sunrise_with_offset(
# now = sunset -> 'before sunrise' with offset +1h not true
now = datetime(2015, 9, 17, 1, 53, 45, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 2
@ -2465,7 +2466,7 @@ async def test_if_action_before_sunrise_with_offset(
# now = sunset -1s -> 'before sunrise' with offset +1h not true
now = datetime(2015, 9, 17, 1, 53, 44, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 2
@ -2504,7 +2505,7 @@ async def test_if_action_before_sunset_with_offset(
# sunrise: 2015-09-16 13:33:18 UTC, sunset: 2015-09-17 01:53:45 UTC
# now = local midnight -> 'before sunset' with offset +1h true
now = datetime(2015, 9, 16, 7, 0, 0, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 1
@ -2516,7 +2517,7 @@ async def test_if_action_before_sunset_with_offset(
# now = sunset + 1s + 1h -> 'before sunset' with offset +1h not true
now = datetime(2015, 9, 17, 2, 53, 46, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 1
@ -2528,7 +2529,7 @@ async def test_if_action_before_sunset_with_offset(
# now = sunset + 1h -> 'before sunset' with offset +1h true
now = datetime(2015, 9, 17, 2, 53, 44, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 2
@ -2540,7 +2541,7 @@ async def test_if_action_before_sunset_with_offset(
# now = UTC midnight -> 'before sunset' with offset +1h true
now = datetime(2015, 9, 17, 0, 0, 0, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 3
@ -2552,7 +2553,7 @@ async def test_if_action_before_sunset_with_offset(
# now = UTC midnight - 1s -> 'before sunset' with offset +1h true
now = datetime(2015, 9, 16, 23, 59, 59, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 4
@ -2564,7 +2565,7 @@ async def test_if_action_before_sunset_with_offset(
# now = sunrise -> 'before sunset' with offset +1h true
now = datetime(2015, 9, 16, 13, 33, 18, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 5
@ -2576,7 +2577,7 @@ async def test_if_action_before_sunset_with_offset(
# now = sunrise -1s -> 'before sunset' with offset +1h true
now = datetime(2015, 9, 16, 13, 33, 17, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 6
@ -2588,7 +2589,7 @@ async def test_if_action_before_sunset_with_offset(
# now = local midnight-1s -> 'after sunrise' with offset +1h not true
now = datetime(2015, 9, 17, 6, 59, 59, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 6
@ -2627,7 +2628,7 @@ async def test_if_action_after_sunrise_with_offset(
# sunrise: 2015-09-16 13:33:18 UTC, sunset: 2015-09-17 01:53:45 UTC
# now = sunrise - 1s + 1h -> 'after sunrise' with offset +1h not true
now = datetime(2015, 9, 16, 14, 33, 17, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 0
@ -2639,7 +2640,7 @@ async def test_if_action_after_sunrise_with_offset(
# now = sunrise + 1h -> 'after sunrise' with offset +1h true
now = datetime(2015, 9, 16, 14, 33, 58, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 1
@ -2651,7 +2652,7 @@ async def test_if_action_after_sunrise_with_offset(
# now = UTC noon -> 'after sunrise' with offset +1h not true
now = datetime(2015, 9, 16, 12, 0, 0, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 1
@ -2663,7 +2664,7 @@ async def test_if_action_after_sunrise_with_offset(
# now = UTC noon - 1s -> 'after sunrise' with offset +1h not true
now = datetime(2015, 9, 16, 11, 59, 59, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 1
@ -2675,7 +2676,7 @@ async def test_if_action_after_sunrise_with_offset(
# now = local noon -> 'after sunrise' with offset +1h true
now = datetime(2015, 9, 16, 19, 1, 0, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 2
@ -2687,7 +2688,7 @@ async def test_if_action_after_sunrise_with_offset(
# now = local noon - 1s -> 'after sunrise' with offset +1h true
now = datetime(2015, 9, 16, 18, 59, 59, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 3
@ -2699,7 +2700,7 @@ async def test_if_action_after_sunrise_with_offset(
# now = sunset -> 'after sunrise' with offset +1h true
now = datetime(2015, 9, 17, 1, 53, 45, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 4
@ -2711,7 +2712,7 @@ async def test_if_action_after_sunrise_with_offset(
# now = sunset + 1s -> 'after sunrise' with offset +1h true
now = datetime(2015, 9, 17, 1, 53, 45, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 5
@ -2723,7 +2724,7 @@ async def test_if_action_after_sunrise_with_offset(
# now = local midnight-1s -> 'after sunrise' with offset +1h true
now = datetime(2015, 9, 17, 6, 59, 59, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 6
@ -2735,7 +2736,7 @@ async def test_if_action_after_sunrise_with_offset(
# now = local midnight -> 'after sunrise' with offset +1h not true
now = datetime(2015, 9, 17, 7, 0, 0, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 6
@ -2774,7 +2775,7 @@ async def test_if_action_after_sunset_with_offset(
# sunrise: 2015-09-16 13:33:18 UTC, sunset: 2015-09-17 01:53:45 UTC
# now = sunset - 1s + 1h -> 'after sunset' with offset +1h not true
now = datetime(2015, 9, 17, 2, 53, 44, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 0
@ -2786,7 +2787,7 @@ async def test_if_action_after_sunset_with_offset(
# now = sunset + 1h -> 'after sunset' with offset +1h true
now = datetime(2015, 9, 17, 2, 53, 45, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 1
@ -2798,7 +2799,7 @@ async def test_if_action_after_sunset_with_offset(
# now = midnight-1s -> 'after sunset' with offset +1h true
now = datetime(2015, 9, 16, 6, 59, 59, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 2
@ -2810,7 +2811,7 @@ async def test_if_action_after_sunset_with_offset(
# now = midnight -> 'after sunset' with offset +1h not true
now = datetime(2015, 9, 16, 7, 0, 0, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 2
@ -2849,7 +2850,7 @@ async def test_if_action_after_and_before_during(
# sunrise: 2015-09-16 13:33:18 UTC, sunset: 2015-09-17 01:53:45 UTC
# now = sunrise - 1s -> 'after sunrise' + 'before sunset' not true
now = datetime(2015, 9, 16, 13, 33, 17, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 0
@ -2865,7 +2866,7 @@ async def test_if_action_after_and_before_during(
# now = sunset + 1s -> 'after sunrise' + 'before sunset' not true
now = datetime(2015, 9, 17, 1, 53, 46, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 0
@ -2877,7 +2878,7 @@ async def test_if_action_after_and_before_during(
# now = sunrise + 1s -> 'after sunrise' + 'before sunset' true
now = datetime(2015, 9, 16, 13, 33, 19, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 1
@ -2893,7 +2894,7 @@ async def test_if_action_after_and_before_during(
# now = sunset - 1s -> 'after sunrise' + 'before sunset' true
now = datetime(2015, 9, 17, 1, 53, 44, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 2
@ -2909,7 +2910,7 @@ async def test_if_action_after_and_before_during(
# now = 9AM local -> 'after sunrise' + 'before sunset' true
now = datetime(2015, 9, 16, 16, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 3
@ -2952,7 +2953,7 @@ async def test_if_action_before_or_after_during(
# sunrise: 2015-09-16 13:33:18 UTC, sunset: 2015-09-17 01:53:45 UTC
# now = sunrise - 1s -> 'before sunrise' | 'after sunset' true
now = datetime(2015, 9, 16, 13, 33, 17, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 1
@ -2968,7 +2969,7 @@ async def test_if_action_before_or_after_during(
# now = sunset + 1s -> 'before sunrise' | 'after sunset' true
now = datetime(2015, 9, 17, 1, 53, 46, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 2
@ -2984,7 +2985,7 @@ async def test_if_action_before_or_after_during(
# now = sunrise + 1s -> 'before sunrise' | 'after sunset' false
now = datetime(2015, 9, 16, 13, 33, 19, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 2
@ -3000,7 +3001,7 @@ async def test_if_action_before_or_after_during(
# now = sunset - 1s -> 'before sunrise' | 'after sunset' false
now = datetime(2015, 9, 17, 1, 53, 44, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 2
@ -3016,7 +3017,7 @@ async def test_if_action_before_or_after_during(
# now = midnight + 1s local -> 'before sunrise' | 'after sunset' true
now = datetime(2015, 9, 16, 7, 0, 1, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 3
@ -3032,7 +3033,7 @@ async def test_if_action_before_or_after_during(
# now = midnight - 1s local -> 'before sunrise' | 'after sunset' true
now = datetime(2015, 9, 17, 6, 59, 59, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 4
@ -3077,7 +3078,7 @@ async def test_if_action_before_sunrise_no_offset_kotzebue(
# sunrise: 2015-07-24 15:21:12 UTC, sunset: 2015-07-25 11:13:33 UTC
# now = sunrise + 1s -> 'before sunrise' not true
now = datetime(2015, 7, 24, 15, 21, 13, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 0
@ -3089,7 +3090,7 @@ async def test_if_action_before_sunrise_no_offset_kotzebue(
# now = sunrise - 1h -> 'before sunrise' true
now = datetime(2015, 7, 24, 14, 21, 12, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 1
@ -3101,7 +3102,7 @@ async def test_if_action_before_sunrise_no_offset_kotzebue(
# now = local midnight -> 'before sunrise' true
now = datetime(2015, 7, 24, 8, 0, 0, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 2
@ -3113,7 +3114,7 @@ async def test_if_action_before_sunrise_no_offset_kotzebue(
# now = local midnight - 1s -> 'before sunrise' not true
now = datetime(2015, 7, 24, 7, 59, 59, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 2
@ -3154,7 +3155,7 @@ async def test_if_action_after_sunrise_no_offset_kotzebue(
# sunrise: 2015-07-24 15:21:12 UTC, sunset: 2015-07-25 11:13:33 UTC
# now = sunrise -> 'after sunrise' true
now = datetime(2015, 7, 24, 15, 21, 12, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 1
@ -3166,7 +3167,7 @@ async def test_if_action_after_sunrise_no_offset_kotzebue(
# now = sunrise - 1h -> 'after sunrise' not true
now = datetime(2015, 7, 24, 14, 21, 12, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 1
@ -3178,7 +3179,7 @@ async def test_if_action_after_sunrise_no_offset_kotzebue(
# now = local midnight -> 'after sunrise' not true
now = datetime(2015, 7, 24, 8, 0, 1, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 1
@ -3190,7 +3191,7 @@ async def test_if_action_after_sunrise_no_offset_kotzebue(
# now = local midnight - 1s -> 'after sunrise' true
now = datetime(2015, 7, 24, 7, 59, 59, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 2
@ -3231,7 +3232,7 @@ async def test_if_action_before_sunset_no_offset_kotzebue(
# sunrise: 2015-07-24 15:21:12 UTC, sunset: 2015-07-25 11:13:33 UTC
# now = sunset + 1s -> 'before sunset' not true
now = datetime(2015, 7, 25, 11, 13, 34, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 0
@ -3243,7 +3244,7 @@ async def test_if_action_before_sunset_no_offset_kotzebue(
# now = sunset - 1h-> 'before sunset' true
now = datetime(2015, 7, 25, 10, 13, 33, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 1
@ -3255,7 +3256,7 @@ async def test_if_action_before_sunset_no_offset_kotzebue(
# now = local midnight -> 'before sunrise' true
now = datetime(2015, 7, 24, 8, 0, 0, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 2
@ -3267,7 +3268,7 @@ async def test_if_action_before_sunset_no_offset_kotzebue(
# now = local midnight - 1s -> 'before sunrise' not true
now = datetime(2015, 7, 24, 7, 59, 59, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 2
@ -3308,7 +3309,7 @@ async def test_if_action_after_sunset_no_offset_kotzebue(
# sunrise: 2015-07-24 15:21:12 UTC, sunset: 2015-07-25 11:13:33 UTC
# now = sunset -> 'after sunset' true
now = datetime(2015, 7, 25, 11, 13, 33, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 1
@ -3320,7 +3321,7 @@ async def test_if_action_after_sunset_no_offset_kotzebue(
# now = sunset - 1s -> 'after sunset' not true
now = datetime(2015, 7, 25, 11, 13, 32, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 1
@ -3332,7 +3333,7 @@ async def test_if_action_after_sunset_no_offset_kotzebue(
# now = local midnight -> 'after sunset' not true
now = datetime(2015, 7, 24, 8, 0, 1, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 1
@ -3344,7 +3345,7 @@ async def test_if_action_after_sunset_no_offset_kotzebue(
# now = local midnight - 1s -> 'after sunset' true
now = datetime(2015, 7, 24, 7, 59, 59, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=now):
with freeze_time(now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 2