2019-02-13 20:21:14 +00:00
|
|
|
"""Offer template automation rules."""
|
2015-12-16 17:52:33 +00:00
|
|
|
import logging
|
|
|
|
|
2016-04-04 19:18:58 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-06-29 05:30:47 +00:00
|
|
|
from homeassistant import exceptions
|
2019-12-08 16:29:39 +00:00
|
|
|
from homeassistant.const import CONF_FOR, CONF_PLATFORM, CONF_VALUE_TEMPLATE
|
2020-10-08 07:44:34 +00:00
|
|
|
from homeassistant.core import HassJob, callback
|
2020-08-21 13:02:24 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv, template
|
2020-09-01 00:07:40 +00:00
|
|
|
from homeassistant.helpers.event import (
|
|
|
|
TrackTemplate,
|
|
|
|
async_call_later,
|
|
|
|
async_track_template_result,
|
|
|
|
)
|
2020-08-21 13:02:24 +00:00
|
|
|
from homeassistant.helpers.template import result_as_boolean
|
2019-08-12 03:38:18 +00:00
|
|
|
|
|
|
|
# mypy: allow-untyped-defs, no-check-untyped-defs
|
|
|
|
|
2015-12-16 17:52:33 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2021-06-11 07:51:12 +00:00
|
|
|
TRIGGER_SCHEMA = IF_ACTION_SCHEMA = cv.TRIGGER_BASE_SCHEMA.extend(
|
2019-07-31 19:25:30 +00:00
|
|
|
{
|
|
|
|
vol.Required(CONF_PLATFORM): "template",
|
|
|
|
vol.Required(CONF_VALUE_TEMPLATE): cv.template,
|
2020-08-12 18:42:06 +00:00
|
|
|
vol.Optional(CONF_FOR): cv.positive_time_period_template,
|
2019-07-31 19:25:30 +00:00
|
|
|
}
|
|
|
|
)
|
2016-04-04 19:18:58 +00:00
|
|
|
|
2015-12-16 17:52:33 +00:00
|
|
|
|
2019-11-07 15:28:45 +00:00
|
|
|
async def async_attach_trigger(
|
2020-08-17 16:54:56 +00:00
|
|
|
hass, config, action, automation_info, *, platform_type="template"
|
2019-11-07 15:28:45 +00:00
|
|
|
):
|
2016-03-07 16:14:55 +00:00
|
|
|
"""Listen for state changes based on configuration."""
|
2021-09-04 00:25:51 +00:00
|
|
|
trigger_data = automation_info["trigger_data"]
|
2016-09-28 04:29:55 +00:00
|
|
|
value_template = config.get(CONF_VALUE_TEMPLATE)
|
|
|
|
value_template.hass = hass
|
2019-06-08 04:45:37 +00:00
|
|
|
time_delta = config.get(CONF_FOR)
|
2019-06-29 05:30:47 +00:00
|
|
|
template.attach(hass, time_delta)
|
2020-08-21 13:02:24 +00:00
|
|
|
delay_cancel = None
|
2020-10-08 07:44:34 +00:00
|
|
|
job = HassJob(action)
|
2021-02-14 18:54:11 +00:00
|
|
|
armed = False
|
|
|
|
|
|
|
|
# Arm at setup if the template is already false.
|
|
|
|
try:
|
2021-03-01 22:34:26 +00:00
|
|
|
if not result_as_boolean(
|
|
|
|
value_template.async_render(automation_info["variables"])
|
|
|
|
):
|
2021-02-14 18:54:11 +00:00
|
|
|
armed = True
|
|
|
|
except exceptions.TemplateError as ex:
|
|
|
|
_LOGGER.warning(
|
|
|
|
"Error initializing 'template' trigger for '%s': %s",
|
|
|
|
automation_info["name"],
|
|
|
|
ex,
|
|
|
|
)
|
2015-12-16 17:52:33 +00:00
|
|
|
|
2016-10-05 03:44:32 +00:00
|
|
|
@callback
|
2020-09-01 00:07:40 +00:00
|
|
|
def template_listener(event, updates):
|
2016-03-07 19:20:07 +00:00
|
|
|
"""Listen for state changes and calls action."""
|
2021-02-14 18:54:11 +00:00
|
|
|
nonlocal delay_cancel, armed
|
2020-09-01 00:07:40 +00:00
|
|
|
result = updates.pop().result
|
2020-08-21 13:02:24 +00:00
|
|
|
|
2021-02-14 18:54:11 +00:00
|
|
|
if isinstance(result, exceptions.TemplateError):
|
|
|
|
_LOGGER.warning(
|
|
|
|
"Error evaluating 'template' trigger for '%s': %s",
|
|
|
|
automation_info["name"],
|
|
|
|
result,
|
|
|
|
)
|
|
|
|
return
|
|
|
|
|
2020-08-21 13:02:24 +00:00
|
|
|
if delay_cancel:
|
|
|
|
# pylint: disable=not-callable
|
|
|
|
delay_cancel()
|
|
|
|
delay_cancel = None
|
|
|
|
|
|
|
|
if not result_as_boolean(result):
|
2021-02-14 18:54:11 +00:00
|
|
|
armed = True
|
2020-08-21 13:02:24 +00:00
|
|
|
return
|
|
|
|
|
2021-02-14 18:54:11 +00:00
|
|
|
# Only fire when previously armed.
|
|
|
|
if not armed:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Fire!
|
|
|
|
armed = False
|
|
|
|
|
2020-12-29 22:16:39 +00:00
|
|
|
entity_id = event and event.data.get("entity_id")
|
|
|
|
from_s = event and event.data.get("old_state")
|
|
|
|
to_s = event and event.data.get("new_state")
|
|
|
|
|
|
|
|
if entity_id is not None:
|
|
|
|
description = f"{entity_id} via template"
|
|
|
|
else:
|
|
|
|
description = "time change or manual update via template"
|
|
|
|
|
|
|
|
template_variables = {
|
|
|
|
"platform": platform_type,
|
|
|
|
"entity_id": entity_id,
|
|
|
|
"from_state": from_s,
|
|
|
|
"to_state": to_s,
|
|
|
|
}
|
|
|
|
trigger_variables = {
|
2021-06-14 15:09:20 +00:00
|
|
|
**trigger_data,
|
2020-12-29 22:16:39 +00:00
|
|
|
"for": time_delta,
|
|
|
|
"description": description,
|
|
|
|
}
|
2019-06-08 04:45:37 +00:00
|
|
|
|
2019-07-02 15:46:26 +00:00
|
|
|
@callback
|
2020-08-21 13:02:24 +00:00
|
|
|
def call_action(*_):
|
2019-07-02 15:46:26 +00:00
|
|
|
"""Call action with right context."""
|
2020-12-29 22:16:39 +00:00
|
|
|
nonlocal trigger_variables
|
2020-10-08 07:44:34 +00:00
|
|
|
hass.async_run_hass_job(
|
|
|
|
job,
|
2020-12-29 22:16:39 +00:00
|
|
|
{"trigger": {**template_variables, **trigger_variables}},
|
2020-08-24 21:01:57 +00:00
|
|
|
(to_s.context if to_s else None),
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-07-02 15:46:26 +00:00
|
|
|
|
|
|
|
if not time_delta:
|
|
|
|
call_action()
|
|
|
|
return
|
|
|
|
|
2019-06-29 05:30:47 +00:00
|
|
|
try:
|
2020-08-12 18:42:06 +00:00
|
|
|
period = cv.positive_time_period(
|
2020-12-29 22:16:39 +00:00
|
|
|
template.render_complex(time_delta, {"trigger": template_variables})
|
2020-08-12 18:42:06 +00:00
|
|
|
)
|
2019-06-29 05:30:47 +00:00
|
|
|
except (exceptions.TemplateError, vol.Invalid) as ex:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error(
|
|
|
|
"Error rendering '%s' for template: %s", automation_info["name"], ex
|
|
|
|
)
|
2019-06-29 05:30:47 +00:00
|
|
|
return
|
|
|
|
|
2020-12-29 22:16:39 +00:00
|
|
|
trigger_variables["for"] = period
|
|
|
|
|
2021-04-21 00:41:36 +00:00
|
|
|
delay_cancel = async_call_later(hass, period.total_seconds(), call_action)
|
2019-06-08 04:45:37 +00:00
|
|
|
|
2020-08-21 13:02:24 +00:00
|
|
|
info = async_track_template_result(
|
2020-09-01 00:07:40 +00:00
|
|
|
hass,
|
|
|
|
[TrackTemplate(value_template, automation_info["variables"])],
|
|
|
|
template_listener,
|
2020-08-21 09:38:25 +00:00
|
|
|
)
|
2020-08-21 13:02:24 +00:00
|
|
|
unsub = info.async_remove
|
2019-06-08 04:45:37 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_remove():
|
|
|
|
"""Remove state listeners async."""
|
|
|
|
unsub()
|
2020-08-21 13:02:24 +00:00
|
|
|
if delay_cancel:
|
2019-06-08 04:45:37 +00:00
|
|
|
# pylint: disable=not-callable
|
2020-08-21 13:02:24 +00:00
|
|
|
delay_cancel()
|
2019-06-08 04:45:37 +00:00
|
|
|
|
|
|
|
return async_remove
|