2019-02-13 21:21:14 +01:00
|
|
|
"""Offer state listening automation rules."""
|
2019-07-08 15:59:58 -05:00
|
|
|
import logging
|
|
|
|
|
2016-04-04 12:18:58 -07:00
|
|
|
import voluptuous as vol
|
2016-02-19 14:49:11 +00:00
|
|
|
|
2019-07-08 15:59:58 -05:00
|
|
|
from homeassistant import exceptions
|
2016-10-04 20:44:32 -07:00
|
|
|
from homeassistant.core import callback
|
2017-09-05 02:01:01 +02:00
|
|
|
from homeassistant.const import MATCH_ALL, CONF_PLATFORM, CONF_FOR
|
2019-07-08 15:59:58 -05:00
|
|
|
from homeassistant.helpers import config_validation as cv, template
|
2019-07-31 12:25:30 -07:00
|
|
|
from homeassistant.helpers.event import async_track_state_change, async_track_same_state
|
2019-07-08 15:59:58 -05:00
|
|
|
|
2019-08-12 06:38:18 +03:00
|
|
|
|
|
|
|
# mypy: allow-untyped-calls, allow-untyped-defs, no-check-untyped-defs
|
|
|
|
|
2019-07-08 15:59:58 -05:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2015-01-15 23:32:27 -08:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
CONF_ENTITY_ID = "entity_id"
|
|
|
|
CONF_FROM = "from"
|
|
|
|
CONF_TO = "to"
|
|
|
|
|
|
|
|
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.Any(
|
|
|
|
vol.All(cv.time_period, cv.positive_timedelta),
|
|
|
|
cv.template,
|
|
|
|
cv.template_complex,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
),
|
|
|
|
cv.key_dependency(CONF_FOR, CONF_TO),
|
|
|
|
)
|
2016-02-20 12:48:05 +01:00
|
|
|
|
|
|
|
|
2019-09-24 14:57:05 -07:00
|
|
|
async def async_attach_trigger(
|
|
|
|
hass, config, action, automation_info, *, platform_type="state"
|
|
|
|
):
|
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)
|
2017-07-17 22:24:05 +02:00
|
|
|
to_state = config.get(CONF_TO, MATCH_ALL)
|
2016-04-28 12:03:57 +02:00
|
|
|
time_delta = config.get(CONF_FOR)
|
2019-07-08 15:59:58 -05:00
|
|
|
template.attach(hass, time_delta)
|
2019-07-31 12:25:30 -07:00
|
|
|
match_all = from_state == MATCH_ALL and to_state == MATCH_ALL
|
2017-11-30 21:03:52 +01:00
|
|
|
unsub_track_same = {}
|
2019-07-08 15:59:58 -05:00
|
|
|
period = {}
|
2017-02-22 09:02:37 +01:00
|
|
|
|
2016-10-04 20:44:32 -07:00
|
|
|
@callback
|
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."""
|
2019-07-31 12:25:30 -07:00
|
|
|
|
2017-09-05 02:01:01 +02:00
|
|
|
@callback
|
2016-04-21 13:59:42 -07:00
|
|
|
def call_action():
|
|
|
|
"""Call action with right context."""
|
2019-07-31 12:25:30 -07:00
|
|
|
hass.async_run_job(
|
|
|
|
action(
|
|
|
|
{
|
|
|
|
"trigger": {
|
2019-09-24 14:57:05 -07:00
|
|
|
"platform": platform_type,
|
2019-07-31 12:25:30 -07:00
|
|
|
"entity_id": entity,
|
|
|
|
"from_state": from_s,
|
|
|
|
"to_state": to_s,
|
|
|
|
"for": time_delta if not time_delta else period[entity],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
context=to_s.context,
|
|
|
|
)
|
|
|
|
)
|
2016-04-21 13:59:42 -07:00
|
|
|
|
2017-05-20 21:18:59 +02:00
|
|
|
# Ignore changes to state attributes if from/to is in use
|
2019-07-31 12:25:30 -07:00
|
|
|
if (
|
|
|
|
not match_all
|
|
|
|
and from_s is not None
|
|
|
|
and to_s is not None
|
|
|
|
and from_s.state == to_s.state
|
|
|
|
):
|
2016-04-21 13:59:42 -07:00
|
|
|
return
|
|
|
|
|
2017-09-05 02:01:01 +02:00
|
|
|
if not time_delta:
|
2017-05-20 21:18:59 +02:00
|
|
|
call_action()
|
2017-05-15 03:34:30 -04:00
|
|
|
return
|
|
|
|
|
2019-07-08 15:59:58 -05:00
|
|
|
variables = {
|
2019-07-31 12:25:30 -07:00
|
|
|
"trigger": {
|
|
|
|
"platform": "state",
|
|
|
|
"entity_id": entity,
|
|
|
|
"from_state": from_s,
|
|
|
|
"to_state": to_s,
|
2019-07-08 15:59:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try:
|
|
|
|
if isinstance(time_delta, template.Template):
|
2019-07-31 12:25:30 -07:00
|
|
|
period[entity] = vol.All(cv.time_period, cv.positive_timedelta)(
|
|
|
|
time_delta.async_render(variables)
|
|
|
|
)
|
2019-07-08 15:59:58 -05:00
|
|
|
elif isinstance(time_delta, dict):
|
|
|
|
time_delta_data = {}
|
2019-07-31 12:25:30 -07:00
|
|
|
time_delta_data.update(template.render_complex(time_delta, variables))
|
|
|
|
period[entity] = vol.All(cv.time_period, cv.positive_timedelta)(
|
|
|
|
time_delta_data
|
|
|
|
)
|
2019-07-08 15:59:58 -05:00
|
|
|
else:
|
|
|
|
period[entity] = time_delta
|
|
|
|
except (exceptions.TemplateError, vol.Invalid) as ex:
|
2019-07-31 12:25:30 -07:00
|
|
|
_LOGGER.error(
|
|
|
|
"Error rendering '%s' for template: %s", automation_info["name"], ex
|
|
|
|
)
|
2019-07-08 15:59:58 -05:00
|
|
|
return
|
|
|
|
|
2017-11-30 21:03:52 +01:00
|
|
|
unsub_track_same[entity] = async_track_same_state(
|
2019-07-31 12:25:30 -07:00
|
|
|
hass,
|
|
|
|
period[entity],
|
|
|
|
call_action,
|
2017-10-10 12:16:19 -07:00
|
|
|
lambda _, _2, to_state: to_state.state == to_s.state,
|
2019-07-31 12:25:30 -07:00
|
|
|
entity_ids=entity,
|
|
|
|
)
|
2015-01-15 23:32:27 -08:00
|
|
|
|
2016-09-30 12:57:24 -07:00
|
|
|
unsub = async_track_state_change(
|
2019-07-31 12:25:30 -07:00
|
|
|
hass, entity_id, state_automation_listener, from_state, to_state
|
|
|
|
)
|
2016-08-25 23:25:57 -07:00
|
|
|
|
2017-02-18 23:17:18 +01:00
|
|
|
@callback
|
2016-09-30 12:57:24 -07:00
|
|
|
def async_remove():
|
|
|
|
"""Remove state listeners async."""
|
2016-08-25 23:25:57 -07:00
|
|
|
unsub()
|
2017-11-30 21:03:52 +01:00
|
|
|
for async_remove in unsub_track_same.values():
|
|
|
|
async_remove()
|
|
|
|
unsub_track_same.clear()
|
2016-08-25 23:25:57 -07:00
|
|
|
|
2016-10-01 01:22:13 -07:00
|
|
|
return async_remove
|