diff --git a/homeassistant/components/homeassistant/triggers/event.py b/homeassistant/components/homeassistant/triggers/event.py index 4498702bac4..afd2b40e842 100644 --- a/homeassistant/components/homeassistant/triggers/event.py +++ b/homeassistant/components/homeassistant/triggers/event.py @@ -25,14 +25,11 @@ TRIGGER_SCHEMA = vol.Schema( ) -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( @@ -40,8 +37,26 @@ async def async_attach_trigger( ): """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): diff --git a/tests/components/homeassistant/triggers/test_event.py b/tests/components/homeassistant/triggers/test_event.py index aa0acae254c..babb2bf4d87 100644 --- a/tests/components/homeassistant/triggers/test_event.py +++ b/tests/components/homeassistant/triggers/test_event.py @@ -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