2015-09-15 07:02:54 +00:00
|
|
|
"""
|
|
|
|
tests.components.automation.test_sun
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Tests sun automation.
|
|
|
|
"""
|
|
|
|
from datetime import datetime
|
|
|
|
import unittest
|
2015-09-16 03:18:24 +00:00
|
|
|
from unittest.mock import patch
|
2015-09-15 07:02:54 +00:00
|
|
|
|
|
|
|
import homeassistant.core as ha
|
|
|
|
from homeassistant.components import sun
|
|
|
|
import homeassistant.components.automation as automation
|
|
|
|
import homeassistant.util.dt as dt_util
|
|
|
|
|
|
|
|
from tests.common import fire_time_changed
|
|
|
|
|
|
|
|
|
|
|
|
class TestAutomationSun(unittest.TestCase):
|
|
|
|
""" Test the sun automation. """
|
|
|
|
|
|
|
|
def setUp(self): # pylint: disable=invalid-name
|
|
|
|
self.hass = ha.HomeAssistant()
|
|
|
|
self.hass.config.components.append('sun')
|
|
|
|
|
|
|
|
self.calls = []
|
|
|
|
|
|
|
|
def record_call(service):
|
|
|
|
self.calls.append(service)
|
|
|
|
|
|
|
|
self.hass.services.register('test', 'automation', record_call)
|
|
|
|
|
|
|
|
def tearDown(self): # pylint: disable=invalid-name
|
|
|
|
""" Stop down stuff we started. """
|
|
|
|
self.hass.stop()
|
|
|
|
|
|
|
|
def test_sunset_trigger(self):
|
|
|
|
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, {
|
|
|
|
sun.STATE_ATTR_NEXT_SETTING: '02:00:00 16-09-2015',
|
|
|
|
})
|
|
|
|
|
2015-09-16 03:18:24 +00:00
|
|
|
now = datetime(2015, 9, 15, 23, tzinfo=dt_util.UTC)
|
2015-09-15 07:02:54 +00:00
|
|
|
trigger_time = datetime(2015, 9, 16, 2, tzinfo=dt_util.UTC)
|
|
|
|
|
2015-09-16 03:18:24 +00:00
|
|
|
with patch('homeassistant.components.automation.sun.dt_util.utcnow',
|
|
|
|
return_value=now):
|
|
|
|
self.assertTrue(automation.setup(self.hass, {
|
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'sun',
|
|
|
|
'event': 'sunset',
|
|
|
|
},
|
|
|
|
'action': {
|
2015-09-19 15:43:56 +00:00
|
|
|
'service': 'test.automation',
|
2015-09-16 03:18:24 +00:00
|
|
|
}
|
2015-09-15 07:02:54 +00:00
|
|
|
}
|
2015-09-16 03:18:24 +00:00
|
|
|
}))
|
2015-09-15 07:02:54 +00:00
|
|
|
|
|
|
|
fire_time_changed(self.hass, trigger_time)
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(1, len(self.calls))
|
|
|
|
|
|
|
|
def test_sunrise_trigger(self):
|
|
|
|
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, {
|
|
|
|
sun.STATE_ATTR_NEXT_RISING: '14:00:00 16-09-2015',
|
|
|
|
})
|
|
|
|
|
2015-09-16 03:18:24 +00:00
|
|
|
now = datetime(2015, 9, 13, 23, tzinfo=dt_util.UTC)
|
2015-09-15 07:02:54 +00:00
|
|
|
trigger_time = datetime(2015, 9, 16, 14, tzinfo=dt_util.UTC)
|
|
|
|
|
2015-09-16 03:18:24 +00:00
|
|
|
with patch('homeassistant.components.automation.sun.dt_util.utcnow',
|
|
|
|
return_value=now):
|
|
|
|
self.assertTrue(automation.setup(self.hass, {
|
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'sun',
|
|
|
|
'event': 'sunrise',
|
|
|
|
},
|
|
|
|
'action': {
|
2015-09-19 15:43:56 +00:00
|
|
|
'service': 'test.automation',
|
2015-09-16 03:18:24 +00:00
|
|
|
}
|
2015-09-15 07:02:54 +00:00
|
|
|
}
|
2015-09-16 03:18:24 +00:00
|
|
|
}))
|
2015-09-15 07:02:54 +00:00
|
|
|
|
|
|
|
fire_time_changed(self.hass, trigger_time)
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(1, len(self.calls))
|
|
|
|
|
|
|
|
def test_sunset_trigger_with_offset(self):
|
|
|
|
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, {
|
|
|
|
sun.STATE_ATTR_NEXT_SETTING: '02:00:00 16-09-2015',
|
|
|
|
})
|
|
|
|
|
2015-09-16 03:18:24 +00:00
|
|
|
now = datetime(2015, 9, 15, 23, tzinfo=dt_util.UTC)
|
2015-09-15 07:02:54 +00:00
|
|
|
trigger_time = datetime(2015, 9, 16, 2, 30, tzinfo=dt_util.UTC)
|
|
|
|
|
2015-09-16 03:18:24 +00:00
|
|
|
with patch('homeassistant.components.automation.sun.dt_util.utcnow',
|
|
|
|
return_value=now):
|
|
|
|
self.assertTrue(automation.setup(self.hass, {
|
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'sun',
|
|
|
|
'event': 'sunset',
|
|
|
|
'offset': '0:30:00'
|
|
|
|
},
|
|
|
|
'action': {
|
2015-09-19 15:43:56 +00:00
|
|
|
'service': 'test.automation',
|
2015-09-16 03:18:24 +00:00
|
|
|
}
|
2015-09-15 07:02:54 +00:00
|
|
|
}
|
2015-09-16 03:18:24 +00:00
|
|
|
}))
|
2015-09-15 07:02:54 +00:00
|
|
|
|
|
|
|
fire_time_changed(self.hass, trigger_time)
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(1, len(self.calls))
|
|
|
|
|
|
|
|
def test_sunrise_trigger_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',
|
|
|
|
})
|
|
|
|
|
2015-09-16 03:18:24 +00:00
|
|
|
now = datetime(2015, 9, 13, 23, tzinfo=dt_util.UTC)
|
2015-09-15 07:02:54 +00:00
|
|
|
trigger_time = datetime(2015, 9, 16, 13, 30, tzinfo=dt_util.UTC)
|
|
|
|
|
2015-09-16 03:18:24 +00:00
|
|
|
with patch('homeassistant.components.automation.sun.dt_util.utcnow',
|
|
|
|
return_value=now):
|
|
|
|
self.assertTrue(automation.setup(self.hass, {
|
|
|
|
automation.DOMAIN: {
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'sun',
|
|
|
|
'event': 'sunrise',
|
|
|
|
'offset': '-0:30:00'
|
|
|
|
},
|
|
|
|
'action': {
|
2015-09-19 15:43:56 +00:00
|
|
|
'service': 'test.automation',
|
2015-09-16 03:18:24 +00:00
|
|
|
}
|
2015-09-15 07:02:54 +00:00
|
|
|
}
|
2015-09-16 03:18:24 +00:00
|
|
|
}))
|
2015-09-15 07:02:54 +00:00
|
|
|
|
|
|
|
fire_time_changed(self.hass, trigger_time)
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
self.assertEqual(1, len(self.calls))
|
2015-12-21 23:09:51 +00:00
|
|
|
|
|
|
|
def test_if_action_before_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, 10, 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',
|
|
|
|
},
|
|
|
|
'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)
|
|
|
|
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',
|
|
|
|
},
|
|
|
|
'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_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, 13, 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',
|
|
|
|
},
|
|
|
|
'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_during(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, 12, 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(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))
|