Fixed now => utcnow

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

No files matched your search

+32 -27
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))