2017-04-06 06:23:02 +00:00
|
|
|
"""The tests for the Event automation."""
|
2023-02-20 15:57:12 +00:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
import pytest
|
2021-01-01 21:31:56 +00:00
|
|
|
|
2019-12-08 16:29:39 +00:00
|
|
|
import homeassistant.components.automation as automation
|
2023-02-20 15:57:12 +00:00
|
|
|
from homeassistant.core import CoreState, HomeAssistant
|
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2017-04-06 06:23:02 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
|
|
|
|
2020-04-30 20:29:50 +00:00
|
|
|
from tests.common import async_mock_service
|
2017-04-06 06:23:02 +00:00
|
|
|
|
|
|
|
|
2023-02-20 15:57:12 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"hass_config",
|
|
|
|
[
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"alias": "hello",
|
|
|
|
"trigger": {"platform": "homeassistant", "event": "start"},
|
|
|
|
"action": {
|
|
|
|
"service": "test.automation",
|
|
|
|
"data_template": {"id": "{{ trigger.id}}"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_if_fires_on_hass_start(
|
2023-02-21 07:48:44 +00:00
|
|
|
hass: HomeAssistant, mock_hass_config: None, hass_config: ConfigType
|
2023-02-20 15:57:12 +00:00
|
|
|
) -> None:
|
2020-01-05 12:09:17 +00:00
|
|
|
"""Test the firing when Home Assistant starts."""
|
2019-07-31 19:25:30 +00:00
|
|
|
calls = async_mock_service(hass, "test", "automation")
|
2017-04-06 06:23:02 +00:00
|
|
|
hass.state = CoreState.not_running
|
|
|
|
|
2023-02-20 15:57:12 +00:00
|
|
|
assert await async_setup_component(hass, automation.DOMAIN, hass_config)
|
2019-07-31 19:25:30 +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()
|
2020-04-24 21:13:39 +00:00
|
|
|
await hass.async_block_till_done()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert automation.is_on(hass, "automation.hello")
|
2017-04-06 06:23:02 +00:00
|
|
|
assert len(calls) == 1
|
|
|
|
|
2023-02-20 15:57:12 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
automation.DOMAIN, automation.SERVICE_RELOAD, blocking=True
|
|
|
|
)
|
2017-04-06 06:23:02 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert automation.is_on(hass, "automation.hello")
|
2017-04-06 06:23:02 +00:00
|
|
|
assert len(calls) == 1
|
2021-03-31 12:56:04 +00:00
|
|
|
assert calls[0].data["id"] == 0
|
2017-04-06 06:23:02 +00:00
|
|
|
|
|
|
|
|
2023-02-21 08:25:05 +00:00
|
|
|
async def test_if_fires_on_hass_shutdown(hass: HomeAssistant) -> None:
|
2020-01-05 12:09:17 +00:00
|
|
|
"""Test the firing when Home Assistant shuts down."""
|
2019-07-31 19:25:30 +00:00
|
|
|
calls = async_mock_service(hass, "test", "automation")
|
2017-04-06 06:23:02 +00:00
|
|
|
hass.state = CoreState.not_running
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"alias": "hello",
|
|
|
|
"trigger": {"platform": "homeassistant", "event": "shutdown"},
|
2021-03-31 12:56:04 +00:00
|
|
|
"action": {
|
|
|
|
"service": "test.automation",
|
|
|
|
"data_template": {"id": "{{ trigger.id}}"},
|
|
|
|
},
|
2017-04-06 06:23:02 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +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()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert automation.is_on(hass, "automation.hello")
|
2020-04-24 21:13:39 +00:00
|
|
|
await hass.async_block_till_done()
|
2017-04-06 06:23:02 +00:00
|
|
|
assert len(calls) == 0
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
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
|
2021-03-31 12:56:04 +00:00
|
|
|
assert calls[0].data["id"] == 0
|