From 7951137693b9e6413ca936063b5b9edd2c0a32de Mon Sep 17 00:00:00 2001 From: andythigpen Date: Mon, 23 Feb 2015 19:23:25 -0600 Subject: [PATCH] Adds event automation module. When events are fired with matching data, the automation.event module executes the corresponding action for automation rules. --- homeassistant/components/automation/event.py | 33 ++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 homeassistant/components/automation/event.py diff --git a/homeassistant/components/automation/event.py b/homeassistant/components/automation/event.py new file mode 100644 index 00000000000..94e1bcc805f --- /dev/null +++ b/homeassistant/components/automation/event.py @@ -0,0 +1,33 @@ +""" +homeassistant.components.automation.event +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Offers event listening automation rules. +""" +import logging +import json +from homeassistant.util import convert + +CONF_EVENT_TYPE = "event_type" +CONF_EVENT_DATA = "event_data" + +_LOGGER = logging.getLogger(__name__) + + +def register(hass, config, action): + """ Listen for events based on config. """ + event_type = config.get(CONF_EVENT_TYPE) + + if event_type is None: + _LOGGER.error("Missing configuration key %s", CONF_EVENT_TYPE) + return False + + event_data = convert(config.get(CONF_EVENT_DATA), json.loads, {}) + + def handle_event(event): + """ Listens for events and calls the action when data matches. """ + if event_data == event.data: + action() + + hass.bus.listen(event_type, handle_event) + return True