2014-11-25 07:15:14 +00:00
|
|
|
"""
|
2015-01-09 04:05:12 +00:00
|
|
|
tests.test_component_sun
|
2014-12-01 07:14:08 +00:00
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2014-11-25 07:15:14 +00:00
|
|
|
|
2014-11-25 07:35:50 +00:00
|
|
|
Tests Sun component.
|
2014-11-25 07:15:14 +00:00
|
|
|
"""
|
|
|
|
# pylint: disable=too-many-public-methods,protected-access
|
|
|
|
import unittest
|
2015-04-29 02:12:05 +00:00
|
|
|
from datetime import timedelta
|
2014-11-25 07:15:14 +00:00
|
|
|
|
2015-07-17 02:49:54 +00:00
|
|
|
from astral import Astral
|
2014-11-25 07:15:14 +00:00
|
|
|
|
2015-08-17 03:44:46 +00:00
|
|
|
import homeassistant.core as ha
|
2015-04-29 02:12:05 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2014-11-25 07:15:14 +00:00
|
|
|
import homeassistant.components.sun as sun
|
|
|
|
|
|
|
|
|
|
|
|
class TestSun(unittest.TestCase):
|
|
|
|
""" Test the sun module. """
|
|
|
|
|
|
|
|
def setUp(self): # pylint: disable=invalid-name
|
|
|
|
self.hass = ha.HomeAssistant()
|
|
|
|
|
|
|
|
def tearDown(self): # pylint: disable=invalid-name
|
|
|
|
""" Stop down stuff we started. """
|
2014-11-29 04:22:08 +00:00
|
|
|
self.hass.stop()
|
2014-11-25 07:15:14 +00:00
|
|
|
|
|
|
|
def test_is_on(self):
|
|
|
|
""" Test is_on method. """
|
|
|
|
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON)
|
|
|
|
self.assertTrue(sun.is_on(self.hass))
|
|
|
|
self.hass.states.set(sun.ENTITY_ID, sun.STATE_BELOW_HORIZON)
|
|
|
|
self.assertFalse(sun.is_on(self.hass))
|
|
|
|
|
|
|
|
def test_setting_rising(self):
|
|
|
|
""" Test retrieving sun setting and rising. """
|
2015-07-17 02:49:54 +00:00
|
|
|
latitude = 32.87336
|
|
|
|
longitude = 117.22743
|
|
|
|
|
2014-11-25 07:15:14 +00:00
|
|
|
# Compare it with the real data
|
2015-07-17 02:49:54 +00:00
|
|
|
self.hass.config.latitude = latitude
|
|
|
|
self.hass.config.longitude = longitude
|
2015-07-17 04:12:18 +00:00
|
|
|
sun.setup(self.hass, {sun.DOMAIN: {sun.CONF_ELEVATION: 0}})
|
2014-11-25 07:15:14 +00:00
|
|
|
|
2015-07-17 02:49:54 +00:00
|
|
|
astral = Astral()
|
2015-04-29 02:12:05 +00:00
|
|
|
utc_now = dt_util.utcnow()
|
2015-07-17 02:49:54 +00:00
|
|
|
|
|
|
|
mod = -1
|
|
|
|
while True:
|
|
|
|
next_rising = (astral.sunrise_utc(utc_now +
|
|
|
|
timedelta(days=mod), latitude, longitude))
|
|
|
|
if next_rising > utc_now:
|
|
|
|
break
|
|
|
|
mod += 1
|
|
|
|
|
|
|
|
mod = -1
|
|
|
|
while True:
|
|
|
|
next_setting = (astral.sunset_utc(utc_now +
|
|
|
|
timedelta(days=mod), latitude, longitude))
|
|
|
|
if next_setting > utc_now:
|
|
|
|
break
|
|
|
|
mod += 1
|
|
|
|
|
|
|
|
self.assertEqual(next_rising, sun.next_rising_utc(self.hass))
|
|
|
|
self.assertEqual(next_setting, sun.next_setting_utc(self.hass))
|
2014-11-25 07:15:14 +00:00
|
|
|
|
|
|
|
# Point it at a state without the proper attributes
|
|
|
|
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON)
|
|
|
|
self.assertIsNone(sun.next_rising(self.hass))
|
|
|
|
self.assertIsNone(sun.next_setting(self.hass))
|
|
|
|
|
|
|
|
# Point it at a non-existing state
|
|
|
|
self.assertIsNone(sun.next_rising(self.hass, 'non.existing'))
|
|
|
|
self.assertIsNone(sun.next_setting(self.hass, 'non.existing'))
|
|
|
|
|
2014-11-26 06:31:36 +00:00
|
|
|
def test_state_change(self):
|
|
|
|
""" Test if the state changes at next setting/rising. """
|
2015-03-19 06:02:58 +00:00
|
|
|
self.hass.config.latitude = '32.87336'
|
|
|
|
self.hass.config.longitude = '117.22743'
|
2015-09-01 07:18:26 +00:00
|
|
|
sun.setup(self.hass, {sun.DOMAIN: {sun.CONF_ELEVATION: 0}})
|
2014-11-26 06:31:36 +00:00
|
|
|
|
|
|
|
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,
|
2015-04-29 02:12:05 +00:00
|
|
|
{ha.ATTR_NOW: test_time + timedelta(seconds=5)})
|
2014-11-26 06:31:36 +00:00
|
|
|
|
2014-12-17 05:46:02 +00:00
|
|
|
self.hass.pool.block_till_done()
|
2014-11-26 06:31:36 +00:00
|
|
|
|
|
|
|
self.assertEqual(test_state, self.hass.states.get(sun.ENTITY_ID).state)
|