core/homeassistant/helpers/sun.py

125 lines
3.6 KiB
Python
Raw Normal View History

"""Helpers for sun events."""
from __future__ import annotations
import datetime
2021-03-17 17:34:19 +00:00
from typing import TYPE_CHECKING
2018-10-31 08:10:28 +00:00
from homeassistant.const import SUN_EVENT_SUNRISE, SUN_EVENT_SUNSET
from homeassistant.core import HomeAssistant, callback
from homeassistant.loader import bind_hass
from homeassistant.util import dt as dt_util
if TYPE_CHECKING:
import astral
2019-07-31 19:25:30 +00:00
DATA_LOCATION_CACHE = "astral_location_cache"
@callback
@bind_hass
def get_astral_location(hass: HomeAssistant) -> astral.Location:
"""Get an astral location for the current Home Assistant configuration."""
from astral import Location # pylint: disable=import-outside-toplevel
latitude = hass.config.latitude
longitude = hass.config.longitude
timezone = str(hass.config.time_zone)
elevation = hass.config.elevation
2019-07-31 19:25:30 +00:00
info = ("", "", latitude, longitude, timezone, elevation)
# Cache astral locations so they aren't recreated with the same args
if DATA_LOCATION_CACHE not in hass.data:
hass.data[DATA_LOCATION_CACHE] = {}
if info not in hass.data[DATA_LOCATION_CACHE]:
hass.data[DATA_LOCATION_CACHE][info] = Location(info)
return hass.data[DATA_LOCATION_CACHE][info]
@callback
@bind_hass
def get_astral_event_next(
hass: HomeAssistant,
2019-07-31 19:25:30 +00:00
event: str,
2021-03-17 17:34:19 +00:00
utc_point_in_time: datetime.datetime | None = None,
offset: datetime.timedelta | None = None,
2019-07-31 19:25:30 +00:00
) -> datetime.datetime:
"""Calculate the next specified solar event."""
location = get_astral_location(hass)
2019-07-31 19:25:30 +00:00
return get_location_astral_event_next(location, event, utc_point_in_time, offset)
@callback
def get_location_astral_event_next(
2021-03-22 12:29:39 +00:00
location: astral.Location,
2019-07-31 19:25:30 +00:00
event: str,
2021-03-17 17:34:19 +00:00
utc_point_in_time: datetime.datetime | None = None,
offset: datetime.timedelta | None = None,
2019-07-31 19:25:30 +00:00
) -> datetime.datetime:
"""Calculate the next specified solar event."""
from astral import AstralError # pylint: disable=import-outside-toplevel
if offset is None:
offset = datetime.timedelta()
if utc_point_in_time is None:
utc_point_in_time = dt_util.utcnow()
mod = -1
while True:
try:
next_dt: datetime.datetime = (
2019-07-31 19:25:30 +00:00
getattr(location, event)(
dt_util.as_local(utc_point_in_time).date()
+ datetime.timedelta(days=mod),
local=False,
)
+ offset
)
if next_dt > utc_point_in_time:
return next_dt
except AstralError:
pass
mod += 1
@callback
@bind_hass
def get_astral_event_date(
hass: HomeAssistant,
2019-07-31 19:25:30 +00:00
event: str,
2021-03-17 17:34:19 +00:00
date: datetime.date | datetime.datetime | None = None,
) -> datetime.datetime | None:
"""Calculate the astral event time for the specified date."""
from astral import AstralError # pylint: disable=import-outside-toplevel
location = get_astral_location(hass)
if date is None:
date = dt_util.now().date()
if isinstance(date, datetime.datetime):
date = dt_util.as_local(date).date()
try:
return getattr(location, event)(date, local=False) # type: ignore
except AstralError:
# Event never occurs for specified date.
return None
@callback
@bind_hass
2019-07-31 19:25:30 +00:00
def is_up(
hass: HomeAssistant, utc_point_in_time: datetime.datetime | None = None
2019-07-31 19:25:30 +00:00
) -> bool:
"""Calculate if the sun is currently up."""
if utc_point_in_time is None:
utc_point_in_time = dt_util.utcnow()
2019-07-31 19:25:30 +00:00
next_sunrise = get_astral_event_next(hass, SUN_EVENT_SUNRISE, utc_point_in_time)
next_sunset = get_astral_event_next(hass, SUN_EVENT_SUNSET, utc_point_in_time)
return next_sunrise > next_sunset