Use class attributes in Times of Day (#129543)

* mypy ignore assignment in Times of Day so we can drop all type checking

* class attributes
This commit is contained in:
G Johansson 2024-10-31 17:37:33 +01:00 committed by GitHub
parent b1dfc3cd23
commit b1d48fe9a2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -5,7 +5,7 @@ from __future__ import annotations
from collections.abc import Callable from collections.abc import Callable
from datetime import datetime, time, timedelta from datetime import datetime, time, timedelta
import logging import logging
from typing import TYPE_CHECKING, Any, Literal, TypeGuard from typing import Any, Literal, TypeGuard
import voluptuous as vol import voluptuous as vol
@ -109,6 +109,9 @@ class TodSensor(BinarySensorEntity):
"""Time of the Day Sensor.""" """Time of the Day Sensor."""
_attr_should_poll = False _attr_should_poll = False
_time_before: datetime
_time_after: datetime
_next_update: datetime
def __init__( def __init__(
self, self,
@ -122,9 +125,6 @@ class TodSensor(BinarySensorEntity):
"""Init the ToD Sensor...""" """Init the ToD Sensor..."""
self._attr_unique_id = unique_id self._attr_unique_id = unique_id
self._attr_name = name self._attr_name = name
self._time_before: datetime | None = None
self._time_after: datetime | None = None
self._next_update: datetime | None = None
self._after_offset = after_offset self._after_offset = after_offset
self._before_offset = before_offset self._before_offset = before_offset
self._before = before self._before = before
@ -134,9 +134,6 @@ class TodSensor(BinarySensorEntity):
@property @property
def is_on(self) -> bool: def is_on(self) -> bool:
"""Return True is sensor is on.""" """Return True is sensor is on."""
if TYPE_CHECKING:
assert self._time_after is not None
assert self._time_before is not None
if self._time_after < self._time_before: if self._time_after < self._time_before:
return self._time_after <= dt_util.utcnow() < self._time_before return self._time_after <= dt_util.utcnow() < self._time_before
return False return False
@ -144,10 +141,6 @@ class TodSensor(BinarySensorEntity):
@property @property
def extra_state_attributes(self) -> dict[str, Any] | None: def extra_state_attributes(self) -> dict[str, Any] | None:
"""Return the state attributes of the sensor.""" """Return the state attributes of the sensor."""
if TYPE_CHECKING:
assert self._time_after is not None
assert self._time_before is not None
assert self._next_update is not None
if time_zone := dt_util.get_default_time_zone(): if time_zone := dt_util.get_default_time_zone():
return { return {
ATTR_AFTER: self._time_after.astimezone(time_zone).isoformat(), ATTR_AFTER: self._time_after.astimezone(time_zone).isoformat(),
@ -244,9 +237,6 @@ class TodSensor(BinarySensorEntity):
def _turn_to_next_day(self) -> None: def _turn_to_next_day(self) -> None:
"""Turn to to the next day.""" """Turn to to the next day."""
if TYPE_CHECKING:
assert self._time_after is not None
assert self._time_before is not None
if _is_sun_event(self._after): if _is_sun_event(self._after):
self._time_after = get_astral_event_next( self._time_after = get_astral_event_next(
self.hass, self._after, self._time_after - self._after_offset self.hass, self._after, self._time_after - self._after_offset
@ -282,17 +272,12 @@ class TodSensor(BinarySensorEntity):
self.async_on_remove(_clean_up_listener) self.async_on_remove(_clean_up_listener)
if TYPE_CHECKING:
assert self._next_update is not None
self._unsub_update = event.async_track_point_in_utc_time( self._unsub_update = event.async_track_point_in_utc_time(
self.hass, self._point_in_time_listener, self._next_update self.hass, self._point_in_time_listener, self._next_update
) )
def _calculate_next_update(self) -> None: def _calculate_next_update(self) -> None:
"""Datetime when the next update to the state.""" """Datetime when the next update to the state."""
if TYPE_CHECKING:
assert self._time_after is not None
assert self._time_before is not None
now = dt_util.utcnow() now = dt_util.utcnow()
if now < self._time_after: if now < self._time_after:
self._next_update = self._time_after self._next_update = self._time_after
@ -309,9 +294,6 @@ class TodSensor(BinarySensorEntity):
self._calculate_next_update() self._calculate_next_update()
self.async_write_ha_state() self.async_write_ha_state()
if TYPE_CHECKING:
assert self._next_update is not None
self._unsub_update = event.async_track_point_in_utc_time( self._unsub_update = event.async_track_point_in_utc_time(
self.hass, self._point_in_time_listener, self._next_update self.hass, self._point_in_time_listener, self._next_update
) )