2019-02-13 20:21:14 +00:00
|
|
|
"""Offer numeric state listening automation rules."""
|
2015-09-12 19:42:52 +00:00
|
|
|
import logging
|
|
|
|
|
2016-04-28 10:03:57 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2016-10-05 03:44:32 +00:00
|
|
|
from homeassistant.core import callback
|
2016-04-28 10:03:57 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_VALUE_TEMPLATE, CONF_PLATFORM, CONF_ENTITY_ID,
|
2017-09-05 00:01:01 +00:00
|
|
|
CONF_BELOW, CONF_ABOVE, CONF_FOR)
|
|
|
|
from homeassistant.helpers.event import (
|
|
|
|
async_track_state_change, async_track_same_state)
|
2016-09-28 04:29:55 +00:00
|
|
|
from homeassistant.helpers import condition, config_validation as cv
|
2015-09-12 19:42:52 +00:00
|
|
|
|
2016-04-28 10:03:57 +00:00
|
|
|
TRIGGER_SCHEMA = vol.All(vol.Schema({
|
|
|
|
vol.Required(CONF_PLATFORM): 'numeric_state',
|
2016-05-14 19:29:57 +00:00
|
|
|
vol.Required(CONF_ENTITY_ID): cv.entity_ids,
|
2017-09-05 00:01:01 +00:00
|
|
|
vol.Optional(CONF_BELOW): vol.Coerce(float),
|
|
|
|
vol.Optional(CONF_ABOVE): vol.Coerce(float),
|
2016-04-28 10:03:57 +00:00
|
|
|
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
|
2017-09-05 00:01:01 +00:00
|
|
|
vol.Optional(CONF_FOR): vol.All(cv.time_period, cv.positive_timedelta),
|
2016-04-28 10:03:57 +00:00
|
|
|
}), cv.has_at_least_one_key(CONF_BELOW, CONF_ABOVE))
|
2015-09-12 19:42:52 +00:00
|
|
|
|
2015-09-13 10:15:21 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2015-09-12 19:42:52 +00:00
|
|
|
|
2015-09-13 11:05:36 +00:00
|
|
|
|
2018-11-05 08:23:58 +00:00
|
|
|
async def async_trigger(hass, config, action, automation_info):
|
2016-03-07 16:14:55 +00:00
|
|
|
"""Listen for state changes based on configuration."""
|
2015-09-12 19:42:52 +00:00
|
|
|
entity_id = config.get(CONF_ENTITY_ID)
|
|
|
|
below = config.get(CONF_BELOW)
|
|
|
|
above = config.get(CONF_ABOVE)
|
2017-09-05 00:01:01 +00:00
|
|
|
time_delta = config.get(CONF_FOR)
|
2015-12-14 18:08:31 +00:00
|
|
|
value_template = config.get(CONF_VALUE_TEMPLATE)
|
2017-11-30 20:03:52 +00:00
|
|
|
unsub_track_same = {}
|
|
|
|
entities_triggered = set()
|
2017-09-05 00:01:01 +00:00
|
|
|
|
2016-09-25 20:33:01 +00:00
|
|
|
if value_template is not None:
|
2016-09-28 04:29:55 +00:00
|
|
|
value_template.hass = hass
|
2015-09-12 19:42:52 +00:00
|
|
|
|
2016-10-05 03:44:32 +00:00
|
|
|
@callback
|
2017-09-05 00:01:01 +00:00
|
|
|
def check_numeric_state(entity, from_s, to_s):
|
2017-10-25 14:01:09 +00:00
|
|
|
"""Return True if criteria are now met."""
|
2016-04-21 20:59:42 +00:00
|
|
|
if to_s is None:
|
2017-09-05 00:01:01 +00:00
|
|
|
return False
|
2016-04-21 20:59:42 +00:00
|
|
|
|
|
|
|
variables = {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'numeric_state',
|
2016-05-14 19:29:57 +00:00
|
|
|
'entity_id': entity,
|
2016-04-21 20:59:42 +00:00
|
|
|
'below': below,
|
|
|
|
'above': above,
|
|
|
|
}
|
|
|
|
}
|
2017-10-25 14:01:09 +00:00
|
|
|
return condition.async_numeric_state(
|
|
|
|
hass, to_s, below, above, value_template, variables)
|
2017-09-05 00:01:01 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def state_automation_listener(entity, from_s, to_s):
|
|
|
|
"""Listen for state changes and calls action."""
|
|
|
|
@callback
|
|
|
|
def call_action():
|
|
|
|
"""Call action with right context."""
|
2018-09-04 19:16:24 +00:00
|
|
|
hass.async_run_job(action({
|
2017-10-25 14:01:09 +00:00
|
|
|
'trigger': {
|
|
|
|
'platform': 'numeric_state',
|
|
|
|
'entity_id': entity,
|
|
|
|
'below': below,
|
|
|
|
'above': above,
|
|
|
|
'from_state': from_s,
|
|
|
|
'to_state': to_s,
|
|
|
|
}
|
2018-09-04 19:16:24 +00:00
|
|
|
}, context=to_s.context))
|
2017-10-25 14:01:09 +00:00
|
|
|
|
|
|
|
matching = check_numeric_state(entity, from_s, to_s)
|
|
|
|
|
2017-11-30 20:03:52 +00:00
|
|
|
if not matching:
|
|
|
|
entities_triggered.discard(entity)
|
|
|
|
elif entity not in entities_triggered:
|
|
|
|
entities_triggered.add(entity)
|
|
|
|
|
2017-10-25 14:01:09 +00:00
|
|
|
if time_delta:
|
2017-11-30 20:03:52 +00:00
|
|
|
unsub_track_same[entity] = async_track_same_state(
|
2017-10-25 14:01:09 +00:00
|
|
|
hass, time_delta, call_action, entity_ids=entity_id,
|
|
|
|
async_check_same_func=check_numeric_state)
|
|
|
|
else:
|
|
|
|
call_action()
|
|
|
|
|
2017-09-05 00:01:01 +00:00
|
|
|
unsub = async_track_state_change(
|
|
|
|
hass, entity_id, state_automation_listener)
|
2015-09-14 18:33:01 +00:00
|
|
|
|
2017-09-05 00:01:01 +00:00
|
|
|
@callback
|
|
|
|
def async_remove():
|
|
|
|
"""Remove state listeners async."""
|
|
|
|
unsub()
|
2017-11-30 20:03:52 +00:00
|
|
|
for async_remove in unsub_track_same.values():
|
|
|
|
async_remove()
|
|
|
|
unsub_track_same.clear()
|
2015-09-14 18:33:01 +00:00
|
|
|
|
2017-09-05 00:01:01 +00:00
|
|
|
return async_remove
|