Enable strict typing for tod (#107284)

This commit is contained in:
Marc Mueller 2024-01-08 10:44:47 +01:00 committed by GitHub
parent c30bf1f6e1
commit d0e6ce193c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 12 deletions

View File

@ -397,6 +397,7 @@ homeassistant.components.tile.*
homeassistant.components.tilt_ble.*
homeassistant.components.time.*
homeassistant.components.time_date.*
homeassistant.components.tod.*
homeassistant.components.todo.*
homeassistant.components.tolo.*
homeassistant.components.tplink.*

View File

@ -4,7 +4,7 @@ from __future__ import annotations
from collections.abc import Callable
from datetime import datetime, time, timedelta
import logging
from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING, Any, Literal, TypeGuard
import voluptuous as vol
@ -35,6 +35,8 @@ from .const import (
CONF_BEFORE_TIME,
)
SunEventType = Literal["sunrise", "sunset"]
_LOGGER = logging.getLogger(__name__)
ATTR_AFTER = "after"
@ -60,7 +62,7 @@ async def async_setup_entry(
) -> None:
"""Initialize Times of the Day config entry."""
if hass.config.time_zone is None:
_LOGGER.error("Timezone is not set in Home Assistant configuration")
_LOGGER.error("Timezone is not set in Home Assistant configuration") # type: ignore[unreachable]
return
after = cv.time(config_entry.options[CONF_AFTER_TIME])
@ -83,7 +85,7 @@ async def async_setup_platform(
) -> None:
"""Set up the ToD sensors."""
if hass.config.time_zone is None:
_LOGGER.error("Timezone is not set in Home Assistant configuration")
_LOGGER.error("Timezone is not set in Home Assistant configuration") # type: ignore[unreachable]
return
after = config[CONF_AFTER]
@ -97,7 +99,7 @@ async def async_setup_platform(
async_add_entities([sensor])
def _is_sun_event(sun_event):
def _is_sun_event(sun_event: time | SunEventType) -> TypeGuard[SunEventType]:
"""Return true if event is sun event not time."""
return sun_event in (SUN_EVENT_SUNRISE, SUN_EVENT_SUNSET)
@ -172,8 +174,8 @@ class TodSensor(BinarySensorEntity):
# Calculate the today's event utc time or
# if not available take next
after_event_date = get_astral_event_date(
self.hass, str(self._after), nowutc
) or get_astral_event_next(self.hass, str(self._after), nowutc)
self.hass, self._after, nowutc
) or get_astral_event_next(self.hass, self._after, nowutc)
else:
# Convert local time provided to UTC today
# datetime.combine(date, time, tzinfo) is not supported
@ -188,13 +190,13 @@ class TodSensor(BinarySensorEntity):
# Calculate the today's event utc time or if not available take
# next
before_event_date = get_astral_event_date(
self.hass, str(self._before), nowutc
) or get_astral_event_next(self.hass, str(self._before), nowutc)
self.hass, self._before, nowutc
) or get_astral_event_next(self.hass, self._before, nowutc)
# Before is earlier than after
if before_event_date < after_event_date:
# Take next day for before
before_event_date = get_astral_event_next(
self.hass, str(self._before), after_event_date
self.hass, self._before, after_event_date
)
else:
# Convert local time provided to UTC today, see above
@ -248,7 +250,7 @@ class TodSensor(BinarySensorEntity):
assert self._time_before is not None
if _is_sun_event(self._after):
self._time_after = get_astral_event_next(
self.hass, str(self._after), self._time_after - self._after_offset
self.hass, self._after, self._time_after - self._after_offset
)
self._time_after += self._after_offset
else:
@ -259,7 +261,7 @@ class TodSensor(BinarySensorEntity):
if _is_sun_event(self._before):
self._time_before = get_astral_event_next(
self.hass, str(self._before), self._time_before - self._before_offset
self.hass, self._before, self._time_before - self._before_offset
)
self._time_before += self._before_offset
else:
@ -274,7 +276,7 @@ class TodSensor(BinarySensorEntity):
self._calculate_next_update()
@callback
def _clean_up_listener():
def _clean_up_listener() -> None:
if self._unsub_update is not None:
self._unsub_update()
self._unsub_update = None

View File

@ -3732,6 +3732,16 @@ disallow_untyped_defs = true
warn_return_any = true
warn_unreachable = true
[mypy-homeassistant.components.tod.*]
check_untyped_defs = true
disallow_incomplete_defs = true
disallow_subclassing_any = true
disallow_untyped_calls = true
disallow_untyped_decorators = true
disallow_untyped_defs = true
warn_return_any = true
warn_unreachable = true
[mypy-homeassistant.components.todo.*]
check_untyped_defs = true
disallow_incomplete_defs = true