mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
ps - clean up sun automation tests
This commit is contained in:
parent
2606e4d641
commit
add24915a3
@ -93,15 +93,10 @@ def if_action(hass, config):
|
|||||||
else:
|
else:
|
||||||
after_func = lambda: sun.next_setting_utc(hass) + after_offset
|
after_func = lambda: sun.next_setting_utc(hass) + after_offset
|
||||||
|
|
||||||
# This is needed for testing
|
|
||||||
time_func = dt_util.utcnow
|
|
||||||
|
|
||||||
def time_if():
|
def time_if():
|
||||||
""" Validate time based if-condition """
|
""" Validate time based if-condition """
|
||||||
|
|
||||||
# This is needed for testing.
|
now = dt_util.utcnow()
|
||||||
nonlocal time_func
|
|
||||||
now = time_func()
|
|
||||||
before = before_func()
|
before = before_func()
|
||||||
after = after_func()
|
after = after_func()
|
||||||
|
|
||||||
|
@ -140,125 +140,147 @@ class TestAutomationSun(unittest.TestCase):
|
|||||||
self.hass.pool.block_till_done()
|
self.hass.pool.block_till_done()
|
||||||
self.assertEqual(1, len(self.calls))
|
self.assertEqual(1, len(self.calls))
|
||||||
|
|
||||||
def test_if_action_before_before(self):
|
def test_if_action_before(self):
|
||||||
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, {
|
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, {
|
||||||
sun.STATE_ATTR_NEXT_RISING: '14:00:00 16-09-2015',
|
sun.STATE_ATTR_NEXT_RISING: '14:00:00 16-09-2015',
|
||||||
})
|
})
|
||||||
|
|
||||||
now = datetime(2015, 9, 16, 10, tzinfo=dt_util.UTC)
|
automation.setup(self.hass, {
|
||||||
entity_id = 'domain.test_entity'
|
automation.DOMAIN: {
|
||||||
|
'trigger': {
|
||||||
with patch('homeassistant.components.automation.sun.dt_util.utcnow',
|
'platform': 'event',
|
||||||
return_value=now):
|
'event_type': 'test_event',
|
||||||
automation.setup(self.hass, {
|
},
|
||||||
automation.DOMAIN: {
|
'condition': {
|
||||||
'trigger': {
|
'platform': 'sun',
|
||||||
'platform': 'event',
|
'before': 'sunrise',
|
||||||
'event_type': 'test_event',
|
},
|
||||||
},
|
'action': {
|
||||||
'condition': {
|
'service': 'test.automation'
|
||||||
'platform': 'sun',
|
|
||||||
'before': 'sunrise',
|
|
||||||
},
|
|
||||||
'action': {
|
|
||||||
'service': 'test.automation'
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
|
||||||
self.hass.bus.fire('test_event')
|
|
||||||
self.hass.pool.block_till_done()
|
|
||||||
self.assertEqual(1, len(self.calls))
|
|
||||||
|
|
||||||
def test_if_action_after_before(self):
|
|
||||||
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, {
|
|
||||||
sun.STATE_ATTR_NEXT_RISING: '14:00:00 16-09-2015',
|
|
||||||
})
|
})
|
||||||
|
|
||||||
now = datetime(2015, 9, 16, 15, tzinfo=dt_util.UTC)
|
now = datetime(2015, 9, 16, 15, tzinfo=dt_util.UTC)
|
||||||
entity_id = 'domain.test_entity'
|
|
||||||
|
|
||||||
with patch('homeassistant.components.automation.sun.dt_util.utcnow',
|
with patch('homeassistant.components.automation.sun.dt_util.utcnow',
|
||||||
return_value=now):
|
return_value=now):
|
||||||
automation.setup(self.hass, {
|
self.hass.bus.fire('test_event')
|
||||||
automation.DOMAIN: {
|
self.hass.pool.block_till_done()
|
||||||
'trigger': {
|
self.assertEqual(0, len(self.calls))
|
||||||
'platform': 'event',
|
|
||||||
'event_type': 'test_event',
|
|
||||||
},
|
|
||||||
'condition': {
|
|
||||||
'platform': 'sun',
|
|
||||||
'before': 'sunrise',
|
|
||||||
},
|
|
||||||
'action': {
|
|
||||||
'service': 'test.automation'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
self.hass.bus.fire('test_event')
|
now = datetime(2015, 9, 16, 10, tzinfo=dt_util.UTC)
|
||||||
self.hass.pool.block_till_done()
|
with patch('homeassistant.components.automation.sun.dt_util.utcnow',
|
||||||
self.assertEqual(0, len(self.calls))
|
return_value=now):
|
||||||
|
self.hass.bus.fire('test_event')
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
self.assertEqual(1, len(self.calls))
|
||||||
|
|
||||||
def test_if_action_before_after(self):
|
def test_if_action_after(self):
|
||||||
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, {
|
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, {
|
||||||
sun.STATE_ATTR_NEXT_RISING: '14:00:00 16-09-2015',
|
sun.STATE_ATTR_NEXT_RISING: '14:00:00 16-09-2015',
|
||||||
})
|
})
|
||||||
|
|
||||||
|
automation.setup(self.hass, {
|
||||||
|
automation.DOMAIN: {
|
||||||
|
'trigger': {
|
||||||
|
'platform': 'event',
|
||||||
|
'event_type': 'test_event',
|
||||||
|
},
|
||||||
|
'condition': {
|
||||||
|
'platform': 'sun',
|
||||||
|
'after': 'sunrise',
|
||||||
|
},
|
||||||
|
'action': {
|
||||||
|
'service': 'test.automation'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
now = datetime(2015, 9, 16, 13, tzinfo=dt_util.UTC)
|
now = datetime(2015, 9, 16, 13, tzinfo=dt_util.UTC)
|
||||||
entity_id = 'domain.test_entity'
|
|
||||||
|
|
||||||
with patch('homeassistant.components.automation.sun.dt_util.utcnow',
|
with patch('homeassistant.components.automation.sun.dt_util.utcnow',
|
||||||
return_value=now):
|
return_value=now):
|
||||||
automation.setup(self.hass, {
|
self.hass.bus.fire('test_event')
|
||||||
automation.DOMAIN: {
|
self.hass.pool.block_till_done()
|
||||||
'trigger': {
|
self.assertEqual(0, len(self.calls))
|
||||||
'platform': 'event',
|
|
||||||
'event_type': 'test_event',
|
|
||||||
},
|
|
||||||
'condition': {
|
|
||||||
'platform': 'sun',
|
|
||||||
'after': 'sunrise',
|
|
||||||
},
|
|
||||||
'action': {
|
|
||||||
'service': 'test.automation'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
self.hass.bus.fire('test_event')
|
|
||||||
self.hass.pool.block_till_done()
|
|
||||||
self.assertEqual(0, len(self.calls))
|
|
||||||
|
|
||||||
def test_if_action_after_after(self):
|
|
||||||
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, {
|
|
||||||
sun.STATE_ATTR_NEXT_SETTING: '14:00:00 16-09-2015',
|
|
||||||
})
|
|
||||||
|
|
||||||
now = datetime(2015, 9, 16, 15, tzinfo=dt_util.UTC)
|
now = datetime(2015, 9, 16, 15, tzinfo=dt_util.UTC)
|
||||||
entity_id = 'domain.test_entity'
|
|
||||||
|
|
||||||
with patch('homeassistant.components.automation.sun.dt_util.utcnow',
|
with patch('homeassistant.components.automation.sun.dt_util.utcnow',
|
||||||
return_value=now):
|
return_value=now):
|
||||||
automation.setup(self.hass, {
|
self.hass.bus.fire('test_event')
|
||||||
automation.DOMAIN: {
|
self.hass.pool.block_till_done()
|
||||||
'trigger': {
|
self.assertEqual(1, len(self.calls))
|
||||||
'platform': 'event',
|
|
||||||
'event_type': 'test_event',
|
|
||||||
},
|
|
||||||
'condition': {
|
|
||||||
'platform': 'sun',
|
|
||||||
'after': 'sunset',
|
|
||||||
},
|
|
||||||
'action': {
|
|
||||||
'service': 'test.automation'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
self.hass.bus.fire('test_event')
|
def test_if_action_before_with_offset(self):
|
||||||
self.hass.pool.block_till_done()
|
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, {
|
||||||
self.assertEqual(1, len(self.calls))
|
sun.STATE_ATTR_NEXT_RISING: '14:00:00 16-09-2015',
|
||||||
|
})
|
||||||
|
|
||||||
|
automation.setup(self.hass, {
|
||||||
|
automation.DOMAIN: {
|
||||||
|
'trigger': {
|
||||||
|
'platform': 'event',
|
||||||
|
'event_type': 'test_event',
|
||||||
|
},
|
||||||
|
'condition': {
|
||||||
|
'platform': 'sun',
|
||||||
|
'before': 'sunrise',
|
||||||
|
'before_offset': '+1:00:00'
|
||||||
|
},
|
||||||
|
'action': {
|
||||||
|
'service': 'test.automation'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
now = datetime(2015, 9, 16, 15, 1, tzinfo=dt_util.UTC)
|
||||||
|
with patch('homeassistant.components.automation.sun.dt_util.utcnow',
|
||||||
|
return_value=now):
|
||||||
|
self.hass.bus.fire('test_event')
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
self.assertEqual(0, len(self.calls))
|
||||||
|
|
||||||
|
now = datetime(2015, 9, 16, 15, tzinfo=dt_util.UTC)
|
||||||
|
with patch('homeassistant.components.automation.sun.dt_util.utcnow',
|
||||||
|
return_value=now):
|
||||||
|
self.hass.bus.fire('test_event')
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
self.assertEqual(1, len(self.calls))
|
||||||
|
|
||||||
|
def test_if_action_after_with_offset(self):
|
||||||
|
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, {
|
||||||
|
sun.STATE_ATTR_NEXT_RISING: '14:00:00 16-09-2015',
|
||||||
|
})
|
||||||
|
|
||||||
|
automation.setup(self.hass, {
|
||||||
|
automation.DOMAIN: {
|
||||||
|
'trigger': {
|
||||||
|
'platform': 'event',
|
||||||
|
'event_type': 'test_event',
|
||||||
|
},
|
||||||
|
'condition': {
|
||||||
|
'platform': 'sun',
|
||||||
|
'after': 'sunrise',
|
||||||
|
'after_offset': '+1:00:00'
|
||||||
|
},
|
||||||
|
'action': {
|
||||||
|
'service': 'test.automation'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
now = datetime(2015, 9, 16, 14, 59, tzinfo=dt_util.UTC)
|
||||||
|
with patch('homeassistant.components.automation.sun.dt_util.utcnow',
|
||||||
|
return_value=now):
|
||||||
|
self.hass.bus.fire('test_event')
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
self.assertEqual(0, len(self.calls))
|
||||||
|
|
||||||
|
now = datetime(2015, 9, 16, 15, tzinfo=dt_util.UTC)
|
||||||
|
with patch('homeassistant.components.automation.sun.dt_util.utcnow',
|
||||||
|
return_value=now):
|
||||||
|
self.hass.bus.fire('test_event')
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
self.assertEqual(1, len(self.calls))
|
||||||
|
|
||||||
def test_if_action_before_and_after_during(self):
|
def test_if_action_before_and_after_during(self):
|
||||||
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, {
|
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, {
|
||||||
@ -266,154 +288,40 @@ class TestAutomationSun(unittest.TestCase):
|
|||||||
sun.STATE_ATTR_NEXT_SETTING: '15:00:00 16-09-2015',
|
sun.STATE_ATTR_NEXT_SETTING: '15:00:00 16-09-2015',
|
||||||
})
|
})
|
||||||
|
|
||||||
|
automation.setup(self.hass, {
|
||||||
|
automation.DOMAIN: {
|
||||||
|
'trigger': {
|
||||||
|
'platform': 'event',
|
||||||
|
'event_type': 'test_event',
|
||||||
|
},
|
||||||
|
'condition': {
|
||||||
|
'platform': 'sun',
|
||||||
|
'after': 'sunrise',
|
||||||
|
'before': 'sunset'
|
||||||
|
},
|
||||||
|
'action': {
|
||||||
|
'service': 'test.automation'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
now = datetime(2015, 9, 16, 9, 59, tzinfo=dt_util.UTC)
|
||||||
|
with patch('homeassistant.components.automation.sun.dt_util.utcnow',
|
||||||
|
return_value=now):
|
||||||
|
self.hass.bus.fire('test_event')
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
self.assertEqual(0, len(self.calls))
|
||||||
|
|
||||||
|
now = datetime(2015, 9, 16, 15, 1, tzinfo=dt_util.UTC)
|
||||||
|
with patch('homeassistant.components.automation.sun.dt_util.utcnow',
|
||||||
|
return_value=now):
|
||||||
|
self.hass.bus.fire('test_event')
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
self.assertEqual(0, len(self.calls))
|
||||||
|
|
||||||
now = datetime(2015, 9, 16, 12, tzinfo=dt_util.UTC)
|
now = datetime(2015, 9, 16, 12, tzinfo=dt_util.UTC)
|
||||||
entity_id = 'domain.test_entity'
|
|
||||||
|
|
||||||
with patch('homeassistant.components.automation.sun.dt_util.utcnow',
|
with patch('homeassistant.components.automation.sun.dt_util.utcnow',
|
||||||
return_value=now):
|
return_value=now):
|
||||||
automation.setup(self.hass, {
|
self.hass.bus.fire('test_event')
|
||||||
automation.DOMAIN: {
|
self.hass.pool.block_till_done()
|
||||||
'trigger': {
|
self.assertEqual(1, len(self.calls))
|
||||||
'platform': 'event',
|
|
||||||
'event_type': 'test_event',
|
|
||||||
},
|
|
||||||
'condition': {
|
|
||||||
'platform': 'sun',
|
|
||||||
'after': 'sunrise',
|
|
||||||
'before': 'sunset'
|
|
||||||
},
|
|
||||||
'action': {
|
|
||||||
'service': 'test.automation'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
self.hass.bus.fire('test_event')
|
|
||||||
self.hass.pool.block_till_done()
|
|
||||||
self.assertEqual(1, len(self.calls))
|
|
||||||
|
|
||||||
def test_if_action_before_and_after_before(self):
|
|
||||||
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, {
|
|
||||||
sun.STATE_ATTR_NEXT_RISING: '10:00:00 16-09-2015',
|
|
||||||
sun.STATE_ATTR_NEXT_SETTING: '15:00:00 16-09-2015',
|
|
||||||
})
|
|
||||||
|
|
||||||
now = datetime(2015, 9, 16, 8, tzinfo=dt_util.UTC)
|
|
||||||
entity_id = 'domain.test_entity'
|
|
||||||
|
|
||||||
with patch('homeassistant.components.automation.sun.dt_util.utcnow',
|
|
||||||
return_value=now):
|
|
||||||
automation.setup(self.hass, {
|
|
||||||
automation.DOMAIN: {
|
|
||||||
'trigger': {
|
|
||||||
'platform': 'event',
|
|
||||||
'event_type': 'test_event',
|
|
||||||
},
|
|
||||||
'condition': {
|
|
||||||
'platform': 'sun',
|
|
||||||
'after': 'sunrise',
|
|
||||||
'before': 'sunset'
|
|
||||||
},
|
|
||||||
'action': {
|
|
||||||
'service': 'test.automation'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
self.hass.bus.fire('test_event')
|
|
||||||
self.hass.pool.block_till_done()
|
|
||||||
self.assertEqual(0, len(self.calls))
|
|
||||||
|
|
||||||
def test_if_action_before_and_after_after(self):
|
|
||||||
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, {
|
|
||||||
sun.STATE_ATTR_NEXT_RISING: '10:00:00 16-09-2015',
|
|
||||||
sun.STATE_ATTR_NEXT_SETTING: '15:00:00 16-09-2015',
|
|
||||||
})
|
|
||||||
|
|
||||||
now = datetime(2015, 9, 16, 16, tzinfo=dt_util.UTC)
|
|
||||||
entity_id = 'domain.test_entity'
|
|
||||||
|
|
||||||
with patch('homeassistant.components.automation.sun.dt_util.utcnow',
|
|
||||||
return_value=now):
|
|
||||||
automation.setup(self.hass, {
|
|
||||||
automation.DOMAIN: {
|
|
||||||
'trigger': {
|
|
||||||
'platform': 'event',
|
|
||||||
'event_type': 'test_event',
|
|
||||||
},
|
|
||||||
'condition': {
|
|
||||||
'platform': 'sun',
|
|
||||||
'after': 'sunrise',
|
|
||||||
'before': 'sunset'
|
|
||||||
},
|
|
||||||
'action': {
|
|
||||||
'service': 'test.automation'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
self.hass.bus.fire('test_event')
|
|
||||||
self.hass.pool.block_till_done()
|
|
||||||
self.assertEqual(0, len(self.calls))
|
|
||||||
|
|
||||||
def test_if_action_offset_before(self):
|
|
||||||
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, {
|
|
||||||
sun.STATE_ATTR_NEXT_RISING: '14:00:00 16-09-2015',
|
|
||||||
})
|
|
||||||
|
|
||||||
now = datetime(2015, 9, 16, 14, 59, tzinfo=dt_util.UTC)
|
|
||||||
entity_id = 'domain.test_entity'
|
|
||||||
|
|
||||||
with patch('homeassistant.components.automation.sun.dt_util.utcnow',
|
|
||||||
return_value=now):
|
|
||||||
automation.setup(self.hass, {
|
|
||||||
automation.DOMAIN: {
|
|
||||||
'trigger': {
|
|
||||||
'platform': 'event',
|
|
||||||
'event_type': 'test_event',
|
|
||||||
},
|
|
||||||
'condition': {
|
|
||||||
'platform': 'sun',
|
|
||||||
'before': 'sunrise',
|
|
||||||
'before_offset': '+1:00:00'
|
|
||||||
},
|
|
||||||
'action': {
|
|
||||||
'service': 'test.automation'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
self.hass.bus.fire('test_event')
|
|
||||||
self.hass.pool.block_till_done()
|
|
||||||
self.assertEqual(1, len(self.calls))
|
|
||||||
|
|
||||||
def test_if_action_offset_after(self):
|
|
||||||
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, {
|
|
||||||
sun.STATE_ATTR_NEXT_RISING: '14:00:00 16-09-2015',
|
|
||||||
})
|
|
||||||
|
|
||||||
now = datetime(2015, 9, 16, 15, tzinfo=dt_util.UTC)
|
|
||||||
entity_id = 'domain.test_entity'
|
|
||||||
|
|
||||||
with patch('homeassistant.components.automation.sun.dt_util.utcnow',
|
|
||||||
return_value=now):
|
|
||||||
automation.setup(self.hass, {
|
|
||||||
automation.DOMAIN: {
|
|
||||||
'trigger': {
|
|
||||||
'platform': 'event',
|
|
||||||
'event_type': 'test_event',
|
|
||||||
},
|
|
||||||
'condition': {
|
|
||||||
'platform': 'sun',
|
|
||||||
'after': 'sunrise',
|
|
||||||
'after_offset': '+1:00:00'
|
|
||||||
},
|
|
||||||
'action': {
|
|
||||||
'service': 'test.automation'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
self.hass.bus.fire('test_event')
|
|
||||||
self.hass.pool.block_till_done()
|
|
||||||
self.assertEqual(1, len(self.calls))
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user