Skip to content

Commit

Permalink
Improve typing of trend component (#99719)
Browse files Browse the repository at this point in the history
* Some typing in trend component

* Add missing type hint

* Enable strict typing on trend
  • Loading branch information
jpbede committed Sep 6, 2023
1 parent 9bc07f5 commit 7a6c876
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
1 change: 1 addition & 0 deletions .strict-typing
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down
37 changes: 20 additions & 17 deletions homeassistant/components/trend/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -12,6 +14,7 @@
DEVICE_CLASSES_SCHEMA,
ENTITY_ID_FORMAT,
PLATFORM_SCHEMA,
BinarySensorDeviceClass,
BinarySensorEntity,
)
from homeassistant.const import (
Expand Down Expand Up @@ -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)
Expand All @@ -141,17 +146,15 @@ def __init__(
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,
Expand Down Expand Up @@ -214,7 +217,7 @@ async def async_update(self) -> None:
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.
Expand Down
10 changes: 10 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7a6c876

Please sign in to comment.