1 """Support for refoss sensors."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
8 from refoss_ha.controller.electricity
import ElectricityXMix
13 SensorEntityDescription,
18 UnitOfElectricCurrent,
19 UnitOfElectricPotential,
28 from .bridge
import RefossDataUpdateCoordinator
32 DISPATCH_DEVICE_DISCOVERED,
36 from .entity
import RefossEntity
39 @dataclass(frozen=True, kw_only=True)
41 """Describes Refoss sensor entity."""
44 fn: Callable[[float], float] =
lambda x: x
47 DEVICETYPE_SENSOR: dict[str, str] = {
52 SENSORS: dict[str, tuple[RefossSensorEntityDescription, ...]] = {
56 translation_key=
"power",
57 device_class=SensorDeviceClass.POWER,
58 state_class=SensorStateClass.MEASUREMENT,
59 native_unit_of_measurement=UnitOfPower.WATT,
60 suggested_display_precision=2,
62 fn=
lambda x: x / 1000.0,
66 translation_key=
"voltage",
67 device_class=SensorDeviceClass.VOLTAGE,
68 state_class=SensorStateClass.MEASUREMENT,
69 native_unit_of_measurement=UnitOfElectricPotential.MILLIVOLT,
70 suggested_display_precision=2,
71 suggested_unit_of_measurement=UnitOfElectricPotential.VOLT,
76 translation_key=
"current",
77 device_class=SensorDeviceClass.CURRENT,
78 state_class=SensorStateClass.MEASUREMENT,
79 native_unit_of_measurement=UnitOfElectricCurrent.MILLIAMPERE,
80 suggested_display_precision=2,
81 suggested_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
86 translation_key=
"power_factor",
87 device_class=SensorDeviceClass.POWER_FACTOR,
88 state_class=SensorStateClass.MEASUREMENT,
89 suggested_display_precision=2,
94 translation_key=
"this_month_energy",
95 device_class=SensorDeviceClass.ENERGY,
96 state_class=SensorStateClass.TOTAL,
97 native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
98 suggested_display_precision=2,
100 fn=
lambda x:
max(0, x),
103 key=
"energy_returned",
104 translation_key=
"this_month_energy_returned",
105 device_class=SensorDeviceClass.ENERGY,
106 state_class=SensorStateClass.TOTAL,
107 native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
108 suggested_display_precision=2,
110 fn=
lambda x: abs(x)
if x < 0
else 0,
118 config_entry: ConfigEntry,
119 async_add_entities: AddEntitiesCallback,
121 """Set up the Refoss device from a config entry."""
124 def init_device(coordinator: RefossDataUpdateCoordinator) ->
None:
125 """Register the device."""
126 device = coordinator.device
128 if not isinstance(device, ElectricityXMix):
131 sensor_type = DEVICETYPE_SENSOR.get(device.device_type,
"")
133 descriptions: tuple[RefossSensorEntityDescription, ...] = SENSORS.get(
139 coordinator=coordinator,
141 description=description,
143 for channel
in device.channels
144 for description
in descriptions
147 for coordinator
in hass.data[DOMAIN][COORDINATORS]:
148 init_device(coordinator)
150 config_entry.async_on_unload(
156 """Refoss Sensor Device."""
158 entity_description: RefossSensorEntityDescription
162 coordinator: RefossDataUpdateCoordinator,
164 description: RefossSensorEntityDescription,
166 """Init Refoss sensor."""
167 super().
__init__(coordinator, channel)
170 device_type = coordinator.device.device_type
171 channel_name = CHANNEL_DISPLAY_NAME[device_type][channel]
176 """Return the native value."""
177 value = self.coordinator.device.get_value(
None __init__(self, RefossDataUpdateCoordinator coordinator, int channel, RefossSensorEntityDescription description)
_attr_translation_placeholders
StateType native_value(self)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)