mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Migrate device_sun_light_trigger tests to use freezegun (#105520)
This commit is contained in:
parent
a66c9bb7b6
commit
319d6db55b
@ -2,6 +2,7 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
from freezegun.api import FrozenDateTimeFactory
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components import (
|
from homeassistant.components import (
|
||||||
@ -72,13 +73,15 @@ async def scanner(hass, enable_custom_integrations):
|
|||||||
return scanner
|
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 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)
|
test_time = datetime(2017, 4, 5, 1, 2, 3, tzinfo=dt_util.UTC)
|
||||||
with patch("homeassistant.util.dt.utcnow", return_value=test_time):
|
freezer.move_to(test_time)
|
||||||
assert await async_setup_component(
|
assert await async_setup_component(
|
||||||
hass, device_sun_light_trigger.DOMAIN, {device_sun_light_trigger.DOMAIN: {}}
|
hass, device_sun_light_trigger.DOMAIN, {device_sun_light_trigger.DOMAIN: {}}
|
||||||
)
|
)
|
||||||
|
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
light.DOMAIN,
|
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)
|
test_time = test_time.replace(hour=3)
|
||||||
with patch("homeassistant.util.dt.utcnow", return_value=test_time):
|
freezer.move_to(test_time)
|
||||||
async_fire_time_changed(hass, test_time)
|
async_fire_time_changed(hass, test_time)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert all(
|
assert all(
|
||||||
hass.states.get(ent_id).state == STATE_ON
|
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(
|
async def test_lights_turn_on_when_coming_home_after_sun_set(
|
||||||
hass: HomeAssistant, scanner
|
hass: HomeAssistant, freezer: FrozenDateTimeFactory, scanner
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test lights turn on when coming home after sun set."""
|
"""Test lights turn on when coming home after sun set."""
|
||||||
test_time = datetime(2017, 4, 5, 3, 2, 3, tzinfo=dt_util.UTC)
|
test_time = datetime(2017, 4, 5, 3, 2, 3, tzinfo=dt_util.UTC)
|
||||||
with patch("homeassistant.util.dt.utcnow", return_value=test_time):
|
freezer.move_to(test_time)
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
light.DOMAIN, light.SERVICE_TURN_OFF, {ATTR_ENTITY_ID: "all"}, blocking=True
|
light.DOMAIN, light.SERVICE_TURN_OFF, {ATTR_ENTITY_ID: "all"}, blocking=True
|
||||||
)
|
)
|
||||||
|
|
||||||
assert await async_setup_component(
|
assert await async_setup_component(
|
||||||
hass, device_sun_light_trigger.DOMAIN, {device_sun_light_trigger.DOMAIN: {}}
|
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(
|
assert all(
|
||||||
hass.states.get(ent_id).state == light.STATE_ON
|
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(
|
async def test_lights_turn_on_when_coming_home_after_sun_set_person(
|
||||||
hass: HomeAssistant, scanner
|
hass: HomeAssistant, freezer: FrozenDateTimeFactory, scanner
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test lights turn on when coming home after sun set."""
|
"""Test lights turn on when coming home after sun set."""
|
||||||
device_1 = f"{DOMAIN}.device_1"
|
device_1 = f"{DOMAIN}.device_1"
|
||||||
device_2 = f"{DOMAIN}.device_2"
|
device_2 = f"{DOMAIN}.device_2"
|
||||||
|
|
||||||
test_time = datetime(2017, 4, 5, 3, 2, 3, tzinfo=dt_util.UTC)
|
test_time = datetime(2017, 4, 5, 3, 2, 3, tzinfo=dt_util.UTC)
|
||||||
with patch("homeassistant.util.dt.utcnow", return_value=test_time):
|
freezer.move_to(test_time)
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
light.DOMAIN, light.SERVICE_TURN_OFF, {ATTR_ENTITY_ID: "all"}, blocking=True
|
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_1, STATE_NOT_HOME)
|
||||||
hass.states.async_set(device_2, STATE_NOT_HOME)
|
hass.states.async_set(device_2, STATE_NOT_HOME)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert all(
|
assert all(
|
||||||
not light.is_on(hass, ent_id)
|
not light.is_on(hass, ent_id)
|
||||||
for ent_id in hass.states.async_entity_ids("light")
|
for ent_id in hass.states.async_entity_ids("light")
|
||||||
)
|
)
|
||||||
assert hass.states.get(device_1).state == "not_home"
|
assert hass.states.get(device_1).state == "not_home"
|
||||||
assert hass.states.get(device_2).state == "not_home"
|
assert hass.states.get(device_2).state == "not_home"
|
||||||
|
|
||||||
assert await async_setup_component(
|
assert await async_setup_component(
|
||||||
hass,
|
hass,
|
||||||
"person",
|
"person",
|
||||||
{"person": [{"id": "me", "name": "Me", "device_trackers": [device_1]}]},
|
{"person": [{"id": "me", "name": "Me", "device_trackers": [device_1]}]},
|
||||||
)
|
)
|
||||||
|
|
||||||
assert await async_setup_component(hass, "group", {})
|
assert await async_setup_component(hass, "group", {})
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
await group.Group.async_create_group(
|
await group.Group.async_create_group(
|
||||||
hass,
|
hass,
|
||||||
"person_me",
|
"person_me",
|
||||||
created_by_service=False,
|
created_by_service=False,
|
||||||
entity_ids=["person.me"],
|
entity_ids=["person.me"],
|
||||||
icon=None,
|
icon=None,
|
||||||
mode=None,
|
mode=None,
|
||||||
object_id=None,
|
object_id=None,
|
||||||
order=None,
|
order=None,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert await async_setup_component(
|
assert await async_setup_component(
|
||||||
hass,
|
hass,
|
||||||
device_sun_light_trigger.DOMAIN,
|
device_sun_light_trigger.DOMAIN,
|
||||||
{device_sun_light_trigger.DOMAIN: {"device_group": "group.person_me"}},
|
{device_sun_light_trigger.DOMAIN: {"device_group": "group.person_me"}},
|
||||||
)
|
)
|
||||||
|
|
||||||
assert all(
|
assert all(
|
||||||
hass.states.get(ent_id).state == STATE_OFF
|
hass.states.get(ent_id).state == STATE_OFF
|
||||||
for ent_id in hass.states.async_entity_ids("light")
|
for ent_id in hass.states.async_entity_ids("light")
|
||||||
)
|
)
|
||||||
assert hass.states.get(device_1).state == "not_home"
|
assert hass.states.get(device_1).state == "not_home"
|
||||||
assert hass.states.get(device_2).state == "not_home"
|
assert hass.states.get(device_2).state == "not_home"
|
||||||
assert hass.states.get("person.me").state == "not_home"
|
assert hass.states.get("person.me").state == "not_home"
|
||||||
|
|
||||||
# Unrelated device has no impact
|
# Unrelated device has no impact
|
||||||
hass.states.async_set(device_2, STATE_HOME)
|
hass.states.async_set(device_2, STATE_HOME)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert all(
|
assert all(
|
||||||
hass.states.get(ent_id).state == STATE_OFF
|
hass.states.get(ent_id).state == STATE_OFF
|
||||||
for ent_id in hass.states.async_entity_ids("light")
|
for ent_id in hass.states.async_entity_ids("light")
|
||||||
)
|
)
|
||||||
assert hass.states.get(device_1).state == "not_home"
|
assert hass.states.get(device_1).state == "not_home"
|
||||||
assert hass.states.get(device_2).state == "home"
|
assert hass.states.get(device_2).state == "home"
|
||||||
assert hass.states.get("person.me").state == "not_home"
|
assert hass.states.get("person.me").state == "not_home"
|
||||||
|
|
||||||
# person home switches on
|
# person home switches on
|
||||||
hass.states.async_set(device_1, STATE_HOME)
|
hass.states.async_set(device_1, STATE_HOME)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert all(
|
assert all(
|
||||||
hass.states.get(ent_id).state == light.STATE_ON
|
hass.states.get(ent_id).state == light.STATE_ON
|
||||||
for ent_id in hass.states.async_entity_ids("light")
|
for ent_id in hass.states.async_entity_ids("light")
|
||||||
)
|
)
|
||||||
assert hass.states.get(device_1).state == "home"
|
assert hass.states.get(device_1).state == "home"
|
||||||
assert hass.states.get(device_2).state == "home"
|
assert hass.states.get(device_2).state == "home"
|
||||||
assert hass.states.get("person.me").state == "home"
|
assert hass.states.get("person.me").state == "home"
|
||||||
|
|
||||||
|
|
||||||
async def test_initialize_start(hass: HomeAssistant) -> None:
|
async def test_initialize_start(hass: HomeAssistant) -> None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user