Fixed now => utcnow

Fixed added time patch to unittest
This commit is contained in:
Stefan Jonasson 2016-02-20 22:15:49 +01:00
parent ff6e071dff
commit f3c95adaca
2 changed files with 42 additions and 37 deletions

View file

@ -90,7 +90,7 @@ def trigger(hass, config, action):
EVENT_STATE_CHANGED, for_state_listener)
if CONF_FOR in config:
target_tm = dt_util.now() + time_delta
target_tm = dt_util.utcnow() + time_delta
for_time_listener = track_point_in_time(
hass, state_for_listener, target_tm)
for_state_listener = track_state_change(
@ -112,7 +112,7 @@ def if_action(hass, config):
if CONF_FOR in config:
time_delta = get_time_config(config)
if time_delta is False:
if not time_delta:
return False
if entity_id is None or state is None:
@ -125,13 +125,13 @@ def if_action(hass, config):
def if_state():
""" Test if condition. """
if hass.states.is_state(entity_id, state):
if CONF_FOR in config:
target_tm = dt_util.now() - time_delta
return target_tm > hass.states.get(entity_id).last_changed
else:
return True
else:
return False
is_state = hass.states.is_state(entity_id, state)
if CONF_FOR not in config:
return is_state
target_tm = dt_util.utcnow() - time_delta
return (is_state and
target_tm > hass.states.get(entity_id).last_changed)
return if_state

View file

@ -6,6 +6,7 @@ Tests state automation.
"""
import unittest
from datetime import timedelta
from unittest.mock import patch
import time
import homeassistant.util.dt as dt_util
import homeassistant.components.automation as automation
@ -425,35 +426,39 @@ class TestAutomationState(unittest.TestCase):
self.assertEqual(1, len(self.calls))
def test_if_fires_on_for_condition(self):
self.hass.states.set('test.entity', 'on')
self.assertTrue(automation.setup(self.hass, {
automation.DOMAIN: {
'trigger': {
'platform': 'event',
'event_type': 'test_event',
},
'condition': {
'platform': 'state',
'entity_id': 'test.entity',
'state': 'on',
'for': {
'seconds': 3
point1 = dt_util.utcnow()
point2 = point1 + timedelta(seconds=10)
with patch('homeassistant.core.dt_util.utcnow') as mock_utcnow:
mock_utcnow.return_value = point1
self.hass.states.set('test.entity', 'on')
self.assertTrue(automation.setup(self.hass, {
automation.DOMAIN: {
'trigger': {
'platform': 'event',
'event_type': 'test_event',
},
'condition': {
'platform': 'state',
'entity_id': 'test.entity',
'state': 'on',
'for': {
'seconds': 5
},
},
},
'action': {
'service': 'test.automation'
'action': {
'service': 'test.automation'
}
}
}
}))
}))
# not enough time has passed
self.hass.bus.fire('test_event')
self.hass.pool.block_till_done()
self.assertEqual(0, len(self.calls))
# not enough time has passed
self.hass.bus.fire('test_event')
self.hass.pool.block_till_done()
self.assertEqual(0, len(self.calls))
# wait until we have passed the condition
time.sleep(4)
self.hass.bus.fire('test_event')
self.hass.pool.block_till_done()
self.assertEqual(1, len(self.calls))
# Time travel 10 secs into the future
mock_utcnow.return_value = point2
self.hass.bus.fire('test_event')
self.hass.pool.block_till_done()
self.assertEqual(1, len(self.calls))