Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions homeassistant/components/homeassistant/triggers/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,38 @@
)


def _populate_schema(config, config_parameter):
if config_parameter not in config:
return None
def _schema_value(value):
if isinstance(value, list):
return vol.In(value)

return vol.Schema(
{vol.Required(key): value for key, value in config[config_parameter].items()},
extra=vol.ALLOW_EXTRA,
)
return value


async def async_attach_trigger(
hass, config, action, automation_info, *, platform_type="event"
):
"""Listen for events based on configuration."""
event_type = config.get(CONF_EVENT_TYPE)
event_data_schema = _populate_schema(config, CONF_EVENT_DATA)
event_context_schema = _populate_schema(config, CONF_EVENT_CONTEXT)

event_data_schema = None
if config.get(CONF_EVENT_DATA):
event_data_schema = vol.Schema(
{
vol.Required(key): value
for key, value in config.get(CONF_EVENT_DATA).items()
},
extra=vol.ALLOW_EXTRA,
)

event_context_schema = None
if config.get(CONF_EVENT_CONTEXT):
event_context_schema = vol.Schema(
{
vol.Required(key): _schema_value(value)
for key, value in config.get(CONF_EVENT_CONTEXT).items()
},
extra=vol.ALLOW_EXTRA,
)

@callback
def handle_event(event):
Expand Down
56 changes: 56 additions & 0 deletions tests/components/homeassistant/triggers/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,59 @@ async def test_if_not_fires_if_event_context_not_matches(
hass.bus.async_fire("test_event", {}, context=context_with_user)
await hass.async_block_till_done()
assert len(calls) == 0


async def test_if_fires_on_multiple_user_ids(hass, calls, context_with_user):
"""Test the firing of event when the trigger has multiple user ids."""
assert await async_setup_component(
hass,
automation.DOMAIN,
{
automation.DOMAIN: {
"trigger": {
"platform": "event",
"event_type": "test_event",
"event_data": {},
"context": {"user_id": [context_with_user.user_id, "another id"]},
},
"action": {"service": "test.automation"},
}
},
)

hass.bus.async_fire("test_event", {}, context=context_with_user)
await hass.async_block_till_done()
assert len(calls) == 1


async def test_event_data_with_list(hass, calls):
"""Test the (non)firing of event when the data schema has lists."""
assert await async_setup_component(
hass,
automation.DOMAIN,
{
automation.DOMAIN: {
"trigger": {
"platform": "event",
"event_type": "test_event",
"event_data": {"some_attr": [1, 2]},
"context": {},
},
"action": {"service": "test.automation"},
}
},
)

hass.bus.async_fire("test_event", {"some_attr": [1, 2]})
await hass.async_block_till_done()
assert len(calls) == 1

# don't match a single value
hass.bus.async_fire("test_event", {"some_attr": 1})
await hass.async_block_till_done()
assert len(calls) == 1

# don't match a containing list
hass.bus.async_fire("test_event", {"some_attr": [1, 2, 3]})
await hass.async_block_till_done()
assert len(calls) == 1