Add more type hints to helpers (#18196)

* Test typing for helpers.__init__ and temperature

* Add type hints to helpers.sun

* Add type hints to helpers.signal

* Add type hints to helpers.entity_values

* Add type hints to helpers.dispatcher
This commit is contained in:
Ville Skyttä 2018-11-04 23:46:42 +02:00 committed by Paulus Schoutsen
parent 959fa81ea6
commit 922f34f72d
5 changed files with 45 additions and 26 deletions

View file

@ -1,23 +1,28 @@
"""Helpers for sun events."""
import datetime
from typing import Optional, Union, TYPE_CHECKING
from homeassistant.const import SUN_EVENT_SUNRISE, SUN_EVENT_SUNSET
from homeassistant.core import callback
from homeassistant.util import dt as dt_util
from homeassistant.loader import bind_hass
from .typing import HomeAssistantType
if TYPE_CHECKING:
import astral # pylint: disable=unused-import
DATA_LOCATION_CACHE = 'astral_location_cache'
@callback
@bind_hass
def get_astral_location(hass):
def get_astral_location(hass: HomeAssistantType) -> 'astral.Location':
"""Get an astral location for the current Home Assistant configuration."""
from astral import Location
latitude = hass.config.latitude
longitude = hass.config.longitude
timezone = hass.config.time_zone.zone
timezone = str(hass.config.time_zone)
elevation = hass.config.elevation
info = ('', '', latitude, longitude, timezone, elevation)
@ -33,9 +38,12 @@ def get_astral_location(hass):
@callback
@bind_hass
def get_astral_event_next(hass, event, utc_point_in_time=None, offset=None):
def get_astral_event_next(
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."""
import astral
from astral import AstralError
location = get_astral_location(hass)
@ -51,19 +59,22 @@ def get_astral_event_next(hass, event, utc_point_in_time=None, offset=None):
next_dt = getattr(location, event)(
dt_util.as_local(utc_point_in_time).date() +
datetime.timedelta(days=mod),
local=False) + offset
local=False) + offset # type: datetime.datetime
if next_dt > utc_point_in_time:
return next_dt
except astral.AstralError:
except AstralError:
pass
mod += 1
@callback
@bind_hass
def get_astral_event_date(hass, event, date=None):
def get_astral_event_date(
hass: HomeAssistantType, event: str,
date: Union[datetime.date, datetime.datetime, None] = None) \
-> Optional[datetime.datetime]:
"""Calculate the astral event time for the specified date."""
import astral
from astral import AstralError
location = get_astral_location(hass)
@ -74,15 +85,16 @@ def get_astral_event_date(hass, event, date=None):
date = dt_util.as_local(date).date()
try:
return getattr(location, event)(date, local=False)
except astral.AstralError:
return getattr(location, event)(date, local=False) # type: ignore
except AstralError:
# Event never occurs for specified date.
return None
@callback
@bind_hass
def is_up(hass, utc_point_in_time=None):
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()