core/tests/components/automation/test_time.py

232 lines
6.6 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
from homeassistant.setup import async_setup_component
2015-08-11 05:21:34 +00:00
import homeassistant.util.dt as dt_util
import homeassistant.components.automation as automation
from tests.common import (
async_fire_time_changed, assert_setup_component, mock_component)
from tests.common import async_mock_service
2015-08-11 05:21:34 +00:00
@pytest.fixture
def calls(hass):
"""Track calls to a mock serivce."""
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."""
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."""
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 }}'
2015-09-15 05:05:40 +00:00
},
}
}
})
2015-09-15 05:05:40 +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)
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.
"""
with assert_setup_component(0):
assert await async_setup_component(hass, automation.DOMAIN, {
2015-09-15 05:05:40 +00:00
automation.DOMAIN: {
'trigger': {
'platform': 'time',
'at': 3605,
# Total seconds. Hour = 3600 second
2015-09-15 05:05:40 +00:00
},
'action': {
2015-09-19 15:43:56 +00:00
'service': 'test.automation'
2015-09-15 05:05:40 +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."""
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
}
}
})
before_10 = dt_util.now().replace(hour=8)
after_10 = dt_util.now().replace(hour=14)
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)
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."""
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'
}
}
})
before_10 = dt_util.now().replace(hour=8)
after_10 = dt_util.now().replace(hour=14)
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)
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."""
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'
}
}
})
days_past_monday = dt_util.now().weekday()
monday = dt_util.now() - timedelta(days=days_past_monday)
tuesday = monday + timedelta(days=1)
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)
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."""
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'
}
}
})
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
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
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
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)