Small clean up for Refoss sensor platform (#120015)

This commit is contained in:
Joost Lekkerkerker 2024-06-20 11:49:03 +02:00 committed by GitHub
parent 4d7a857555
commit e89b9b0093
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -35,12 +35,12 @@ from .const import (
from .entity import RefossEntity from .entity import RefossEntity
@dataclass(frozen=True) @dataclass(frozen=True, kw_only=True)
class RefossSensorEntityDescription(SensorEntityDescription): class RefossSensorEntityDescription(SensorEntityDescription):
"""Describes Refoss sensor entity.""" """Describes Refoss sensor entity."""
subkey: str | None = None subkey: str
fn: Callable[[float], float] | None = None fn: Callable[[float], float] = lambda x: x
SENSORS: dict[str, tuple[RefossSensorEntityDescription, ...]] = { SENSORS: dict[str, tuple[RefossSensorEntityDescription, ...]] = {
@ -50,10 +50,10 @@ SENSORS: dict[str, tuple[RefossSensorEntityDescription, ...]] = {
translation_key="power", translation_key="power",
device_class=SensorDeviceClass.POWER, device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfPower.WATT, native_unit_of_measurement=UnitOfPower.KILO_WATT,
suggested_unit_of_measurement=UnitOfPower.WATT,
suggested_display_precision=2, suggested_display_precision=2,
subkey="power", subkey="power",
fn=lambda x: x / 1000.0,
), ),
RefossSensorEntityDescription( RefossSensorEntityDescription(
key="voltage", key="voltage",
@ -115,24 +115,25 @@ async def async_setup_entry(
"""Set up the Refoss device from a config entry.""" """Set up the Refoss device from a config entry."""
@callback @callback
def init_device(coordinator): def init_device(coordinator: RefossDataUpdateCoordinator) -> None:
"""Register the device.""" """Register the device."""
device = coordinator.device device = coordinator.device
if not isinstance(device, ElectricityXMix): if not isinstance(device, ElectricityXMix):
return return
descriptions = SENSORS.get(device.device_type) descriptions: tuple[RefossSensorEntityDescription, ...] = SENSORS.get(
new_entities = [] device.device_type, ()
for channel in device.channels: )
for description in descriptions:
entity = RefossSensor( async_add_entities(
RefossSensor(
coordinator=coordinator, coordinator=coordinator,
channel=channel, channel=channel,
description=description, description=description,
) )
new_entities.append(entity) for channel in device.channels
for description in descriptions
async_add_entities(new_entities) )
for coordinator in hass.data[DOMAIN][COORDINATORS]: for coordinator in hass.data[DOMAIN][COORDINATORS]:
init_device(coordinator) init_device(coordinator)
@ -169,6 +170,4 @@ class RefossSensor(RefossEntity, SensorEntity):
) )
if value is None: if value is None:
return None return None
if self.entity_description.fn is not None:
return self.entity_description.fn(value) return self.entity_description.fn(value)
return value