core/homeassistant/components/automation/state.py

82 lines
2.6 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
at https://home-assistant.io/docs/automation/trigger/#state-trigger
2015-01-16 07:32:27 +00:00
"""
import asyncio
import voluptuous as vol
2016-02-19 14:49:11 +00:00
from homeassistant.core import callback
from homeassistant.const import MATCH_ALL, CONF_PLATFORM, CONF_FOR
from homeassistant.helpers.event import (
async_track_state_change, async_track_same_state)
import homeassistant.helpers.config_validation as cv
2015-01-16 07:32:27 +00:00
CONF_ENTITY_ID = 'entity_id'
CONF_FROM = 'from'
CONF_TO = 'to'
2015-01-16 07:32:27 +00:00
TRIGGER_SCHEMA = vol.All(vol.Schema({
vol.Required(CONF_PLATFORM): 'state',
vol.Required(CONF_ENTITY_ID): cv.entity_ids,
# These are str on purpose. Want to catch YAML conversions
vol.Optional(CONF_FROM): str,
vol.Optional(CONF_TO): str,
vol.Optional(CONF_FOR): vol.All(cv.time_period, cv.positive_timedelta),
}), cv.key_dependency(CONF_FOR, CONF_TO))
@asyncio.coroutine
2016-10-01 08:22:13 +00:00
def async_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)
to_state = config.get(CONF_TO, MATCH_ALL)
time_delta = config.get(CONF_FOR)
match_all = (from_state == MATCH_ALL and to_state == MATCH_ALL)
unsub_track_same = {}
@callback
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."""
@callback
def call_action():
"""Call action with right context."""
hass.async_run_job(action, {
'trigger': {
'platform': 'state',
'entity_id': entity,
'from_state': from_s,
'to_state': to_s,
'for': time_delta,
}
})
# Ignore changes to state attributes if from/to is in use
if (not match_all and from_s is not None and to_s is not None and
from_s.state == to_s.state):
return
if not time_delta:
call_action()
return
unsub_track_same[entity] = async_track_same_state(
2017-10-10 19:16:19 +00:00
hass, time_delta, call_action,
lambda _, _2, to_state: to_state.state == to_s.state,
entity_ids=entity_id)
2015-01-16 07:32:27 +00:00
unsub = async_track_state_change(
hass, entity_id, state_automation_listener, from_state, to_state)
@callback
def async_remove():
"""Remove state listeners async."""
unsub()
for async_remove in unsub_track_same.values():
async_remove()
unsub_track_same.clear()
2016-10-01 08:22:13 +00:00
return async_remove