diff --git a/homeassistant/helpers/condition.py b/homeassistant/helpers/condition.py index a0753b0f76622d..87b84a8081538f 100644 --- a/homeassistant/helpers/condition.py +++ b/homeassistant/helpers/condition.py @@ -166,10 +166,10 @@ def async_numeric_state(hass: HomeAssistant, entity, below=None, above=None, _LOGGER.warning("Value cannot be processed as a number: %s", value) return False - if below is not None and value > below: + if below is not None and value >= below: return False - if above is not None and value < above: + if above is not None and value <= above: return False return True diff --git a/tests/components/automation/test_numeric_state.py b/tests/components/automation/test_numeric_state.py index 0fca1d96a69860..355e26abf9ba25 100644 --- a/tests/components/automation/test_numeric_state.py +++ b/tests/components/automation/test_numeric_state.py @@ -102,6 +102,29 @@ def test_if_not_fires_on_entity_change_below_to_below(self): self.hass.block_till_done() self.assertEqual(0, len(self.calls)) + def test_if_not_below_fires_on_entity_change_to_equal(self): + """"Test the firing with changed entity.""" + self.hass.states.set('test.entity', 11) + self.hass.block_till_done() + + assert setup_component(self.hass, automation.DOMAIN, { + automation.DOMAIN: { + 'trigger': { + 'platform': 'numeric_state', + 'entity_id': 'test.entity', + 'below': 10, + }, + 'action': { + 'service': 'test.automation' + } + } + }) + + # 10 is not below 10 so this should not fire again + self.hass.states.set('test.entity', 10) + self.hass.block_till_done() + self.assertEqual(0, len(self.calls)) + def test_if_fires_on_entity_change_above(self): """"Test the firing with changed entity.""" assert setup_component(self.hass, automation.DOMAIN, { @@ -169,6 +192,30 @@ def test_if_not_fires_on_entity_change_above_to_above(self): self.hass.block_till_done() self.assertEqual(0, len(self.calls)) + def test_if_not_above_fires_on_entity_change_to_equal(self): + """"Test the firing with changed entity.""" + # set initial state + self.hass.states.set('test.entity', 9) + self.hass.block_till_done() + + assert setup_component(self.hass, automation.DOMAIN, { + automation.DOMAIN: { + 'trigger': { + 'platform': 'numeric_state', + 'entity_id': 'test.entity', + 'above': 10, + }, + 'action': { + 'service': 'test.automation' + } + } + }) + + # 10 is not above 10 so this should not fire again + self.hass.states.set('test.entity', 10) + self.hass.block_till_done() + self.assertEqual(0, len(self.calls)) + def test_if_fires_on_entity_change_below_range(self): """"Test the firing with changed entity.""" assert setup_component(self.hass, automation.DOMAIN, { @@ -494,7 +541,6 @@ def test_not_fires_on_attr_change_with_attr_not_below_multiple_attr(self): def test_if_action(self): """"Test if action.""" entity_id = 'domain.test_entity' - test_state = 10 assert setup_component(self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { @@ -504,8 +550,8 @@ def test_if_action(self): 'condition': { 'condition': 'numeric_state', 'entity_id': entity_id, - 'above': test_state, - 'below': test_state + 2 + 'above': 8, + 'below': 12, }, 'action': { 'service': 'test.automation' @@ -513,19 +559,19 @@ def test_if_action(self): } }) - self.hass.states.set(entity_id, test_state) + self.hass.states.set(entity_id, 10) self.hass.bus.fire('test_event') self.hass.block_till_done() self.assertEqual(1, len(self.calls)) - self.hass.states.set(entity_id, test_state - 1) + self.hass.states.set(entity_id, 8) self.hass.bus.fire('test_event') self.hass.block_till_done() self.assertEqual(1, len(self.calls)) - self.hass.states.set(entity_id, test_state + 1) + self.hass.states.set(entity_id, 9) self.hass.bus.fire('test_event') self.hass.block_till_done()