2015-02-24 01:23:25 +00:00
|
|
|
"""
|
2016-03-07 19:20:07 +00:00
|
|
|
Offer event listening automation rules.
|
2015-10-13 19:07:24 +00:00
|
|
|
|
|
|
|
For more details about this automation rule, please refer to the documentation
|
2015-11-09 12:12:18 +00:00
|
|
|
at https://home-assistant.io/components/automation/#event-trigger
|
2015-02-24 01:23:25 +00:00
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
|
|
|
CONF_EVENT_TYPE = "event_type"
|
|
|
|
CONF_EVENT_DATA = "event_data"
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2015-09-14 05:25:42 +00:00
|
|
|
def trigger(hass, config, action):
|
2016-03-07 16:14:55 +00:00
|
|
|
"""Listen for events based on configuration."""
|
2015-02-24 01:23:25 +00:00
|
|
|
event_type = config.get(CONF_EVENT_TYPE)
|
|
|
|
|
|
|
|
if event_type is None:
|
|
|
|
_LOGGER.error("Missing configuration key %s", CONF_EVENT_TYPE)
|
|
|
|
return False
|
|
|
|
|
2015-09-19 15:27:34 +00:00
|
|
|
event_data = config.get(CONF_EVENT_DATA)
|
2015-02-24 01:23:25 +00:00
|
|
|
|
|
|
|
def handle_event(event):
|
2016-03-07 19:20:07 +00:00
|
|
|
"""Listen for events and calls the action when data matches."""
|
2015-09-19 15:27:34 +00:00
|
|
|
if not event_data or all(val == event.data.get(key) for key, val
|
|
|
|
in event_data.items()):
|
2016-04-21 20:59:42 +00:00
|
|
|
action({
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'event',
|
|
|
|
'event': event,
|
|
|
|
},
|
|
|
|
})
|
2015-02-24 01:23:25 +00:00
|
|
|
|
|
|
|
hass.bus.listen(event_type, handle_event)
|
|
|
|
return True
|