2016-03-09 09:25:50 +00:00
|
|
|
"""The tests for the time automation."""
|
2015-09-14 05:25:42 +00:00
|
|
|
from datetime import timedelta
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import Mock, patch
|
2015-08-11 05:21:34 +00:00
|
|
|
|
2018-10-26 09:31:14 +00:00
|
|
|
import pytest
|
2020-11-20 14:43:28 +00:00
|
|
|
import voluptuous as vol
|
2018-10-26 09:31:14 +00:00
|
|
|
|
2020-11-09 13:50:54 +00:00
|
|
|
from homeassistant.components import automation, sensor
|
2020-11-20 14:43:28 +00:00
|
|
|
from homeassistant.components.homeassistant.triggers import time
|
2020-11-09 13:50:54 +00:00
|
|
|
from homeassistant.const import ATTR_DEVICE_CLASS, ATTR_ENTITY_ID, SERVICE_TURN_OFF
|
2018-10-26 09:31:14 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2015-08-11 05:21:34 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
|
|
|
|
2019-12-08 16:29:39 +00:00
|
|
|
from tests.common import (
|
|
|
|
assert_setup_component,
|
|
|
|
async_fire_time_changed,
|
|
|
|
async_mock_service,
|
|
|
|
mock_component,
|
|
|
|
)
|
2015-08-11 05:21:34 +00:00
|
|
|
|
|
|
|
|
2018-10-26 09:31:14 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def calls(hass):
|
2020-01-27 18:56:26 +00:00
|
|
|
"""Track calls to a mock service."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return async_mock_service(hass, "test", "automation")
|
2015-08-11 05:21:34 +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")
|
2015-08-11 05:21:34 +00:00
|
|
|
|
|
|
|
|
2018-10-26 09:31:14 +00:00
|
|
|
async def test_if_fires_using_at(hass, calls):
|
|
|
|
"""Test for firing at."""
|
2020-08-18 12:37:04 +00:00
|
|
|
now = dt_util.now()
|
2020-07-21 21:22:55 +00:00
|
|
|
|
2020-08-18 12:37:04 +00:00
|
|
|
trigger_dt = now.replace(hour=5, minute=0, second=0, microsecond=0) + timedelta(2)
|
|
|
|
time_that_will_not_match_right_away = trigger_dt - timedelta(minutes=1)
|
2015-09-15 05:05:40 +00:00
|
|
|
|
2020-07-21 21:22:55 +00:00
|
|
|
with patch(
|
2020-08-18 12:37:04 +00:00
|
|
|
"homeassistant.util.dt.utcnow",
|
|
|
|
return_value=dt_util.as_utc(time_that_will_not_match_right_away),
|
2020-07-21 21:22:55 +00:00
|
|
|
):
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {"platform": "time", "at": "5:00:00"},
|
|
|
|
"action": {
|
|
|
|
"service": "test.automation",
|
|
|
|
"data_template": {
|
2021-03-31 12:56:04 +00:00
|
|
|
"some": "{{ trigger.platform }} - {{ trigger.now.hour }}",
|
|
|
|
"id": "{{ trigger.id}}",
|
2020-07-21 21:22:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2020-08-18 12:37:04 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-07-21 21:22:55 +00:00
|
|
|
|
2020-08-18 12:37:04 +00:00
|
|
|
async_fire_time_changed(hass, trigger_dt + timedelta(seconds=1))
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-08-18 12:37:04 +00:00
|
|
|
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 1
|
|
|
|
assert calls[0].data["some"] == "time - 5"
|
2021-03-31 12:56:04 +00:00
|
|
|
assert calls[0].data["id"] == 0
|
2015-09-15 05:05:40 +00:00
|
|
|
|
|
|
|
|
2020-08-11 20:16:28 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"has_date,has_time", [(True, True), (True, False), (False, True)]
|
|
|
|
)
|
|
|
|
async def test_if_fires_using_at_input_datetime(hass, calls, has_date, has_time):
|
|
|
|
"""Test for firing at input_datetime."""
|
|
|
|
await async_setup_component(
|
|
|
|
hass,
|
|
|
|
"input_datetime",
|
|
|
|
{"input_datetime": {"trigger": {"has_date": has_date, "has_time": has_time}}},
|
|
|
|
)
|
|
|
|
now = dt_util.now()
|
|
|
|
|
|
|
|
trigger_dt = now.replace(
|
|
|
|
hour=5 if has_time else 0, minute=0, second=0, microsecond=0
|
|
|
|
) + timedelta(2)
|
|
|
|
|
|
|
|
await hass.services.async_call(
|
|
|
|
"input_datetime",
|
|
|
|
"set_datetime",
|
|
|
|
{
|
|
|
|
ATTR_ENTITY_ID: "input_datetime.trigger",
|
|
|
|
"datetime": str(trigger_dt.replace(tzinfo=None)),
|
|
|
|
},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
time_that_will_not_match_right_away = trigger_dt - timedelta(minutes=1)
|
|
|
|
|
2020-10-19 07:42:00 +00:00
|
|
|
some_data = "{{ trigger.platform }}-{{ trigger.now.day }}-{{ trigger.now.hour }}-{{trigger.entity_id}}"
|
2020-08-11 20:16:28 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.util.dt.utcnow",
|
|
|
|
return_value=dt_util.as_utc(time_that_will_not_match_right_away),
|
|
|
|
):
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {"platform": "time", "at": "input_datetime.trigger"},
|
|
|
|
"action": {
|
|
|
|
"service": "test.automation",
|
|
|
|
"data_template": {"some": some_data},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2020-08-18 12:37:04 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-08-11 20:16:28 +00:00
|
|
|
|
2020-08-18 12:37:04 +00:00
|
|
|
async_fire_time_changed(hass, trigger_dt + timedelta(seconds=1))
|
2020-08-11 20:16:28 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert len(calls) == 1
|
2020-10-19 07:42:00 +00:00
|
|
|
assert (
|
|
|
|
calls[0].data["some"]
|
|
|
|
== f"time-{trigger_dt.day}-{trigger_dt.hour}-input_datetime.trigger"
|
|
|
|
)
|
2020-08-11 20:16:28 +00:00
|
|
|
|
|
|
|
if has_date:
|
|
|
|
trigger_dt += timedelta(days=1)
|
|
|
|
if has_time:
|
|
|
|
trigger_dt += timedelta(hours=1)
|
|
|
|
|
|
|
|
await hass.services.async_call(
|
|
|
|
"input_datetime",
|
|
|
|
"set_datetime",
|
|
|
|
{
|
|
|
|
ATTR_ENTITY_ID: "input_datetime.trigger",
|
|
|
|
"datetime": str(trigger_dt.replace(tzinfo=None)),
|
|
|
|
},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
|
2020-08-18 12:37:04 +00:00
|
|
|
async_fire_time_changed(hass, trigger_dt + timedelta(seconds=1))
|
2020-08-11 20:16:28 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert len(calls) == 2
|
2020-10-19 07:42:00 +00:00
|
|
|
assert (
|
|
|
|
calls[1].data["some"]
|
|
|
|
== f"time-{trigger_dt.day}-{trigger_dt.hour}-input_datetime.trigger"
|
|
|
|
)
|
2020-08-11 20:16:28 +00:00
|
|
|
|
|
|
|
|
2020-07-30 00:51:30 +00:00
|
|
|
async def test_if_fires_using_multiple_at(hass, calls):
|
|
|
|
"""Test for firing at."""
|
|
|
|
|
2020-08-18 12:37:04 +00:00
|
|
|
now = dt_util.now()
|
2020-07-30 00:51:30 +00:00
|
|
|
|
2020-08-18 12:37:04 +00:00
|
|
|
trigger_dt = now.replace(hour=5, minute=0, second=0, microsecond=0) + timedelta(2)
|
|
|
|
time_that_will_not_match_right_away = trigger_dt - timedelta(minutes=1)
|
2020-07-30 00:51:30 +00:00
|
|
|
|
|
|
|
with patch(
|
2020-08-18 12:37:04 +00:00
|
|
|
"homeassistant.util.dt.utcnow",
|
|
|
|
return_value=dt_util.as_utc(time_that_will_not_match_right_away),
|
2020-07-30 00:51:30 +00:00
|
|
|
):
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {"platform": "time", "at": ["5:00:00", "6:00:00"]},
|
|
|
|
"action": {
|
|
|
|
"service": "test.automation",
|
|
|
|
"data_template": {
|
|
|
|
"some": "{{ trigger.platform }} - {{ trigger.now.hour }}"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2020-08-18 12:37:04 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-07-30 00:51:30 +00:00
|
|
|
|
2020-08-18 12:37:04 +00:00
|
|
|
async_fire_time_changed(hass, trigger_dt + timedelta(seconds=1))
|
2020-07-30 00:51:30 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-08-18 12:37:04 +00:00
|
|
|
|
2020-07-30 00:51:30 +00:00
|
|
|
assert len(calls) == 1
|
|
|
|
assert calls[0].data["some"] == "time - 5"
|
|
|
|
|
2020-08-18 12:37:04 +00:00
|
|
|
async_fire_time_changed(hass, trigger_dt + timedelta(hours=1, seconds=1))
|
2020-07-30 00:51:30 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-08-18 12:37:04 +00:00
|
|
|
|
2020-07-30 00:51:30 +00:00
|
|
|
assert len(calls) == 2
|
|
|
|
assert calls[1].data["some"] == "time - 6"
|
|
|
|
|
|
|
|
|
2018-10-26 09:31:14 +00:00
|
|
|
async def test_if_not_fires_using_wrong_at(hass, calls):
|
|
|
|
"""YAML translates time values to total seconds.
|
2015-09-15 05:05:40 +00:00
|
|
|
|
2018-10-26 09:31:14 +00:00
|
|
|
This should break the before rule.
|
|
|
|
"""
|
2020-07-21 21:22:55 +00:00
|
|
|
now = dt_util.utcnow()
|
|
|
|
|
|
|
|
time_that_will_not_match_right_away = now.replace(
|
|
|
|
year=now.year + 1, hour=1, minute=0, second=0
|
|
|
|
)
|
2015-09-15 05:05:40 +00:00
|
|
|
|
2020-07-21 21:22:55 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away
|
|
|
|
):
|
|
|
|
with assert_setup_component(0, automation.DOMAIN):
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "time",
|
|
|
|
"at": 3605,
|
|
|
|
# Total seconds. Hour = 3600 second
|
|
|
|
},
|
|
|
|
"action": {"service": "test.automation"},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2020-08-18 12:37:04 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-07-21 21:22:55 +00:00
|
|
|
|
|
|
|
async_fire_time_changed(
|
|
|
|
hass, now.replace(year=now.year + 1, hour=1, minute=0, second=5)
|
|
|
|
)
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
async def test_if_action_before(hass, calls):
|
|
|
|
"""Test for if action before."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {"platform": "event", "event_type": "test_event"},
|
|
|
|
"condition": {"condition": "time", "before": "10:00"},
|
|
|
|
"action": {"service": "test.automation"},
|
2015-09-15 05:05:40 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2020-08-18 12:37:04 +00:00
|
|
|
await hass.async_block_till_done()
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
before_10 = dt_util.now().replace(hour=8)
|
|
|
|
after_10 = dt_util.now().replace(hour=14)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.helpers.condition.dt_util.now", return_value=before_10):
|
|
|
|
hass.bus.async_fire("test_event")
|
2018-10-26 09:31:14 +00:00
|
|
|
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
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.helpers.condition.dt_util.now", return_value=after_10):
|
|
|
|
hass.bus.async_fire("test_event")
|
2018-10-26 09:31:14 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
async def test_if_action_after(hass, calls):
|
|
|
|
"""Test for if action after."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {"platform": "event", "event_type": "test_event"},
|
|
|
|
"condition": {"condition": "time", "after": "10:00"},
|
|
|
|
"action": {"service": "test.automation"},
|
2018-10-26 09:31:14 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2020-08-18 12:37:04 +00:00
|
|
|
await hass.async_block_till_done()
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
before_10 = dt_util.now().replace(hour=8)
|
|
|
|
after_10 = dt_util.now().replace(hour=14)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.helpers.condition.dt_util.now", return_value=before_10):
|
|
|
|
hass.bus.async_fire("test_event")
|
2018-10-26 09:31:14 +00:00
|
|
|
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.helpers.condition.dt_util.now", return_value=after_10):
|
|
|
|
hass.bus.async_fire("test_event")
|
2018-10-26 09:31:14 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
async def test_if_action_one_weekday(hass, calls):
|
|
|
|
"""Test for if action with one weekday."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {"platform": "event", "event_type": "test_event"},
|
|
|
|
"condition": {"condition": "time", "weekday": "mon"},
|
|
|
|
"action": {"service": "test.automation"},
|
2018-10-26 09:31:14 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2020-08-18 12:37:04 +00:00
|
|
|
await hass.async_block_till_done()
|
2018-10-26 09:31:14 +00:00
|
|
|
|
|
|
|
days_past_monday = dt_util.now().weekday()
|
|
|
|
monday = dt_util.now() - timedelta(days=days_past_monday)
|
|
|
|
tuesday = monday + timedelta(days=1)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.helpers.condition.dt_util.now", return_value=monday):
|
|
|
|
hass.bus.async_fire("test_event")
|
2018-10-26 09:31:14 +00:00
|
|
|
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
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.helpers.condition.dt_util.now", return_value=tuesday):
|
|
|
|
hass.bus.async_fire("test_event")
|
2018-10-26 09:31:14 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
async def test_if_action_list_weekday(hass, calls):
|
|
|
|
"""Test for action with a list of weekdays."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {"platform": "event", "event_type": "test_event"},
|
|
|
|
"condition": {"condition": "time", "weekday": ["mon", "tue"]},
|
|
|
|
"action": {"service": "test.automation"},
|
2018-10-26 09:31:14 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2020-08-18 12:37:04 +00:00
|
|
|
await hass.async_block_till_done()
|
2015-09-15 05:05:40 +00:00
|
|
|
|
2018-10-26 09:31:14 +00:00
|
|
|
days_past_monday = dt_util.now().weekday()
|
|
|
|
monday = dt_util.now() - timedelta(days=days_past_monday)
|
|
|
|
tuesday = monday + timedelta(days=1)
|
|
|
|
wednesday = tuesday + timedelta(days=1)
|
2015-09-15 05:05:40 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.helpers.condition.dt_util.now", return_value=monday):
|
|
|
|
hass.bus.async_fire("test_event")
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2015-09-15 05:05:40 +00:00
|
|
|
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 1
|
2015-09-15 05:05:40 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.helpers.condition.dt_util.now", return_value=tuesday):
|
|
|
|
hass.bus.async_fire("test_event")
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2015-09-15 05:05:40 +00:00
|
|
|
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 2
|
2015-09-15 05:05:40 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.helpers.condition.dt_util.now", return_value=wednesday):
|
|
|
|
hass.bus.async_fire("test_event")
|
2018-10-26 09:31:14 +00:00
|
|
|
await hass.async_block_till_done()
|
2015-09-15 05:05:40 +00:00
|
|
|
|
2020-05-01 14:37:25 +00:00
|
|
|
assert len(calls) == 2
|
2020-07-30 00:51:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_untrack_time_change(hass):
|
|
|
|
"""Test for removing tracked time changes."""
|
|
|
|
mock_track_time_change = Mock()
|
|
|
|
with patch(
|
2020-08-17 16:54:56 +00:00
|
|
|
"homeassistant.components.homeassistant.triggers.time.async_track_time_change",
|
2020-07-30 00:51:30 +00:00
|
|
|
return_value=mock_track_time_change,
|
|
|
|
):
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"alias": "test",
|
|
|
|
"trigger": {
|
|
|
|
"platform": "time",
|
|
|
|
"at": ["5:00:00", "6:00:00", "7:00:00"],
|
|
|
|
},
|
|
|
|
"action": {"service": "test.automation", "data": {"test": "test"}},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2020-08-18 12:37:04 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-07-30 00:51:30 +00:00
|
|
|
|
|
|
|
await hass.services.async_call(
|
|
|
|
automation.DOMAIN,
|
|
|
|
SERVICE_TURN_OFF,
|
|
|
|
{ATTR_ENTITY_ID: "automation.test"},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert len(mock_track_time_change.mock_calls) == 3
|
2020-11-09 13:50:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_if_fires_using_at_sensor(hass, calls):
|
|
|
|
"""Test for firing at sensor time."""
|
|
|
|
now = dt_util.now()
|
|
|
|
|
|
|
|
trigger_dt = now.replace(hour=5, minute=0, second=0, microsecond=0) + timedelta(2)
|
|
|
|
|
|
|
|
hass.states.async_set(
|
|
|
|
"sensor.next_alarm",
|
|
|
|
trigger_dt.isoformat(),
|
|
|
|
{ATTR_DEVICE_CLASS: sensor.DEVICE_CLASS_TIMESTAMP},
|
|
|
|
)
|
|
|
|
|
|
|
|
time_that_will_not_match_right_away = trigger_dt - timedelta(minutes=1)
|
|
|
|
|
|
|
|
some_data = "{{ trigger.platform }}-{{ trigger.now.day }}-{{ trigger.now.hour }}-{{trigger.entity_id}}"
|
|
|
|
with patch(
|
|
|
|
"homeassistant.util.dt.utcnow",
|
|
|
|
return_value=dt_util.as_utc(time_that_will_not_match_right_away),
|
|
|
|
):
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {"platform": "time", "at": "sensor.next_alarm"},
|
|
|
|
"action": {
|
|
|
|
"service": "test.automation",
|
|
|
|
"data_template": {"some": some_data},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
async_fire_time_changed(hass, trigger_dt + timedelta(seconds=1))
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert len(calls) == 1
|
|
|
|
assert (
|
|
|
|
calls[0].data["some"]
|
|
|
|
== f"time-{trigger_dt.day}-{trigger_dt.hour}-sensor.next_alarm"
|
|
|
|
)
|
|
|
|
|
|
|
|
trigger_dt += timedelta(days=1, hours=1)
|
|
|
|
|
|
|
|
hass.states.async_set(
|
|
|
|
"sensor.next_alarm",
|
|
|
|
trigger_dt.isoformat(),
|
|
|
|
{ATTR_DEVICE_CLASS: sensor.DEVICE_CLASS_TIMESTAMP},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
async_fire_time_changed(hass, trigger_dt + timedelta(seconds=1))
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert len(calls) == 2
|
|
|
|
assert (
|
|
|
|
calls[1].data["some"]
|
|
|
|
== f"time-{trigger_dt.day}-{trigger_dt.hour}-sensor.next_alarm"
|
|
|
|
)
|
|
|
|
|
|
|
|
for broken in ("unknown", "unavailable", "invalid-ts"):
|
|
|
|
hass.states.async_set(
|
|
|
|
"sensor.next_alarm",
|
|
|
|
trigger_dt.isoformat(),
|
|
|
|
{ATTR_DEVICE_CLASS: sensor.DEVICE_CLASS_TIMESTAMP},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
hass.states.async_set(
|
|
|
|
"sensor.next_alarm",
|
|
|
|
broken,
|
|
|
|
{ATTR_DEVICE_CLASS: sensor.DEVICE_CLASS_TIMESTAMP},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
async_fire_time_changed(hass, trigger_dt + timedelta(seconds=1))
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# We should not have listened to anything
|
|
|
|
assert len(calls) == 2
|
|
|
|
|
|
|
|
# Now without device class
|
|
|
|
hass.states.async_set(
|
|
|
|
"sensor.next_alarm",
|
|
|
|
trigger_dt.isoformat(),
|
|
|
|
{ATTR_DEVICE_CLASS: sensor.DEVICE_CLASS_TIMESTAMP},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
hass.states.async_set(
|
|
|
|
"sensor.next_alarm",
|
|
|
|
trigger_dt.isoformat(),
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
async_fire_time_changed(hass, trigger_dt + timedelta(seconds=1))
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# We should not have listened to anything
|
|
|
|
assert len(calls) == 2
|
2020-11-20 14:43:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"conf",
|
|
|
|
[
|
|
|
|
{"platform": "time", "at": "input_datetime.bla"},
|
|
|
|
{"platform": "time", "at": "sensor.bla"},
|
|
|
|
{"platform": "time", "at": "12:34"},
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_schema_valid(conf):
|
|
|
|
"""Make sure we don't accept number for 'at' value."""
|
|
|
|
time.TRIGGER_SCHEMA(conf)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"conf",
|
|
|
|
[
|
|
|
|
{"platform": "time", "at": "binary_sensor.bla"},
|
|
|
|
{"platform": "time", "at": 745},
|
|
|
|
{"platform": "time", "at": "25:00"},
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_schema_invalid(conf):
|
|
|
|
"""Make sure we don't accept number for 'at' value."""
|
|
|
|
with pytest.raises(vol.Invalid):
|
|
|
|
time.TRIGGER_SCHEMA(conf)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_datetime_in_past_on_load(hass, calls):
|
|
|
|
"""Test time trigger works if input_datetime is in past."""
|
|
|
|
await async_setup_component(
|
|
|
|
hass,
|
|
|
|
"input_datetime",
|
|
|
|
{"input_datetime": {"my_trigger": {"has_date": True, "has_time": True}}},
|
|
|
|
)
|
|
|
|
|
|
|
|
now = dt_util.now()
|
|
|
|
past = now - timedelta(days=2)
|
|
|
|
future = now + timedelta(days=1)
|
|
|
|
|
|
|
|
await hass.services.async_call(
|
|
|
|
"input_datetime",
|
|
|
|
"set_datetime",
|
|
|
|
{
|
|
|
|
ATTR_ENTITY_ID: "input_datetime.my_trigger",
|
|
|
|
"datetime": str(past.replace(tzinfo=None)),
|
|
|
|
},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {"platform": "time", "at": "input_datetime.my_trigger"},
|
|
|
|
"action": {
|
|
|
|
"service": "test.automation",
|
|
|
|
"data_template": {
|
|
|
|
"some": "{{ trigger.platform }}-{{ trigger.now.day }}-{{ trigger.now.hour }}-{{trigger.entity_id}}"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
async_fire_time_changed(hass, now)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert len(calls) == 0
|
|
|
|
|
|
|
|
await hass.services.async_call(
|
|
|
|
"input_datetime",
|
|
|
|
"set_datetime",
|
|
|
|
{
|
|
|
|
ATTR_ENTITY_ID: "input_datetime.my_trigger",
|
|
|
|
"datetime": str(future.replace(tzinfo=None)),
|
|
|
|
},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
async_fire_time_changed(hass, future + timedelta(seconds=1))
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert len(calls) == 1
|
|
|
|
assert (
|
|
|
|
calls[0].data["some"]
|
|
|
|
== f"time-{future.day}-{future.hour}-input_datetime.my_trigger"
|
|
|
|
)
|