mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Strict typing derivative (#82785)
This commit is contained in:
parent
4c3d481b7b
commit
db480191ad
@ -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.*
|
||||||
|
@ -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
|
||||||
|
10
mypy.ini
10
mypy.ini
@ -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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user