mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Fixed logic
This commit is contained in:
parent
50f5f1860c
commit
a2ca60159d
@ -15,6 +15,7 @@ CONF_ABOVE = "state_above"
|
|||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def register(hass, config, action):
|
def register(hass, config, action):
|
||||||
""" Listen for state changes based on `config`. """
|
""" Listen for state changes based on `config`. """
|
||||||
entity_id = config.get(CONF_ENTITY_ID)
|
entity_id = config.get(CONF_ENTITY_ID)
|
||||||
@ -27,7 +28,9 @@ def register(hass, config, action):
|
|||||||
above = config.get(CONF_ABOVE)
|
above = 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 %s or %s", CONF_BELOW, CONF_ABOVE)
|
_LOGGER.error("Missing configuration key. One of %s or %s is required",
|
||||||
|
CONF_BELOW, CONF_ABOVE)
|
||||||
|
return False
|
||||||
|
|
||||||
def numeric_in_range(value, range_start, range_end):
|
def numeric_in_range(value, range_start, range_end):
|
||||||
""" Checks if value is inside the range
|
""" Checks if value is inside the range
|
||||||
@ -44,18 +47,18 @@ def register(hass, config, action):
|
|||||||
value = float(value)
|
value = float(value)
|
||||||
|
|
||||||
if range_start is not None and range_end is not None:
|
if range_start is not None and range_end is not None:
|
||||||
return float(range_start) < value < float(range_end)
|
return float(range_start) <= value < float(range_end)
|
||||||
elif range_end is not None:
|
elif range_end is not None:
|
||||||
return value < float(range_end)
|
return value < float(range_end)
|
||||||
else:
|
else:
|
||||||
return value > float(range_start)
|
return float(range_start) <= value
|
||||||
|
|
||||||
def state_automation_listener(entity, from_s, to_s):
|
def state_automation_listener(entity, from_s, to_s):
|
||||||
""" 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 numeric_in_range(to_s.state, above, below) and \
|
if numeric_in_range(to_s.state, above, below) and \
|
||||||
from_s is None or not numeric_in_range(from_s.state, above, below):
|
(from_s is None or not numeric_in_range(from_s.state, above, below)):
|
||||||
action()
|
action()
|
||||||
|
|
||||||
track_state_change(
|
track_state_change(
|
||||||
|
@ -215,7 +215,7 @@ class TestAutomationState(unittest.TestCase):
|
|||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|
||||||
# 9 is below 10
|
# 4 is below 5 so it should not fire
|
||||||
self.hass.states.set('test.entity', 4)
|
self.hass.states.set('test.entity', 4)
|
||||||
self.hass.pool.block_till_done()
|
self.hass.pool.block_till_done()
|
||||||
self.assertEqual(0, len(self.calls))
|
self.assertEqual(0, len(self.calls))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user