mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Small clean up for Refoss sensor platform (#120015)
This commit is contained in:
parent
4d7a857555
commit
e89b9b0093
@ -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(
|
|
||||||
coordinator=coordinator,
|
|
||||||
channel=channel,
|
|
||||||
description=description,
|
|
||||||
)
|
|
||||||
new_entities.append(entity)
|
|
||||||
|
|
||||||
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]:
|
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
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user