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
|
|
|
"""
|
2017-02-18 22:17:18 +00:00
|
|
|
import asyncio
|
2016-04-04 19:18:58 +00:00
|
|
|
import voluptuous as vol
|
2016-02-19 14:49:11 +00:00
|
|
|
|
2016-10-05 03:44:32 +00:00
|
|
|
from homeassistant.core import callback
|
2016-04-04 19:18:58 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2016-08-26 06:25:35 +00:00
|
|
|
from homeassistant.const import MATCH_ALL, CONF_PLATFORM
|
2016-09-30 19:57:24 +00:00
|
|
|
from homeassistant.helpers.event import (
|
|
|
|
async_track_state_change, async_track_point_in_utc_time)
|
2016-04-04 19:18:58 +00:00
|
|
|
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
|
|
|
|
2016-04-28 10:03:57 +00:00
|
|
|
TRIGGER_SCHEMA = vol.All(
|
|
|
|
vol.Schema({
|
|
|
|
vol.Required(CONF_PLATFORM): 'state',
|
2016-05-03 05:05:09 +00:00
|
|
|
vol.Required(CONF_ENTITY_ID): cv.entity_ids,
|
2016-04-04 19:18:58 +00:00
|
|
|
# These are str on purpose. Want to catch YAML conversions
|
|
|
|
CONF_FROM: str,
|
|
|
|
CONF_TO: str,
|
2016-04-28 10:03:57 +00:00
|
|
|
CONF_STATE: str,
|
|
|
|
CONF_FOR: vol.All(cv.time_period, cv.positive_timedelta),
|
2016-04-04 19:18:58 +00:00
|
|
|
}),
|
|
|
|
vol.Any(cv.key_dependency(CONF_FOR, CONF_TO),
|
|
|
|
cv.key_dependency(CONF_FOR, CONF_STATE))
|
2016-04-28 10:03:57 +00:00
|
|
|
)
|
2016-02-20 11:48:05 +00:00
|
|
|
|
|
|
|
|
2017-02-18 22:17:18 +00:00
|
|
|
@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)
|
2015-09-15 15:56:06 +00:00
|
|
|
to_state = config.get(CONF_TO) or config.get(CONF_STATE) or MATCH_ALL
|
2016-04-28 10:03:57 +00:00
|
|
|
time_delta = config.get(CONF_FOR)
|
2016-09-30 19:57:24 +00:00
|
|
|
async_remove_state_for_cancel = None
|
|
|
|
async_remove_state_for_listener = None
|
2015-01-16 07:32:27 +00:00
|
|
|
|
2017-02-22 08:02:37 +00:00
|
|
|
@callback
|
|
|
|
def clear_listener():
|
|
|
|
"""Clear all unsub listener."""
|
|
|
|
nonlocal async_remove_state_for_cancel, async_remove_state_for_listener
|
|
|
|
|
|
|
|
# pylint: disable=not-callable
|
|
|
|
if async_remove_state_for_listener is not None:
|
|
|
|
async_remove_state_for_listener()
|
|
|
|
async_remove_state_for_listener = None
|
|
|
|
if async_remove_state_for_cancel is not None:
|
|
|
|
async_remove_state_for_cancel()
|
|
|
|
async_remove_state_for_cancel = None
|
|
|
|
|
2016-10-05 03:44:32 +00:00
|
|
|
@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."""
|
2016-09-30 19:57:24 +00:00
|
|
|
nonlocal async_remove_state_for_cancel, async_remove_state_for_listener
|
2016-08-26 06:25:57 +00:00
|
|
|
|
2016-04-21 20:59:42 +00:00
|
|
|
def call_action():
|
|
|
|
"""Call action with right context."""
|
2016-10-05 03:44:32 +00:00
|
|
|
hass.async_run_job(action, {
|
2016-04-21 20:59:42 +00:00
|
|
|
'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-10-05 03:44:32 +00:00
|
|
|
@callback
|
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."""
|
2017-02-22 08:02:37 +00:00
|
|
|
nonlocal async_remove_state_for_listener
|
|
|
|
async_remove_state_for_listener = None
|
2016-12-09 07:24:03 +00:00
|
|
|
clear_listener()
|
2016-04-21 20:59:42 +00:00
|
|
|
call_action()
|
2016-02-19 14:49:11 +00:00
|
|
|
|
2016-10-05 03:44:32 +00:00
|
|
|
@callback
|
2016-02-19 14:49:11 +00:00
|
|
|
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-04-28 10:03:57 +00:00
|
|
|
if inner_to_s.state == to_s.state:
|
2016-02-19 14:49:11 +00:00
|
|
|
return
|
2016-12-09 07:24:03 +00:00
|
|
|
clear_listener()
|
2016-04-21 20:59:42 +00:00
|
|
|
|
2017-02-22 08:02:37 +00:00
|
|
|
# cleanup previous listener
|
|
|
|
clear_listener()
|
|
|
|
|
2016-09-30 19:57:24 +00:00
|
|
|
async_remove_state_for_listener = async_track_point_in_utc_time(
|
2016-04-21 20:59:42 +00:00
|
|
|
hass, state_for_listener, dt_util.utcnow() + time_delta)
|
|
|
|
|
2016-09-30 19:57:24 +00:00
|
|
|
async_remove_state_for_cancel = async_track_state_change(
|
2016-05-03 05:05:09 +00:00
|
|
|
hass, entity, state_for_cancel_listener)
|
2015-01-16 07:32:27 +00:00
|
|
|
|
2016-09-30 19:57:24 +00:00
|
|
|
unsub = async_track_state_change(
|
|
|
|
hass, entity_id, state_automation_listener, from_state, to_state)
|
2016-08-26 06:25:57 +00:00
|
|
|
|
2017-02-18 22:17:18 +00:00
|
|
|
@callback
|
2016-09-30 19:57:24 +00:00
|
|
|
def async_remove():
|
|
|
|
"""Remove state listeners async."""
|
2016-08-26 06:25:57 +00:00
|
|
|
unsub()
|
2017-02-22 08:02:37 +00:00
|
|
|
clear_listener()
|
2016-08-26 06:25:57 +00:00
|
|
|
|
2016-10-01 08:22:13 +00:00
|
|
|
return async_remove
|