Enable strict type checks for PEGELONLINE (#143563)

enable strict type checks for pegel_online
This commit is contained in:
Michael
2025-04-24 14:42:47 +02:00
committed by GitHub
parent 9e0a7122f5
commit 49522d93df
3 changed files with 26 additions and 12 deletions

View File

@ -386,6 +386,7 @@ homeassistant.components.pandora.*
homeassistant.components.panel_custom.*
homeassistant.components.peblar.*
homeassistant.components.peco.*
homeassistant.components.pegel_online.*
homeassistant.components.persistent_notification.*
homeassistant.components.person.*
homeassistant.components.pi_hole.*

View File

@ -2,9 +2,10 @@
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from aiopegelonline.models import CurrentMeasurement
from aiopegelonline.models import CurrentMeasurement, StationMeasurements
from homeassistant.components.sensor import (
SensorDeviceClass,
@ -24,67 +25,67 @@ from .entity import PegelOnlineEntity
class PegelOnlineSensorEntityDescription(SensorEntityDescription):
"""PEGELONLINE sensor entity description."""
measurement_key: str
measurement_fn: Callable[[StationMeasurements], CurrentMeasurement | None]
SENSORS: tuple[PegelOnlineSensorEntityDescription, ...] = (
PegelOnlineSensorEntityDescription(
key="air_temperature",
translation_key="air_temperature",
measurement_key="air_temperature",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.TEMPERATURE,
entity_registry_enabled_default=False,
measurement_fn=lambda data: data.air_temperature,
),
PegelOnlineSensorEntityDescription(
key="clearance_height",
translation_key="clearance_height",
measurement_key="clearance_height",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.DISTANCE,
measurement_fn=lambda data: data.clearance_height,
),
PegelOnlineSensorEntityDescription(
key="oxygen_level",
translation_key="oxygen_level",
measurement_key="oxygen_level",
state_class=SensorStateClass.MEASUREMENT,
entity_registry_enabled_default=False,
measurement_fn=lambda data: data.oxygen_level,
),
PegelOnlineSensorEntityDescription(
key="ph_value",
measurement_key="ph_value",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.PH,
entity_registry_enabled_default=False,
measurement_fn=lambda data: data.ph_value,
),
PegelOnlineSensorEntityDescription(
key="water_speed",
translation_key="water_speed",
measurement_key="water_speed",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.SPEED,
entity_registry_enabled_default=False,
measurement_fn=lambda data: data.water_speed,
),
PegelOnlineSensorEntityDescription(
key="water_flow",
translation_key="water_flow",
measurement_key="water_flow",
state_class=SensorStateClass.MEASUREMENT,
entity_registry_enabled_default=False,
measurement_fn=lambda data: data.water_flow,
),
PegelOnlineSensorEntityDescription(
key="water_level",
translation_key="water_level",
measurement_key="water_level",
state_class=SensorStateClass.MEASUREMENT,
measurement_fn=lambda data: data.water_level,
),
PegelOnlineSensorEntityDescription(
key="water_temperature",
translation_key="water_temperature",
measurement_key="water_temperature",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.TEMPERATURE,
entity_registry_enabled_default=False,
measurement_fn=lambda data: data.water_temperature,
),
)
@ -101,7 +102,7 @@ async def async_setup_entry(
[
PegelOnlineSensor(coordinator, description)
for description in SENSORS
if getattr(coordinator.data, description.measurement_key) is not None
if description.measurement_fn(coordinator.data) is not None
]
)
@ -135,7 +136,9 @@ class PegelOnlineSensor(PegelOnlineEntity, SensorEntity):
@property
def measurement(self) -> CurrentMeasurement:
"""Return the measurement data of the entity."""
return getattr(self.coordinator.data, self.entity_description.measurement_key)
measurement = self.entity_description.measurement_fn(self.coordinator.data)
assert measurement is not None # we ensure existence in async_setup_entry
return measurement
@property
def native_value(self) -> float:

10
mypy.ini generated
View File

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