Support matching multiple user ids in event trigger (#41036)

This commit is contained in:
On Freund 2020-10-08 10:10:42 +03:00 committed by GitHub
parent 392d5c673a
commit b5e57ed4fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 80 additions and 9 deletions

View file

@ -25,14 +25,11 @@ TRIGGER_SCHEMA = vol.Schema(
) )
def _populate_schema(config, config_parameter): def _schema_value(value):
if config_parameter not in config: if isinstance(value, list):
return None return vol.In(value)
return vol.Schema( return value
{vol.Required(key): value for key, value in config[config_parameter].items()},
extra=vol.ALLOW_EXTRA,
)
async def async_attach_trigger( async def async_attach_trigger(
@ -40,8 +37,26 @@ async def async_attach_trigger(
): ):
"""Listen for events based on configuration.""" """Listen for events based on configuration."""
event_type = config.get(CONF_EVENT_TYPE) 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 @callback
def handle_event(event): def handle_event(event):

View file

@ -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) hass.bus.async_fire("test_event", {}, context=context_with_user)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 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