2015-09-12 19:42:52 +00:00
|
|
|
"""
|
2015-09-14 18:33:01 +00:00
|
|
|
homeassistant.components.automation.numeric_state
|
2015-11-09 12:12:18 +00:00
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2015-09-14 18:33:01 +00:00
|
|
|
Offers 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
|
2015-11-09 12:12:18 +00:00
|
|
|
at https://home-assistant.io/components/automation/#numeric-state-trigger
|
2015-09-12 19:42:52 +00:00
|
|
|
"""
|
|
|
|
import logging
|
2016-02-19 05:27:50 +00:00
|
|
|
from functools import partial
|
2015-09-12 19:42:52 +00:00
|
|
|
|
2015-12-14 18:08:31 +00:00
|
|
|
from homeassistant.const import CONF_VALUE_TEMPLATE
|
2015-09-12 19:42:52 +00:00
|
|
|
from homeassistant.helpers.event import track_state_change
|
2016-02-23 20:06:50 +00:00
|
|
|
from homeassistant.helpers import template
|
2015-09-12 19:42:52 +00:00
|
|
|
|
2015-09-15 05:05:40 +00:00
|
|
|
CONF_ENTITY_ID = "entity_id"
|
|
|
|
CONF_BELOW = "below"
|
|
|
|
CONF_ABOVE = "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
|
|
|
|
2016-01-13 05:53:27 +00:00
|
|
|
def _renderer(hass, value_template, state):
|
2016-03-07 16:14:55 +00:00
|
|
|
"""Render the state value."""
|
2016-01-13 05:53:27 +00:00
|
|
|
if value_template is None:
|
|
|
|
return state.state
|
|
|
|
|
|
|
|
return template.render(hass, value_template, {'state': state})
|
|
|
|
|
|
|
|
|
2015-09-14 05:25:42 +00:00
|
|
|
def 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)
|
|
|
|
|
|
|
|
if entity_id is None:
|
2015-09-13 10:15:21 +00:00
|
|
|
_LOGGER.error("Missing configuration key %s", CONF_ENTITY_ID)
|
2015-09-12 19:42:52 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
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)
|
2015-09-12 19:42:52 +00:00
|
|
|
|
|
|
|
if below is None and above is None:
|
2015-09-13 18:16:51 +00:00
|
|
|
_LOGGER.error("Missing configuration key."
|
|
|
|
" One of %s or %s is required",
|
2015-09-13 11:05:36 +00:00
|
|
|
CONF_BELOW, CONF_ABOVE)
|
|
|
|
return False
|
2015-09-12 19:42:52 +00:00
|
|
|
|
2016-01-13 05:53:27 +00:00
|
|
|
renderer = partial(_renderer, hass, value_template)
|
2015-12-14 18:08:31 +00:00
|
|
|
|
2015-09-14 18:33:01 +00:00
|
|
|
# pylint: disable=unused-argument
|
2015-09-12 19:42:52 +00:00
|
|
|
def state_automation_listener(entity, from_s, to_s):
|
2016-03-07 16:14:55 +00:00
|
|
|
"""Listens for state changes and calls action."""
|
2015-09-12 19:42:52 +00:00
|
|
|
# Fire action if we go from outside range into range
|
2015-12-15 15:57:30 +00:00
|
|
|
if _in_range(above, below, renderer(to_s)) and \
|
|
|
|
(from_s is None or not _in_range(above, below, renderer(from_s))):
|
2015-09-12 19:42:52 +00:00
|
|
|
action()
|
|
|
|
|
|
|
|
track_state_change(
|
|
|
|
hass, entity_id, state_automation_listener)
|
|
|
|
|
|
|
|
return True
|
2015-09-14 18:33:01 +00:00
|
|
|
|
|
|
|
|
2015-09-15 05:51:28 +00:00
|
|
|
def if_action(hass, config):
|
2016-03-07 16:14:55 +00:00
|
|
|
"""Wraps action method with state based condition."""
|
2015-09-14 18:33:01 +00:00
|
|
|
entity_id = config.get(CONF_ENTITY_ID)
|
|
|
|
|
|
|
|
if entity_id is None:
|
|
|
|
_LOGGER.error("Missing configuration key %s", CONF_ENTITY_ID)
|
2015-09-15 05:51:28 +00:00
|
|
|
return None
|
2015-09-14 18:33:01 +00:00
|
|
|
|
|
|
|
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)
|
2015-09-14 18:33:01 +00:00
|
|
|
|
|
|
|
if below is None and above is None:
|
|
|
|
_LOGGER.error("Missing configuration key."
|
|
|
|
" One of %s or %s is required",
|
|
|
|
CONF_BELOW, CONF_ABOVE)
|
2015-09-15 05:51:28 +00:00
|
|
|
return None
|
2015-09-14 18:33:01 +00:00
|
|
|
|
2016-01-13 05:53:27 +00:00
|
|
|
renderer = partial(_renderer, hass, value_template)
|
2015-12-14 18:08:31 +00:00
|
|
|
|
2015-09-15 05:51:28 +00:00
|
|
|
def if_numeric_state():
|
2016-03-07 16:14:55 +00:00
|
|
|
"""Test numeric state condition."""
|
2015-09-14 18:33:01 +00:00
|
|
|
state = hass.states.get(entity_id)
|
2015-12-15 15:57:30 +00:00
|
|
|
return state is not None and _in_range(above, below, renderer(state))
|
2015-09-14 18:33:01 +00:00
|
|
|
|
2015-09-15 05:51:28 +00:00
|
|
|
return if_numeric_state
|
2015-09-14 18:33:01 +00:00
|
|
|
|
|
|
|
|
2015-12-15 15:57:30 +00:00
|
|
|
def _in_range(range_start, range_end, value):
|
2016-03-07 16:14:55 +00:00
|
|
|
"""Checks if value is inside the range."""
|
2015-09-14 18:33:01 +00:00
|
|
|
try:
|
|
|
|
value = float(value)
|
|
|
|
except ValueError:
|
2015-12-18 00:37:39 +00:00
|
|
|
_LOGGER.warning("Value returned from template is not a number: %s",
|
|
|
|
value)
|
2015-09-14 18:33:01 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
if range_start is not None and range_end is not None:
|
|
|
|
return float(range_start) <= value < float(range_end)
|
|
|
|
elif range_end is not None:
|
|
|
|
return value < float(range_end)
|
|
|
|
else:
|
|
|
|
return float(range_start) <= value
|