core/tests/components/automation/test_time.py

393 lines
12 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
2015-08-11 05:21:34 +00:00
import unittest
2015-09-14 05:25:42 +00:00
from unittest.mock import patch
2015-08-11 05:21:34 +00:00
from homeassistant.bootstrap import _setup_component
2015-08-11 05:21:34 +00:00
import homeassistant.util.dt as dt_util
import homeassistant.components.automation as automation
2016-02-14 23:08:23 +00:00
from tests.common import fire_time_changed, get_test_home_assistant
2015-08-11 05:21:34 +00:00
class TestAutomationTime(unittest.TestCase):
2016-03-09 09:25:50 +00:00
"""Test the event automation."""
2015-08-11 05:21:34 +00:00
def setUp(self): # pylint: disable=invalid-name
2016-03-09 09:25:50 +00:00
"""Setup things to be run when tests are started."""
2016-02-14 23:08:23 +00:00
self.hass = get_test_home_assistant()
self.hass.config.components.append('group')
2015-08-11 05:21:34 +00:00
self.calls = []
def record_call(service):
self.calls.append(service)
self.hass.services.register('test', 'automation', record_call)
def tearDown(self): # pylint: disable=invalid-name
2016-03-09 09:25:50 +00:00
"""Stop everything that was started."""
2015-08-11 05:21:34 +00:00
self.hass.stop()
2015-09-15 05:05:40 +00:00
def test_if_fires_when_hour_matches(self):
2016-03-09 09:25:50 +00:00
"""Test for firing if hour is matching."""
assert _setup_component(self.hass, automation.DOMAIN, {
2015-09-15 05:05:40 +00:00
automation.DOMAIN: {
'trigger': {
'platform': 'time',
'hours': 0,
},
'action': {
2015-09-19 15:43:56 +00:00
'service': 'test.automation'
2015-09-15 05:05:40 +00:00
}
}
})
2015-09-15 05:05:40 +00:00
fire_time_changed(self.hass, dt_util.utcnow().replace(hour=0))
self.hass.block_till_done()
self.assertEqual(1, len(self.calls))
2015-09-15 05:05:40 +00:00
automation.turn_off(self.hass)
self.hass.block_till_done()
fire_time_changed(self.hass, dt_util.utcnow().replace(hour=0))
self.hass.block_till_done()
2015-09-15 05:05:40 +00:00
self.assertEqual(1, len(self.calls))
def test_if_fires_when_minute_matches(self):
2016-03-09 09:25:50 +00:00
"""Test for firing if minutes are matching."""
assert _setup_component(self.hass, automation.DOMAIN, {
2015-09-15 05:05:40 +00:00
automation.DOMAIN: {
'trigger': {
'platform': 'time',
'minutes': 0,
},
'action': {
2015-09-19 15:43:56 +00:00
'service': 'test.automation'
2015-09-15 05:05:40 +00:00
}
}
})
2015-09-15 05:05:40 +00:00
fire_time_changed(self.hass, dt_util.utcnow().replace(minute=0))
self.hass.block_till_done()
2015-09-15 05:05:40 +00:00
self.assertEqual(1, len(self.calls))
def test_if_fires_when_second_matches(self):
2016-03-09 09:25:50 +00:00
"""Test for firing if seconds are matching."""
assert _setup_component(self.hass, automation.DOMAIN, {
2015-09-15 05:05:40 +00:00
automation.DOMAIN: {
'trigger': {
'platform': 'time',
'seconds': 0,
},
'action': {
2015-09-19 15:43:56 +00:00
'service': 'test.automation'
2015-09-15 05:05:40 +00:00
}
}
})
2015-09-15 05:05:40 +00:00
fire_time_changed(self.hass, dt_util.utcnow().replace(second=0))
self.hass.block_till_done()
2015-09-15 05:05:40 +00:00
self.assertEqual(1, len(self.calls))
def test_if_fires_when_all_matches(self):
2016-03-09 09:25:50 +00:00
"""Test for firing if everything matches."""
assert _setup_component(self.hass, automation.DOMAIN, {
2015-09-15 05:05:40 +00:00
automation.DOMAIN: {
'trigger': {
'platform': 'time',
'hours': 1,
'minutes': 2,
'seconds': 3,
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
}
}
})
2015-09-15 05:05:40 +00:00
fire_time_changed(self.hass, dt_util.utcnow().replace(
hour=1, minute=2, second=3))
2015-09-15 05:05:40 +00:00
self.hass.block_till_done()
2015-09-15 15:56:06 +00:00
self.assertEqual(1, len(self.calls))
def test_if_fires_periodic_seconds(self):
2016-03-09 09:25:50 +00:00
"""Test for firing periodically every second."""
assert _setup_component(self.hass, automation.DOMAIN, {
automation.DOMAIN: {
'trigger': {
'platform': 'time',
'seconds': "/2",
},
'action': {
'service': 'test.automation'
}
}
})
fire_time_changed(self.hass, dt_util.utcnow().replace(
hour=0, minute=0, second=2))
self.hass.block_till_done()
self.assertEqual(1, len(self.calls))
def test_if_fires_periodic_minutes(self):
2016-03-09 09:25:50 +00:00
"""Test for firing periodically every minute."""
assert _setup_component(self.hass, automation.DOMAIN, {
automation.DOMAIN: {
'trigger': {
'platform': 'time',
'minutes': "/2",
},
'action': {
'service': 'test.automation'
}
}
})
fire_time_changed(self.hass, dt_util.utcnow().replace(
hour=0, minute=2, second=0))
self.hass.block_till_done()
self.assertEqual(1, len(self.calls))
def test_if_fires_periodic_hours(self):
2016-03-09 09:25:50 +00:00
"""Test for firing periodically every hour."""
assert _setup_component(self.hass, automation.DOMAIN, {
automation.DOMAIN: {
'trigger': {
'platform': 'time',
'hours': "/2",
},
'action': {
'service': 'test.automation'
}
}
})
fire_time_changed(self.hass, dt_util.utcnow().replace(
hour=2, minute=0, second=0))
self.hass.block_till_done()
self.assertEqual(1, len(self.calls))
2015-09-15 15:56:06 +00:00
def test_if_fires_using_after(self):
2016-03-09 09:25:50 +00:00
"""Test for firing after."""
assert _setup_component(self.hass, automation.DOMAIN, {
2015-09-15 15:56:06 +00:00
automation.DOMAIN: {
'trigger': {
'platform': 'time',
'after': '5:00:00',
},
'action': {
'service': 'test.automation',
'data_template': {
'some': '{{ trigger.platform }} - '
'{{ trigger.now.hour }}'
},
2015-09-15 15:56:06 +00:00
}
}
})
2015-09-15 15:56:06 +00:00
fire_time_changed(self.hass, dt_util.utcnow().replace(
hour=5, minute=0, second=0))
self.hass.block_till_done()
2015-09-15 05:05:40 +00:00
self.assertEqual(1, len(self.calls))
self.assertEqual('time - 5', self.calls[0].data['some'])
2015-09-15 05:05:40 +00:00
def test_if_not_working_if_no_values_in_conf_provided(self):
2016-03-09 09:25:50 +00:00
"""Test for failure if no configuration."""
assert not _setup_component(self.hass, automation.DOMAIN, {
automation.DOMAIN: {
'trigger': {
'platform': 'time',
},
'action': {
'service': 'test.automation'
}
}
})
fire_time_changed(self.hass, dt_util.utcnow().replace(
hour=5, minute=0, second=0))
self.hass.block_till_done()
self.assertEqual(0, len(self.calls))
def test_if_not_fires_using_wrong_after(self):
2016-03-09 09:25:50 +00:00
"""YAML translates time values to total seconds.
This should break the before rule.
"""
assert not _setup_component(self.hass, automation.DOMAIN, {
automation.DOMAIN: {
'trigger': {
'platform': 'time',
'after': 3605,
# Total seconds. Hour = 3600 second
},
'action': {
2015-09-19 15:43:56 +00:00
'service': 'test.automation'
}
}
})
fire_time_changed(self.hass, dt_util.utcnow().replace(
hour=1, minute=0, second=5))
self.hass.block_till_done()
self.assertEqual(0, len(self.calls))
2015-09-15 05:05:40 +00:00
def test_if_action_before(self):
2016-03-09 09:25:50 +00:00
"""Test for if action before."""
assert _setup_component(self.hass, automation.DOMAIN, {
2015-09-15 05:05:40 +00:00
automation.DOMAIN: {
'trigger': {
'platform': 'event',
'event_type': 'test_event'
},
'condition': {
'platform': 'time',
'before': '10:00',
},
'action': {
2015-09-19 15:43:56 +00:00
'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',
2015-09-15 05:05:40 +00:00
return_value=before_10):
self.hass.bus.fire('test_event')
self.hass.block_till_done()
2015-09-15 05:05:40 +00:00
self.assertEqual(1, len(self.calls))
with patch('homeassistant.helpers.condition.dt_util.now',
2015-09-15 05:05:40 +00:00
return_value=after_10):
self.hass.bus.fire('test_event')
self.hass.block_till_done()
2015-09-15 05:05:40 +00:00
self.assertEqual(1, len(self.calls))
def test_if_action_after(self):
2016-03-09 09:25:50 +00:00
"""Test for if action after."""
assert _setup_component(self.hass, automation.DOMAIN, {
2015-09-15 05:05:40 +00:00
automation.DOMAIN: {
'trigger': {
'platform': 'event',
'event_type': 'test_event'
},
'condition': {
'platform': 'time',
'after': '10:00',
},
'action': {
2015-09-19 15:43:56 +00:00
'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',
2015-09-15 05:05:40 +00:00
return_value=before_10):
self.hass.bus.fire('test_event')
self.hass.block_till_done()
2015-09-15 05:05:40 +00:00
self.assertEqual(0, len(self.calls))
with patch('homeassistant.helpers.condition.dt_util.now',
2015-09-15 05:05:40 +00:00
return_value=after_10):
self.hass.bus.fire('test_event')
self.hass.block_till_done()
2015-09-15 05:05:40 +00:00
self.assertEqual(1, len(self.calls))
def test_if_action_one_weekday(self):
2016-03-09 09:25:50 +00:00
"""Test for if action with one weekday."""
assert _setup_component(self.hass, automation.DOMAIN, {
2015-09-15 05:05:40 +00:00
automation.DOMAIN: {
'trigger': {
'platform': 'event',
'event_type': 'test_event'
},
'condition': {
'platform': 'time',
'weekday': 'mon',
},
'action': {
2015-09-19 15:43:56 +00:00
'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)
with patch('homeassistant.helpers.condition.dt_util.now',
2015-09-15 05:05:40 +00:00
return_value=monday):
self.hass.bus.fire('test_event')
self.hass.block_till_done()
2015-09-15 05:05:40 +00:00
self.assertEqual(1, len(self.calls))
with patch('homeassistant.helpers.condition.dt_util.now',
2015-09-15 05:05:40 +00:00
return_value=tuesday):
self.hass.bus.fire('test_event')
self.hass.block_till_done()
2015-09-15 05:05:40 +00:00
self.assertEqual(1, len(self.calls))
def test_if_action_list_weekday(self):
2016-03-09 09:25:50 +00:00
"""Test for action with a list of weekdays."""
assert _setup_component(self.hass, automation.DOMAIN, {
2015-09-15 05:05:40 +00:00
automation.DOMAIN: {
'trigger': {
'platform': 'event',
'event_type': 'test_event'
},
'condition': {
'platform': 'time',
'weekday': ['mon', 'tue'],
},
'action': {
2015-09-19 15:43:56 +00:00
'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)
with patch('homeassistant.helpers.condition.dt_util.now',
2015-09-15 05:05:40 +00:00
return_value=monday):
self.hass.bus.fire('test_event')
self.hass.block_till_done()
2015-09-15 05:05:40 +00:00
self.assertEqual(1, len(self.calls))
with patch('homeassistant.helpers.condition.dt_util.now',
2015-09-15 05:05:40 +00:00
return_value=tuesday):
self.hass.bus.fire('test_event')
self.hass.block_till_done()
2015-09-15 05:05:40 +00:00
self.assertEqual(2, len(self.calls))
with patch('homeassistant.helpers.condition.dt_util.now',
2015-09-15 05:05:40 +00:00
return_value=wednesday):
self.hass.bus.fire('test_event')
self.hass.block_till_done()
2015-09-15 05:05:40 +00:00
self.assertEqual(2, len(self.calls))