This commit is contained in:
Paulus Schoutsen
2019-07-31 12:25:30 -07:00
parent da05dfe708
commit 4de97abc3a
2676 changed files with 163166 additions and 140084 deletions

View File

@@ -11,12 +11,12 @@ from .typing import HomeAssistantType
if TYPE_CHECKING:
import astral # pylint: disable=unused-import
DATA_LOCATION_CACHE = 'astral_location_cache'
DATA_LOCATION_CACHE = "astral_location_cache"
@callback
@bind_hass
def get_astral_location(hass: HomeAssistantType) -> 'astral.Location':
def get_astral_location(hass: HomeAssistantType) -> "astral.Location":
"""Get an astral location for the current Home Assistant configuration."""
from astral import Location
@@ -24,7 +24,7 @@ def get_astral_location(hass: HomeAssistantType) -> 'astral.Location':
longitude = hass.config.longitude
timezone = str(hass.config.time_zone)
elevation = hass.config.elevation
info = ('', '', latitude, longitude, timezone, elevation)
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:
@@ -39,20 +39,23 @@ def get_astral_location(hass: HomeAssistantType) -> 'astral.Location':
@callback
@bind_hass
def get_astral_event_next(
hass: HomeAssistantType, event: str,
utc_point_in_time: Optional[datetime.datetime] = None,
offset: Optional[datetime.timedelta] = None) -> datetime.datetime:
hass: HomeAssistantType,
event: str,
utc_point_in_time: Optional[datetime.datetime] = None,
offset: Optional[datetime.timedelta] = None,
) -> datetime.datetime:
"""Calculate the next specified solar event."""
location = get_astral_location(hass)
return get_location_astral_event_next(
location, event, utc_point_in_time, offset)
return get_location_astral_event_next(location, event, utc_point_in_time, offset)
@callback
def get_location_astral_event_next(
location: 'astral.Location', event: str,
utc_point_in_time: Optional[datetime.datetime] = None,
offset: Optional[datetime.timedelta] = None) -> datetime.datetime:
location: "astral.Location",
event: str,
utc_point_in_time: Optional[datetime.datetime] = None,
offset: Optional[datetime.timedelta] = None,
) -> datetime.datetime:
"""Calculate the next specified solar event."""
from astral import AstralError
@@ -65,10 +68,14 @@ def get_location_astral_event_next(
mod = -1
while True:
try:
next_dt = getattr(location, event)(
dt_util.as_local(utc_point_in_time).date() +
datetime.timedelta(days=mod),
local=False) + offset # type: datetime.datetime
next_dt = (
getattr(location, event)(
dt_util.as_local(utc_point_in_time).date()
+ datetime.timedelta(days=mod),
local=False,
)
+ offset
) # type: datetime.datetime
if next_dt > utc_point_in_time:
return next_dt
except AstralError:
@@ -79,9 +86,10 @@ def get_location_astral_event_next(
@callback
@bind_hass
def get_astral_event_date(
hass: HomeAssistantType, event: str,
date: Union[datetime.date, datetime.datetime, None] = None) \
-> Optional[datetime.datetime]:
hass: HomeAssistantType,
event: str,
date: Union[datetime.date, datetime.datetime, None] = None,
) -> Optional[datetime.datetime]:
"""Calculate the astral event time for the specified date."""
from astral import AstralError
@@ -102,15 +110,14 @@ def get_astral_event_date(
@callback
@bind_hass
def is_up(hass: HomeAssistantType,
utc_point_in_time: Optional[datetime.datetime] = None) -> bool:
def is_up(
hass: HomeAssistantType, utc_point_in_time: Optional[datetime.datetime] = None
) -> bool:
"""Calculate if the sun is currently up."""
if utc_point_in_time is None:
utc_point_in_time = dt_util.utcnow()
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)
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