Migrate device_sun_light_trigger tests to use freezegun (#105520)

This commit is contained in:
Jan-Philipp Benecke 2023-12-12 08:29:10 +01:00 committed by GitHub
parent a66c9bb7b6
commit 319d6db55b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,6 +2,7 @@
from datetime import datetime
from unittest.mock import patch
from freezegun.api import FrozenDateTimeFactory
import pytest
from homeassistant.components import (
@ -72,13 +73,15 @@ async def scanner(hass, enable_custom_integrations):
return scanner
async def test_lights_on_when_sun_sets(hass: HomeAssistant, scanner) -> None:
async def test_lights_on_when_sun_sets(
hass: HomeAssistant, freezer: FrozenDateTimeFactory, scanner
) -> None:
"""Test lights go on when there is someone home and the sun sets."""
test_time = datetime(2017, 4, 5, 1, 2, 3, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=test_time):
assert await async_setup_component(
hass, device_sun_light_trigger.DOMAIN, {device_sun_light_trigger.DOMAIN: {}}
)
freezer.move_to(test_time)
assert await async_setup_component(
hass, device_sun_light_trigger.DOMAIN, {device_sun_light_trigger.DOMAIN: {}}
)
await hass.services.async_call(
light.DOMAIN,
@ -88,9 +91,9 @@ async def test_lights_on_when_sun_sets(hass: HomeAssistant, scanner) -> None:
)
test_time = test_time.replace(hour=3)
with patch("homeassistant.util.dt.utcnow", return_value=test_time):
async_fire_time_changed(hass, test_time)
await hass.async_block_till_done()
freezer.move_to(test_time)
async_fire_time_changed(hass, test_time)
await hass.async_block_till_done()
assert all(
hass.states.get(ent_id).state == STATE_ON
@ -128,22 +131,22 @@ async def test_lights_turn_off_when_everyone_leaves(
async def test_lights_turn_on_when_coming_home_after_sun_set(
hass: HomeAssistant, scanner
hass: HomeAssistant, freezer: FrozenDateTimeFactory, scanner
) -> None:
"""Test lights turn on when coming home after sun set."""
test_time = datetime(2017, 4, 5, 3, 2, 3, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=test_time):
await hass.services.async_call(
light.DOMAIN, light.SERVICE_TURN_OFF, {ATTR_ENTITY_ID: "all"}, blocking=True
)
freezer.move_to(test_time)
await hass.services.async_call(
light.DOMAIN, light.SERVICE_TURN_OFF, {ATTR_ENTITY_ID: "all"}, blocking=True
)
assert await async_setup_component(
hass, device_sun_light_trigger.DOMAIN, {device_sun_light_trigger.DOMAIN: {}}
)
assert await async_setup_component(
hass, device_sun_light_trigger.DOMAIN, {device_sun_light_trigger.DOMAIN: {}}
)
hass.states.async_set(f"{DOMAIN}.device_2", STATE_HOME)
hass.states.async_set(f"{DOMAIN}.device_2", STATE_HOME)
await hass.async_block_till_done()
await hass.async_block_till_done()
assert all(
hass.states.get(ent_id).state == light.STATE_ON
@ -152,85 +155,85 @@ async def test_lights_turn_on_when_coming_home_after_sun_set(
async def test_lights_turn_on_when_coming_home_after_sun_set_person(
hass: HomeAssistant, scanner
hass: HomeAssistant, freezer: FrozenDateTimeFactory, scanner
) -> None:
"""Test lights turn on when coming home after sun set."""
device_1 = f"{DOMAIN}.device_1"
device_2 = f"{DOMAIN}.device_2"
test_time = datetime(2017, 4, 5, 3, 2, 3, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=test_time):
await hass.services.async_call(
light.DOMAIN, light.SERVICE_TURN_OFF, {ATTR_ENTITY_ID: "all"}, blocking=True
)
hass.states.async_set(device_1, STATE_NOT_HOME)
hass.states.async_set(device_2, STATE_NOT_HOME)
await hass.async_block_till_done()
freezer.move_to(test_time)
await hass.services.async_call(
light.DOMAIN, light.SERVICE_TURN_OFF, {ATTR_ENTITY_ID: "all"}, blocking=True
)
hass.states.async_set(device_1, STATE_NOT_HOME)
hass.states.async_set(device_2, STATE_NOT_HOME)
await hass.async_block_till_done()
assert all(
not light.is_on(hass, ent_id)
for ent_id in hass.states.async_entity_ids("light")
)
assert hass.states.get(device_1).state == "not_home"
assert hass.states.get(device_2).state == "not_home"
assert all(
not light.is_on(hass, ent_id)
for ent_id in hass.states.async_entity_ids("light")
)
assert hass.states.get(device_1).state == "not_home"
assert hass.states.get(device_2).state == "not_home"
assert await async_setup_component(
hass,
"person",
{"person": [{"id": "me", "name": "Me", "device_trackers": [device_1]}]},
)
assert await async_setup_component(
hass,
"person",
{"person": [{"id": "me", "name": "Me", "device_trackers": [device_1]}]},
)
assert await async_setup_component(hass, "group", {})
await hass.async_block_till_done()
await group.Group.async_create_group(
hass,
"person_me",
created_by_service=False,
entity_ids=["person.me"],
icon=None,
mode=None,
object_id=None,
order=None,
)
assert await async_setup_component(hass, "group", {})
await hass.async_block_till_done()
await group.Group.async_create_group(
hass,
"person_me",
created_by_service=False,
entity_ids=["person.me"],
icon=None,
mode=None,
object_id=None,
order=None,
)
assert await async_setup_component(
hass,
device_sun_light_trigger.DOMAIN,
{device_sun_light_trigger.DOMAIN: {"device_group": "group.person_me"}},
)
assert await async_setup_component(
hass,
device_sun_light_trigger.DOMAIN,
{device_sun_light_trigger.DOMAIN: {"device_group": "group.person_me"}},
)
assert all(
hass.states.get(ent_id).state == STATE_OFF
for ent_id in hass.states.async_entity_ids("light")
)
assert hass.states.get(device_1).state == "not_home"
assert hass.states.get(device_2).state == "not_home"
assert hass.states.get("person.me").state == "not_home"
assert all(
hass.states.get(ent_id).state == STATE_OFF
for ent_id in hass.states.async_entity_ids("light")
)
assert hass.states.get(device_1).state == "not_home"
assert hass.states.get(device_2).state == "not_home"
assert hass.states.get("person.me").state == "not_home"
# Unrelated device has no impact
hass.states.async_set(device_2, STATE_HOME)
await hass.async_block_till_done()
# Unrelated device has no impact
hass.states.async_set(device_2, STATE_HOME)
await hass.async_block_till_done()
assert all(
hass.states.get(ent_id).state == STATE_OFF
for ent_id in hass.states.async_entity_ids("light")
)
assert hass.states.get(device_1).state == "not_home"
assert hass.states.get(device_2).state == "home"
assert hass.states.get("person.me").state == "not_home"
assert all(
hass.states.get(ent_id).state == STATE_OFF
for ent_id in hass.states.async_entity_ids("light")
)
assert hass.states.get(device_1).state == "not_home"
assert hass.states.get(device_2).state == "home"
assert hass.states.get("person.me").state == "not_home"
# person home switches on
hass.states.async_set(device_1, STATE_HOME)
await hass.async_block_till_done()
await hass.async_block_till_done()
# person home switches on
hass.states.async_set(device_1, STATE_HOME)
await hass.async_block_till_done()
await hass.async_block_till_done()
assert all(
hass.states.get(ent_id).state == light.STATE_ON
for ent_id in hass.states.async_entity_ids("light")
)
assert hass.states.get(device_1).state == "home"
assert hass.states.get(device_2).state == "home"
assert hass.states.get("person.me").state == "home"
assert all(
hass.states.get(ent_id).state == light.STATE_ON
for ent_id in hass.states.async_entity_ids("light")
)
assert hass.states.get(device_1).state == "home"
assert hass.states.get(device_2).state == "home"
assert hass.states.get("person.me").state == "home"
async def test_initialize_start(hass: HomeAssistant) -> None: