Refactor sun component for correctness (#7295)
* Refactor sun component for correctness * Convert datetimes to dates for astral * Fix tests for updated code * Fix times now that calcs are fixed * Move sun functions to helpers * Fix flake on new file * Additional tweaks from review * Update requirements
This commit is contained in:
1 parent
419d97fc06
commit
40d27cde0e
19 files changed
+754
-756
No files matched your search
+23
-15
@@ -25,6 +25,7 @@ from homeassistant.components import sun
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
from unittest.mock import patch
|
||||
|
||||
|
||||
class TestEventHelpers(unittest.TestCase):
|
||||
@@ -302,24 +303,27 @@ class TestEventHelpers(unittest.TestCase):
|
||||
|
||||
# Get next sunrise/sunset
|
||||
astral = Astral()
|
||||
utc_now = dt_util.utcnow()
|
||||
utc_now = datetime(2014, 5, 24, 12, 0, 0, tzinfo=dt_util.UTC)
|
||||
utc_today = utc_now.date()
|
||||
|
||||
mod = -1
|
||||
while True:
|
||||
next_rising = (astral.sunrise_utc(utc_now +
|
||||
timedelta(days=mod), latitude, longitude))
|
||||
next_rising = (astral.sunrise_utc(
|
||||
utc_today + timedelta(days=mod), latitude, longitude))
|
||||
if next_rising > utc_now:
|
||||
break
|
||||
mod += 1
|
||||
|
||||
# Track sunrise
|
||||
runs = []
|
||||
unsub = track_sunrise(self.hass, lambda: runs.append(1))
|
||||
with patch('homeassistant.util.dt.utcnow', return_value=utc_now):
|
||||
unsub = track_sunrise(self.hass, lambda: runs.append(1))
|
||||
|
||||
offset_runs = []
|
||||
offset = timedelta(minutes=30)
|
||||
unsub2 = track_sunrise(self.hass, lambda: offset_runs.append(1),
|
||||
offset)
|
||||
with patch('homeassistant.util.dt.utcnow', return_value=utc_now):
|
||||
unsub2 = track_sunrise(self.hass, lambda: offset_runs.append(1),
|
||||
offset)
|
||||
|
||||
# run tests
|
||||
self._send_time_changed(next_rising - offset)
|
||||
@@ -334,7 +338,7 @@ class TestEventHelpers(unittest.TestCase):
|
||||
|
||||
self._send_time_changed(next_rising + offset)
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(2, len(runs))
|
||||
self.assertEqual(1, len(runs))
|
||||
self.assertEqual(1, len(offset_runs))
|
||||
|
||||
unsub()
|
||||
@@ -342,7 +346,7 @@ class TestEventHelpers(unittest.TestCase):
|
||||
|
||||
self._send_time_changed(next_rising + offset)
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(2, len(runs))
|
||||
self.assertEqual(1, len(runs))
|
||||
self.assertEqual(1, len(offset_runs))
|
||||
|
||||
def test_track_sunset(self):
|
||||
@@ -358,23 +362,27 @@ class TestEventHelpers(unittest.TestCase):
|
||||
|
||||
# Get next sunrise/sunset
|
||||
astral = Astral()
|
||||
utc_now = dt_util.utcnow()
|
||||
utc_now = datetime(2014, 5, 24, 12, 0, 0, tzinfo=dt_util.UTC)
|
||||
utc_today = utc_now.date()
|
||||
|
||||
mod = -1
|
||||
while True:
|
||||
next_setting = (astral.sunset_utc(utc_now +
|
||||
timedelta(days=mod), latitude, longitude))
|
||||
next_setting = (astral.sunset_utc(
|
||||
utc_today + timedelta(days=mod), latitude, longitude))
|
||||
if next_setting > utc_now:
|
||||
break
|
||||
mod += 1
|
||||
|
||||
# Track sunset
|
||||
runs = []
|
||||
unsub = track_sunset(self.hass, lambda: runs.append(1))
|
||||
with patch('homeassistant.util.dt.utcnow', return_value=utc_now):
|
||||
unsub = track_sunset(self.hass, lambda: runs.append(1))
|
||||
|
||||
offset_runs = []
|
||||
offset = timedelta(minutes=30)
|
||||
unsub2 = track_sunset(self.hass, lambda: offset_runs.append(1), offset)
|
||||
with patch('homeassistant.util.dt.utcnow', return_value=utc_now):
|
||||
unsub2 = track_sunset(
|
||||
self.hass, lambda: offset_runs.append(1), offset)
|
||||
|
||||
# Run tests
|
||||
self._send_time_changed(next_setting - offset)
|
||||
@@ -389,7 +397,7 @@ class TestEventHelpers(unittest.TestCase):
|
||||
|
||||
self._send_time_changed(next_setting + offset)
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(2, len(runs))
|
||||
self.assertEqual(1, len(runs))
|
||||
self.assertEqual(1, len(offset_runs))
|
||||
|
||||
unsub()
|
||||
@@ -397,7 +405,7 @@ class TestEventHelpers(unittest.TestCase):
|
||||
|
||||
self._send_time_changed(next_setting + offset)
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(2, len(runs))
|
||||
self.assertEqual(1, len(runs))
|
||||
self.assertEqual(1, len(offset_runs))
|
||||
|
||||
def _send_time_changed(self, now):
|
||||
|
||||
@@ -0,0 +1,227 @@
|
||||
"""The tests for the Sun helpers."""
|
||||
# pylint: disable=protected-access
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
from datetime import timedelta, datetime
|
||||
|
||||
import homeassistant.util.dt as dt_util
|
||||
import homeassistant.helpers.sun as sun
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
class TestSun(unittest.TestCase):
|
||||
"""Test the sun helpers."""
|
||||
|
||||
def setUp(self):
|
||||
"""Setup things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
|
||||
def tearDown(self):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
def test_next_events(self):
|
||||
"""Test retrieving next sun events."""
|
||||
utc_now = datetime(2016, 11, 1, 8, 0, 0, tzinfo=dt_util.UTC)
|
||||
from astral import Astral
|
||||
|
||||
astral = Astral()
|
||||
utc_today = utc_now.date()
|
||||
|
||||
latitude = self.hass.config.latitude
|
||||
longitude = self.hass.config.longitude
|
||||
|
||||
mod = -1
|
||||
while True:
|
||||
next_dawn = (astral.dawn_utc(
|
||||
utc_today + timedelta(days=mod), latitude, longitude))
|
||||
if next_dawn > utc_now:
|
||||
break
|
||||
mod += 1
|
||||
|
||||
mod = -1
|
||||
while True:
|
||||
next_dusk = (astral.dusk_utc(
|
||||
utc_today + timedelta(days=mod), latitude, longitude))
|
||||
if next_dusk > utc_now:
|
||||
break
|
||||
mod += 1
|
||||
|
||||
mod = -1
|
||||
while True:
|
||||
next_midnight = (astral.solar_midnight_utc(
|
||||
utc_today + timedelta(days=mod), longitude))
|
||||
if next_midnight > utc_now:
|
||||
break
|
||||
mod += 1
|
||||
|
||||
mod = -1
|
||||
while True:
|
||||
next_noon = (astral.solar_noon_utc(
|
||||
utc_today + timedelta(days=mod), longitude))
|
||||
if next_noon > utc_now:
|
||||
break
|
||||
mod += 1
|
||||
|
||||
mod = -1
|
||||
while True:
|
||||
next_rising = (astral.sunrise_utc(
|
||||
utc_today + timedelta(days=mod), latitude, longitude))
|
||||
if next_rising > utc_now:
|
||||
break
|
||||
mod += 1
|
||||
|
||||
mod = -1
|
||||
while True:
|
||||
next_setting = (astral.sunset_utc(
|
||||
utc_today + timedelta(days=mod), latitude, longitude))
|
||||
if next_setting > utc_now:
|
||||
break
|
||||
mod += 1
|
||||
|
||||
with patch('homeassistant.helpers.condition.dt_util.utcnow',
|
||||
return_value=utc_now):
|
||||
self.assertEqual(next_dawn, sun.get_astral_event_next(
|
||||
self.hass, 'dawn'))
|
||||
self.assertEqual(next_dusk, sun.get_astral_event_next(
|
||||
self.hass, 'dusk'))
|
||||
self.assertEqual(next_midnight, sun.get_astral_event_next(
|
||||
self.hass, 'solar_midnight'))
|
||||
self.assertEqual(next_noon, sun.get_astral_event_next(
|
||||
self.hass, 'solar_noon'))
|
||||
self.assertEqual(next_rising, sun.get_astral_event_next(
|
||||
self.hass, 'sunrise'))
|
||||
self.assertEqual(next_setting, sun.get_astral_event_next(
|
||||
self.hass, 'sunset'))
|
||||
|
||||
def test_date_events(self):
|
||||
"""Test retrieving next sun events."""
|
||||
utc_now = datetime(2016, 11, 1, 8, 0, 0, tzinfo=dt_util.UTC)
|
||||
from astral import Astral
|
||||
|
||||
astral = Astral()
|
||||
utc_today = utc_now.date()
|
||||
|
||||
latitude = self.hass.config.latitude
|
||||
longitude = self.hass.config.longitude
|
||||
|
||||
dawn = astral.dawn_utc(utc_today, latitude, longitude)
|
||||
dusk = astral.dusk_utc(utc_today, latitude, longitude)
|
||||
midnight = astral.solar_midnight_utc(utc_today, longitude)
|
||||
noon = astral.solar_noon_utc(utc_today, longitude)
|
||||
sunrise = astral.sunrise_utc(utc_today, latitude, longitude)
|
||||
sunset = astral.sunset_utc(utc_today, latitude, longitude)
|
||||
|
||||
self.assertEqual(dawn, sun.get_astral_event_date(
|
||||
self.hass, 'dawn', utc_today))
|
||||
self.assertEqual(dusk, sun.get_astral_event_date(
|
||||
self.hass, 'dusk', utc_today))
|
||||
self.assertEqual(midnight, sun.get_astral_event_date(
|
||||
self.hass, 'solar_midnight', utc_today))
|
||||
self.assertEqual(noon, sun.get_astral_event_date(
|
||||
self.hass, 'solar_noon', utc_today))
|
||||
self.assertEqual(sunrise, sun.get_astral_event_date(
|
||||
self.hass, 'sunrise', utc_today))
|
||||
self.assertEqual(sunset, sun.get_astral_event_date(
|
||||
self.hass, 'sunset', utc_today))
|
||||
|
||||
def test_date_events_default_date(self):
|
||||
"""Test retrieving next sun events."""
|
||||
utc_now = datetime(2016, 11, 1, 8, 0, 0, tzinfo=dt_util.UTC)
|
||||
from astral import Astral
|
||||
|
||||
astral = Astral()
|
||||
utc_today = utc_now.date()
|
||||
|
||||
latitude = self.hass.config.latitude
|
||||
longitude = self.hass.config.longitude
|
||||
|
||||
dawn = astral.dawn_utc(utc_today, latitude, longitude)
|
||||
dusk = astral.dusk_utc(utc_today, latitude, longitude)
|
||||
midnight = astral.solar_midnight_utc(utc_today, longitude)
|
||||
noon = astral.solar_noon_utc(utc_today, longitude)
|
||||
sunrise = astral.sunrise_utc(utc_today, latitude, longitude)
|
||||
sunset = astral.sunset_utc(utc_today, latitude, longitude)
|
||||
|
||||
with patch('homeassistant.util.dt.now', return_value=utc_now):
|
||||
self.assertEqual(dawn, sun.get_astral_event_date(
|
||||
self.hass, 'dawn', utc_today))
|
||||
self.assertEqual(dusk, sun.get_astral_event_date(
|
||||
self.hass, 'dusk', utc_today))
|
||||
self.assertEqual(midnight, sun.get_astral_event_date(
|
||||
self.hass, 'solar_midnight', utc_today))
|
||||
self.assertEqual(noon, sun.get_astral_event_date(
|
||||
self.hass, 'solar_noon', utc_today))
|
||||
self.assertEqual(sunrise, sun.get_astral_event_date(
|
||||
self.hass, 'sunrise', utc_today))
|
||||
self.assertEqual(sunset, sun.get_astral_event_date(
|
||||
self.hass, 'sunset', utc_today))
|
||||
|
||||
def test_date_events_accepts_datetime(self):
|
||||
"""Test retrieving next sun events."""
|
||||
utc_now = datetime(2016, 11, 1, 8, 0, 0, tzinfo=dt_util.UTC)
|
||||
from astral import Astral
|
||||
|
||||
astral = Astral()
|
||||
utc_today = utc_now.date()
|
||||
|
||||
latitude = self.hass.config.latitude
|
||||
longitude = self.hass.config.longitude
|
||||
|
||||
dawn = astral.dawn_utc(utc_today, latitude, longitude)
|
||||
dusk = astral.dusk_utc(utc_today, latitude, longitude)
|
||||
midnight = astral.solar_midnight_utc(utc_today, longitude)
|
||||
noon = astral.solar_noon_utc(utc_today, longitude)
|
||||
sunrise = astral.sunrise_utc(utc_today, latitude, longitude)
|
||||
sunset = astral.sunset_utc(utc_today, latitude, longitude)
|
||||
|
||||
self.assertEqual(dawn, sun.get_astral_event_date(
|
||||
self.hass, 'dawn', utc_now))
|
||||
self.assertEqual(dusk, sun.get_astral_event_date(
|
||||
self.hass, 'dusk', utc_now))
|
||||
self.assertEqual(midnight, sun.get_astral_event_date(
|
||||
self.hass, 'solar_midnight', utc_now))
|
||||
self.assertEqual(noon, sun.get_astral_event_date(
|
||||
self.hass, 'solar_noon', utc_now))
|
||||
self.assertEqual(sunrise, sun.get_astral_event_date(
|
||||
self.hass, 'sunrise', utc_now))
|
||||
self.assertEqual(sunset, sun.get_astral_event_date(
|
||||
self.hass, 'sunset', utc_now))
|
||||
|
||||
def test_is_up(self):
|
||||
"""Test retrieving next sun events."""
|
||||
utc_now = datetime(2016, 11, 1, 12, 0, 0, tzinfo=dt_util.UTC)
|
||||
with patch('homeassistant.helpers.condition.dt_util.utcnow',
|
||||
return_value=utc_now):
|
||||
self.assertFalse(sun.is_up(self.hass))
|
||||
|
||||
utc_now = datetime(2016, 11, 1, 18, 0, 0, tzinfo=dt_util.UTC)
|
||||
with patch('homeassistant.helpers.condition.dt_util.utcnow',
|
||||
return_value=utc_now):
|
||||
self.assertTrue(sun.is_up(self.hass))
|
||||
|
||||
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)
|
||||
|
||||
print(sun.get_astral_event_date(self.hass, 'sunrise',
|
||||
datetime(2017, 7, 25)))
|
||||
print(sun.get_astral_event_date(self.hass, 'sunset',
|
||||
datetime(2017, 7, 25)))
|
||||
|
||||
print(sun.get_astral_event_date(self.hass, 'sunrise',
|
||||
datetime(2017, 7, 26)))
|
||||
print(sun.get_astral_event_date(self.hass, 'sunset',
|
||||
datetime(2017, 7, 26)))
|
||||
|
||||
assert sun.get_astral_event_next(self.hass, 'sunrise', june) == \
|
||||
datetime(2016, 7, 25, 23, 23, 39, tzinfo=dt_util.UTC)
|
||||
assert sun.get_astral_event_next(self.hass, 'sunset', june) == \
|
||||
datetime(2016, 7, 26, 22, 19, 1, tzinfo=dt_util.UTC)
|
||||
assert sun.get_astral_event_date(self.hass, 'sunrise', june) is None
|
||||
assert sun.get_astral_event_date(self.hass, 'sunset', june) is None
|
||||
Reference in new issue
Block a user