2015-09-12 19:42:52 +00:00
|
|
|
"""
|
2016-03-07 19:20:07 +00:00
|
|
|
Offer numeric state listening automation rules.
|
2015-10-13 19:07:53 +00:00
|
|
|
|
|
|
|
For more details about this automation rule, please refer to the documentation
|
2017-04-06 06:23:02 +00:00
|
|
|
at https://home-assistant.io/docs/automation/trigger/#numeric-state-trigger
|
2015-09-12 19:42:52 +00:00
|
|
|
"""
|
2017-02-18 22:17:18 +00:00
|
|
|
import asyncio
|
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,
|
|
|
|
CONF_BELOW, CONF_ABOVE)
|
2016-10-01 08:22:13 +00:00
|
|
|
from homeassistant.helpers.event import async_track_state_change
|
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,
|
2016-04-28 10:03:57 +00:00
|
|
|
CONF_BELOW: vol.Coerce(float),
|
|
|
|
CONF_ABOVE: vol.Coerce(float),
|
|
|
|
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
|
|
|
|
}), 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
|
|
|
|
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-09-12 19:42:52 +00:00
|
|
|
entity_id = config.get(CONF_ENTITY_ID)
|
|
|
|
below = config.get(CONF_BELOW)
|
|
|
|
above = config.get(CONF_ABOVE)
|
2015-12-14 18:08:31 +00:00
|
|
|
value_template = config.get(CONF_VALUE_TEMPLATE)
|
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
|
2015-09-12 19:42:52 +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-04-21 20:59:42 +00:00
|
|
|
if to_s is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
2015-09-14 18:33:01 +00:00
|
|
|
|
2016-04-28 10:03:57 +00:00
|
|
|
# If new one doesn't match, nothing to do
|
2016-09-30 19:57:24 +00:00
|
|
|
if not condition.async_numeric_state(
|
2016-04-28 10:03:57 +00:00
|
|
|
hass, to_s, below, above, value_template, variables):
|
|
|
|
return
|
2015-09-14 18:33:01 +00:00
|
|
|
|
2016-04-28 10:03:57 +00:00
|
|
|
# Only match if old didn't exist or existed but didn't match
|
|
|
|
# Written as: skip if old one did exist and matched
|
2016-09-30 19:57:24 +00:00
|
|
|
if from_s is not None and condition.async_numeric_state(
|
2016-04-28 10:03:57 +00:00
|
|
|
hass, from_s, below, above, value_template, variables):
|
|
|
|
return
|
2015-09-14 18:33:01 +00:00
|
|
|
|
2016-04-28 10:03:57 +00:00
|
|
|
variables['trigger']['from_state'] = from_s
|
|
|
|
variables['trigger']['to_state'] = to_s
|
2015-09-14 18:33:01 +00:00
|
|
|
|
2016-10-05 03:44:32 +00:00
|
|
|
hass.async_run_job(action, variables)
|
2015-09-14 18:33:01 +00:00
|
|
|
|
2016-10-01 08:22:13 +00:00
|
|
|
return async_track_state_change(hass, entity_id, state_automation_listener)
|