2016-03-09 09:25:50 +00:00
|
|
|
"""The tests for the sun automation."""
|
2015-09-15 07:02:54 +00:00
|
|
|
from datetime import datetime
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import patch
|
2016-12-02 05:45:19 +00:00
|
|
|
|
2018-10-26 09:31:14 +00:00
|
|
|
import pytest
|
2015-09-15 07:02:54 +00:00
|
|
|
|
|
|
|
from homeassistant.components import sun
|
|
|
|
import homeassistant.components.automation as automation
|
2020-09-26 17:28:26 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_ENTITY_ID,
|
|
|
|
ENTITY_MATCH_ALL,
|
|
|
|
SERVICE_TURN_OFF,
|
|
|
|
SERVICE_TURN_ON,
|
|
|
|
SUN_EVENT_SUNRISE,
|
|
|
|
SUN_EVENT_SUNSET,
|
|
|
|
)
|
2019-12-08 16:29:39 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2015-09-15 07:02:54 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
|
|
|
|
2019-12-08 16:29:39 +00:00
|
|
|
from tests.common import async_fire_time_changed, async_mock_service, mock_component
|
2021-03-02 08:02:04 +00:00
|
|
|
from tests.components.blueprint.conftest import stub_blueprint_populate # noqa: F401
|
2015-09-15 07:02:54 +00:00
|
|
|
|
2019-05-02 07:46:32 +00:00
|
|
|
ORIG_TIME_ZONE = dt_util.DEFAULT_TIME_ZONE
|
|
|
|
|
2015-09-15 07:02:54 +00:00
|
|
|
|
2018-10-26 09:31:14 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def calls(hass):
|
2019-05-02 07:46:32 +00:00
|
|
|
"""Track calls to a mock service."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return async_mock_service(hass, "test", "automation")
|
2015-09-15 07:02:54 +00:00
|
|
|
|
|
|
|
|
2018-10-26 09:31:14 +00:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def setup_comp(hass):
|
|
|
|
"""Initialize components."""
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_component(hass, "group")
|
2021-05-08 05:46:26 +00:00
|
|
|
hass.config.set_time_zone(hass.config.time_zone)
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.loop.run_until_complete(
|
|
|
|
async_setup_component(hass, sun.DOMAIN, {sun.DOMAIN: {sun.CONF_ELEVATION: 0}})
|
|
|
|
)
|
2015-09-15 07:02:54 +00:00
|
|
|
|
|
|
|
|
2019-05-02 07:46:32 +00:00
|
|
|
def teardown():
|
|
|
|
"""Restore."""
|
|
|
|
dt_util.set_default_time_zone(ORIG_TIME_ZONE)
|
|
|
|
|
|
|
|
|
2020-06-29 16:39:24 +00:00
|
|
|
async def test_sunset_trigger(hass, calls, legacy_patchable_time):
|
2018-10-26 09:31:14 +00:00
|
|
|
"""Test the sunset trigger."""
|
|
|
|
now = datetime(2015, 9, 15, 23, tzinfo=dt_util.UTC)
|
|
|
|
trigger_time = datetime(2015, 9, 16, 2, tzinfo=dt_util.UTC)
|
2015-09-15 07:02:54 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
|
|
|
await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {"platform": "sun", "event": SUN_EVENT_SUNSET},
|
2021-03-31 12:56:04 +00:00
|
|
|
"action": {
|
|
|
|
"service": "test.automation",
|
|
|
|
"data_template": {"id": "{{ trigger.id}}"},
|
|
|
|
},
|
2015-12-21 23:09:51 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2015-12-21 23:09:51 +00:00
|
|
|
|
2020-09-26 17:28:26 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
automation.DOMAIN,
|
|
|
|
SERVICE_TURN_OFF,
|
|
|
|
{ATTR_ENTITY_ID: ENTITY_MATCH_ALL},
|
|
|
|
blocking=True,
|
|
|
|
)
|
2015-12-21 23:09:51 +00:00
|
|
|
|
2018-10-26 09:31:14 +00:00
|
|
|
async_fire_time_changed(hass, trigger_time)
|
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 0
|
2018-10-26 09:31:14 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
2020-09-26 17:28:26 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
automation.DOMAIN,
|
|
|
|
SERVICE_TURN_ON,
|
|
|
|
{ATTR_ENTITY_ID: ENTITY_MATCH_ALL},
|
|
|
|
blocking=True,
|
|
|
|
)
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
async_fire_time_changed(hass, trigger_time)
|
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 1
|
2021-03-31 12:56:04 +00:00
|
|
|
assert calls[0].data["id"] == 0
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
|
2020-06-29 16:39:24 +00:00
|
|
|
async def test_sunrise_trigger(hass, calls, legacy_patchable_time):
|
2018-10-26 09:31:14 +00:00
|
|
|
"""Test the sunrise trigger."""
|
|
|
|
now = datetime(2015, 9, 13, 23, tzinfo=dt_util.UTC)
|
|
|
|
trigger_time = datetime(2015, 9, 16, 14, tzinfo=dt_util.UTC)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
|
|
|
await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {"platform": "sun", "event": SUN_EVENT_SUNRISE},
|
|
|
|
"action": {"service": "test.automation"},
|
2015-12-27 01:48:20 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2015-12-21 23:09:51 +00:00
|
|
|
|
2018-10-26 09:31:14 +00:00
|
|
|
async_fire_time_changed(hass, trigger_time)
|
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 1
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
|
2020-06-29 16:39:24 +00:00
|
|
|
async def test_sunset_trigger_with_offset(hass, calls, legacy_patchable_time):
|
2018-10-26 09:31:14 +00:00
|
|
|
"""Test the sunset trigger with offset."""
|
|
|
|
now = datetime(2015, 9, 15, 23, tzinfo=dt_util.UTC)
|
|
|
|
trigger_time = datetime(2015, 9, 16, 2, 30, tzinfo=dt_util.UTC)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
|
|
|
await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "sun",
|
|
|
|
"event": SUN_EVENT_SUNSET,
|
|
|
|
"offset": "0:30:00",
|
|
|
|
},
|
|
|
|
"action": {
|
|
|
|
"service": "test.automation",
|
|
|
|
"data_template": {
|
|
|
|
"some": "{{ trigger.%s }}"
|
|
|
|
% "}} - {{ trigger.".join(("platform", "event", "offset"))
|
|
|
|
},
|
2018-10-26 09:31:14 +00:00
|
|
|
},
|
2015-12-27 01:48:20 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2015-12-21 23:09:51 +00:00
|
|
|
|
2018-10-26 09:31:14 +00:00
|
|
|
async_fire_time_changed(hass, trigger_time)
|
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 1
|
|
|
|
assert calls[0].data["some"] == "sun - sunset - 0:30:00"
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
|
2020-06-29 16:39:24 +00:00
|
|
|
async def test_sunrise_trigger_with_offset(hass, calls, legacy_patchable_time):
|
2018-10-26 09:31:14 +00:00
|
|
|
"""Test the sunrise trigger with offset."""
|
|
|
|
now = datetime(2015, 9, 13, 23, tzinfo=dt_util.UTC)
|
|
|
|
trigger_time = datetime(2015, 9, 16, 13, 30, tzinfo=dt_util.UTC)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
|
|
|
await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "sun",
|
|
|
|
"event": SUN_EVENT_SUNRISE,
|
|
|
|
"offset": "-0:30:00",
|
|
|
|
},
|
|
|
|
"action": {"service": "test.automation"},
|
2015-12-21 23:09:51 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2015-12-21 23:09:51 +00:00
|
|
|
|
2018-10-26 09:31:14 +00:00
|
|
|
async_fire_time_changed(hass, trigger_time)
|
|
|
|
await hass.async_block_till_done()
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 1
|