diff --git a/tests/components/automation/test_init.py b/tests/components/automation/test_init.py index 2459542b629..e06984e9f7d 100644 --- a/tests/components/automation/test_init.py +++ b/tests/components/automation/test_init.py @@ -1,5 +1,6 @@ """The tests for the automation component.""" import unittest +from datetime import timedelta from unittest.mock import patch from homeassistant.core import callback @@ -9,7 +10,8 @@ from homeassistant.const import ATTR_ENTITY_ID from homeassistant.exceptions import HomeAssistantError import homeassistant.util.dt as dt_util -from tests.common import get_test_home_assistant, assert_setup_component +from tests.common import get_test_home_assistant, assert_setup_component, \ + fire_time_changed class TestAutomation(unittest.TestCase): @@ -85,6 +87,59 @@ class TestAutomation(unittest.TestCase): assert state is not None assert state.attributes.get('entity_id') == ('automation.hello',) + def test_action_delay(self): + """Test action delay.""" + assert setup_component(self.hass, automation.DOMAIN, { + automation.DOMAIN: { + 'alias': 'hello', + 'trigger': { + 'platform': 'event', + 'event_type': 'test_event', + }, + 'action': [ + { + 'service': 'test.automation', + 'data_template': { + 'some': '{{ trigger.platform }} - ' + '{{ trigger.event.event_type }}' + } + }, + {'delay': {'minutes': '10'}}, + { + 'service': 'test.automation', + 'data_template': { + 'some': '{{ trigger.platform }} - ' + '{{ trigger.event.event_type }}' + } + }, + ] + } + }) + + time = dt_util.utcnow() + + with patch('homeassistant.components.automation.utcnow', + return_value=time): + self.hass.bus.fire('test_event') + self.hass.block_till_done() + + assert len(self.calls) == 1 + assert self.calls[0].data['some'] == 'event - test_event' + + future = dt_util.utcnow() + timedelta(minutes=10) + fire_time_changed(self.hass, future) + self.hass.block_till_done() + + assert len(self.calls) == 2 + assert self.calls[1].data['some'] == 'event - test_event' + + state = self.hass.states.get('automation.hello') + assert state is not None + assert state.attributes.get('last_triggered') == time + state = self.hass.states.get('group.all_automations') + assert state is not None + assert state.attributes.get('entity_id') == ('automation.hello',) + def test_service_specify_entity_id(self): """Test service data.""" assert setup_component(self.hass, automation.DOMAIN, {