core/homeassistant/components/automation/state.py

133 lines
4.3 KiB
Python
Raw Normal View History

2015-01-16 07:32:27 +00:00
"""
Offers state listening automation rules.
2015-10-13 19:08:18 +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/#state-trigger
2015-01-16 07:32:27 +00:00
"""
import logging
2016-02-19 14:49:11 +00:00
from datetime import timedelta
2015-01-16 07:32:27 +00:00
2016-02-19 14:49:11 +00:00
import homeassistant.util.dt as dt_util
from homeassistant.const import (
EVENT_STATE_CHANGED, EVENT_TIME_CHANGED, MATCH_ALL)
from homeassistant.components.automation.time import (
CONF_HOURS, CONF_MINUTES, CONF_SECONDS)
from homeassistant.helpers.event import track_state_change, track_point_in_time
2015-01-16 07:32:27 +00:00
2015-09-15 05:05:40 +00:00
CONF_ENTITY_ID = "entity_id"
CONF_FROM = "from"
CONF_TO = "to"
2015-09-14 05:25:42 +00:00
CONF_STATE = "state"
2016-02-19 14:49:11 +00:00
CONF_FOR = "for"
2015-01-16 07:32:27 +00:00
def get_time_config(config):
2016-03-07 16:14:55 +00:00
"""Helper function to extract the time specified in the configuration."""
2016-02-22 08:40:27 +00:00
if CONF_FOR not in config:
return None
2016-02-22 08:40:27 +00:00
hours = config[CONF_FOR].get(CONF_HOURS)
minutes = config[CONF_FOR].get(CONF_MINUTES)
seconds = config[CONF_FOR].get(CONF_SECONDS)
2016-02-22 08:40:27 +00:00
if hours is None and minutes is None and seconds is None:
logging.getLogger(__name__).error(
"Received invalid value for '%s': %s",
config[CONF_FOR], CONF_FOR)
return None
2016-02-22 08:40:27 +00:00
if config.get(CONF_TO) is None and config.get(CONF_STATE) is None:
logging.getLogger(__name__).error(
"For: requires a to: value'%s': %s",
config[CONF_FOR], CONF_FOR)
return None
return timedelta(hours=(hours or 0.0),
minutes=(minutes or 0.0),
seconds=(seconds or 0.0))
2015-09-14 05:25:42 +00:00
def trigger(hass, config, action):
2016-03-07 16:14:55 +00:00
"""Listen for state changes based on configuration."""
2015-01-16 07:32:27 +00:00
entity_id = config.get(CONF_ENTITY_ID)
if entity_id is None:
logging.getLogger(__name__).error(
2015-09-14 05:25:42 +00:00
"Missing trigger configuration key %s", CONF_ENTITY_ID)
2016-02-22 08:40:27 +00:00
return None
2015-01-16 07:32:27 +00:00
from_state = config.get(CONF_FROM, MATCH_ALL)
2015-09-15 15:56:06 +00:00
to_state = config.get(CONF_TO) or config.get(CONF_STATE) or MATCH_ALL
2016-02-22 08:40:27 +00:00
time_delta = get_time_config(config)
2015-01-16 07:32:27 +00:00
if isinstance(from_state, bool) or isinstance(to_state, bool):
logging.getLogger(__name__).error(
'Config error. Surround to/from values with quotes.')
2016-02-22 08:40:27 +00:00
return None
2016-02-22 08:40:27 +00:00
if CONF_FOR in config and time_delta is None:
return None
2016-02-19 14:49:11 +00:00
2015-01-16 07:32:27 +00:00
def state_automation_listener(entity, from_s, to_s):
2016-03-07 16:14:55 +00:00
"""Listens for state changes and calls action."""
2016-02-19 14:49:11 +00:00
def state_for_listener(now):
2016-03-07 16:14:55 +00:00
"""Fires on state changes after a delay and calls action."""
2016-02-19 14:49:11 +00:00
hass.bus.remove_listener(
EVENT_STATE_CHANGED, for_state_listener)
action()
def state_for_cancel_listener(entity, inner_from_s, inner_to_s):
2016-03-07 16:14:55 +00:00
"""
Fires on state changes and cancels for listener if state changed.
"""
2016-02-19 14:49:11 +00:00
if inner_to_s == to_s:
return
hass.bus.remove_listener(EVENT_TIME_CHANGED, for_time_listener)
hass.bus.remove_listener(
EVENT_STATE_CHANGED, for_state_listener)
2016-02-22 08:40:27 +00:00
if time_delta is not None:
target_tm = dt_util.utcnow() + time_delta
2016-02-19 14:49:11 +00:00
for_time_listener = track_point_in_time(
hass, state_for_listener, target_tm)
for_state_listener = track_state_change(
hass, entity_id, state_for_cancel_listener,
MATCH_ALL, MATCH_ALL)
else:
action()
2015-01-16 07:32:27 +00:00
2015-08-04 16:13:35 +00:00
track_state_change(
hass, entity_id, state_automation_listener, from_state, to_state)
2015-01-16 07:32:27 +00:00
return True
2015-09-14 05:25:42 +00:00
def if_action(hass, config):
2016-03-07 16:14:55 +00:00
"""Wraps action method with state based condition."""
2015-09-15 05:05:40 +00:00
entity_id = config.get(CONF_ENTITY_ID)
2015-09-14 05:25:42 +00:00
state = config.get(CONF_STATE)
if entity_id is None or state is None:
logging.getLogger(__name__).error(
2015-09-15 05:12:51 +00:00
"Missing if-condition configuration key %s or %s", CONF_ENTITY_ID,
CONF_STATE)
return None
2015-09-14 05:25:42 +00:00
2016-02-22 08:40:27 +00:00
time_delta = get_time_config(config)
if CONF_FOR in config and time_delta is None:
return None
state = str(state)
2015-09-14 05:25:42 +00:00
def if_state():
2016-03-07 16:14:55 +00:00
"""Test if condition."""
is_state = hass.states.is_state(entity_id, state)
2016-02-22 08:40:27 +00:00
return (time_delta is None and is_state or
time_delta is not None and
dt_util.utcnow() - time_delta >
hass.states.get(entity_id).last_changed)
return if_state