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
|
2017-04-06 06:23:02 +00:00
|
|
|
at https://home-assistant.io/docs/automation/trigger/#event-trigger
|
2015-02-24 01:23:25 +00:00
|
|
|
"""
|
2017-02-18 22:17:18 +00:00
|
|
|
import asyncio
|
2015-02-24 01:23:25 +00:00
|
|
|
import logging
|
|
|
|
|
2016-04-28 10:03:57 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2017-04-06 06:23:02 +00:00
|
|
|
from homeassistant.core import callback, CoreState
|
|
|
|
from homeassistant.const import CONF_PLATFORM, EVENT_HOMEASSISTANT_START
|
2016-04-28 10:03:57 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv
|
|
|
|
|
2017-04-30 05:04:49 +00:00
|
|
|
CONF_EVENT_TYPE = 'event_type'
|
|
|
|
CONF_EVENT_DATA = 'event_data'
|
2015-02-24 01:23:25 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2016-04-28 10:03:57 +00:00
|
|
|
TRIGGER_SCHEMA = vol.Schema({
|
|
|
|
vol.Required(CONF_PLATFORM): 'event',
|
|
|
|
vol.Required(CONF_EVENT_TYPE): cv.string,
|
|
|
|
vol.Optional(CONF_EVENT_DATA): dict,
|
|
|
|
})
|
|
|
|
|
2015-02-24 01:23:25 +00:00
|
|
|
|
2017-02-18 22:17:18 +00:00
|
|
|
@asyncio.coroutine
|
2016-10-01 08:22:13 +00:00
|
|
|
def async_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)
|
2015-09-19 15:27:34 +00:00
|
|
|
event_data = config.get(CONF_EVENT_DATA)
|
2015-02-24 01:23:25 +00:00
|
|
|
|
2017-04-06 06:23:02 +00:00
|
|
|
if (event_type == EVENT_HOMEASSISTANT_START and
|
|
|
|
hass.state == CoreState.starting):
|
|
|
|
_LOGGER.warning('Deprecation: Automations should not listen to event '
|
|
|
|
"'homeassistant_start'. Use platform 'homeassistant' "
|
|
|
|
'instead. Feature will be removed in 0.45')
|
|
|
|
hass.async_run_job(action, {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'event',
|
|
|
|
'event': None,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return lambda: None
|
|
|
|
|
2016-10-05 03:44:32 +00:00
|
|
|
@callback
|
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-10-05 03:44:32 +00:00
|
|
|
hass.async_run_job(action, {
|
2016-04-21 20:59:42 +00:00
|
|
|
'trigger': {
|
|
|
|
'platform': 'event',
|
|
|
|
'event': event,
|
|
|
|
},
|
|
|
|
})
|
2015-02-24 01:23:25 +00:00
|
|
|
|
2016-10-01 08:22:13 +00:00
|
|
|
return hass.bus.async_listen(event_type, handle_event)
|