mirror of
https://github.com/home-assistant/core.git
synced 2025-07-13 08:17:08 +00:00
Prevent triggering twice
This commit is contained in:
parent
fe2ae16210
commit
4c33eba378
@ -27,13 +27,20 @@ def trigger(hass, config, action):
|
|||||||
# Get all entity ids
|
# Get all entity ids
|
||||||
all_entity_ids = hass.states.entity_ids()
|
all_entity_ids = hass.states.entity_ids()
|
||||||
|
|
||||||
# pylint: disable=unused-argument
|
# Local variable to keep track of if the action has already been triggered
|
||||||
|
already_triggered = False
|
||||||
|
|
||||||
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. """
|
||||||
|
nonlocal already_triggered
|
||||||
|
template_result = _check_template(hass, value_template)
|
||||||
|
|
||||||
# Check to see if template returns true
|
# Check to see if template returns true
|
||||||
if _check_template(hass, value_template):
|
if template_result and not already_triggered:
|
||||||
|
already_triggered = True
|
||||||
action()
|
action()
|
||||||
|
elif not template_result:
|
||||||
|
already_triggered = False
|
||||||
|
|
||||||
track_state_change(hass, all_entity_ids, state_automation_listener)
|
track_state_change(hass, all_entity_ids, state_automation_listener)
|
||||||
|
|
||||||
|
@ -274,15 +274,19 @@ class TestAutomationTemplate(unittest.TestCase):
|
|||||||
|
|
||||||
self.hass.states.set('test.entity', 'work')
|
self.hass.states.set('test.entity', 'work')
|
||||||
self.hass.pool.block_till_done()
|
self.hass.pool.block_till_done()
|
||||||
self.assertEqual(2, len(self.calls))
|
self.assertEqual(1, len(self.calls))
|
||||||
|
|
||||||
|
self.hass.states.set('test.entity', 'not_home')
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
self.assertEqual(1, len(self.calls))
|
||||||
|
|
||||||
self.hass.states.set('test.entity', 'world')
|
self.hass.states.set('test.entity', 'world')
|
||||||
self.hass.pool.block_till_done()
|
self.hass.pool.block_till_done()
|
||||||
self.assertEqual(2, len(self.calls))
|
self.assertEqual(1, len(self.calls))
|
||||||
|
|
||||||
self.hass.states.set('test.entity', 'home')
|
self.hass.states.set('test.entity', 'home')
|
||||||
self.hass.pool.block_till_done()
|
self.hass.pool.block_till_done()
|
||||||
self.assertEqual(3, len(self.calls))
|
self.assertEqual(2, len(self.calls))
|
||||||
|
|
||||||
def test_if_action(self):
|
def test_if_action(self):
|
||||||
automation.setup(self.hass, {
|
automation.setup(self.hass, {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user