Improve typing of Steamist sensors (#63674)

This commit is contained in:
Franck Nijhof 2022-01-08 17:47:57 +01:00 committed by GitHub
parent fa15f91dbf
commit 51754f796b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,7 +1,10 @@
"""Support for Steamist sensors.""" """Support for Steamist sensors."""
from __future__ import annotations from __future__ import annotations
from typing import cast from collections.abc import Callable
from dataclasses import dataclass
from aiosteamist import SteamistStatus
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
SensorDeviceClass, SensorDeviceClass,
@ -26,17 +29,34 @@ UNIT_MAPPINGS = {
"F": TEMP_FAHRENHEIT, "F": TEMP_FAHRENHEIT,
} }
STEAMIST_SENSORS = (
SensorEntityDescription( @dataclass
class SteamistSensorEntityDescriptionMixin:
"""Mixin for required keys."""
value_fn: Callable[[SteamistStatus], int | None]
@dataclass
class SteamistSensorEntityDescription(
SensorEntityDescription, SteamistSensorEntityDescriptionMixin
):
"""Describes a Steamist sensor entity."""
SENSORS: tuple[SteamistSensorEntityDescription, ...] = (
SteamistSensorEntityDescription(
key=_KEY_MINUTES_REMAIN, key=_KEY_MINUTES_REMAIN,
name="Steam Minutes Remain", name="Steam Minutes Remain",
native_unit_of_measurement=TIME_MINUTES, native_unit_of_measurement=TIME_MINUTES,
value_fn=lambda status: status.minutes_remain,
), ),
SensorEntityDescription( SteamistSensorEntityDescription(
key=_KEY_TEMP, key=_KEY_TEMP,
name="Steam Temperature", name="Steam Temperature",
device_class=SensorDeviceClass.TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda status: status.temp,
), ),
) )
@ -53,7 +73,7 @@ async def async_setup_entry(
async_add_entities( async_add_entities(
[ [
SteamistSensorEntity(coordinator, config_entry, description) SteamistSensorEntity(coordinator, config_entry, description)
for description in STEAMIST_SENSORS for description in SENSORS
] ]
) )
@ -61,11 +81,13 @@ async def async_setup_entry(
class SteamistSensorEntity(SteamistEntity, SensorEntity): class SteamistSensorEntity(SteamistEntity, SensorEntity):
"""Representation of an Steamist steam switch.""" """Representation of an Steamist steam switch."""
entity_description: SteamistSensorEntityDescription
def __init__( def __init__(
self, self,
coordinator: SteamistDataUpdateCoordinator, coordinator: SteamistDataUpdateCoordinator,
entry: ConfigEntry, entry: ConfigEntry,
description: SensorEntityDescription, description: SteamistSensorEntityDescription,
) -> None: ) -> None:
"""Initialize the sensor entity.""" """Initialize the sensor entity."""
super().__init__(coordinator, entry, description) super().__init__(coordinator, entry, description)
@ -75,6 +97,6 @@ class SteamistSensorEntity(SteamistEntity, SensorEntity):
] ]
@property @property
def native_value(self) -> int: def native_value(self) -> int | None:
"""Return the native value of the sensor.""" """Return the native value of the sensor."""
return cast(int, getattr(self._status, self.entity_description.key)) return self.entity_description.value_fn(self._status)