From 23a630e0bcbd2aec6a598a19ebaf2929eba97e5b Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Thu, 24 Mar 2022 05:06:43 +0100 Subject: [PATCH] Update Times of the Day tests to use freezegun (#68327) --- tests/components/tod/test_binary_sensor.py | 935 +++++++-------------- 1 file changed, 309 insertions(+), 626 deletions(-) diff --git a/tests/components/tod/test_binary_sensor.py b/tests/components/tod/test_binary_sensor.py index ecdabdb910d..ed675267c65 100644 --- a/tests/components/tod/test_binary_sensor.py +++ b/tests/components/tod/test_binary_sensor.py @@ -1,7 +1,7 @@ """Test Times of the Day Binary Sensor.""" from datetime import datetime, timedelta -from unittest.mock import patch +from freezegun import freeze_time import pytest from homeassistant.const import STATE_OFF, STATE_ON @@ -19,11 +19,24 @@ def mock_legacy_time(legacy_patchable_time): yield +@pytest.fixture +def hass_time_zone(): + """Return default hass timezone.""" + return "US/Pacific" + + @pytest.fixture(autouse=True) -def setup_fixture(hass): +def setup_fixture(hass, hass_time_zone): """Set up things to be run when tests are started.""" hass.config.latitude = 50.27583 hass.config.longitude = 18.98583 + hass.config.set_time_zone(hass_time_zone) + + +@pytest.fixture +def hass_tz_info(hass): + """Return timezone info for the hass timezone.""" + return dt_util.get_time_zone(hass.config.time_zone) async def test_setup(hass): @@ -58,11 +71,9 @@ async def test_setup_no_sensors(hass): ) +@freeze_time("2019-01-10 18:43:00-08:00") async def test_in_period_on_start(hass): """Test simple setting.""" - test_time = datetime( - 2019, 1, 10, 18, 43, 0, tzinfo=dt_util.get_time_zone(hass.config.time_zone) - ) config = { "binary_sensor": [ { @@ -73,164 +84,123 @@ async def test_in_period_on_start(hass): } ] } - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=test_time, - ): - await async_setup_component(hass, "binary_sensor", config) - await hass.async_block_till_done() + await async_setup_component(hass, "binary_sensor", config) + await hass.async_block_till_done() state = hass.states.get("binary_sensor.evening") assert state.state == STATE_ON +@freeze_time("2019-01-10 22:30:00-08:00") async def test_midnight_turnover_before_midnight_inside_period(hass): """Test midnight turnover setting before midnight inside period .""" - test_time = datetime( - 2019, 1, 10, 22, 30, 0, tzinfo=dt_util.get_time_zone(hass.config.time_zone) - ) config = { "binary_sensor": [ {"platform": "tod", "name": "Night", "after": "22:00", "before": "5:00"} ] } - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=test_time, - ): - await async_setup_component(hass, "binary_sensor", config) - await hass.async_block_till_done() + await async_setup_component(hass, "binary_sensor", config) + await hass.async_block_till_done() state = hass.states.get("binary_sensor.night") assert state.state == STATE_ON -async def test_midnight_turnover_after_midnight_inside_period(hass): +async def test_midnight_turnover_after_midnight_inside_period( + hass, freezer, hass_tz_info +): """Test midnight turnover setting before midnight inside period .""" - test_time = datetime( - 2019, 1, 10, 21, 0, 0, tzinfo=dt_util.get_time_zone(hass.config.time_zone) - ) + test_time = datetime(2019, 1, 10, 21, 0, 0, tzinfo=hass_tz_info) config = { "binary_sensor": [ {"platform": "tod", "name": "Night", "after": "22:00", "before": "5:00"} ] } - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=test_time, - ): - await async_setup_component(hass, "binary_sensor", config) - await hass.async_block_till_done() + freezer.move_to(test_time) + await async_setup_component(hass, "binary_sensor", config) + await hass.async_block_till_done() - state = hass.states.get("binary_sensor.night") - assert state.state == STATE_OFF + state = hass.states.get("binary_sensor.night") + assert state.state == STATE_OFF - await hass.async_block_till_done() + await hass.async_block_till_done() - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=test_time + timedelta(hours=1), - ): + freezer.move_to(test_time + timedelta(hours=1)) + hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: dt_util.utcnow()}) - hass.bus.async_fire( - ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: test_time + timedelta(hours=1)} - ) - - await hass.async_block_till_done() - state = hass.states.get("binary_sensor.night") - assert state.state == STATE_ON + await hass.async_block_till_done() + state = hass.states.get("binary_sensor.night") + assert state.state == STATE_ON +@freeze_time("2019-01-10 20:30:00-08:00") async def test_midnight_turnover_before_midnight_outside_period(hass): """Test midnight turnover setting before midnight outside period.""" - test_time = datetime(2019, 1, 10, 20, 30, 0, tzinfo=dt_util.UTC) config = { "binary_sensor": [ {"platform": "tod", "name": "Night", "after": "22:00", "before": "5:00"} ] } - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=test_time, - ): - await async_setup_component(hass, "binary_sensor", config) - await hass.async_block_till_done() + await async_setup_component(hass, "binary_sensor", config) + await hass.async_block_till_done() state = hass.states.get("binary_sensor.night") assert state.state == STATE_OFF +@freeze_time("2019-01-10 10:00:00-08:00") async def test_after_happens_tomorrow(hass): """Test when both before and after are in the future, and after is later than before.""" - test_time = datetime(2019, 1, 10, 10, 00, 0, tzinfo=dt_util.UTC) config = { "binary_sensor": [ {"platform": "tod", "name": "Night", "after": "23:00", "before": "12:00"} ] } - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=test_time, - ): - await async_setup_component(hass, "binary_sensor", config) - await hass.async_block_till_done() + await async_setup_component(hass, "binary_sensor", config) + await hass.async_block_till_done() state = hass.states.get("binary_sensor.night") assert state.state == STATE_ON -async def test_midnight_turnover_after_midnight_outside_period(hass): +async def test_midnight_turnover_after_midnight_outside_period( + hass, freezer, hass_tz_info +): """Test midnight turnover setting before midnight inside period .""" - test_time = datetime( - 2019, 1, 10, 20, 0, 0, tzinfo=dt_util.get_time_zone(hass.config.time_zone) - ) + test_time = datetime(2019, 1, 10, 20, 0, 0, tzinfo=hass_tz_info) config = { "binary_sensor": [ {"platform": "tod", "name": "Night", "after": "22:00", "before": "5:00"} ] } - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=test_time, - ): - await async_setup_component(hass, "binary_sensor", config) - await hass.async_block_till_done() + freezer.move_to(test_time) + await async_setup_component(hass, "binary_sensor", config) + await hass.async_block_till_done() state = hass.states.get("binary_sensor.night") assert state.state == STATE_OFF - switchover_time = datetime( - 2019, 1, 11, 4, 59, 0, tzinfo=dt_util.get_time_zone(hass.config.time_zone) - ) - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=switchover_time, - ): + switchover_time = datetime(2019, 1, 11, 4, 59, 0, tzinfo=hass_tz_info) + freezer.move_to(switchover_time) - hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: switchover_time}) - await hass.async_block_till_done() - state = hass.states.get("binary_sensor.night") - assert state.state == STATE_ON + hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: dt_util.utcnow()}) + await hass.async_block_till_done() + state = hass.states.get("binary_sensor.night") + assert state.state == STATE_ON - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=switchover_time + timedelta(minutes=1, seconds=1), - ): + freezer.move_to(switchover_time + timedelta(minutes=1, seconds=1)) - hass.bus.async_fire( - ha.EVENT_TIME_CHANGED, - {ha.ATTR_NOW: switchover_time + timedelta(minutes=1, seconds=1)}, - ) + hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: dt_util.utcnow()}) - await hass.async_block_till_done() - state = hass.states.get("binary_sensor.night") - assert state.state == STATE_OFF + await hass.async_block_till_done() + state = hass.states.get("binary_sensor.night") + assert state.state == STATE_OFF -async def test_from_sunrise_to_sunset(hass): +async def test_from_sunrise_to_sunset(hass, freezer, hass_tz_info): """Test period from sunrise to sunset.""" - test_time = datetime(2019, 1, 12, tzinfo=dt_util.UTC) + test_time = datetime(2019, 1, 12, tzinfo=hass_tz_info) sunrise = dt_util.as_local( get_astral_event_date(hass, "sunrise", dt_util.as_utc(test_time)) ) @@ -248,88 +218,46 @@ async def test_from_sunrise_to_sunset(hass): ] } entity_id = "binary_sensor.day" - testtime = sunrise + timedelta(seconds=-1) - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): - await async_setup_component(hass, "binary_sensor", config) - await hass.async_block_till_done() - - await hass.async_block_till_done() - state = hass.states.get(entity_id) - assert state.state == STATE_OFF - - testtime = sunrise - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): - - hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime}) - await hass.async_block_till_done() - - state = hass.states.get(entity_id) - assert state.state == STATE_ON - - testtime = sunrise + timedelta(seconds=1) - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): - - hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime}) - await hass.async_block_till_done() - - state = hass.states.get(entity_id) - assert state.state == STATE_ON - + freezer.move_to(sunrise + timedelta(seconds=-1)) + await async_setup_component(hass, "binary_sensor", config) await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert state.state == STATE_OFF - testtime = sunset + timedelta(seconds=-1) - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): - - hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime}) - await hass.async_block_till_done() - - state = hass.states.get(entity_id) - assert state.state == STATE_ON - + freezer.move_to(sunrise) + hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: dt_util.utcnow()}) await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert state.state == STATE_ON - testtime = sunset - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): - - hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime}) - await hass.async_block_till_done() - - state = hass.states.get(entity_id) - assert state.state == STATE_OFF - + freezer.move_to(sunrise + timedelta(seconds=1)) + hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: dt_util.utcnow()}) await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert state.state == STATE_ON - testtime = sunset + timedelta(seconds=1) - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): + freezer.move_to(sunset + timedelta(seconds=-1)) + hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: dt_util.utcnow()}) + await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert state.state == STATE_ON - hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime}) - await hass.async_block_till_done() + freezer.move_to(sunset) + hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: dt_util.utcnow()}) + await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert state.state == STATE_OFF - state = hass.states.get(entity_id) - assert state.state == STATE_OFF + freezer.move_to(sunset + timedelta(seconds=1)) + hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: dt_util.utcnow()}) + await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert state.state == STATE_OFF -async def test_from_sunset_to_sunrise(hass): +async def test_from_sunset_to_sunrise(hass, freezer, hass_tz_info): """Test period from sunset to sunrise.""" - test_time = datetime(2019, 1, 12, tzinfo=dt_util.UTC) + test_time = datetime(2019, 1, 12, tzinfo=hass_tz_info) sunset = dt_util.as_local(get_astral_event_date(hass, "sunset", test_time)) sunrise = dt_util.as_local(get_astral_event_next(hass, "sunrise", sunset)) # assert sunset == sunrise @@ -344,90 +272,52 @@ async def test_from_sunset_to_sunrise(hass): ] } entity_id = "binary_sensor.night" - testtime = sunset + timedelta(minutes=-1) - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): - await async_setup_component(hass, "binary_sensor", config) - await hass.async_block_till_done() + freezer.move_to(sunset + timedelta(seconds=-1)) + await async_setup_component(hass, "binary_sensor", config) + await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert state.state == STATE_OFF - await hass.async_block_till_done() - state = hass.states.get(entity_id) - assert state.state == STATE_OFF + freezer.move_to(sunset) + hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: dt_util.utcnow()}) + await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert state.state == STATE_ON - testtime = sunset - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): + freezer.move_to(sunset + timedelta(minutes=1)) + hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: dt_util.utcnow()}) + await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert state.state == STATE_ON - hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime}) - await hass.async_block_till_done() + freezer.move_to(sunrise + timedelta(minutes=-1)) + hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: dt_util.utcnow()}) + await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert state.state == STATE_ON - state = hass.states.get(entity_id) - assert state.state == STATE_ON + freezer.move_to(sunrise) + hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: dt_util.utcnow()}) + await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert state.state == STATE_OFF - testtime = sunset + timedelta(minutes=1) - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): - - hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime}) - await hass.async_block_till_done() - - state = hass.states.get(entity_id) - assert state.state == STATE_ON - - testtime = sunrise + timedelta(minutes=-1) - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): - - hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime}) - await hass.async_block_till_done() - - state = hass.states.get(entity_id) - assert state.state == STATE_ON - - testtime = sunrise - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): - - hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime}) - await hass.async_block_till_done() - - state = hass.states.get(entity_id) - await hass.async_block_till_done() - # assert state == "dupa" - assert state.state == STATE_OFF - - testtime = sunrise + timedelta(minutes=1) - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): - - hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime}) - await hass.async_block_till_done() - - state = hass.states.get(entity_id) - assert state.state == STATE_OFF + freezer.move_to(sunrise + timedelta(minutes=1)) + hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: dt_util.utcnow()}) + await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert state.state == STATE_OFF -async def test_offset(hass): +async def test_offset(hass, freezer, hass_tz_info): """Test offset.""" - after = datetime( - 2019, 1, 10, 18, 0, 0, tzinfo=dt_util.get_time_zone(hass.config.time_zone) - ) + timedelta(hours=1, minutes=34) + after = datetime(2019, 1, 10, 18, 0, 0, tzinfo=hass_tz_info) + timedelta( + hours=1, minutes=34 + ) - before = datetime( - 2019, 1, 10, 22, 0, 0, tzinfo=dt_util.get_time_zone(hass.config.time_zone) - ) + timedelta(hours=1, minutes=45) + before = datetime(2019, 1, 10, 22, 0, 0, tzinfo=hass_tz_info) + timedelta( + hours=1, minutes=45 + ) entity_id = "binary_sensor.evening" config = { @@ -442,67 +332,42 @@ async def test_offset(hass): } ] } - testtime = after + timedelta(seconds=-1) - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): - await async_setup_component(hass, "binary_sensor", config) - await hass.async_block_till_done() - + freezer.move_to(after + timedelta(seconds=-1)) + await async_setup_component(hass, "binary_sensor", config) + await hass.async_block_till_done() state = hass.states.get(entity_id) assert state.state == STATE_OFF - testtime = after - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): - hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime}) - await hass.async_block_till_done() + freezer.move_to(after) + hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: dt_util.utcnow()}) + await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert state.state == STATE_ON - state = hass.states.get(entity_id) - assert state.state == STATE_ON + freezer.move_to(before + timedelta(seconds=-1)) + hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: dt_util.utcnow()}) + await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert state.state == STATE_ON - testtime = before + timedelta(seconds=-1) - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): - hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime}) - await hass.async_block_till_done() + freezer.move_to(before) + hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: dt_util.utcnow()}) + await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert state.state == STATE_OFF - state = hass.states.get(entity_id) - assert state.state == STATE_ON - - testtime = before - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): - hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime}) - await hass.async_block_till_done() - - state = hass.states.get(entity_id) - assert state.state == STATE_OFF - - testtime = before + timedelta(seconds=1) - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): - hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime}) - await hass.async_block_till_done() - - state = hass.states.get(entity_id) - assert state.state == STATE_OFF + freezer.move_to(before + timedelta(seconds=1)) + hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: dt_util.utcnow()}) + await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert state.state == STATE_OFF -async def test_offset_overnight(hass): +async def test_offset_overnight(hass, freezer, hass_tz_info): """Test offset overnight.""" - after = datetime( - 2019, 1, 10, 18, 0, 0, tzinfo=dt_util.get_time_zone(hass.config.time_zone) - ) + timedelta(hours=1, minutes=34) + after = datetime(2019, 1, 10, 18, 0, 0, tzinfo=hass_tz_info) + timedelta( + hours=1, minutes=34 + ) entity_id = "binary_sensor.evening" config = { "binary_sensor": [ @@ -516,35 +381,25 @@ async def test_offset_overnight(hass): } ] } - testtime = after + timedelta(seconds=-1) - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): - await async_setup_component(hass, "binary_sensor", config) - await hass.async_block_till_done() - + freezer.move_to(after + timedelta(seconds=-1)) + await async_setup_component(hass, "binary_sensor", config) + await hass.async_block_till_done() state = hass.states.get(entity_id) assert state.state == STATE_OFF - testtime = after - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): - hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime}) - await hass.async_block_till_done() - - state = hass.states.get(entity_id) - assert state.state == STATE_ON + freezer.move_to(after) + hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: dt_util.utcnow()}) + await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert state.state == STATE_ON -async def test_norwegian_case_winter(hass): +async def test_norwegian_case_winter(hass, freezer, hass_tz_info): """Test location in Norway where the sun doesn't set in summer.""" hass.config.latitude = 69.6 hass.config.longitude = 18.8 - test_time = datetime(2010, 1, 1, tzinfo=dt_util.UTC) + test_time = datetime(2010, 1, 1, tzinfo=hass_tz_info) sunrise = dt_util.as_local( get_astral_event_next(hass, "sunrise", dt_util.as_utc(test_time)) ) @@ -562,104 +417,56 @@ async def test_norwegian_case_winter(hass): ] } entity_id = "binary_sensor.day" - testtime = test_time - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): - await async_setup_component(hass, "binary_sensor", config) - await hass.async_block_till_done() - - await hass.async_block_till_done() - state = hass.states.get(entity_id) - assert state.state == STATE_OFF - - testtime = sunrise + timedelta(seconds=-1) - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): - - hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime}) - await hass.async_block_till_done() - - state = hass.states.get(entity_id) - assert state.state == STATE_OFF - - testtime = sunrise - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): - - hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime}) - await hass.async_block_till_done() - - state = hass.states.get(entity_id) - assert state.state == STATE_ON - - testtime = sunrise + timedelta(seconds=1) - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): - - hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime}) - await hass.async_block_till_done() - - state = hass.states.get(entity_id) - assert state.state == STATE_ON - + freezer.move_to(test_time) + await async_setup_component(hass, "binary_sensor", config) await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert state.state == STATE_OFF - testtime = sunset + timedelta(seconds=-1) - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): - - hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime}) - await hass.async_block_till_done() - - state = hass.states.get(entity_id) - assert state.state == STATE_ON - + freezer.move_to(sunrise + timedelta(seconds=-1)) + hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: dt_util.utcnow()}) await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert state.state == STATE_OFF - testtime = sunset - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): - - hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime}) - await hass.async_block_till_done() - - state = hass.states.get(entity_id) - assert state.state == STATE_OFF - + freezer.move_to(sunrise) + hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: dt_util.utcnow()}) await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert state.state == STATE_ON - testtime = sunset + timedelta(seconds=1) - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): + freezer.move_to(sunrise + timedelta(seconds=1)) + hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: dt_util.utcnow()}) + await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert state.state == STATE_ON - hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime}) - await hass.async_block_till_done() + freezer.move_to(sunset + timedelta(seconds=-1)) + hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: dt_util.utcnow()}) + await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert state.state == STATE_ON - state = hass.states.get(entity_id) - assert state.state == STATE_OFF + freezer.move_to(sunset) + hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: dt_util.utcnow()}) + await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert state.state == STATE_OFF + + freezer.move_to(sunset + timedelta(seconds=1)) + hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: dt_util.utcnow()}) + await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert state.state == STATE_OFF -async def test_norwegian_case_summer(hass): +async def test_norwegian_case_summer(hass, freezer, hass_tz_info): """Test location in Norway where the sun doesn't set in summer.""" hass.config.latitude = 69.6 hass.config.longitude = 18.8 hass.config.elevation = 10.0 - test_time = datetime(2010, 6, 1, tzinfo=dt_util.UTC) + test_time = datetime(2010, 6, 1, tzinfo=hass_tz_info) sunrise = dt_util.as_local( get_astral_event_next(hass, "sunrise", dt_util.as_utc(test_time)) @@ -678,100 +485,52 @@ async def test_norwegian_case_summer(hass): ] } entity_id = "binary_sensor.day" - testtime = test_time - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): - await async_setup_component(hass, "binary_sensor", config) - await hass.async_block_till_done() - - await hass.async_block_till_done() - state = hass.states.get(entity_id) - assert state.state == STATE_OFF - - testtime = sunrise + timedelta(seconds=-1) - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): - - hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime}) - await hass.async_block_till_done() - - state = hass.states.get(entity_id) - assert state.state == STATE_OFF - - testtime = sunrise - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): - - hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime}) - await hass.async_block_till_done() - - state = hass.states.get(entity_id) - assert state.state == STATE_ON - - testtime = sunrise + timedelta(seconds=1) - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): - - hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime}) - await hass.async_block_till_done() - - state = hass.states.get(entity_id) - assert state.state == STATE_ON - + freezer.move_to(test_time) + await async_setup_component(hass, "binary_sensor", config) await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert state.state == STATE_OFF - testtime = sunset + timedelta(seconds=-1) - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): - - hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime}) - await hass.async_block_till_done() - - state = hass.states.get(entity_id) - assert state.state == STATE_ON - + freezer.move_to(sunrise + timedelta(seconds=-1)) + hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: dt_util.utcnow()}) await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert state.state == STATE_OFF - testtime = sunset - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): - - hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime}) - await hass.async_block_till_done() - - state = hass.states.get(entity_id) - assert state.state == STATE_OFF - + freezer.move_to(sunrise) + hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: dt_util.utcnow()}) await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert state.state == STATE_ON - testtime = sunset + timedelta(seconds=1) - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): + freezer.move_to(sunrise + timedelta(seconds=1)) + hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: dt_util.utcnow()}) + await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert state.state == STATE_ON - hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime}) - await hass.async_block_till_done() + freezer.move_to(sunset + timedelta(seconds=-1)) + hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: dt_util.utcnow()}) + await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert state.state == STATE_ON - state = hass.states.get(entity_id) - assert state.state == STATE_OFF + freezer.move_to(sunset) + hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: dt_util.utcnow()}) + await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert state.state == STATE_OFF + + freezer.move_to(sunset + timedelta(seconds=1)) + hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: dt_util.utcnow()}) + await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert state.state == STATE_OFF -async def test_sun_offset(hass): +async def test_sun_offset(hass, freezer, hass_tz_info): """Test sun event with offset.""" - test_time = datetime(2019, 1, 12, tzinfo=dt_util.UTC) + test_time = datetime(2019, 1, 12, tzinfo=hass_tz_info) sunrise = dt_util.as_local( get_astral_event_date(hass, "sunrise", dt_util.as_utc(test_time)) + timedelta(hours=-1, minutes=-30) @@ -793,106 +552,61 @@ async def test_sun_offset(hass): ] } entity_id = "binary_sensor.day" - testtime = sunrise + timedelta(seconds=-1) - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): - await async_setup_component(hass, "binary_sensor", config) - await hass.async_block_till_done() + freezer.move_to(sunrise + timedelta(seconds=-1)) + await async_setup_component(hass, "binary_sensor", config) + await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert state.state == STATE_OFF - await hass.async_block_till_done() - state = hass.states.get(entity_id) - assert state.state == STATE_OFF + freezer.move_to(sunrise) + hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: dt_util.utcnow()}) + await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert state.state == STATE_ON - testtime = sunrise - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): + freezer.move_to(sunrise + timedelta(seconds=1)) + hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: dt_util.utcnow()}) + await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert state.state == STATE_ON - hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime}) - await hass.async_block_till_done() - - state = hass.states.get(entity_id) - assert state.state == STATE_ON - - testtime = sunrise + timedelta(seconds=1) - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): - - hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime}) - await hass.async_block_till_done() - - state = hass.states.get(entity_id) - assert state.state == STATE_ON + freezer.move_to(sunset + timedelta(seconds=-1)) + hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: dt_util.utcnow()}) + await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert state.state == STATE_ON await hass.async_block_till_done() - testtime = sunset + timedelta(seconds=-1) - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): - - hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime}) - await hass.async_block_till_done() - - state = hass.states.get(entity_id) - assert state.state == STATE_ON - + freezer.move_to(sunset) + hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: dt_util.utcnow()}) await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert state.state == STATE_OFF - testtime = sunset - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): - - hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime}) - await hass.async_block_till_done() - - state = hass.states.get(entity_id) - assert state.state == STATE_OFF - + freezer.move_to(sunset + timedelta(seconds=1)) + hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: dt_util.utcnow()}) await hass.async_block_till_done() - - testtime = sunset + timedelta(seconds=1) - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): - - hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime}) - await hass.async_block_till_done() - - state = hass.states.get(entity_id) - assert state.state == STATE_OFF + state = hass.states.get(entity_id) + assert state.state == STATE_OFF test_time = test_time + timedelta(days=1) sunrise = dt_util.as_local( get_astral_event_date(hass, "sunrise", dt_util.as_utc(test_time)) + timedelta(hours=-1, minutes=-30) ) - testtime = sunrise - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=testtime, - ): - - hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime}) - await hass.async_block_till_done() - - state = hass.states.get(entity_id) - assert state.state == STATE_ON + freezer.move_to(sunrise) + hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: dt_util.utcnow()}) + await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert state.state == STATE_ON -async def test_dst(hass): +async def test_dst(hass, freezer, hass_tz_info): """Test sun event with offset.""" - hass.config.set_time_zone("CET") - test_time = datetime(2019, 3, 30, 3, 0, 0, tzinfo=dt_util.UTC) + hass.config.time_zone = "CET" + dt_util.set_default_time_zone(dt_util.get_time_zone("CET")) + test_time = datetime(2019, 3, 30, 3, 0, 0, tzinfo=hass_tz_info) config = { "binary_sensor": [ {"platform": "tod", "name": "Day", "after": "2:30", "before": "2:40"} @@ -902,25 +616,22 @@ async def test_dst(hass): # after 2019-03-30 03:00 CET the next update should ge scheduled # at 3:30 not 2:30 local time entity_id = "binary_sensor.day" - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=test_time, - ): - await async_setup_component(hass, "binary_sensor", config) - await hass.async_block_till_done() + freezer.move_to(test_time) + await async_setup_component(hass, "binary_sensor", config) + await hass.async_block_till_done() - await hass.async_block_till_done() - state = hass.states.get(entity_id) - assert state.attributes["after"] == "2019-03-31T03:30:00+02:00" - assert state.attributes["before"] == "2019-03-31T03:40:00+02:00" - assert state.attributes["next_update"] == "2019-03-31T03:30:00+02:00" - assert state.state == STATE_OFF + await hass.async_block_till_done() + state = hass.states.get(entity_id) + assert state.attributes["after"] == "2019-03-31T03:30:00+02:00" + assert state.attributes["before"] == "2019-03-31T03:40:00+02:00" + assert state.attributes["next_update"] == "2019-03-31T03:30:00+02:00" + assert state.state == STATE_OFF +@freeze_time("2019-01-10 18:43:00") +@pytest.mark.parametrize("hass_time_zone", ("UTC",)) async def test_simple_before_after_does_not_loop_utc_not_in_range(hass): """Test simple before after.""" - hass.config.set_time_zone("UTC") - test_time = datetime(2019, 1, 10, 18, 43, 0, tzinfo=dt_util.UTC) config = { "binary_sensor": [ { @@ -931,12 +642,8 @@ async def test_simple_before_after_does_not_loop_utc_not_in_range(hass): } ] } - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=test_time, - ): - await async_setup_component(hass, "binary_sensor", config) - await hass.async_block_till_done() + await async_setup_component(hass, "binary_sensor", config) + await hass.async_block_till_done() state = hass.states.get("binary_sensor.night") assert state.state == STATE_OFF @@ -945,10 +652,10 @@ async def test_simple_before_after_does_not_loop_utc_not_in_range(hass): assert state.attributes["next_update"] == "2019-01-10T22:00:00+00:00" +@freeze_time("2019-01-10 22:43:00") +@pytest.mark.parametrize("hass_time_zone", ("UTC",)) async def test_simple_before_after_does_not_loop_utc_in_range(hass): """Test simple before after.""" - hass.config.set_time_zone("UTC") - test_time = datetime(2019, 1, 10, 22, 43, 0, tzinfo=dt_util.UTC) config = { "binary_sensor": [ { @@ -959,12 +666,8 @@ async def test_simple_before_after_does_not_loop_utc_in_range(hass): } ] } - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=test_time, - ): - await async_setup_component(hass, "binary_sensor", config) - await hass.async_block_till_done() + await async_setup_component(hass, "binary_sensor", config) + await hass.async_block_till_done() state = hass.states.get("binary_sensor.night") assert state.state == STATE_ON @@ -973,10 +676,10 @@ async def test_simple_before_after_does_not_loop_utc_in_range(hass): assert state.attributes["next_update"] == "2019-01-11T06:00:00+00:00" +@freeze_time("2019-01-11 06:00:00") +@pytest.mark.parametrize("hass_time_zone", ("UTC",)) async def test_simple_before_after_does_not_loop_utc_fire_at_before(hass): """Test simple before after.""" - hass.config.set_time_zone("UTC") - test_time = datetime(2019, 1, 11, 6, 0, 0, tzinfo=dt_util.UTC) config = { "binary_sensor": [ { @@ -987,12 +690,8 @@ async def test_simple_before_after_does_not_loop_utc_fire_at_before(hass): } ] } - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=test_time, - ): - await async_setup_component(hass, "binary_sensor", config) - await hass.async_block_till_done() + await async_setup_component(hass, "binary_sensor", config) + await hass.async_block_till_done() state = hass.states.get("binary_sensor.night") assert state.state == STATE_OFF @@ -1001,10 +700,10 @@ async def test_simple_before_after_does_not_loop_utc_fire_at_before(hass): assert state.attributes["next_update"] == "2019-01-11T22:00:00+00:00" +@freeze_time("2019-01-10 22:00:00") +@pytest.mark.parametrize("hass_time_zone", ("UTC",)) async def test_simple_before_after_does_not_loop_utc_fire_at_after(hass): """Test simple before after.""" - hass.config.set_time_zone("UTC") - test_time = datetime(2019, 1, 10, 22, 0, 0, tzinfo=dt_util.UTC) config = { "binary_sensor": [ { @@ -1015,12 +714,8 @@ async def test_simple_before_after_does_not_loop_utc_fire_at_after(hass): } ] } - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=test_time, - ): - await async_setup_component(hass, "binary_sensor", config) - await hass.async_block_till_done() + await async_setup_component(hass, "binary_sensor", config) + await hass.async_block_till_done() state = hass.states.get("binary_sensor.night") assert state.state == STATE_ON @@ -1029,10 +724,10 @@ async def test_simple_before_after_does_not_loop_utc_fire_at_after(hass): assert state.attributes["next_update"] == "2019-01-11T06:00:00+00:00" +@freeze_time("2019-01-10 22:00:00") +@pytest.mark.parametrize("hass_time_zone", ("UTC",)) async def test_simple_before_after_does_not_loop_utc_both_before_now(hass): """Test simple before after.""" - hass.config.set_time_zone("UTC") - test_time = datetime(2019, 1, 10, 22, 0, 0, tzinfo=dt_util.UTC) config = { "binary_sensor": [ { @@ -1043,12 +738,8 @@ async def test_simple_before_after_does_not_loop_utc_both_before_now(hass): } ] } - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=test_time, - ): - await async_setup_component(hass, "binary_sensor", config) - await hass.async_block_till_done() + await async_setup_component(hass, "binary_sensor", config) + await hass.async_block_till_done() state = hass.states.get("binary_sensor.morning") assert state.state == STATE_OFF @@ -1057,10 +748,10 @@ async def test_simple_before_after_does_not_loop_utc_both_before_now(hass): assert state.attributes["next_update"] == "2019-01-11T00:00:00+00:00" +@freeze_time("2019-01-10 17:43:00+01:00") +@pytest.mark.parametrize("hass_time_zone", ("Europe/Berlin",)) async def test_simple_before_after_does_not_loop_berlin_not_in_range(hass): """Test simple before after.""" - hass.config.set_time_zone("Europe/Berlin") - test_time = datetime(2019, 1, 10, 18, 43, 0, tzinfo=dt_util.UTC) config = { "binary_sensor": [ { @@ -1071,12 +762,8 @@ async def test_simple_before_after_does_not_loop_berlin_not_in_range(hass): } ] } - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=test_time, - ): - await async_setup_component(hass, "binary_sensor", config) - await hass.async_block_till_done() + await async_setup_component(hass, "binary_sensor", config) + await hass.async_block_till_done() state = hass.states.get("binary_sensor.dark") assert state.state == STATE_OFF @@ -1085,10 +772,10 @@ async def test_simple_before_after_does_not_loop_berlin_not_in_range(hass): assert state.attributes["next_update"] == "2019-01-11T00:00:00+01:00" +@freeze_time("2019-01-11 00:43:00+01:00") +@pytest.mark.parametrize("hass_time_zone", ("Europe/Berlin",)) async def test_simple_before_after_does_not_loop_berlin_in_range(hass): """Test simple before after.""" - hass.config.set_time_zone("Europe/Berlin") - test_time = datetime(2019, 1, 10, 23, 43, 0, tzinfo=dt_util.UTC) config = { "binary_sensor": [ { @@ -1099,12 +786,8 @@ async def test_simple_before_after_does_not_loop_berlin_in_range(hass): } ] } - with patch( - "homeassistant.components.tod.binary_sensor.dt_util.utcnow", - return_value=test_time, - ): - await async_setup_component(hass, "binary_sensor", config) - await hass.async_block_till_done() + await async_setup_component(hass, "binary_sensor", config) + await hass.async_block_till_done() state = hass.states.get("binary_sensor.dark") assert state.state == STATE_ON