2019-02-13 21:21:14 +01:00
|
|
|
"""Offer template automation rules."""
|
2015-12-16 10:52:33 -07:00
|
|
|
import logging
|
|
|
|
|
2016-04-04 12:18:58 -07:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2016-10-04 20:44:32 -07:00
|
|
|
from homeassistant.core import callback
|
2019-06-07 23:45:37 -05:00
|
|
|
from homeassistant.const import CONF_VALUE_TEMPLATE, CONF_PLATFORM, CONF_FOR
|
|
|
|
from homeassistant.helpers import condition
|
|
|
|
from homeassistant.helpers.event import (
|
|
|
|
async_track_same_state, async_track_template)
|
2016-04-04 12:18:58 -07:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
2015-12-16 10:52:33 -07:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2016-04-04 12:18:58 -07:00
|
|
|
TRIGGER_SCHEMA = IF_ACTION_SCHEMA = vol.Schema({
|
|
|
|
vol.Required(CONF_PLATFORM): 'template',
|
|
|
|
vol.Required(CONF_VALUE_TEMPLATE): cv.template,
|
2019-06-07 23:45:37 -05:00
|
|
|
vol.Optional(CONF_FOR): vol.All(cv.time_period, cv.positive_timedelta),
|
2016-04-04 12:18:58 -07:00
|
|
|
})
|
|
|
|
|
2015-12-16 10:52:33 -07:00
|
|
|
|
2018-11-05 09:23:58 +01:00
|
|
|
async def async_trigger(hass, config, action, automation_info):
|
2016-03-07 17:14:55 +01:00
|
|
|
"""Listen for state changes based on configuration."""
|
2016-09-27 21:29:55 -07:00
|
|
|
value_template = config.get(CONF_VALUE_TEMPLATE)
|
|
|
|
value_template.hass = hass
|
2019-06-07 23:45:37 -05:00
|
|
|
time_delta = config.get(CONF_FOR)
|
|
|
|
unsub_track_same = None
|
2015-12-16 10:52:33 -07:00
|
|
|
|
2016-10-04 20:44:32 -07:00
|
|
|
@callback
|
2017-02-12 22:27:53 +01:00
|
|
|
def template_listener(entity_id, from_s, to_s):
|
2016-03-07 20:20:07 +01:00
|
|
|
"""Listen for state changes and calls action."""
|
2019-06-07 23:45:37 -05:00
|
|
|
nonlocal unsub_track_same
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def call_action():
|
|
|
|
"""Call action with right context."""
|
|
|
|
hass.async_run_job(action({
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'template',
|
|
|
|
'entity_id': entity_id,
|
|
|
|
'from_state': from_s,
|
|
|
|
'to_state': to_s,
|
|
|
|
},
|
|
|
|
}, context=(to_s.context if to_s else None)))
|
|
|
|
|
|
|
|
if not time_delta:
|
|
|
|
call_action()
|
|
|
|
return
|
|
|
|
|
|
|
|
unsub_track_same = async_track_same_state(
|
|
|
|
hass, time_delta, call_action,
|
|
|
|
lambda _, _2, _3: condition.async_template(hass, value_template),
|
|
|
|
value_template.extract_entities())
|
|
|
|
|
|
|
|
unsub = async_track_template(
|
|
|
|
hass, value_template, template_listener)
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_remove():
|
|
|
|
"""Remove state listeners async."""
|
|
|
|
unsub()
|
|
|
|
if unsub_track_same:
|
|
|
|
# pylint: disable=not-callable
|
|
|
|
unsub_track_same()
|
|
|
|
|
|
|
|
return async_remove
|