diff --git a/tests/test_component_device_scanner.py b/tests/test_component_device_scanner.py index 1accc1c5ff8..47ad6f7d798 100644 --- a/tests/test_component_device_scanner.py +++ b/tests/test_component_device_scanner.py @@ -117,9 +117,17 @@ class TestComponentsDeviceTracker(unittest.TestCase): dev3 = device_tracker.ENTITY_ID_FORMAT.format('DEV3') now = datetime.now() - nowAlmostMinGone = (now + device_tracker.TIME_DEVICE_NOT_FOUND - - timedelta(seconds=1)) - nowMinGone = nowAlmostMinGone + timedelta(seconds=2) + + # Device scanner scans every 12 seconds. We need to sync our times to + # be every 12 seconds or else the time_changed event will be ignored. + nowAlmostMinimumGone = now + device_tracker.TIME_DEVICE_NOT_FOUND + nowAlmostMinimumGone -= timedelta( + seconds=(nowAlmostMinimumGone.second % 12)) + + nowMinimumGone = now + device_tracker.TIME_DEVICE_NOT_FOUND + nowMinimumGone += timedelta(seconds=12-(nowMinimumGone.second % 12)) + + print(now, nowAlmostMinimumGone, nowMinimumGone) # Test initial is correct self.assertTrue(device_tracker.is_on(self.hass)) @@ -168,7 +176,7 @@ class TestComponentsDeviceTracker(unittest.TestCase): # Test if device leaves what happens, test the time span self.hass.bus.fire( - ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: nowAlmostMinGone}) + ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: nowAlmostMinimumGone}) self.hass.pool.block_till_done() @@ -179,7 +187,8 @@ class TestComponentsDeviceTracker(unittest.TestCase): self.assertTrue(device_tracker.is_on(self.hass, dev3)) # Now test if gone for longer then error margin - self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: nowMinGone}) + self.hass.bus.fire( + ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: nowMinimumGone}) self.hass.pool.block_till_done()