2016-03-09 09:25:50 +00:00
|
|
|
"""The tests for the Sun component."""
|
2016-10-30 21:18:53 +00:00
|
|
|
# pylint: disable=protected-access
|
2014-11-25 07:15:14 +00:00
|
|
|
import unittest
|
2016-06-02 06:02:46 +00:00
|
|
|
from unittest.mock import patch
|
|
|
|
from datetime import timedelta, datetime
|
2014-11-25 07:15:14 +00:00
|
|
|
|
2016-09-26 21:20:36 +00:00
|
|
|
from homeassistant.bootstrap import setup_component
|
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
|
|
|
|
|
2016-02-14 23:08:23 +00:00
|
|
|
from tests.common import get_test_home_assistant
|
|
|
|
|
2014-11-25 07:15:14 +00:00
|
|
|
|
2016-12-02 05:45:19 +00:00
|
|
|
# pylint: disable=invalid-name
|
2014-11-25 07:15:14 +00:00
|
|
|
class TestSun(unittest.TestCase):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the sun module."""
|
2014-11-25 07:15:14 +00:00
|
|
|
|
2016-10-30 21:18:53 +00:00
|
|
|
def setUp(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Setup things to be run when tests are started."""
|
2016-02-14 23:08:23 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
2014-11-25 07:15:14 +00:00
|
|
|
|
2016-10-30 21:18:53 +00:00
|
|
|
def tearDown(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Stop everything that was 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):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test is_on method."""
|
2014-11-25 07:15:14 +00:00
|
|
|
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):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test retrieving sun setting and rising."""
|
2016-09-26 21:20:36 +00:00
|
|
|
setup_component(self.hass, sun.DOMAIN, {
|
|
|
|
sun.DOMAIN: {sun.CONF_ELEVATION: 0}})
|
2014-11-25 07:15:14 +00:00
|
|
|
|
2016-06-02 06:02:46 +00:00
|
|
|
from astral import Astral
|
|
|
|
|
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
|
|
|
|
2016-04-08 01:32:21 +00:00
|
|
|
latitude = self.hass.config.latitude
|
|
|
|
longitude = self.hass.config.longitude
|
|
|
|
|
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):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test if the state changes at next setting/rising."""
|
2016-09-26 21:20:36 +00:00
|
|
|
setup_component(self.hass, sun.DOMAIN, {
|
|
|
|
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
|
|
|
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2014-11-26 06:31:36 +00:00
|
|
|
|
|
|
|
self.assertEqual(test_state, self.hass.states.get(sun.ENTITY_ID).state)
|
2016-06-02 06:02:46 +00:00
|
|
|
|
|
|
|
def test_norway_in_june(self):
|
|
|
|
"""Test location in Norway where the sun doesn't set in summer."""
|
|
|
|
self.hass.config.latitude = 69.6
|
|
|
|
self.hass.config.longitude = 18.8
|
|
|
|
|
|
|
|
june = datetime(2016, 6, 1, tzinfo=dt_util.UTC)
|
|
|
|
|
2016-07-26 05:02:12 +00:00
|
|
|
with patch('homeassistant.helpers.condition.dt_util.utcnow',
|
2016-06-02 06:02:46 +00:00
|
|
|
return_value=june):
|
2016-09-26 21:20:36 +00:00
|
|
|
assert setup_component(self.hass, sun.DOMAIN, {
|
|
|
|
sun.DOMAIN: {sun.CONF_ELEVATION: 0}})
|
2016-06-02 06:02:46 +00:00
|
|
|
|
|
|
|
state = self.hass.states.get(sun.ENTITY_ID)
|
|
|
|
|
|
|
|
assert state is not None
|
|
|
|
assert sun.next_rising_utc(self.hass) == \
|
|
|
|
datetime(2016, 7, 25, 23, 38, 21, tzinfo=dt_util.UTC)
|
|
|
|
assert sun.next_setting_utc(self.hass) == \
|
|
|
|
datetime(2016, 7, 26, 22, 4, 18, tzinfo=dt_util.UTC)
|