hass-core/homeassistant/components/automation/state.py

134 lines
4.3 KiB
Python
Raw Normal View History

2015-01-15 23:32:27 -08:00
"""
2016-03-07 20:20:07 +01:00
Offer state listening automation rules.
2015-10-13 21:08:18 +02:00
For more details about this automation rule, please refer to the documentation
2015-11-09 13:12:18 +01:00
at https://home-assistant.io/components/automation/#state-trigger
2015-01-15 23:32:27 -08:00
"""
2016-02-19 14:49:11 +00:00
from datetime import timedelta
2015-01-15 23:32:27 -08: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-15 23:32:27 -08:00
2015-09-14 22:05:40 -07:00
CONF_ENTITY_ID = "entity_id"
CONF_FROM = "from"
CONF_TO = "to"
2015-09-13 22:25:42 -07:00
CONF_STATE = "state"
2016-02-19 14:49:11 +00:00
CONF_FOR = "for"
2015-01-15 23:32:27 -08: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-15 23:32:27 -08:00
def get_time_config(config):
2016-03-07 17:14:55 +01:00
"""Helper function to extract the time specified in the configuration."""
2016-02-22 09:40:27 +01:00
if CONF_FOR not in config:
return None
2016-02-22 09:40:27 +01: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 09:40:27 +01:00
return timedelta(hours=(hours or 0.0),
minutes=(minutes or 0.0),
seconds=(seconds or 0.0))
2015-09-13 22:25:42 -07:00
def trigger(hass, config, action):
2016-03-07 17:14:55 +01:00
"""Listen for state changes based on configuration."""
2015-01-15 23:32:27 -08:00
entity_id = config.get(CONF_ENTITY_ID)
from_state = config.get(CONF_FROM, MATCH_ALL)
2015-09-15 08:56:06 -07:00
to_state = config.get(CONF_TO) or config.get(CONF_STATE) or MATCH_ALL
2016-02-22 09:40:27 +01:00
time_delta = get_time_config(config)
2015-01-15 23:32:27 -08:00
def state_automation_listener(entity, from_s, to_s):
2016-03-07 20:20:07 +01:00
"""Listen for state changes and calls action."""
def call_action():
"""Call action with right context."""
action({
'trigger': {
'platform': 'state',
'entity_id': entity,
'from_state': from_s,
'to_state': to_s,
'for': time_delta,
}
})
if time_delta is None:
call_action()
return
2016-02-19 14:49:11 +00:00
def state_for_listener(now):
2016-03-07 20:20:07 +01:00
"""Fire on state changes after a delay and calls action."""
2016-02-19 14:49:11 +00:00
hass.bus.remove_listener(
2016-04-21 15:52:20 -07:00
EVENT_STATE_CHANGED, attached_state_for_cancel)
call_action()
2016-02-19 14:49:11 +00:00
def state_for_cancel_listener(entity, inner_from_s, inner_to_s):
2016-03-07 20:20:07 +01: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,
attached_state_for_listener)
hass.bus.remove_listener(EVENT_STATE_CHANGED,
2016-04-21 15:52:20 -07:00
attached_state_for_cancel)
attached_state_for_listener = track_point_in_time(
hass, state_for_listener, dt_util.utcnow() + time_delta)
2016-04-21 15:52:20 -07:00
attached_state_for_cancel = track_state_change(
hass, entity_id, state_for_cancel_listener)
2015-01-15 23:32:27 -08:00
2015-08-04 18:13:35 +02:00
track_state_change(
hass, entity_id, state_automation_listener, from_state, to_state)
2015-01-15 23:32:27 -08:00
return True
2015-09-13 22:25:42 -07:00
def if_action(hass, config):
2016-03-07 20:20:07 +01:00
"""Wrap action method with state based condition."""
2015-09-14 22:05:40 -07:00
entity_id = config.get(CONF_ENTITY_ID)
2015-09-13 22:25:42 -07:00
state = config.get(CONF_STATE)
2016-02-22 09:40:27 +01:00
time_delta = get_time_config(config)
2015-09-13 22:25:42 -07:00
def if_state(variables):
2016-03-07 17:14:55 +01:00
"""Test if condition."""
is_state = hass.states.is_state(entity_id, state)
2016-02-22 09:40:27 +01: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