core/tests/components/automation/test_sun.py

392 lines
14 KiB
Python
Raw Normal View History

2016-03-09 09:25:50 +00:00
"""The tests for the sun automation."""
2015-09-15 07:02:54 +00:00
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
from homeassistant.bootstrap import setup_component
2015-09-15 07:02:54 +00:00
from homeassistant.components import sun
import homeassistant.components.automation as automation
import homeassistant.util.dt as dt_util
2016-02-14 23:08:23 +00:00
from tests.common import fire_time_changed, get_test_home_assistant
2015-09-15 07:02:54 +00:00
class TestAutomationSun(unittest.TestCase):
2016-03-09 09:25:50 +00:00
"""Test the sun automation."""
2015-09-15 07:02:54 +00:00
def setUp(self): # pylint: disable=invalid-name
2016-03-09 09:25:50 +00:00
"""Setup things to be run when tests are started."""
2016-02-14 23:08:23 +00:00
self.hass = get_test_home_assistant()
self.hass.config.components.append('group')
2015-09-15 07:02:54 +00:00
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
2016-03-09 09:25:50 +00:00
"""Stop everything that was started."""
2015-09-15 07:02:54 +00:00
self.hass.stop()
def test_sunset_trigger(self):
2016-03-09 09:25:50 +00:00
"""Test the sunset trigger."""
2015-09-15 07:02:54 +00:00
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, {
2016-04-16 07:55:35 +00:00
sun.STATE_ATTR_NEXT_SETTING: '2015-09-16T02:00:00Z',
2015-09-15 07:02:54 +00:00
})
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)
with patch('homeassistant.util.dt.utcnow',
2015-09-16 03:18:24 +00:00
return_value=now):
setup_component(self.hass, automation.DOMAIN, {
2015-09-16 03:18:24 +00:00
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-15 07:02:54 +00:00
automation.turn_off(self.hass)
self.hass.block_till_done()
fire_time_changed(self.hass, trigger_time)
self.hass.block_till_done()
self.assertEqual(0, len(self.calls))
with patch('homeassistant.util.dt.utcnow',
return_value=now):
automation.turn_on(self.hass)
self.hass.block_till_done()
2015-09-15 07:02:54 +00:00
fire_time_changed(self.hass, trigger_time)
self.hass.block_till_done()
2015-09-15 07:02:54 +00:00
self.assertEqual(1, len(self.calls))
def test_sunrise_trigger(self):
2016-03-09 09:25:50 +00:00
"""Test the sunrise trigger."""
2015-09-15 07:02:54 +00:00
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, {
2016-04-16 07:55:35 +00:00
sun.STATE_ATTR_NEXT_RISING: '2015-09-16T14:00:00Z',
2015-09-15 07:02:54 +00:00
})
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)
with patch('homeassistant.util.dt.utcnow',
2015-09-16 03:18:24 +00:00
return_value=now):
setup_component(self.hass, automation.DOMAIN, {
2015-09-16 03:18:24 +00:00
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-15 07:02:54 +00:00
fire_time_changed(self.hass, trigger_time)
self.hass.block_till_done()
2015-09-15 07:02:54 +00:00
self.assertEqual(1, len(self.calls))
def test_sunset_trigger_with_offset(self):
2016-03-09 09:25:50 +00:00
"""Test the sunset trigger with offset."""
2015-09-15 07:02:54 +00:00
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, {
2016-04-16 07:55:35 +00:00
sun.STATE_ATTR_NEXT_SETTING: '2015-09-16T02:00:00Z',
2015-09-15 07:02:54 +00:00
})
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)
with patch('homeassistant.util.dt.utcnow',
2015-09-16 03:18:24 +00:00
return_value=now):
setup_component(self.hass, automation.DOMAIN, {
2015-09-16 03:18:24 +00:00
automation.DOMAIN: {
'trigger': {
'platform': 'sun',
'event': 'sunset',
'offset': '0:30:00'
},
'action': {
2015-09-19 15:43:56 +00:00
'service': 'test.automation',
'data_template': {
'some':
'{{ trigger.%s }}' % '}} - {{ trigger.'.join((
'platform', 'event', 'offset'))
},
2015-09-16 03:18:24 +00:00
}
2015-09-15 07:02:54 +00:00
}
})
2015-09-15 07:02:54 +00:00
fire_time_changed(self.hass, trigger_time)
self.hass.block_till_done()
2015-09-15 07:02:54 +00:00
self.assertEqual(1, len(self.calls))
self.assertEqual('sun - sunset - 0:30:00', self.calls[0].data['some'])
2015-09-15 07:02:54 +00:00
def test_sunrise_trigger_with_offset(self):
2016-03-09 09:25:50 +00:00
"""Test the runrise trigger with offset."""
2015-09-15 07:02:54 +00:00
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, {
2016-04-16 07:55:35 +00:00
sun.STATE_ATTR_NEXT_RISING: '2015-09-16T14:00:00Z',
2015-09-15 07:02:54 +00:00
})
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)
with patch('homeassistant.util.dt.utcnow',
2015-09-16 03:18:24 +00:00
return_value=now):
setup_component(self.hass, automation.DOMAIN, {
2015-09-16 03:18:24 +00:00
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-15 07:02:54 +00:00
fire_time_changed(self.hass, trigger_time)
self.hass.block_till_done()
2015-09-15 07:02:54 +00:00
self.assertEqual(1, len(self.calls))
2015-12-21 23:09:51 +00:00
2015-12-27 01:48:20 +00:00
def test_if_action_before(self):
2016-03-09 09:25:50 +00:00
"""Test if action was before."""
2015-12-21 23:09:51 +00:00
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, {
2016-04-16 07:55:35 +00:00
sun.STATE_ATTR_NEXT_RISING: '2015-09-16T14:00:00Z',
2015-12-21 23:09:51 +00:00
})
setup_component(self.hass, automation.DOMAIN, {
2015-12-27 01:48:20 +00:00
automation.DOMAIN: {
'trigger': {
'platform': 'event',
'event_type': 'test_event',
},
'condition': {
2016-10-01 08:22:13 +00:00
'condition': 'sun',
2015-12-27 01:48:20 +00:00
'before': 'sunrise',
},
'action': {
'service': 'test.automation'
2015-12-21 23:09:51 +00:00
}
2015-12-27 01:48:20 +00:00
}
2015-12-21 23:09:51 +00:00
})
now = datetime(2015, 9, 16, 15, tzinfo=dt_util.UTC)
with patch('homeassistant.util.dt.now',
2015-12-21 23:09:51 +00:00
return_value=now):
2015-12-27 01:48:20 +00:00
self.hass.bus.fire('test_event')
self.hass.block_till_done()
2015-12-27 01:48:20 +00:00
self.assertEqual(0, len(self.calls))
2015-12-21 23:09:51 +00:00
2015-12-27 01:48:20 +00:00
now = datetime(2015, 9, 16, 10, tzinfo=dt_util.UTC)
with patch('homeassistant.util.dt.now',
2015-12-27 01:48:20 +00:00
return_value=now):
self.hass.bus.fire('test_event')
self.hass.block_till_done()
2015-12-27 01:48:20 +00:00
self.assertEqual(1, len(self.calls))
2015-12-21 23:09:51 +00:00
2015-12-27 01:48:20 +00:00
def test_if_action_after(self):
2016-03-09 09:25:50 +00:00
"""Test if action was after."""
2015-12-21 23:09:51 +00:00
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, {
2016-04-16 07:55:35 +00:00
sun.STATE_ATTR_NEXT_RISING: '2015-09-16T14:00:00Z',
2015-12-21 23:09:51 +00:00
})
setup_component(self.hass, automation.DOMAIN, {
2015-12-27 01:48:20 +00:00
automation.DOMAIN: {
'trigger': {
'platform': 'event',
'event_type': 'test_event',
},
'condition': {
2016-10-01 08:22:13 +00:00
'condition': 'sun',
2015-12-27 01:48:20 +00:00
'after': 'sunrise',
},
'action': {
'service': 'test.automation'
}
}
})
2015-12-21 23:09:51 +00:00
2015-12-27 01:48:20 +00:00
now = datetime(2015, 9, 16, 13, tzinfo=dt_util.UTC)
with patch('homeassistant.util.dt.now',
2015-12-21 23:09:51 +00:00
return_value=now):
2015-12-27 01:48:20 +00:00
self.hass.bus.fire('test_event')
self.hass.block_till_done()
2015-12-27 01:48:20 +00:00
self.assertEqual(0, len(self.calls))
2015-12-21 23:28:26 +00:00
now = datetime(2015, 9, 16, 15, tzinfo=dt_util.UTC)
with patch('homeassistant.util.dt.now',
2015-12-21 23:28:26 +00:00
return_value=now):
2015-12-27 01:48:20 +00:00
self.hass.bus.fire('test_event')
self.hass.block_till_done()
2015-12-27 01:48:20 +00:00
self.assertEqual(1, len(self.calls))
2015-12-21 23:28:26 +00:00
2015-12-27 01:48:20 +00:00
def test_if_action_before_with_offset(self):
2016-03-09 09:25:50 +00:00
"""Test if action was before offset."""
2015-12-21 23:09:51 +00:00
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, {
2016-04-16 07:55:35 +00:00
sun.STATE_ATTR_NEXT_RISING: '2015-09-16T14:00:00Z',
2015-12-21 23:09:51 +00:00
})
setup_component(self.hass, automation.DOMAIN, {
2015-12-27 01:48:20 +00:00
automation.DOMAIN: {
'trigger': {
'platform': 'event',
'event_type': 'test_event',
},
'condition': {
2016-10-01 08:22:13 +00:00
'condition': 'sun',
2015-12-27 01:48:20 +00:00
'before': 'sunrise',
'before_offset': '+1:00:00'
},
'action': {
'service': 'test.automation'
}
}
})
2015-12-21 23:09:51 +00:00
2015-12-27 01:48:20 +00:00
now = datetime(2015, 9, 16, 15, 1, tzinfo=dt_util.UTC)
with patch('homeassistant.util.dt.now',
2015-12-21 23:09:51 +00:00
return_value=now):
2015-12-27 01:48:20 +00:00
self.hass.bus.fire('test_event')
self.hass.block_till_done()
2015-12-27 01:48:20 +00:00
self.assertEqual(0, len(self.calls))
2015-12-21 23:09:51 +00:00
2015-12-27 01:48:20 +00:00
now = datetime(2015, 9, 16, 15, tzinfo=dt_util.UTC)
with patch('homeassistant.util.dt.now',
2015-12-27 01:48:20 +00:00
return_value=now):
self.hass.bus.fire('test_event')
self.hass.block_till_done()
2015-12-27 01:48:20 +00:00
self.assertEqual(1, len(self.calls))
2015-12-21 23:09:51 +00:00
2015-12-27 01:48:20 +00:00
def test_if_action_after_with_offset(self):
2016-03-09 09:25:50 +00:00
"""Test if action was after offset."""
2015-12-21 23:09:51 +00:00
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, {
2016-04-16 07:55:35 +00:00
sun.STATE_ATTR_NEXT_RISING: '2015-09-16T14:00:00Z',
2015-12-21 23:09:51 +00:00
})
setup_component(self.hass, automation.DOMAIN, {
2015-12-27 01:48:20 +00:00
automation.DOMAIN: {
'trigger': {
'platform': 'event',
'event_type': 'test_event',
},
'condition': {
2016-10-01 08:22:13 +00:00
'condition': 'sun',
2015-12-27 01:48:20 +00:00
'after': 'sunrise',
'after_offset': '+1:00:00'
},
'action': {
'service': 'test.automation'
}
}
})
2015-12-21 23:09:51 +00:00
2015-12-27 01:48:20 +00:00
now = datetime(2015, 9, 16, 14, 59, tzinfo=dt_util.UTC)
with patch('homeassistant.util.dt.now',
2015-12-21 23:09:51 +00:00
return_value=now):
2015-12-27 01:48:20 +00:00
self.hass.bus.fire('test_event')
self.hass.block_till_done()
2015-12-27 01:48:20 +00:00
self.assertEqual(0, len(self.calls))
2015-12-21 23:09:51 +00:00
2015-12-27 01:48:20 +00:00
now = datetime(2015, 9, 16, 15, tzinfo=dt_util.UTC)
with patch('homeassistant.util.dt.now',
2015-12-27 01:48:20 +00:00
return_value=now):
self.hass.bus.fire('test_event')
self.hass.block_till_done()
2015-12-27 01:48:20 +00:00
self.assertEqual(1, len(self.calls))
2015-12-21 23:09:51 +00:00
2015-12-27 01:48:20 +00:00
def test_if_action_before_and_after_during(self):
2016-03-09 09:25:50 +00:00
"""Test if action was before and after during."""
2015-12-21 23:09:51 +00:00
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, {
2016-04-16 07:55:35 +00:00
sun.STATE_ATTR_NEXT_RISING: '2015-09-16T10:00:00Z',
sun.STATE_ATTR_NEXT_SETTING: '2015-09-16T15:00:00Z',
2015-12-21 23:09:51 +00:00
})
setup_component(self.hass, automation.DOMAIN, {
2015-12-27 01:48:20 +00:00
automation.DOMAIN: {
'trigger': {
'platform': 'event',
'event_type': 'test_event',
},
'condition': {
2016-10-01 08:22:13 +00:00
'condition': 'sun',
2015-12-27 01:48:20 +00:00
'after': 'sunrise',
'before': 'sunset'
},
'action': {
'service': 'test.automation'
2015-12-21 23:09:51 +00:00
}
2015-12-27 01:48:20 +00:00
}
2015-12-21 23:09:51 +00:00
})
2015-12-27 01:48:20 +00:00
now = datetime(2015, 9, 16, 9, 59, tzinfo=dt_util.UTC)
with patch('homeassistant.util.dt.now',
2015-12-21 23:09:51 +00:00
return_value=now):
2015-12-27 01:48:20 +00:00
self.hass.bus.fire('test_event')
self.hass.block_till_done()
2015-12-27 01:48:20 +00:00
self.assertEqual(0, len(self.calls))
2015-12-21 23:09:51 +00:00
2015-12-27 01:48:20 +00:00
now = datetime(2015, 9, 16, 15, 1, tzinfo=dt_util.UTC)
with patch('homeassistant.util.dt.now',
2015-12-21 23:09:51 +00:00
return_value=now):
2015-12-27 01:48:20 +00:00
self.hass.bus.fire('test_event')
self.hass.block_till_done()
2015-12-27 01:48:20 +00:00
self.assertEqual(0, len(self.calls))
2015-12-21 23:09:51 +00:00
2015-12-27 01:48:20 +00:00
now = datetime(2015, 9, 16, 12, tzinfo=dt_util.UTC)
with patch('homeassistant.util.dt.now',
2016-01-03 07:36:22 +00:00
return_value=now):
self.hass.bus.fire('test_event')
self.hass.block_till_done()
2016-01-03 07:36:22 +00:00
self.assertEqual(1, len(self.calls))
def test_if_action_after_different_tz(self):
2016-03-09 09:25:50 +00:00
"""Test if action was after in a different timezone."""
2016-01-03 07:36:22 +00:00
import pytz
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, {
2016-04-16 07:55:35 +00:00
sun.STATE_ATTR_NEXT_SETTING: '2015-09-16T17:30:00Z',
2016-01-03 07:36:22 +00:00
})
setup_component(self.hass, automation.DOMAIN, {
2016-01-03 07:36:22 +00:00
automation.DOMAIN: {
'trigger': {
'platform': 'event',
'event_type': 'test_event',
},
'condition': {
2016-10-01 08:22:13 +00:00
'condition': 'sun',
2016-01-03 07:36:22 +00:00
'after': 'sunset',
},
'action': {
'service': 'test.automation'
}
}
})
# Before
now = datetime(2015, 9, 16, 17, tzinfo=pytz.timezone('US/Mountain'))
with patch('homeassistant.util.dt.now',
2016-01-03 07:36:22 +00:00
return_value=now):
self.hass.bus.fire('test_event')
self.hass.block_till_done()
2016-01-03 07:36:22 +00:00
self.assertEqual(0, len(self.calls))
# After
now = datetime(2015, 9, 16, 18, tzinfo=pytz.timezone('US/Mountain'))
with patch('homeassistant.util.dt.now',
2015-12-27 01:48:20 +00:00
return_value=now):
self.hass.bus.fire('test_event')
self.hass.block_till_done()
2015-12-27 01:48:20 +00:00
self.assertEqual(1, len(self.calls))