Fix event trigger (#39884)

Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
Paulus Schoutsen 2020-09-10 11:25:56 +02:00
parent f0295d562d
commit 36f52a26f6
2 changed files with 21 additions and 7 deletions

View File

@ -28,11 +28,15 @@ 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 = ( event_data_schema = None
vol.Schema(config.get(CONF_EVENT_DATA), extra=vol.ALLOW_EXTRA) if config.get(CONF_EVENT_DATA):
if config.get(CONF_EVENT_DATA) event_data_schema = vol.Schema(
else None {
) vol.Required(key): value
for key, value in config.get(CONF_EVENT_DATA).items()
},
extra=vol.ALLOW_EXTRA,
)
@callback @callback
def handle_event(event): def handle_event(event):

View File

@ -84,17 +84,27 @@ async def test_if_fires_on_event_with_data(hass, calls):
"trigger": { "trigger": {
"platform": "event", "platform": "event",
"event_type": "test_event", "event_type": "test_event",
"event_data": {"some_attr": "some_value"}, "event_data": {
"some_attr": "some_value",
"second_attr": "second_value",
},
}, },
"action": {"service": "test.automation"}, "action": {"service": "test.automation"},
} }
}, },
) )
hass.bus.async_fire("test_event", {"some_attr": "some_value", "another": "value"}) hass.bus.async_fire(
"test_event",
{"some_attr": "some_value", "another": "value", "second_attr": "second_value"},
)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(calls) == 1
hass.bus.async_fire("test_event", {"some_attr": "some_value", "another": "value"})
await hass.async_block_till_done()
assert len(calls) == 1 # No new call
async def test_if_fires_on_event_with_empty_data_config(hass, calls): async def test_if_fires_on_event_with_empty_data_config(hass, calls):
"""Test the firing of events with empty data config. """Test the firing of events with empty data config.