Fix for sun if condition

This commit is contained in:
Philip Lundrigan 2016-01-03 00:36:22 -07:00
parent cf1f9a29bf
commit c9ff0ab7eb
2 changed files with 55 additions and 16 deletions

View file

@ -82,21 +82,21 @@ def if_action(hass, config):
if before is None:
before_func = lambda: None
elif before == EVENT_SUNRISE:
before_func = lambda: sun.next_rising_utc(hass) + before_offset
before_func = lambda: sun.next_rising(hass) + before_offset
else:
before_func = lambda: sun.next_setting_utc(hass) + before_offset
before_func = lambda: sun.next_setting(hass) + before_offset
if after is None:
after_func = lambda: None
elif after == EVENT_SUNRISE:
after_func = lambda: sun.next_rising_utc(hass) + after_offset
after_func = lambda: sun.next_rising(hass) + after_offset
else:
after_func = lambda: sun.next_setting_utc(hass) + after_offset
after_func = lambda: sun.next_setting(hass) + after_offset
def time_if():
""" Validate time based if-condition """
now = dt_util.utcnow()
now = dt_util.now()
before = before_func()
after = after_func()

View file

@ -162,14 +162,14 @@ class TestAutomationSun(unittest.TestCase):
})
now = datetime(2015, 9, 16, 15, tzinfo=dt_util.UTC)
with patch('homeassistant.components.automation.sun.dt_util.utcnow',
with patch('homeassistant.components.automation.sun.dt_util.now',
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, 10, tzinfo=dt_util.UTC)
with patch('homeassistant.components.automation.sun.dt_util.utcnow',
with patch('homeassistant.components.automation.sun.dt_util.now',
return_value=now):
self.hass.bus.fire('test_event')
self.hass.pool.block_till_done()
@ -197,14 +197,14 @@ class TestAutomationSun(unittest.TestCase):
})
now = datetime(2015, 9, 16, 13, tzinfo=dt_util.UTC)
with patch('homeassistant.components.automation.sun.dt_util.utcnow',
with patch('homeassistant.components.automation.sun.dt_util.now',
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',
with patch('homeassistant.components.automation.sun.dt_util.now',
return_value=now):
self.hass.bus.fire('test_event')
self.hass.pool.block_till_done()
@ -233,14 +233,14 @@ class TestAutomationSun(unittest.TestCase):
})
now = datetime(2015, 9, 16, 15, 1, tzinfo=dt_util.UTC)
with patch('homeassistant.components.automation.sun.dt_util.utcnow',
with patch('homeassistant.components.automation.sun.dt_util.now',
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',
with patch('homeassistant.components.automation.sun.dt_util.now',
return_value=now):
self.hass.bus.fire('test_event')
self.hass.pool.block_till_done()
@ -269,14 +269,14 @@ class TestAutomationSun(unittest.TestCase):
})
now = datetime(2015, 9, 16, 14, 59, tzinfo=dt_util.UTC)
with patch('homeassistant.components.automation.sun.dt_util.utcnow',
with patch('homeassistant.components.automation.sun.dt_util.now',
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',
with patch('homeassistant.components.automation.sun.dt_util.now',
return_value=now):
self.hass.bus.fire('test_event')
self.hass.pool.block_till_done()
@ -306,21 +306,60 @@ class TestAutomationSun(unittest.TestCase):
})
now = datetime(2015, 9, 16, 9, 59, tzinfo=dt_util.UTC)
with patch('homeassistant.components.automation.sun.dt_util.utcnow',
with patch('homeassistant.components.automation.sun.dt_util.now',
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',
with patch('homeassistant.components.automation.sun.dt_util.now',
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)
with patch('homeassistant.components.automation.sun.dt_util.utcnow',
with patch('homeassistant.components.automation.sun.dt_util.now',
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_different_tz(self):
import pytz
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, {
sun.STATE_ATTR_NEXT_SETTING: '17:30:00 16-09-2015',
})
automation.setup(self.hass, {
automation.DOMAIN: {
'trigger': {
'platform': 'event',
'event_type': 'test_event',
},
'condition': {
'platform': 'sun',
'after': 'sunset',
},
'action': {
'service': 'test.automation'
}
}
})
# Before
now = datetime(2015, 9, 16, 17, tzinfo=pytz.timezone('US/Mountain'))
with patch('homeassistant.components.automation.sun.dt_util.now',
return_value=now):
self.hass.bus.fire('test_event')
self.hass.pool.block_till_done()
self.assertEqual(0, len(self.calls))
# After
now = datetime(2015, 9, 16, 18, tzinfo=pytz.timezone('US/Mountain'))
with patch('homeassistant.components.automation.sun.dt_util.now',
return_value=now):
self.hass.bus.fire('test_event')
self.hass.pool.block_till_done()