Fire numeric_state action when first state change matches criteria (#10125)

* Fire numeric_state action when first state change matches criteria

* Remove lint

* Update numeric_state.py
This commit is contained in:
Anders Melchiorsen 2017-10-25 16:01:09 +02:00 committed by Pascal Vizeli
parent 5fabfced38
commit 61ccbb59ce
2 changed files with 88 additions and 43 deletions

View file

@ -86,7 +86,7 @@ class TestAutomationNumericState(unittest.TestCase):
def test_if_not_fires_on_entity_change_below_to_below(self):
""""Test the firing with changed entity."""
self.hass.states.set('test.entity', 9)
self.hass.states.set('test.entity', 11)
self.hass.block_till_done()
assert setup_component(self.hass, automation.DOMAIN, {
@ -102,10 +102,15 @@ class TestAutomationNumericState(unittest.TestCase):
}
})
# 9 is below 10 so this should not fire again
self.hass.states.set('test.entity', 8)
# 9 is below 10 so this should fire
self.hass.states.set('test.entity', 9)
self.hass.block_till_done()
self.assertEqual(0, len(self.calls))
self.assertEqual(1, len(self.calls))
# already below so should not fire again
self.hass.states.set('test.entity', 5)
self.hass.block_till_done()
self.assertEqual(1, len(self.calls))
def test_if_not_below_fires_on_entity_change_to_equal(self):
""""Test the firing with changed entity."""
@ -130,6 +135,52 @@ class TestAutomationNumericState(unittest.TestCase):
self.hass.block_till_done()
self.assertEqual(0, len(self.calls))
def test_if_fires_on_initial_entity_below(self):
""""Test the firing when starting with a match."""
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',
'below': 10,
},
'action': {
'service': 'test.automation'
}
}
})
# Fire on first update even if initial state was already below
self.hass.states.set('test.entity', 8)
self.hass.block_till_done()
self.assertEqual(1, len(self.calls))
def test_if_fires_on_initial_entity_above(self):
""""Test the firing when starting with a match."""
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',
'above': 10,
},
'action': {
'service': 'test.automation'
}
}
})
# Fire on first update even if initial state was already above
self.hass.states.set('test.entity', 12)
self.hass.block_till_done()
self.assertEqual(1, 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, {
@ -176,7 +227,7 @@ class TestAutomationNumericState(unittest.TestCase):
def test_if_not_fires_on_entity_change_above_to_above(self):
""""Test the firing with changed entity."""
# set initial state
self.hass.states.set('test.entity', 11)
self.hass.states.set('test.entity', 9)
self.hass.block_till_done()
assert setup_component(self.hass, automation.DOMAIN, {
@ -192,10 +243,15 @@ class TestAutomationNumericState(unittest.TestCase):
}
})
# 11 is above 10 so this should fire again
# 12 is above 10 so this should fire
self.hass.states.set('test.entity', 12)
self.hass.block_till_done()
self.assertEqual(0, len(self.calls))
self.assertEqual(1, len(self.calls))
# already above, should not fire again
self.hass.states.set('test.entity', 15)
self.hass.block_till_done()
self.assertEqual(1, len(self.calls))
def test_if_not_above_fires_on_entity_change_to_equal(self):
""""Test the firing with changed entity."""