Add attribuet config to numeric state platform to allow trigger based in attributes rather than states.
This commit is contained in:
parent
41a0f2c198
commit
f4c3fbe8fd
1 changed files with 9 additions and 5 deletions
|
@ -14,6 +14,7 @@ from homeassistant.helpers.event import track_state_change
|
||||||
CONF_ENTITY_ID = "entity_id"
|
CONF_ENTITY_ID = "entity_id"
|
||||||
CONF_BELOW = "below"
|
CONF_BELOW = "below"
|
||||||
CONF_ABOVE = "above"
|
CONF_ABOVE = "above"
|
||||||
|
CONF_ATTRIBUTE = "attribute"
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -28,6 +29,7 @@ def trigger(hass, config, action):
|
||||||
|
|
||||||
below = config.get(CONF_BELOW)
|
below = config.get(CONF_BELOW)
|
||||||
above = config.get(CONF_ABOVE)
|
above = config.get(CONF_ABOVE)
|
||||||
|
attribute = config.get(CONF_ATTRIBUTE)
|
||||||
|
|
||||||
if below is None and above is None:
|
if below is None and above is None:
|
||||||
_LOGGER.error("Missing configuration key."
|
_LOGGER.error("Missing configuration key."
|
||||||
|
@ -40,8 +42,8 @@ def trigger(hass, config, action):
|
||||||
""" Listens for state changes and calls action. """
|
""" Listens for state changes and calls action. """
|
||||||
|
|
||||||
# Fire action if we go from outside range into range
|
# Fire action if we go from outside range into range
|
||||||
if _in_range(to_s.state, above, below) and \
|
if _in_range(to_s, above, below, attribute) and \
|
||||||
(from_s is None or not _in_range(from_s.state, above, below)):
|
(from_s is None or not _in_range(from_s, above, below, attribute)):
|
||||||
action()
|
action()
|
||||||
|
|
||||||
track_state_change(
|
track_state_change(
|
||||||
|
@ -61,6 +63,7 @@ def if_action(hass, config):
|
||||||
|
|
||||||
below = config.get(CONF_BELOW)
|
below = config.get(CONF_BELOW)
|
||||||
above = config.get(CONF_ABOVE)
|
above = config.get(CONF_ABOVE)
|
||||||
|
attribute = config.get(CONF_ABOVE)
|
||||||
|
|
||||||
if below is None and above is None:
|
if below is None and above is None:
|
||||||
_LOGGER.error("Missing configuration key."
|
_LOGGER.error("Missing configuration key."
|
||||||
|
@ -71,14 +74,15 @@ def if_action(hass, config):
|
||||||
def if_numeric_state():
|
def if_numeric_state():
|
||||||
""" Test numeric state condition. """
|
""" Test numeric state condition. """
|
||||||
state = hass.states.get(entity_id)
|
state = hass.states.get(entity_id)
|
||||||
return state is not None and _in_range(state.state, above, below)
|
return state is not None and _in_range(state, above, below, attribute)
|
||||||
|
|
||||||
return if_numeric_state
|
return if_numeric_state
|
||||||
|
|
||||||
|
|
||||||
def _in_range(value, range_start, range_end):
|
def _in_range(state, range_start, range_end, attribute):
|
||||||
""" Checks if value is inside the range """
|
""" Checks if value is inside the range """
|
||||||
|
value = (state.state if attribute is None
|
||||||
|
else state.attributes.get(attribute))
|
||||||
try:
|
try:
|
||||||
value = float(value)
|
value = float(value)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue