From 6659e4baaae7ffcb618f88a94877965fa36502c9 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 10 May 2022 10:15:17 -0500 Subject: [PATCH 1/4] wip --- tests/components/history_stats/test_sensor.py | 76 ++++++------------- 1 file changed, 22 insertions(+), 54 deletions(-) diff --git a/tests/components/history_stats/test_sensor.py b/tests/components/history_stats/test_sensor.py index bfa0c8f415eea1..4da3b8d0659e20 100644 --- a/tests/components/history_stats/test_sensor.py +++ b/tests/components/history_stats/test_sensor.py @@ -1308,8 +1308,8 @@ def _fake_states(*args, **kwargs): assert hass.states.get("sensor.sensor4").state == "83.3" -async def test_measure_cet(hass, recorder_mock): - """Test the history statistics sensor measure with a non-UTC timezone.""" +async def test_end_time_with_microseconds_zeroed(hass, recorder_mock): + """Test the history statistics sensor that has the end time microseconds zeroed out.""" hass.config.set_time_zone("Europe/Berlin") start_time = dt_util.utcnow() - timedelta(minutes=60) t0 = start_time + timedelta(minutes=20) @@ -1329,61 +1329,29 @@ def _fake_states(*args, **kwargs): ] } - await async_setup_component( - hass, - "sensor", - { - "sensor": [ - { - "platform": "history_stats", - "entity_id": "binary_sensor.test_id", - "name": "sensor1", - "state": "on", - "start": "{{ as_timestamp(now()) - 3600 }}", - "end": "{{ now() }}", - "type": "time", - }, - { - "platform": "history_stats", - "entity_id": "binary_sensor.test_id", - "name": "sensor2", - "state": "on", - "start": "{{ as_timestamp(now()) - 3600 }}", - "end": "{{ now() }}", - "type": "time", - }, - { - "platform": "history_stats", - "entity_id": "binary_sensor.test_id", - "name": "sensor3", - "state": "on", - "start": "{{ as_timestamp(now()) - 3600 }}", - "end": "{{ now() }}", - "type": "count", - }, - { - "platform": "history_stats", - "entity_id": "binary_sensor.test_id", - "name": "sensor4", - "state": "on", - "start": "{{ as_timestamp(now()) - 3600 }}", - "end": "{{ now() }}", - "type": "ratio", - }, - ] - }, - ) - await hass.async_block_till_done() - with patch( "homeassistant.components.recorder.history.state_changes_during_period", _fake_states, ): - for i in range(1, 5): - await async_update_entity(hass, f"sensor.sensor{i}") + await async_setup_component( + hass, + "sensor", + { + "sensor": [ + { + "platform": "history_stats", + "entity_id": "binary_sensor.heatpump_compressor_state", + "name": "heatpump_compressor_today", + "state": "on", + "start": "{{ now().replace(hour=0, minute=0, second=0, microsecond=0) }}", + "end": "{{ now().replace(microsecond=0) }}", + "type": "time", + }, + ] + }, + ) + await hass.async_block_till_done() + await async_update_entity(hass, "sensor.heatpump_compressor_today") await hass.async_block_till_done() - assert hass.states.get("sensor.sensor1").state == "0.83" - assert hass.states.get("sensor.sensor2").state == "0.83" - assert hass.states.get("sensor.sensor3").state == "1" - assert hass.states.get("sensor.sensor4").state == "83.3" + assert hass.states.get("sensor.heatpump_compressor_today").state == "0.83" From 16cab2ad1427bc1725ff2176641ad3e350ca9885 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 10 May 2022 10:23:26 -0500 Subject: [PATCH 2/4] adjust --- tests/components/history_stats/test_sensor.py | 115 ++++++++++++------ 1 file changed, 75 insertions(+), 40 deletions(-) diff --git a/tests/components/history_stats/test_sensor.py b/tests/components/history_stats/test_sensor.py index 4da3b8d0659e20..d2bcbb2f4420e4 100644 --- a/tests/components/history_stats/test_sensor.py +++ b/tests/components/history_stats/test_sensor.py @@ -1311,47 +1311,82 @@ def _fake_states(*args, **kwargs): async def test_end_time_with_microseconds_zeroed(hass, recorder_mock): """Test the history statistics sensor that has the end time microseconds zeroed out.""" hass.config.set_time_zone("Europe/Berlin") - start_time = dt_util.utcnow() - timedelta(minutes=60) - t0 = start_time + timedelta(minutes=20) - t1 = t0 + timedelta(minutes=10) - t2 = t1 + timedelta(minutes=10) - - # Start t0 t1 t2 End - # |--20min--|--20min--|--10min--|--10min--| - # |---off---|---on----|---off---|---on----| + start_of_today = dt_util.utcnow().replace(hour=0, minute=0, second=0, microsecond=0) + with freeze_time(start_of_today): + start_time = start_of_today + timedelta(minutes=60) + t0 = start_time + timedelta(minutes=20) + t1 = t0 + timedelta(minutes=10) + t2 = t1 + timedelta(minutes=10) + + def _fake_states(*args, **kwargs): + return { + "binary_sensor.heatpump_compressor_state": [ + ha.State( + "binary_sensor.heatpump_compressor_state", "on", last_changed=t0 + ), + ha.State( + "binary_sensor.heatpump_compressor_state", + "off", + last_changed=t1, + ), + ha.State( + "binary_sensor.heatpump_compressor_state", "on", last_changed=t2 + ), + ] + } - def _fake_states(*args, **kwargs): - return { - "binary_sensor.test_id": [ - ha.State("binary_sensor.test_id", "on", last_changed=t0), - ha.State("binary_sensor.test_id", "off", last_changed=t1), - ha.State("binary_sensor.test_id", "on", last_changed=t2), - ] - } + with patch( + "homeassistant.components.recorder.history.state_changes_during_period", + _fake_states, + ): + await async_setup_component( + hass, + "sensor", + { + "sensor": [ + { + "platform": "history_stats", + "entity_id": "binary_sensor.heatpump_compressor_state", + "name": "heatpump_compressor_today", + "state": "on", + "start": "{{ now().replace(hour=0, minute=0, second=0, microsecond=0) }}", + "end": "{{ now().replace(microsecond=0) }}", + "type": "time", + }, + ] + }, + ) + await hass.async_block_till_done() + await async_update_entity(hass, "sensor.heatpump_compressor_today") + await hass.async_block_till_done() - with patch( - "homeassistant.components.recorder.history.state_changes_during_period", - _fake_states, - ): - await async_setup_component( - hass, - "sensor", - { - "sensor": [ - { - "platform": "history_stats", - "entity_id": "binary_sensor.heatpump_compressor_state", - "name": "heatpump_compressor_today", - "state": "on", - "start": "{{ now().replace(hour=0, minute=0, second=0, microsecond=0) }}", - "end": "{{ now().replace(microsecond=0) }}", - "type": "time", - }, - ] - }, - ) + assert hass.states.get("sensor.heatpump_compressor_today").state == "1.83" + time_200 = start_of_today + timedelta(hours=2) + with freeze_time(time_200): + async_fire_time_changed(hass, time_200) await hass.async_block_till_done() - await async_update_entity(hass, "sensor.heatpump_compressor_today") + assert hass.states.get("sensor.heatpump_compressor_today").state == "3.83" + hass.states.async_set("binary_sensor.heatpump_compressor_state", "off") await hass.async_block_till_done() - - assert hass.states.get("sensor.heatpump_compressor_today").state == "0.83" + time_400 = start_of_today + timedelta(hours=4) + with freeze_time(time_400): + async_fire_time_changed(hass, time_400) + await hass.async_block_till_done() + assert hass.states.get("sensor.heatpump_compressor_today").state == "3.83" + hass.states.async_set("binary_sensor.heatpump_compressor_state", "on") + await hass.async_block_till_done() + time_600 = start_of_today + timedelta(hours=6) + with freeze_time(time_600): + async_fire_time_changed(hass, time_600) + await hass.async_block_till_done() + assert hass.states.get("sensor.heatpump_compressor_today").state == "5.83" + rolled_to_next_day = start_of_today + timedelta(days=1) + assert rolled_to_next_day.hour == 0 + assert rolled_to_next_day.minute == 0 + assert rolled_to_next_day.second == 0 + assert rolled_to_next_day.microsecond == 0 + + with freeze_time(rolled_to_next_day): + async_fire_time_changed(hass, rolled_to_next_day) + await hass.async_block_till_done() + assert hass.states.get("sensor.heatpump_compressor_today").state == "0.00" From 5506b91831c2d383099a03368fe11b5585fdfd73 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 10 May 2022 10:29:35 -0500 Subject: [PATCH 3/4] tweak --- tests/components/history_stats/test_sensor.py | 106 +++++++++--------- 1 file changed, 52 insertions(+), 54 deletions(-) diff --git a/tests/components/history_stats/test_sensor.py b/tests/components/history_stats/test_sensor.py index d2bcbb2f4420e4..92779d4dcd6853 100644 --- a/tests/components/history_stats/test_sensor.py +++ b/tests/components/history_stats/test_sensor.py @@ -1311,75 +1311,73 @@ def _fake_states(*args, **kwargs): async def test_end_time_with_microseconds_zeroed(hass, recorder_mock): """Test the history statistics sensor that has the end time microseconds zeroed out.""" hass.config.set_time_zone("Europe/Berlin") - start_of_today = dt_util.utcnow().replace(hour=0, minute=0, second=0, microsecond=0) - with freeze_time(start_of_today): - start_time = start_of_today + timedelta(minutes=60) - t0 = start_time + timedelta(minutes=20) - t1 = t0 + timedelta(minutes=10) - t2 = t1 + timedelta(minutes=10) - - def _fake_states(*args, **kwargs): - return { - "binary_sensor.heatpump_compressor_state": [ - ha.State( - "binary_sensor.heatpump_compressor_state", "on", last_changed=t0 - ), - ha.State( - "binary_sensor.heatpump_compressor_state", - "off", - last_changed=t1, - ), - ha.State( - "binary_sensor.heatpump_compressor_state", "on", last_changed=t2 - ), - ] - } + start_of_today = dt_util.now().replace(hour=0, minute=0, second=0, microsecond=0) + start_time = start_of_today + timedelta(minutes=60) + t0 = start_time + timedelta(minutes=20) + t1 = t0 + timedelta(minutes=10) + t2 = t1 + timedelta(minutes=10) + time_200 = start_of_today + timedelta(hours=2) - with patch( - "homeassistant.components.recorder.history.state_changes_during_period", - _fake_states, - ): - await async_setup_component( - hass, - "sensor", - { - "sensor": [ - { - "platform": "history_stats", - "entity_id": "binary_sensor.heatpump_compressor_state", - "name": "heatpump_compressor_today", - "state": "on", - "start": "{{ now().replace(hour=0, minute=0, second=0, microsecond=0) }}", - "end": "{{ now().replace(microsecond=0) }}", - "type": "time", - }, - ] - }, - ) - await hass.async_block_till_done() - await async_update_entity(hass, "sensor.heatpump_compressor_today") - await hass.async_block_till_done() + def _fake_states(*args, **kwargs): + return { + "binary_sensor.heatpump_compressor_state": [ + ha.State( + "binary_sensor.heatpump_compressor_state", "on", last_changed=t0 + ), + ha.State( + "binary_sensor.heatpump_compressor_state", + "off", + last_changed=t1, + ), + ha.State( + "binary_sensor.heatpump_compressor_state", "on", last_changed=t2 + ), + ] + } - assert hass.states.get("sensor.heatpump_compressor_today").state == "1.83" - time_200 = start_of_today + timedelta(hours=2) - with freeze_time(time_200): + with freeze_time(time_200), patch( + "homeassistant.components.recorder.history.state_changes_during_period", + _fake_states, + ): + await async_setup_component( + hass, + "sensor", + { + "sensor": [ + { + "platform": "history_stats", + "entity_id": "binary_sensor.heatpump_compressor_state", + "name": "heatpump_compressor_today", + "state": "on", + "start": "{{ now().replace(hour=0, minute=0, second=0, microsecond=0) }}", + "end": "{{ now().replace(microsecond=0) }}", + "type": "time", + }, + ] + }, + ) + await hass.async_block_till_done() + await async_update_entity(hass, "sensor.heatpump_compressor_today") + await hass.async_block_till_done() + assert hass.states.get("sensor.heatpump_compressor_today").state == "1.83" async_fire_time_changed(hass, time_200) await hass.async_block_till_done() - assert hass.states.get("sensor.heatpump_compressor_today").state == "3.83" + assert hass.states.get("sensor.heatpump_compressor_today").state == "1.83" hass.states.async_set("binary_sensor.heatpump_compressor_state", "off") await hass.async_block_till_done() + time_400 = start_of_today + timedelta(hours=4) with freeze_time(time_400): async_fire_time_changed(hass, time_400) await hass.async_block_till_done() - assert hass.states.get("sensor.heatpump_compressor_today").state == "3.83" + assert hass.states.get("sensor.heatpump_compressor_today").state == "1.83" hass.states.async_set("binary_sensor.heatpump_compressor_state", "on") await hass.async_block_till_done() time_600 = start_of_today + timedelta(hours=6) with freeze_time(time_600): async_fire_time_changed(hass, time_600) await hass.async_block_till_done() - assert hass.states.get("sensor.heatpump_compressor_today").state == "5.83" + assert hass.states.get("sensor.heatpump_compressor_today").state == "3.83" rolled_to_next_day = start_of_today + timedelta(days=1) assert rolled_to_next_day.hour == 0 assert rolled_to_next_day.minute == 0 @@ -1389,4 +1387,4 @@ def _fake_states(*args, **kwargs): with freeze_time(rolled_to_next_day): async_fire_time_changed(hass, rolled_to_next_day) await hass.async_block_till_done() - assert hass.states.get("sensor.heatpump_compressor_today").state == "0.00" + assert hass.states.get("sensor.heatpump_compressor_today").state == "0.0" From 8cdb373dd5bffaf2716df11f2dbc00ef8f8baa64 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 10 May 2022 10:32:10 -0500 Subject: [PATCH 4/4] tweak --- tests/components/history_stats/test_sensor.py | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/tests/components/history_stats/test_sensor.py b/tests/components/history_stats/test_sensor.py index 92779d4dcd6853..297e3a5a3633d2 100644 --- a/tests/components/history_stats/test_sensor.py +++ b/tests/components/history_stats/test_sensor.py @@ -1308,6 +1308,87 @@ def _fake_states(*args, **kwargs): assert hass.states.get("sensor.sensor4").state == "83.3" +async def test_measure_cet(hass, recorder_mock): + """Test the history statistics sensor measure with a non-UTC timezone.""" + hass.config.set_time_zone("Europe/Berlin") + start_time = dt_util.utcnow() - timedelta(minutes=60) + t0 = start_time + timedelta(minutes=20) + t1 = t0 + timedelta(minutes=10) + t2 = t1 + timedelta(minutes=10) + + # Start t0 t1 t2 End + # |--20min--|--20min--|--10min--|--10min--| + # |---off---|---on----|---off---|---on----| + + def _fake_states(*args, **kwargs): + return { + "binary_sensor.test_id": [ + ha.State("binary_sensor.test_id", "on", last_changed=t0), + ha.State("binary_sensor.test_id", "off", last_changed=t1), + ha.State("binary_sensor.test_id", "on", last_changed=t2), + ] + } + + await async_setup_component( + hass, + "sensor", + { + "sensor": [ + { + "platform": "history_stats", + "entity_id": "binary_sensor.test_id", + "name": "sensor1", + "state": "on", + "start": "{{ as_timestamp(now()) - 3600 }}", + "end": "{{ now() }}", + "type": "time", + }, + { + "platform": "history_stats", + "entity_id": "binary_sensor.test_id", + "name": "sensor2", + "state": "on", + "start": "{{ as_timestamp(now()) - 3600 }}", + "end": "{{ now() }}", + "type": "time", + }, + { + "platform": "history_stats", + "entity_id": "binary_sensor.test_id", + "name": "sensor3", + "state": "on", + "start": "{{ as_timestamp(now()) - 3600 }}", + "end": "{{ now() }}", + "type": "count", + }, + { + "platform": "history_stats", + "entity_id": "binary_sensor.test_id", + "name": "sensor4", + "state": "on", + "start": "{{ as_timestamp(now()) - 3600 }}", + "end": "{{ now() }}", + "type": "ratio", + }, + ] + }, + ) + await hass.async_block_till_done() + + with patch( + "homeassistant.components.recorder.history.state_changes_during_period", + _fake_states, + ): + for i in range(1, 5): + await async_update_entity(hass, f"sensor.sensor{i}") + await hass.async_block_till_done() + + assert hass.states.get("sensor.sensor1").state == "0.83" + assert hass.states.get("sensor.sensor2").state == "0.83" + assert hass.states.get("sensor.sensor3").state == "1" + assert hass.states.get("sensor.sensor4").state == "83.3" + + async def test_end_time_with_microseconds_zeroed(hass, recorder_mock): """Test the history statistics sensor that has the end time microseconds zeroed out.""" hass.config.set_time_zone("Europe/Berlin")