2017-04-06 06:23:02 +00:00
|
|
|
"""The tests for the Event automation."""
|
|
|
|
from unittest.mock import patch, Mock
|
|
|
|
|
|
|
|
from homeassistant.core import CoreState
|
|
|
|
from homeassistant.setup import async_setup_component
|
|
|
|
import homeassistant.components.automation as automation
|
|
|
|
|
2017-06-25 17:53:15 +00:00
|
|
|
from tests.common import async_mock_service, mock_coro
|
2017-04-06 06:23:02 +00:00
|
|
|
|
|
|
|
|
2019-06-09 00:10:23 +00:00
|
|
|
async def test_if_fires_on_hass_start(hass):
|
2017-04-06 06:23:02 +00:00
|
|
|
"""Test the firing when HASS starts."""
|
2017-06-25 17:53:15 +00:00
|
|
|
calls = async_mock_service(hass, 'test', 'automation')
|
2017-04-06 06:23:02 +00:00
|
|
|
hass.state = CoreState.not_running
|
|
|
|
config = {
|
|
|
|
automation.DOMAIN: {
|
|
|
|
'alias': 'hello',
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'homeassistant',
|
|
|
|
'event': 'start',
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-09 00:10:23 +00:00
|
|
|
assert await async_setup_component(hass, automation.DOMAIN, config)
|
2019-06-08 06:08:22 +00:00
|
|
|
assert automation.is_on(hass, 'automation.hello')
|
2017-04-06 06:23:02 +00:00
|
|
|
assert len(calls) == 0
|
|
|
|
|
2019-06-09 00:10:23 +00:00
|
|
|
await hass.async_start()
|
2017-04-06 06:23:02 +00:00
|
|
|
assert automation.is_on(hass, 'automation.hello')
|
|
|
|
assert len(calls) == 1
|
|
|
|
|
|
|
|
with patch('homeassistant.config.async_hass_config_yaml',
|
|
|
|
Mock(return_value=mock_coro(config))):
|
2019-06-09 00:10:23 +00:00
|
|
|
await hass.services.async_call(
|
2017-04-06 06:23:02 +00:00
|
|
|
automation.DOMAIN, automation.SERVICE_RELOAD, blocking=True)
|
|
|
|
|
|
|
|
assert automation.is_on(hass, 'automation.hello')
|
|
|
|
assert len(calls) == 1
|
|
|
|
|
|
|
|
|
2019-06-09 00:10:23 +00:00
|
|
|
async def test_if_fires_on_hass_shutdown(hass):
|
2017-04-06 06:23:02 +00:00
|
|
|
"""Test the firing when HASS starts."""
|
2017-06-25 17:53:15 +00:00
|
|
|
calls = async_mock_service(hass, 'test', 'automation')
|
2017-04-06 06:23:02 +00:00
|
|
|
hass.state = CoreState.not_running
|
|
|
|
|
2019-06-09 00:10:23 +00:00
|
|
|
assert await async_setup_component(hass, automation.DOMAIN, {
|
2017-04-06 06:23:02 +00:00
|
|
|
automation.DOMAIN: {
|
|
|
|
'alias': 'hello',
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'homeassistant',
|
|
|
|
'event': 'shutdown',
|
|
|
|
},
|
|
|
|
'action': {
|
|
|
|
'service': 'test.automation',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2019-06-08 06:08:22 +00:00
|
|
|
assert automation.is_on(hass, 'automation.hello')
|
2017-04-06 06:23:02 +00:00
|
|
|
assert len(calls) == 0
|
|
|
|
|
2019-06-09 00:10:23 +00:00
|
|
|
await hass.async_start()
|
2017-04-06 06:23:02 +00:00
|
|
|
assert automation.is_on(hass, 'automation.hello')
|
|
|
|
assert len(calls) == 0
|
|
|
|
|
|
|
|
with patch.object(hass.loop, 'stop'):
|
2019-06-09 00:10:23 +00:00
|
|
|
await hass.async_stop()
|
2017-04-06 06:23:02 +00:00
|
|
|
assert len(calls) == 1
|