2015-09-12 19:42:52 +00:00
|
|
|
"""
|
|
|
|
homeassistant.components.automation.state
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Offers state listening automation rules.
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from homeassistant.helpers.event import track_state_change
|
|
|
|
|
|
|
|
|
|
|
|
CONF_ENTITY_ID = "state_entity_id"
|
|
|
|
CONF_BELOW = "state_below"
|
|
|
|
CONF_ABOVE = "state_above"
|
|
|
|
|
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
|
|
|
|
2015-09-12 19:42:52 +00:00
|
|
|
def register(hass, config, action):
|
|
|
|
""" Listen for state changes based on `config`. """
|
|
|
|
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)
|
|
|
|
|
|
|
|
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
|
|
|
|
2015-09-13 18:34:45 +00:00
|
|
|
def _in_range(value, range_start, range_end):
|
2015-09-12 19:42:52 +00:00
|
|
|
""" Checks if value is inside the range
|
|
|
|
:param value:
|
|
|
|
:param range_start:
|
|
|
|
:param range_end:
|
|
|
|
:return:
|
|
|
|
"""
|
2015-09-13 10:15:21 +00:00
|
|
|
|
2015-09-13 17:59:26 +00:00
|
|
|
try:
|
|
|
|
value = float(value)
|
|
|
|
except ValueError:
|
2015-09-13 10:15:21 +00:00
|
|
|
_LOGGER.warn("Missing value in numeric check")
|
|
|
|
return False
|
|
|
|
|
2015-09-12 19:42:52 +00:00
|
|
|
if range_start is not None and range_end is not None:
|
2015-09-13 11:05:36 +00:00
|
|
|
return float(range_start) <= value < float(range_end)
|
2015-09-12 19:42:52 +00:00
|
|
|
elif range_end is not None:
|
|
|
|
return value < float(range_end)
|
|
|
|
else:
|
2015-09-13 11:05:36 +00:00
|
|
|
return float(range_start) <= value
|
2015-09-12 19:42:52 +00:00
|
|
|
|
|
|
|
def state_automation_listener(entity, from_s, to_s):
|
|
|
|
""" Listens for state changes and calls action. """
|
|
|
|
|
|
|
|
# Fire action if we go from outside range into range
|
2015-09-13 18:34:45 +00:00
|
|
|
if _in_range(to_s.state, above, below) and \
|
|
|
|
(from_s is None or not _in_range(from_s.state, above, below)):
|
2015-09-12 19:42:52 +00:00
|
|
|
action()
|
|
|
|
|
|
|
|
track_state_change(
|
|
|
|
hass, entity_id, state_automation_listener)
|
|
|
|
|
|
|
|
return True
|