2016-03-09 10:25:50 +01:00
|
|
|
"""The tests for the Event automation."""
|
2018-10-26 11:31:14 +02:00
|
|
|
import pytest
|
2015-08-10 22:21:34 -07:00
|
|
|
|
2019-12-08 17:29:39 +01:00
|
|
|
import homeassistant.components.automation as automation
|
2018-10-26 11:31:14 +02:00
|
|
|
from homeassistant.core import Context
|
|
|
|
from homeassistant.setup import async_setup_component
|
2015-08-10 22:21:34 -07:00
|
|
|
|
2019-12-08 17:29:39 +01:00
|
|
|
from tests.common import async_mock_service, mock_component
|
2018-09-26 09:49:55 +02:00
|
|
|
from tests.components.automation import common
|
2016-02-14 15:08:23 -08:00
|
|
|
|
2015-08-10 22:21:34 -07:00
|
|
|
|
2018-10-26 11:31:14 +02:00
|
|
|
@pytest.fixture
|
|
|
|
def calls(hass):
|
|
|
|
"""Track calls to a mock serivce."""
|
2019-07-31 12:25:30 -07:00
|
|
|
return async_mock_service(hass, "test", "automation")
|
2015-08-10 22:21:34 -07:00
|
|
|
|
|
|
|
|
2018-10-26 11:31:14 +02:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def setup_comp(hass):
|
|
|
|
"""Initialize components."""
|
2019-07-31 12:25:30 -07:00
|
|
|
mock_component(hass, "group")
|
2015-08-10 22:21:34 -07:00
|
|
|
|
|
|
|
|
2018-10-26 11:31:14 +02:00
|
|
|
async def test_if_fires_on_event(hass, calls):
|
|
|
|
"""Test the firing of events."""
|
|
|
|
context = Context()
|
2015-08-10 22:21:34 -07:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {"platform": "event", "event_type": "test_event"},
|
|
|
|
"action": {"service": "test.automation"},
|
2015-08-10 22:21:34 -07:00
|
|
|
}
|
2019-07-31 12:25:30 -07:00
|
|
|
},
|
|
|
|
)
|
2018-10-26 11:31:14 +02:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
hass.bus.async_fire("test_event", context=context)
|
2018-10-26 11:31:14 +02:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert 1 == len(calls)
|
2019-03-01 10:08:38 -08:00
|
|
|
assert calls[0].context.parent_id == context.id
|
2018-10-26 11:31:14 +02:00
|
|
|
|
|
|
|
await common.async_turn_off(hass)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
hass.bus.async_fire("test_event")
|
2018-10-26 11:31:14 +02:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert 1 == len(calls)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_if_fires_on_event_extra_data(hass, calls):
|
|
|
|
"""Test the firing of events still matches with event data."""
|
2019-07-31 12:25:30 -07:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {"platform": "event", "event_type": "test_event"},
|
|
|
|
"action": {"service": "test.automation"},
|
2017-10-23 19:41:58 -04:00
|
|
|
}
|
2019-07-31 12:25:30 -07:00
|
|
|
},
|
|
|
|
)
|
2018-10-26 11:31:14 +02:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
hass.bus.async_fire("test_event", {"extra_key": "extra_data"})
|
2018-10-26 11:31:14 +02:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert 1 == len(calls)
|
|
|
|
|
|
|
|
await common.async_turn_off(hass)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
hass.bus.async_fire("test_event")
|
2018-10-26 11:31:14 +02:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert 1 == len(calls)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_if_fires_on_event_with_data(hass, calls):
|
|
|
|
"""Test the firing of events with data."""
|
2019-07-31 12:25:30 -07:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "event",
|
|
|
|
"event_type": "test_event",
|
|
|
|
"event_data": {"some_attr": "some_value"},
|
|
|
|
},
|
|
|
|
"action": {"service": "test.automation"},
|
2015-08-10 22:21:34 -07:00
|
|
|
}
|
2019-07-31 12:25:30 -07:00
|
|
|
},
|
|
|
|
)
|
2018-10-26 11:31:14 +02:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
hass.bus.async_fire("test_event", {"some_attr": "some_value", "another": "value"})
|
2018-10-26 11:31:14 +02:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert 1 == len(calls)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_if_fires_on_event_with_empty_data_config(hass, calls):
|
|
|
|
"""Test the firing of events with empty data config.
|
|
|
|
|
|
|
|
The frontend automation editor can produce configurations with an
|
|
|
|
empty dict for event_data instead of no key.
|
|
|
|
"""
|
2019-07-31 12:25:30 -07:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "event",
|
|
|
|
"event_type": "test_event",
|
|
|
|
"event_data": {},
|
|
|
|
},
|
|
|
|
"action": {"service": "test.automation"},
|
2017-10-23 19:41:58 -04:00
|
|
|
}
|
2019-07-31 12:25:30 -07:00
|
|
|
},
|
|
|
|
)
|
2018-10-26 11:31:14 +02:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
hass.bus.async_fire("test_event", {"some_attr": "some_value", "another": "value"})
|
2018-10-26 11:31:14 +02:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert 1 == len(calls)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_if_fires_on_event_with_nested_data(hass, calls):
|
|
|
|
"""Test the firing of events with nested data."""
|
2019-07-31 12:25:30 -07:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "event",
|
|
|
|
"event_type": "test_event",
|
|
|
|
"event_data": {"parent_attr": {"some_attr": "some_value"}},
|
|
|
|
},
|
|
|
|
"action": {"service": "test.automation"},
|
2017-10-07 16:13:32 -04:00
|
|
|
}
|
2019-07-31 12:25:30 -07:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
hass.bus.async_fire(
|
|
|
|
"test_event", {"parent_attr": {"some_attr": "some_value", "another": "value"}}
|
|
|
|
)
|
2018-10-26 11:31:14 +02:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert 1 == len(calls)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_if_not_fires_if_event_data_not_matches(hass, calls):
|
|
|
|
"""Test firing of event if no match."""
|
2019-07-31 12:25:30 -07:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: {
|
|
|
|
"trigger": {
|
|
|
|
"platform": "event",
|
|
|
|
"event_type": "test_event",
|
|
|
|
"event_data": {"some_attr": "some_value"},
|
|
|
|
},
|
|
|
|
"action": {"service": "test.automation"},
|
2015-08-10 22:21:34 -07:00
|
|
|
}
|
2019-07-31 12:25:30 -07:00
|
|
|
},
|
|
|
|
)
|
2015-08-10 22:21:34 -07:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
hass.bus.async_fire("test_event", {"some_attr": "some_other_value"})
|
2018-10-26 11:31:14 +02:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert 0 == len(calls)
|