Fix infinite loop in sun.sun (#89723)
This commit is contained in:
parent
4ddcb14053
commit
858fc30fcd
2 changed files with 22 additions and 3 deletions
|
@ -82,7 +82,8 @@ def get_location_astral_event_next(
|
|||
kwargs["observer_elevation"] = elevation
|
||||
|
||||
mod = -1
|
||||
while True:
|
||||
first_err = None
|
||||
while mod < 367:
|
||||
try:
|
||||
next_dt = (
|
||||
cast(_AstralSunEventCallable, getattr(location, event))(
|
||||
|
@ -94,9 +95,13 @@ def get_location_astral_event_next(
|
|||
)
|
||||
if next_dt > utc_point_in_time:
|
||||
return next_dt
|
||||
except ValueError:
|
||||
pass
|
||||
except ValueError as err:
|
||||
if not first_err:
|
||||
first_err = err
|
||||
mod += 1
|
||||
raise ValueError(
|
||||
f"Unable to find event after one year, initial ValueError: {first_err}"
|
||||
) from first_err
|
||||
|
||||
|
||||
@callback
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
from datetime import datetime, timedelta
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.const import SUN_EVENT_SUNRISE, SUN_EVENT_SUNSET
|
||||
from homeassistant.core import HomeAssistant
|
||||
import homeassistant.helpers.sun as sun
|
||||
|
@ -192,3 +194,15 @@ def test_norway_in_june(hass: HomeAssistant) -> None:
|
|||
)
|
||||
assert sun.get_astral_event_date(hass, SUN_EVENT_SUNRISE, june) is None
|
||||
assert sun.get_astral_event_date(hass, SUN_EVENT_SUNSET, june) is None
|
||||
|
||||
|
||||
def test_impossible_elevation(hass: HomeAssistant) -> None:
|
||||
"""Test altitude where the sun can't set."""
|
||||
hass.config.latitude = 69.6
|
||||
hass.config.longitude = 18.8
|
||||
hass.config.elevation = 10000000
|
||||
|
||||
june = datetime(2016, 6, 1, tzinfo=dt_util.UTC)
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
sun.get_astral_event_next(hass, SUN_EVENT_SUNRISE, june)
|
||||
|
|
Loading…
Add table
Reference in a new issue