-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When events are fired with matching data, the automation.event module executes the corresponding action for automation rules.
- Loading branch information
1 parent
b5a3a72
commit 7951137
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |