From 7a6c8767b3f3a06d59dfac1ccdc5cb3ce7733687 Mon Sep 17 00:00:00 2001 From: Jan-Philipp Benecke Date: Wed, 6 Sep 2023 18:51:38 +0200 Subject: [PATCH] Improve typing of trend component (#99719) * Some typing in trend component * Add missing type hint * Enable strict typing on trend --- .strict-typing | 1 + .../components/trend/binary_sensor.py | 37 ++++++++++--------- mypy.ini | 10 +++++ 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/.strict-typing b/.strict-typing index f49e576a774..30d20a6fc54 100644 --- a/.strict-typing +++ b/.strict-typing @@ -336,6 +336,7 @@ homeassistant.components.trafikverket_camera.* homeassistant.components.trafikverket_ferry.* homeassistant.components.trafikverket_train.* homeassistant.components.trafikverket_weatherstation.* +homeassistant.components.trend.* homeassistant.components.tts.* homeassistant.components.twentemilieu.* homeassistant.components.unifi.* diff --git a/homeassistant/components/trend/binary_sensor.py b/homeassistant/components/trend/binary_sensor.py index 815403e1e87..089e82b0f07 100644 --- a/homeassistant/components/trend/binary_sensor.py +++ b/homeassistant/components/trend/binary_sensor.py @@ -2,8 +2,10 @@ from __future__ import annotations from collections import deque +from collections.abc import Mapping import logging import math +from typing import Any import numpy as np import voluptuous as vol @@ -12,6 +14,7 @@ from homeassistant.components.binary_sensor import ( DEVICE_CLASSES_SCHEMA, ENTITY_ID_FORMAT, PLATFORM_SCHEMA, + BinarySensorDeviceClass, BinarySensorEntity, ) from homeassistant.const import ( @@ -117,20 +120,22 @@ class SensorTrend(BinarySensorEntity): """Representation of a trend Sensor.""" _attr_should_poll = False + _gradient = 0.0 + _state: bool | None = None def __init__( self, - hass, - device_id, - friendly_name, - entity_id, - attribute, - device_class, - invert, - max_samples, - min_gradient, - sample_duration, - ): + hass: HomeAssistant, + device_id: str, + friendly_name: str, + entity_id: str, + attribute: str, + device_class: BinarySensorDeviceClass, + invert: bool, + max_samples: int, + min_gradient: float, + sample_duration: int, + ) -> None: """Initialize the sensor.""" self._hass = hass self.entity_id = generate_entity_id(ENTITY_ID_FORMAT, device_id, hass=hass) @@ -141,17 +146,15 @@ class SensorTrend(BinarySensorEntity): self._invert = invert self._sample_duration = sample_duration self._min_gradient = min_gradient - self._gradient = None - self._state = None - self.samples = deque(maxlen=max_samples) + self.samples: deque = deque(maxlen=max_samples) @property - def is_on(self): + def is_on(self) -> bool | None: """Return true if sensor is on.""" return self._state @property - def extra_state_attributes(self): + def extra_state_attributes(self) -> Mapping[str, Any]: """Return the state attributes of the sensor.""" return { ATTR_ENTITY_ID: self._entity_id, @@ -214,7 +217,7 @@ class SensorTrend(BinarySensorEntity): if self._invert: self._state = not self._state - def _calculate_gradient(self): + def _calculate_gradient(self) -> None: """Compute the linear trend gradient of the current samples. This need run inside executor. diff --git a/mypy.ini b/mypy.ini index 6303f2b2706..1c3fc1a52ed 100644 --- a/mypy.ini +++ b/mypy.ini @@ -3123,6 +3123,16 @@ disallow_untyped_defs = true warn_return_any = true warn_unreachable = true +[mypy-homeassistant.components.trend.*] +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.tts.*] check_untyped_defs = true disallow_incomplete_defs = true