Strict typing derivative (#82785)

This commit is contained in:
G Johansson 2022-11-28 15:12:22 +01:00 committed by GitHub
parent 4c3d481b7b
commit db480191ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 13 deletions

View File

@ -86,6 +86,7 @@ homeassistant.components.cpuspeed.*
homeassistant.components.crownstone.* homeassistant.components.crownstone.*
homeassistant.components.deconz.* homeassistant.components.deconz.*
homeassistant.components.demo.* homeassistant.components.demo.*
homeassistant.components.derivative.*
homeassistant.components.device_automation.* homeassistant.components.device_automation.*
homeassistant.components.device_tracker.* homeassistant.components.device_tracker.*
homeassistant.components.devolo_home_control.* homeassistant.components.devolo_home_control.*

View File

@ -4,6 +4,7 @@ from __future__ import annotations
from datetime import datetime, timedelta from datetime import datetime, timedelta
from decimal import Decimal, DecimalException from decimal import Decimal, DecimalException
import logging import logging
from typing import TYPE_CHECKING
import voluptuous as vol import voluptuous as vol
@ -137,20 +138,20 @@ class DerivativeSensor(RestoreEntity, SensorEntity):
def __init__( def __init__(
self, self,
*, *,
name, name: str | None,
round_digits, round_digits: int,
source_entity, source_entity: str,
time_window, time_window: timedelta,
unit_of_measurement, unit_of_measurement: str | None,
unit_prefix, unit_prefix: str | None,
unit_time, unit_time: str,
unique_id, unique_id: str | None,
): ) -> None:
"""Initialize the derivative sensor.""" """Initialize the derivative sensor."""
self._attr_unique_id = unique_id self._attr_unique_id = unique_id
self._sensor_source_id = source_entity self._sensor_source_id = source_entity
self._round_digits = round_digits self._round_digits = round_digits
self._state = 0 self._state: float | int | Decimal = 0
# List of tuples with (timestamp_start, timestamp_end, derivative) # List of tuples with (timestamp_start, timestamp_end, derivative)
self._state_list: list[tuple[datetime, datetime, Decimal]] = [] self._state_list: list[tuple[datetime, datetime, Decimal]] = []
@ -231,7 +232,9 @@ class DerivativeSensor(RestoreEntity, SensorEntity):
(old_state.last_updated, new_state.last_updated, new_derivative) (old_state.last_updated, new_state.last_updated, new_derivative)
) )
def calculate_weight(start, end, now): def calculate_weight(
start: datetime, end: datetime, now: datetime
) -> float:
window_start = now - timedelta(seconds=self._time_window) window_start = now - timedelta(seconds=self._time_window)
if start < window_start: if start < window_start:
weight = (end - window_start).total_seconds() / self._time_window weight = (end - window_start).total_seconds() / self._time_window
@ -259,6 +262,9 @@ class DerivativeSensor(RestoreEntity, SensorEntity):
) )
@property @property
def native_value(self): def native_value(self) -> float | int | Decimal:
"""Return the state of the sensor.""" """Return the state of the sensor."""
return round(self._state, self._round_digits) value = round(self._state, self._round_digits)
if TYPE_CHECKING:
assert isinstance(value, (float, int, Decimal))
return value

View File

@ -613,6 +613,16 @@ disallow_untyped_defs = true
warn_return_any = true warn_return_any = true
warn_unreachable = true warn_unreachable = true
[mypy-homeassistant.components.derivative.*]
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.device_automation.*] [mypy-homeassistant.components.device_automation.*]
check_untyped_defs = true check_untyped_defs = true
disallow_incomplete_defs = true disallow_incomplete_defs = true