mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
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:
committed by
Paulus Schoutsen
parent
419d97fc06
commit
40d27cde0e
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user