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

View File

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