core/tests/components/automation/test_time.py

210 lines
6.2 KiB
Python
Raw Normal View History

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
from unittest.mock import patch
2015-08-11 05:21:34 +00:00
import pytest
import homeassistant.components.automation as automation
from homeassistant.setup import async_setup_component
2015-08-11 05:21:34 +00:00
import homeassistant.util.dt as dt_util
from tests.common import (
assert_setup_component,
async_fire_time_changed,
async_mock_service,
mock_component,
)
2015-08-11 05:21:34 +00:00
@pytest.fixture
def calls(hass):
"""Track calls to a mock serivce."""
2019-07-31 19:25:30 +00:00
return async_mock_service(hass, "test", "automation")
2015-08-11 05:21:34 +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
async def test_if_fires_using_at(hass, calls):
"""Test for firing at."""
2019-07-31 19:25:30 +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": {
"some": "{{ trigger.platform }} - {{ trigger.now.hour }}"
2019-07-31 19:25:30 +00:00
},
2015-09-15 05:05:40 +00:00
},
}
2019-07-31 19:25:30 +00:00
},
)
2015-09-15 05:05:40 +00:00
2019-07-31 19:25:30 +00:00
async_fire_time_changed(hass, dt_util.utcnow().replace(hour=5, minute=0, second=0))
2015-09-15 05:05:40 +00:00
await hass.async_block_till_done()
assert 1 == len(calls)
2019-07-31 19:25:30 +00:00
assert "time - 5" == calls[0].data["some"]
2015-09-15 05:05:40 +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
This should break the before rule.
"""
2019-04-09 20:59:15 +00:00
with assert_setup_component(0, automation.DOMAIN):
2019-07-31 19:25:30 +00:00
assert await async_setup_component(
hass,
automation.DOMAIN,
{
automation.DOMAIN: {
"trigger": {
"platform": "time",
"at": 3605,
# Total seconds. Hour = 3600 second
},
"action": {"service": "test.automation"},
2015-09-15 05:05:40 +00:00
}
2019-07-31 19:25:30 +00:00
},
)
2015-09-15 05:05:40 +00:00
2019-07-31 19:25:30 +00:00
async_fire_time_changed(hass, dt_util.utcnow().replace(hour=1, minute=0, second=5))
await hass.async_block_till_done()
assert 0 == len(calls)
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
},
)
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")
await hass.async_block_till_done()
assert 1 == len(calls)
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")
await hass.async_block_till_done()
assert 1 == len(calls)
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"},
}
2019-07-31 19:25:30 +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")
await hass.async_block_till_done()
assert 0 == len(calls)
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")
await hass.async_block_till_done()
assert 1 == len(calls)
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"},
}
2019-07-31 19:25:30 +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")
await hass.async_block_till_done()
assert 1 == len(calls)
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")
await hass.async_block_till_done()
assert 1 == len(calls)
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"},
}
2019-07-31 19:25:30 +00:00
},
)
2015-09-15 05:05:40 +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")
await hass.async_block_till_done()
2015-09-15 05:05:40 +00:00
assert 1 == len(calls)
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")
await hass.async_block_till_done()
2015-09-15 05:05:40 +00:00
assert 2 == len(calls)
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")
await hass.async_block_till_done()
2015-09-15 05:05:40 +00:00
assert 2 == len(calls)