Prevent triggering twice

pull/765/head
Philip Lundrigan 2015-12-16 15:07:14 -07:00
parent fe2ae16210
commit 4c33eba378
2 changed files with 16 additions and 5 deletions
homeassistant/components/automation
tests/components/automation

View File

@ -27,13 +27,20 @@ def trigger(hass, config, action):
# Get all 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):
""" Listens for state changes and calls action. """
nonlocal already_triggered
template_result = _check_template(hass, value_template)
# Check to see if template returns true
if _check_template(hass, value_template):
if template_result and not already_triggered:
already_triggered = True
action()
elif not template_result:
already_triggered = False
track_state_change(hass, all_entity_ids, state_automation_listener)

View File

@ -274,15 +274,19 @@ class TestAutomationTemplate(unittest.TestCase):
self.hass.states.set('test.entity', 'work')
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.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.pool.block_till_done()
self.assertEqual(3, len(self.calls))
self.assertEqual(2, len(self.calls))
def test_if_action(self):
automation.setup(self.hass, {