Add sun component test for state change
This commit is contained in:
parent
4405d09d38
commit
cda04b7ece
2 changed files with 42 additions and 9 deletions
|
@ -5,7 +5,7 @@ homeassistant.components.sun
|
||||||
Provides functionality to keep track of the sun.
|
Provides functionality to keep track of the sun.
|
||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
from datetime import timedelta
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
import homeassistant as ha
|
import homeassistant as ha
|
||||||
import homeassistant.util as util
|
import homeassistant.util as util
|
||||||
|
@ -96,15 +96,20 @@ def setup(hass, config):
|
||||||
logger.error("Error setting up: %s", ", ".join(errors))
|
logger.error("Error setting up: %s", ", ".join(errors))
|
||||||
return False
|
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
|
""" Method to update the current state of the sun and
|
||||||
set time of next setting and rising. """
|
set time of next setting and rising. """
|
||||||
|
utc_offset = datetime.utcnow() - datetime.now()
|
||||||
|
utc_now = now + utc_offset
|
||||||
|
|
||||||
observer = ephem.Observer()
|
observer = ephem.Observer()
|
||||||
observer.lat = latitude # pylint: disable=assigning-non-slot
|
observer.lat = latitude # pylint: disable=assigning-non-slot
|
||||||
observer.long = longitude # pylint: disable=assigning-non-slot
|
observer.long = longitude # pylint: disable=assigning-non-slot
|
||||||
|
|
||||||
next_rising_dt = ephem.localtime(observer.next_rising(sun))
|
next_rising_dt = ephem.localtime(
|
||||||
next_setting_dt = ephem.localtime(observer.next_setting(sun))
|
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:
|
if next_rising_dt > next_setting_dt:
|
||||||
new_state = STATE_ABOVE_HORIZON
|
new_state = STATE_ABOVE_HORIZON
|
||||||
|
@ -124,10 +129,10 @@ def setup(hass, config):
|
||||||
|
|
||||||
hass.states.set(ENTITY_ID, new_state, state_attributes)
|
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,
|
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
|
return True
|
||||||
|
|
|
@ -45,9 +45,12 @@ class TestSun(unittest.TestCase):
|
||||||
observer.lat = '32.87336' # pylint: disable=assigning-non-slot
|
observer.lat = '32.87336' # pylint: disable=assigning-non-slot
|
||||||
observer.long = '117.22743' # 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
|
body_sun = ephem.Sun() # pylint: disable=no-member
|
||||||
next_rising_dt = ephem.localtime(observer.next_rising(body_sun))
|
next_rising_dt = ephem.localtime(
|
||||||
next_setting_dt = ephem.localtime(observer.next_setting(body_sun))
|
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
|
# Home Assistant strips out microseconds
|
||||||
# strip it out of the datetime objects
|
# 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_rising(self.hass, 'non.existing'))
|
||||||
self.assertIsNone(sun.next_setting(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):
|
def test_setup(self):
|
||||||
""" Test Sun setup with empty and wrong configs. """
|
""" Test Sun setup with empty and wrong configs. """
|
||||||
self.assertFalse(sun.setup(self.hass, {}))
|
self.assertFalse(sun.setup(self.hass, {}))
|
||||||
|
|
Loading…
Add table
Reference in a new issue