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_train.*
homeassistant.components.trafikverket_weatherstation.*
homeassistant.components.trend.*
homeassistant.components.tts.*
homeassistant.components.twentemilieu.*
homeassistant.components.unifi.*

View File

@ -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.

View File

@ -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