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