mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
Refactor sun component for correctness (#7295)
* Refactor sun component for correctness * Convert datetimes to dates for astral * Fix tests for updated code * Fix times now that calcs are fixed * Move sun functions to helpers * Fix flake on new file * Additional tweaks from review * Update requirements
This commit is contained in:
committed by
Paulus Schoutsen
parent
419d97fc06
commit
40d27cde0e
@@ -22,7 +22,8 @@ class TestAutomationSun(unittest.TestCase):
|
||||
"""Setup things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
mock_component(self.hass, 'group')
|
||||
mock_component(self.hass, 'sun')
|
||||
setup_component(self.hass, sun.DOMAIN, {
|
||||
sun.DOMAIN: {sun.CONF_ELEVATION: 0}})
|
||||
|
||||
self.calls = []
|
||||
|
||||
@@ -39,10 +40,6 @@ class TestAutomationSun(unittest.TestCase):
|
||||
|
||||
def test_sunset_trigger(self):
|
||||
"""Test the sunset trigger."""
|
||||
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, {
|
||||
sun.STATE_ATTR_NEXT_SETTING: '2015-09-16T02:00:00Z',
|
||||
})
|
||||
|
||||
now = datetime(2015, 9, 15, 23, tzinfo=dt_util.UTC)
|
||||
trigger_time = datetime(2015, 9, 16, 2, tzinfo=dt_util.UTC)
|
||||
|
||||
@@ -78,10 +75,6 @@ class TestAutomationSun(unittest.TestCase):
|
||||
|
||||
def test_sunrise_trigger(self):
|
||||
"""Test the sunrise trigger."""
|
||||
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, {
|
||||
sun.STATE_ATTR_NEXT_RISING: '2015-09-16T14:00:00Z',
|
||||
})
|
||||
|
||||
now = datetime(2015, 9, 13, 23, tzinfo=dt_util.UTC)
|
||||
trigger_time = datetime(2015, 9, 16, 14, tzinfo=dt_util.UTC)
|
||||
|
||||
@@ -105,10 +98,6 @@ class TestAutomationSun(unittest.TestCase):
|
||||
|
||||
def test_sunset_trigger_with_offset(self):
|
||||
"""Test the sunset trigger with offset."""
|
||||
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, {
|
||||
sun.STATE_ATTR_NEXT_SETTING: '2015-09-16T02:00:00Z',
|
||||
})
|
||||
|
||||
now = datetime(2015, 9, 15, 23, tzinfo=dt_util.UTC)
|
||||
trigger_time = datetime(2015, 9, 16, 2, 30, tzinfo=dt_util.UTC)
|
||||
|
||||
@@ -139,10 +128,6 @@ class TestAutomationSun(unittest.TestCase):
|
||||
|
||||
def test_sunrise_trigger_with_offset(self):
|
||||
"""Test the runrise trigger with offset."""
|
||||
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, {
|
||||
sun.STATE_ATTR_NEXT_RISING: '2015-09-16T14:00:00Z',
|
||||
})
|
||||
|
||||
now = datetime(2015, 9, 13, 23, tzinfo=dt_util.UTC)
|
||||
trigger_time = datetime(2015, 9, 16, 13, 30, tzinfo=dt_util.UTC)
|
||||
|
||||
@@ -167,10 +152,6 @@ class TestAutomationSun(unittest.TestCase):
|
||||
|
||||
def test_if_action_before(self):
|
||||
"""Test if action was before."""
|
||||
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, {
|
||||
sun.STATE_ATTR_NEXT_RISING: '2015-09-16T14:00:00Z',
|
||||
})
|
||||
|
||||
setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
@@ -188,14 +169,14 @@ class TestAutomationSun(unittest.TestCase):
|
||||
})
|
||||
|
||||
now = datetime(2015, 9, 16, 15, tzinfo=dt_util.UTC)
|
||||
with patch('homeassistant.util.dt.now',
|
||||
with patch('homeassistant.util.dt.utcnow',
|
||||
return_value=now):
|
||||
self.hass.bus.fire('test_event')
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(0, len(self.calls))
|
||||
|
||||
now = datetime(2015, 9, 16, 10, tzinfo=dt_util.UTC)
|
||||
with patch('homeassistant.util.dt.now',
|
||||
with patch('homeassistant.util.dt.utcnow',
|
||||
return_value=now):
|
||||
self.hass.bus.fire('test_event')
|
||||
self.hass.block_till_done()
|
||||
@@ -203,10 +184,6 @@ class TestAutomationSun(unittest.TestCase):
|
||||
|
||||
def test_if_action_after(self):
|
||||
"""Test if action was after."""
|
||||
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, {
|
||||
sun.STATE_ATTR_NEXT_RISING: '2015-09-16T14:00:00Z',
|
||||
})
|
||||
|
||||
setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
@@ -224,14 +201,14 @@ class TestAutomationSun(unittest.TestCase):
|
||||
})
|
||||
|
||||
now = datetime(2015, 9, 16, 13, tzinfo=dt_util.UTC)
|
||||
with patch('homeassistant.util.dt.now',
|
||||
with patch('homeassistant.util.dt.utcnow',
|
||||
return_value=now):
|
||||
self.hass.bus.fire('test_event')
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(0, len(self.calls))
|
||||
|
||||
now = datetime(2015, 9, 16, 15, tzinfo=dt_util.UTC)
|
||||
with patch('homeassistant.util.dt.now',
|
||||
with patch('homeassistant.util.dt.utcnow',
|
||||
return_value=now):
|
||||
self.hass.bus.fire('test_event')
|
||||
self.hass.block_till_done()
|
||||
@@ -239,10 +216,6 @@ class TestAutomationSun(unittest.TestCase):
|
||||
|
||||
def test_if_action_before_with_offset(self):
|
||||
"""Test if action was before offset."""
|
||||
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, {
|
||||
sun.STATE_ATTR_NEXT_RISING: '2015-09-16T14:00:00Z',
|
||||
})
|
||||
|
||||
setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
@@ -260,15 +233,15 @@ class TestAutomationSun(unittest.TestCase):
|
||||
}
|
||||
})
|
||||
|
||||
now = datetime(2015, 9, 16, 15, 1, tzinfo=dt_util.UTC)
|
||||
with patch('homeassistant.util.dt.now',
|
||||
now = datetime(2015, 9, 16, 14, 32, 44, tzinfo=dt_util.UTC)
|
||||
with patch('homeassistant.util.dt.utcnow',
|
||||
return_value=now):
|
||||
self.hass.bus.fire('test_event')
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(0, len(self.calls))
|
||||
|
||||
now = datetime(2015, 9, 16, 15, tzinfo=dt_util.UTC)
|
||||
with patch('homeassistant.util.dt.now',
|
||||
now = datetime(2015, 9, 16, 14, 32, 43, tzinfo=dt_util.UTC)
|
||||
with patch('homeassistant.util.dt.utcnow',
|
||||
return_value=now):
|
||||
self.hass.bus.fire('test_event')
|
||||
self.hass.block_till_done()
|
||||
@@ -276,10 +249,6 @@ class TestAutomationSun(unittest.TestCase):
|
||||
|
||||
def test_if_action_after_with_offset(self):
|
||||
"""Test if action was after offset."""
|
||||
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, {
|
||||
sun.STATE_ATTR_NEXT_RISING: '2015-09-16T14:00:00Z',
|
||||
})
|
||||
|
||||
setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
@@ -297,15 +266,15 @@ class TestAutomationSun(unittest.TestCase):
|
||||
}
|
||||
})
|
||||
|
||||
now = datetime(2015, 9, 16, 14, 59, tzinfo=dt_util.UTC)
|
||||
with patch('homeassistant.util.dt.now',
|
||||
now = datetime(2015, 9, 16, 14, 32, 42, tzinfo=dt_util.UTC)
|
||||
with patch('homeassistant.util.dt.utcnow',
|
||||
return_value=now):
|
||||
self.hass.bus.fire('test_event')
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(0, len(self.calls))
|
||||
|
||||
now = datetime(2015, 9, 16, 15, tzinfo=dt_util.UTC)
|
||||
with patch('homeassistant.util.dt.now',
|
||||
now = datetime(2015, 9, 16, 14, 32, 43, tzinfo=dt_util.UTC)
|
||||
with patch('homeassistant.util.dt.utcnow',
|
||||
return_value=now):
|
||||
self.hass.bus.fire('test_event')
|
||||
self.hass.block_till_done()
|
||||
@@ -313,11 +282,6 @@ class TestAutomationSun(unittest.TestCase):
|
||||
|
||||
def test_if_action_before_and_after_during(self):
|
||||
"""Test if action was before and after during."""
|
||||
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, {
|
||||
sun.STATE_ATTR_NEXT_RISING: '2015-09-16T10:00:00Z',
|
||||
sun.STATE_ATTR_NEXT_SETTING: '2015-09-16T15:00:00Z',
|
||||
})
|
||||
|
||||
setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
@@ -335,62 +299,22 @@ class TestAutomationSun(unittest.TestCase):
|
||||
}
|
||||
})
|
||||
|
||||
now = datetime(2015, 9, 16, 9, 59, tzinfo=dt_util.UTC)
|
||||
with patch('homeassistant.util.dt.now',
|
||||
now = datetime(2015, 9, 16, 13, 8, 51, tzinfo=dt_util.UTC)
|
||||
with patch('homeassistant.util.dt.utcnow',
|
||||
return_value=now):
|
||||
self.hass.bus.fire('test_event')
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(0, len(self.calls))
|
||||
|
||||
now = datetime(2015, 9, 16, 15, 1, tzinfo=dt_util.UTC)
|
||||
with patch('homeassistant.util.dt.now',
|
||||
now = datetime(2015, 9, 17, 2, 25, 18, tzinfo=dt_util.UTC)
|
||||
with patch('homeassistant.util.dt.utcnow',
|
||||
return_value=now):
|
||||
self.hass.bus.fire('test_event')
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(0, len(self.calls))
|
||||
|
||||
now = datetime(2015, 9, 16, 12, tzinfo=dt_util.UTC)
|
||||
with patch('homeassistant.util.dt.now',
|
||||
return_value=now):
|
||||
self.hass.bus.fire('test_event')
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(1, len(self.calls))
|
||||
|
||||
def test_if_action_after_different_tz(self):
|
||||
"""Test if action was after in a different timezone."""
|
||||
import pytz
|
||||
|
||||
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, {
|
||||
sun.STATE_ATTR_NEXT_SETTING: '2015-09-16T17:30:00Z',
|
||||
})
|
||||
|
||||
setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'event',
|
||||
'event_type': 'test_event',
|
||||
},
|
||||
'condition': {
|
||||
'condition': 'sun',
|
||||
'after': 'sunset',
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation'
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
# Before
|
||||
now = datetime(2015, 9, 16, 17, tzinfo=pytz.timezone('US/Mountain'))
|
||||
with patch('homeassistant.util.dt.now',
|
||||
return_value=now):
|
||||
self.hass.bus.fire('test_event')
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(0, len(self.calls))
|
||||
|
||||
# After
|
||||
now = datetime(2015, 9, 16, 18, tzinfo=pytz.timezone('US/Mountain'))
|
||||
with patch('homeassistant.util.dt.now',
|
||||
now = datetime(2015, 9, 16, 16, tzinfo=dt_util.UTC)
|
||||
with patch('homeassistant.util.dt.utcnow',
|
||||
return_value=now):
|
||||
self.hass.bus.fire('test_event')
|
||||
self.hass.block_till_done()
|
||||
|
||||
Reference in New Issue
Block a user