core/homeassistant/components/automation/state.py

121 lines
4.0 KiB
Python
Raw Normal View History

2015-01-16 07:32:27 +00:00
"""
2016-03-07 19:20:07 +00:00
Offer 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
"""
2016-02-19 14:49:11 +00:00
from datetime import timedelta
2015-01-16 07:32:27 +00:00
import voluptuous as vol
2016-02-19 14:49:11 +00:00
import homeassistant.util.dt as dt_util
2016-02-19 14:49:11 +00:00
from homeassistant.const import (
EVENT_STATE_CHANGED, EVENT_TIME_CHANGED, MATCH_ALL, CONF_PLATFORM)
2016-02-19 14:49:11 +00:00
from homeassistant.components.automation.time import (
CONF_HOURS, CONF_MINUTES, CONF_SECONDS)
from homeassistant.helpers.event import track_state_change, track_point_in_time
import homeassistant.helpers.config_validation as cv
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
BASE_SCHEMA = vol.Schema({
vol.Required(CONF_PLATFORM): 'state',
vol.Required(CONF_ENTITY_ID): cv.entity_id,
# These are str on purpose. Want to catch YAML conversions
CONF_STATE: str,
CONF_FOR: vol.All(vol.Schema({
CONF_HOURS: vol.Coerce(int),
CONF_MINUTES: vol.Coerce(int),
CONF_SECONDS: vol.Coerce(int),
}), cv.has_at_least_one_key(CONF_HOURS, CONF_MINUTES, CONF_SECONDS)),
})
TRIGGER_SCHEMA = vol.Schema(vol.All(
BASE_SCHEMA.extend({
# These are str on purpose. Want to catch YAML conversions
CONF_FROM: str,
CONF_TO: str,
}),
vol.Any(cv.key_dependency(CONF_FOR, CONF_TO),
cv.key_dependency(CONF_FOR, CONF_STATE))
))
IF_ACTION_SCHEMA = vol.Schema(vol.All(
BASE_SCHEMA,
cv.key_dependency(CONF_FOR, CONF_STATE)
))
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
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)
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
def state_automation_listener(entity, from_s, to_s):
2016-03-07 19:20:07 +00:00
"""Listen for state changes and calls action."""
2016-02-19 14:49:11 +00:00
def state_for_listener(now):
2016-03-07 19:20:07 +00:00
"""Fire 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 19:20:07 +00:00
"""Fire on changes and cancel for listener if 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 19:20:07 +00:00
"""Wrap 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)
2016-02-22 08:40:27 +00:00
time_delta = get_time_config(config)
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