mirror of
https://github.com/home-assistant/core.git
synced 2025-07-13 00:07:10 +00:00
Improve typing [helpers.sun] (#70892)
This commit is contained in:
parent
7dfe8591c4
commit
964c764dae
@ -20,6 +20,7 @@ homeassistant.helpers.entity_values
|
|||||||
homeassistant.helpers.event
|
homeassistant.helpers.event
|
||||||
homeassistant.helpers.reload
|
homeassistant.helpers.reload
|
||||||
homeassistant.helpers.script_variables
|
homeassistant.helpers.script_variables
|
||||||
|
homeassistant.helpers.sun
|
||||||
homeassistant.helpers.translation
|
homeassistant.helpers.translation
|
||||||
homeassistant.util.async_
|
homeassistant.util.async_
|
||||||
homeassistant.util.color
|
homeassistant.util.color
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
"""Helpers for sun events."""
|
"""Helpers for sun events."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Callable
|
||||||
import datetime
|
import datetime
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING, Any, cast
|
||||||
|
|
||||||
from homeassistant.const import SUN_EVENT_SUNRISE, SUN_EVENT_SUNSET
|
from homeassistant.const import SUN_EVENT_SUNRISE, SUN_EVENT_SUNSET
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
@ -11,11 +12,14 @@ from homeassistant.util import dt as dt_util
|
|||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
import astral
|
import astral
|
||||||
|
import astral.location
|
||||||
|
|
||||||
DATA_LOCATION_CACHE = "astral_location_cache"
|
DATA_LOCATION_CACHE = "astral_location_cache"
|
||||||
|
|
||||||
ELEVATION_AGNOSTIC_EVENTS = ("noon", "midnight")
|
ELEVATION_AGNOSTIC_EVENTS = ("noon", "midnight")
|
||||||
|
|
||||||
|
_AstralSunEventCallable = Callable[..., datetime.datetime]
|
||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
@bind_hass
|
@bind_hass
|
||||||
@ -73,15 +77,15 @@ def get_location_astral_event_next(
|
|||||||
if utc_point_in_time is None:
|
if utc_point_in_time is None:
|
||||||
utc_point_in_time = dt_util.utcnow()
|
utc_point_in_time = dt_util.utcnow()
|
||||||
|
|
||||||
kwargs = {"local": False}
|
kwargs: dict[str, Any] = {"local": False}
|
||||||
if event not in ELEVATION_AGNOSTIC_EVENTS:
|
if event not in ELEVATION_AGNOSTIC_EVENTS:
|
||||||
kwargs["observer_elevation"] = elevation
|
kwargs["observer_elevation"] = elevation
|
||||||
|
|
||||||
mod = -1
|
mod = -1
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
next_dt: datetime.datetime = (
|
next_dt = (
|
||||||
getattr(location, event)(
|
cast(_AstralSunEventCallable, getattr(location, event))(
|
||||||
dt_util.as_local(utc_point_in_time).date()
|
dt_util.as_local(utc_point_in_time).date()
|
||||||
+ datetime.timedelta(days=mod),
|
+ datetime.timedelta(days=mod),
|
||||||
**kwargs,
|
**kwargs,
|
||||||
@ -111,12 +115,12 @@ def get_astral_event_date(
|
|||||||
if isinstance(date, datetime.datetime):
|
if isinstance(date, datetime.datetime):
|
||||||
date = dt_util.as_local(date).date()
|
date = dt_util.as_local(date).date()
|
||||||
|
|
||||||
kwargs = {"local": False}
|
kwargs: dict[str, Any] = {"local": False}
|
||||||
if event not in ELEVATION_AGNOSTIC_EVENTS:
|
if event not in ELEVATION_AGNOSTIC_EVENTS:
|
||||||
kwargs["observer_elevation"] = elevation
|
kwargs["observer_elevation"] = elevation
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return getattr(location, event)(date, **kwargs) # type: ignore[no-any-return]
|
return cast(_AstralSunEventCallable, getattr(location, event))(date, **kwargs)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
# Event never occurs for specified date.
|
# Event never occurs for specified date.
|
||||||
return None
|
return None
|
||||||
|
3
mypy.ini
3
mypy.ini
@ -71,6 +71,9 @@ disallow_any_generics = true
|
|||||||
[mypy-homeassistant.helpers.script_variables]
|
[mypy-homeassistant.helpers.script_variables]
|
||||||
disallow_any_generics = true
|
disallow_any_generics = true
|
||||||
|
|
||||||
|
[mypy-homeassistant.helpers.sun]
|
||||||
|
disallow_any_generics = true
|
||||||
|
|
||||||
[mypy-homeassistant.helpers.translation]
|
[mypy-homeassistant.helpers.translation]
|
||||||
disallow_any_generics = true
|
disallow_any_generics = true
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user