Skip to content

Commit

Permalink
Fix: noon and midnight adjustment
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasBackx committed Dec 15, 2024
1 parent ccf4652 commit 1138a43
Showing 1 changed file with 15 additions and 24 deletions.
39 changes: 15 additions & 24 deletions custom_components/adaptive_lighting/color_and_brightness.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,30 +105,21 @@ def noon_and_midnight(
sunrise: datetime.datetime | None = None,
) -> tuple[datetime.datetime, datetime.datetime]:
"""Return the (adjusted) noon and midnight times for the given datetime."""
if (
self.sunrise_time is None
and self.sunset_time is None
and self.min_sunrise_time is None
and self.max_sunrise_time is None
and self.min_sunset_time is None
and self.max_sunset_time is None
):
solar_noon = self.astral_location.noon(dt, local=False)
solar_midnight = self.astral_location.midnight(dt, local=False)
return solar_noon, solar_midnight

if sunset is None:
sunset = self.sunset(dt)
if sunrise is None:
sunrise = self.sunrise(dt)

middle = abs(sunset - sunrise) / 2
if sunset > sunrise:
noon = sunrise + middle
midnight = noon + timedelta(hours=12) * (1 if noon.hour < 12 else -1)
else:
midnight = sunset + middle
noon = midnight + timedelta(hours=12) * (1 if midnight.hour < 12 else -1)
sunrise = sunrise or self.sunrise(dt)
sunset = sunset or self.sunset(dt)

solar_noon = self.astral_location.noon(dt, local=False)
solar_midnight = self.astral_location.midnight(dt, local=False)

solar_sunrise= self.astral_location.sunrise(dt, local=False)
solar_sunset= self.astral_location.sunset(dt, local=False)

sunrise_offset = solar_sunrise - sunrise
sunset_offset = solar_sunset - sunset

noon = solar_noon - sunrise_offset
midnight = solar_midnight - sunset_offset

return noon, midnight

def sun_events(self, dt: datetime.datetime) -> list[tuple[str, float]]:
Expand Down

0 comments on commit 1138a43

Please sign in to comment.