From c9ff0ab7eb0ef8e52089d1d16699f6235739f52b Mon Sep 17 00:00:00 2001 From: Philip Lundrigan Date: Sun, 3 Jan 2016 00:36:22 -0700 Subject: [PATCH] Fix for sun if condition --- homeassistant/components/automation/sun.py | 10 ++-- tests/components/automation/test_sun.py | 61 ++++++++++++++++++---- 2 files changed, 55 insertions(+), 16 deletions(-) diff --git a/homeassistant/components/automation/sun.py b/homeassistant/components/automation/sun.py index 394dc904be1..064f6a0a16a 100644 --- a/homeassistant/components/automation/sun.py +++ b/homeassistant/components/automation/sun.py @@ -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() diff --git a/tests/components/automation/test_sun.py b/tests/components/automation/test_sun.py index 26ecc26c72a..db4782cfd46 100644 --- a/tests/components/automation/test_sun.py +++ b/tests/components/automation/test_sun.py @@ -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()