Fix Norway (#2197)

This commit is contained in:
Paulus Schoutsen 2016-06-01 23:02:46 -07:00
parent d0988422d4
commit f868df1035
2 changed files with 39 additions and 11 deletions

View File

@ -165,20 +165,28 @@ class Sun(Entity):
def update_as_of(self, utc_point_in_time):
"""Calculate sun state at a point in UTC time."""
import astral
mod = -1
while True:
next_rising_dt = self.location.sunrise(
utc_point_in_time + timedelta(days=mod), local=False)
if next_rising_dt > utc_point_in_time:
break
try:
next_rising_dt = self.location.sunrise(
utc_point_in_time + timedelta(days=mod), local=False)
if next_rising_dt > utc_point_in_time:
break
except astral.AstralError:
pass
mod += 1
mod = -1
while True:
next_setting_dt = (self.location.sunset(
utc_point_in_time + timedelta(days=mod), local=False))
if next_setting_dt > utc_point_in_time:
break
try:
next_setting_dt = (self.location.sunset(
utc_point_in_time + timedelta(days=mod), local=False))
if next_setting_dt > utc_point_in_time:
break
except astral.AstralError:
pass
mod += 1
self.next_rising = next_rising_dt

View File

@ -1,9 +1,8 @@
"""The tests for the Sun component."""
# pylint: disable=too-many-public-methods,protected-access
import unittest
from datetime import timedelta
from astral import Astral
from unittest.mock import patch
from datetime import timedelta, datetime
import homeassistant.core as ha
import homeassistant.util.dt as dt_util
@ -34,6 +33,8 @@ class TestSun(unittest.TestCase):
"""Test retrieving sun setting and rising."""
sun.setup(self.hass, {sun.DOMAIN: {sun.CONF_ELEVATION: 0}})
from astral import Astral
astral = Astral()
utc_now = dt_util.utcnow()
@ -87,3 +88,22 @@ class TestSun(unittest.TestCase):
self.hass.pool.block_till_done()
self.assertEqual(test_state, self.hass.states.get(sun.ENTITY_ID).state)
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)
with patch('homeassistant.helpers.condition.dt_util.now',
return_value=june):
assert sun.setup(self.hass, {sun.DOMAIN: {sun.CONF_ELEVATION: 0}})
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)