Improve typing of trend component (#99719)

* Some typing in trend component

* Add missing type hint

* Enable strict typing on trend
This commit is contained in:
Jan-Philipp Benecke 2023-09-06 18:51:38 +02:00 committed by GitHub
parent 9bc07f50f9
commit 7a6c8767b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 17 deletions

View File

@ -336,6 +336,7 @@ homeassistant.components.trafikverket_camera.*
homeassistant.components.trafikverket_ferry.* homeassistant.components.trafikverket_ferry.*
homeassistant.components.trafikverket_train.* homeassistant.components.trafikverket_train.*
homeassistant.components.trafikverket_weatherstation.* homeassistant.components.trafikverket_weatherstation.*
homeassistant.components.trend.*
homeassistant.components.tts.* homeassistant.components.tts.*
homeassistant.components.twentemilieu.* homeassistant.components.twentemilieu.*
homeassistant.components.unifi.* homeassistant.components.unifi.*

View File

@ -2,8 +2,10 @@
from __future__ import annotations from __future__ import annotations
from collections import deque from collections import deque
from collections.abc import Mapping
import logging import logging
import math import math
from typing import Any
import numpy as np import numpy as np
import voluptuous as vol import voluptuous as vol
@ -12,6 +14,7 @@ from homeassistant.components.binary_sensor import (
DEVICE_CLASSES_SCHEMA, DEVICE_CLASSES_SCHEMA,
ENTITY_ID_FORMAT, ENTITY_ID_FORMAT,
PLATFORM_SCHEMA, PLATFORM_SCHEMA,
BinarySensorDeviceClass,
BinarySensorEntity, BinarySensorEntity,
) )
from homeassistant.const import ( from homeassistant.const import (
@ -117,20 +120,22 @@ class SensorTrend(BinarySensorEntity):
"""Representation of a trend Sensor.""" """Representation of a trend Sensor."""
_attr_should_poll = False _attr_should_poll = False
_gradient = 0.0
_state: bool | None = None
def __init__( def __init__(
self, self,
hass, hass: HomeAssistant,
device_id, device_id: str,
friendly_name, friendly_name: str,
entity_id, entity_id: str,
attribute, attribute: str,
device_class, device_class: BinarySensorDeviceClass,
invert, invert: bool,
max_samples, max_samples: int,
min_gradient, min_gradient: float,
sample_duration, sample_duration: int,
): ) -> None:
"""Initialize the sensor.""" """Initialize the sensor."""
self._hass = hass self._hass = hass
self.entity_id = generate_entity_id(ENTITY_ID_FORMAT, device_id, 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._invert = invert
self._sample_duration = sample_duration self._sample_duration = sample_duration
self._min_gradient = min_gradient self._min_gradient = min_gradient
self._gradient = None self.samples: deque = deque(maxlen=max_samples)
self._state = None
self.samples = deque(maxlen=max_samples)
@property @property
def is_on(self): def is_on(self) -> bool | None:
"""Return true if sensor is on.""" """Return true if sensor is on."""
return self._state return self._state
@property @property
def extra_state_attributes(self): def extra_state_attributes(self) -> Mapping[str, Any]:
"""Return the state attributes of the sensor.""" """Return the state attributes of the sensor."""
return { return {
ATTR_ENTITY_ID: self._entity_id, ATTR_ENTITY_ID: self._entity_id,
@ -214,7 +217,7 @@ class SensorTrend(BinarySensorEntity):
if self._invert: if self._invert:
self._state = not self._state self._state = not self._state
def _calculate_gradient(self): def _calculate_gradient(self) -> None:
"""Compute the linear trend gradient of the current samples. """Compute the linear trend gradient of the current samples.
This need run inside executor. This need run inside executor.

View File

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