From 6918993c75f86f0cf10582d3ea7e80d224f223fd Mon Sep 17 00:00:00 2001 From: Adam Mills Date: Fri, 12 Jan 2018 01:06:09 -0500 Subject: [PATCH] Fix state for trigger with forced updates (#11595) --- homeassistant/components/automation/state.py | 2 +- tests/components/automation/test_state.py | 34 ++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/automation/state.py b/homeassistant/components/automation/state.py index e4d096d35fd..9243f960850 100644 --- a/homeassistant/components/automation/state.py +++ b/homeassistant/components/automation/state.py @@ -55,7 +55,7 @@ def async_trigger(hass, config, action): # Ignore changes to state attributes if from/to is in use if (not match_all and from_s is not None and to_s is not None and - from_s.last_changed == to_s.last_changed): + from_s.state == to_s.state): return if not time_delta: diff --git a/tests/components/automation/test_state.py b/tests/components/automation/test_state.py index b1ee0841e2d..bf54d24492a 100644 --- a/tests/components/automation/test_state.py +++ b/tests/components/automation/test_state.py @@ -409,6 +409,40 @@ class TestAutomationState(unittest.TestCase): self.hass.block_till_done() self.assertEqual(1, len(self.calls)) + def test_if_fires_on_entity_change_with_for_multiple_force_update(self): + """Test for firing on entity change with for and force update.""" + assert setup_component(self.hass, automation.DOMAIN, { + automation.DOMAIN: { + 'trigger': { + 'platform': 'state', + 'entity_id': 'test.force_entity', + 'to': 'world', + 'for': { + 'seconds': 5 + }, + }, + 'action': { + 'service': 'test.automation' + } + } + }) + + utcnow = dt_util.utcnow() + with patch('homeassistant.core.dt_util.utcnow') as mock_utcnow: + mock_utcnow.return_value = utcnow + self.hass.states.set('test.force_entity', 'world', None, True) + self.hass.block_till_done() + for _ in range(0, 4): + mock_utcnow.return_value += timedelta(seconds=1) + fire_time_changed(self.hass, mock_utcnow.return_value) + self.hass.states.set('test.force_entity', 'world', None, True) + self.hass.block_till_done() + self.assertEqual(0, len(self.calls)) + mock_utcnow.return_value += timedelta(seconds=4) + fire_time_changed(self.hass, mock_utcnow.return_value) + self.hass.block_till_done() + self.assertEqual(1, len(self.calls)) + def test_if_fires_on_entity_change_with_for(self): """Test for firing on entity change with for.""" assert setup_component(self.hass, automation.DOMAIN, {