From cda04b7ece8c62afb54efc296952631978fc6d53 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Tue, 25 Nov 2014 22:31:36 -0800 Subject: [PATCH] Add sun component test for state change --- homeassistant/components/sun.py | 19 ++++++++++++------- test/test_component_sun.py | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/sun.py b/homeassistant/components/sun.py index 5ab4d6f598b..c6acbc05230 100644 --- a/homeassistant/components/sun.py +++ b/homeassistant/components/sun.py @@ -5,7 +5,7 @@ homeassistant.components.sun Provides functionality to keep track of the sun. """ import logging -from datetime import timedelta +from datetime import datetime, timedelta import homeassistant as ha import homeassistant.util as util @@ -96,15 +96,20 @@ def setup(hass, config): logger.error("Error setting up: %s", ", ".join(errors)) return False - def update_sun_state(now): # pylint: disable=unused-argument + def update_sun_state(now): """ Method to update the current state of the sun and set time of next setting and rising. """ + utc_offset = datetime.utcnow() - datetime.now() + utc_now = now + utc_offset + observer = ephem.Observer() observer.lat = latitude # pylint: disable=assigning-non-slot observer.long = longitude # pylint: disable=assigning-non-slot - next_rising_dt = ephem.localtime(observer.next_rising(sun)) - next_setting_dt = ephem.localtime(observer.next_setting(sun)) + next_rising_dt = ephem.localtime( + observer.next_rising(sun, start=utc_now)) + next_setting_dt = ephem.localtime( + observer.next_setting(sun, start=utc_now)) if next_rising_dt > next_setting_dt: new_state = STATE_ABOVE_HORIZON @@ -124,10 +129,10 @@ def setup(hass, config): hass.states.set(ENTITY_ID, new_state, state_attributes) - # +10 seconds to be sure that the change has occured + # +1 second so Ephem will report it has set hass.track_point_in_time(update_sun_state, - next_change + timedelta(seconds=10)) + next_change + timedelta(seconds=1)) - update_sun_state(None) + update_sun_state(datetime.now()) return True diff --git a/test/test_component_sun.py b/test/test_component_sun.py index f850cfedaa2..f7cf7c90dab 100644 --- a/test/test_component_sun.py +++ b/test/test_component_sun.py @@ -45,9 +45,12 @@ class TestSun(unittest.TestCase): observer.lat = '32.87336' # pylint: disable=assigning-non-slot observer.long = '117.22743' # pylint: disable=assigning-non-slot + utc_now = dt.datetime.utcnow() body_sun = ephem.Sun() # pylint: disable=no-member - next_rising_dt = ephem.localtime(observer.next_rising(body_sun)) - next_setting_dt = ephem.localtime(observer.next_setting(body_sun)) + next_rising_dt = ephem.localtime( + observer.next_rising(body_sun, start=utc_now)) + next_setting_dt = ephem.localtime( + observer.next_setting(body_sun, start=utc_now)) # Home Assistant strips out microseconds # strip it out of the datetime objects @@ -68,6 +71,31 @@ class TestSun(unittest.TestCase): self.assertIsNone(sun.next_rising(self.hass, 'non.existing')) self.assertIsNone(sun.next_setting(self.hass, 'non.existing')) + def test_state_change(self): + """ Test if the state changes at next setting/rising. """ + self.assertTrue(sun.setup( + self.hass, + {ha.DOMAIN: { + ha.CONF_LATITUDE: '32.87336', + ha.CONF_LONGITUDE: '117.22743' + }})) + + if sun.is_on(self.hass): + test_state = sun.STATE_BELOW_HORIZON + test_time = sun.next_setting(self.hass) + else: + test_state = sun.STATE_ABOVE_HORIZON + test_time = sun.next_rising(self.hass) + + self.assertIsNotNone(test_time) + + self.hass.bus.fire(ha.EVENT_TIME_CHANGED, + {ha.ATTR_NOW: test_time + dt.timedelta(seconds=5)}) + + self.hass._pool.block_till_done() + + self.assertEqual(test_state, self.hass.states.get(sun.ENTITY_ID).state) + def test_setup(self): """ Test Sun setup with empty and wrong configs. """ self.assertFalse(sun.setup(self.hass, {}))