8000 Event trigger nested conditions by emlove · Pull Request #9732 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Event trigger nested conditions #9732

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 7, 2017
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
8000
26 changes: 16 additions & 10 deletions homeassistant/components/automation/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,32 @@
TRIGGER_SCHEMA = vol.Schema({
vol.Required(CONF_PLATFORM): 'event',
vol.Required(CONF_EVENT_TYPE): cv.string,
vol.Optional(CONF_EVENT_DATA): dict,
vol.Optional(CONF_EVENT_DATA, default={}): dict,
})


@asyncio.coroutine
def async_trigger(hass, config, action):
"""Listen for events based on configuration."""
event_type = config.get(CONF_EVENT_TYPE)
event_data = config.get(CONF_EVENT_DATA)
event_data_schema = vol.Schema(
config.get(CONF_EVENT_DATA),
extra=vol.ALLOW_EXTRA)

@callback
def handle_event(event):
"""Listen for events and calls the action when data matches."""
if not event_data or all(val == event.data.get(key) for key, val
in event_data.items()):
hass.async_run_job(action, {
'trigger': {
'platform': 'event',
'event': event,
},
})
try:
event_data_schema(event.data)
except vol.Invalid:
# If event data doesn't match requested schema, skip event
return

hass.async_run_job(action, {
'trigger': {
'platform': 'event',
'event': event,
},
})

return hass.bus.async_listen(event_type, handle_event)
28 changes: 28 additions & 0 deletions tests/components/automation/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,34 @@ def test_if_fires_on_event_with_data(self):
self.hass.block_till_done()
self.assertEqual(1, len(self.calls))

def test_if_fires_on_event_with_nested_data(self):
"""Test the firing of events with nested data."""
assert setup_component(self.hass, automation.DOMAIN, {
automation.DOMAIN: {
'trigger': {
'platform': 'event',
'event_type': 'test_event',
'event_data': {
'parent_attr': {
'some_attr': 'some_value'
}
}
},
'action': {
'service': 'test.automation',
}
}
})

self.hass.bus.fire('test_event', {
'parent_attr': {
'some_attr': 'some_value',
'another': 'value'
}
})
self.hass.block_till_done()
self.assertEqual(1, len(self.calls))

def test_if_not_fires_if_event_data_not_matches(self):
"""Test firing of event if no match."""
assert setup_component(self.hass, automation.DOMAIN, {
Expand Down
0