1 """Support for IoTaWatt Energy monitor."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
9 from iotawattpy.sensor
import Sensor
14 SensorEntityDescription,
21 UnitOfElectricCurrent,
22 UnitOfElectricPotential,
34 from .const
import DOMAIN, VOLT_AMPERE_REACTIVE, VOLT_AMPERE_REACTIVE_HOURS
35 from .coordinator
import IotawattUpdater
37 _LOGGER = logging.getLogger(__name__)
40 @dataclass(frozen=True)
42 """Class describing IotaWatt sensor entities."""
44 value: Callable |
None =
None
47 ENTITY_DESCRIPTION_KEY_MAP: dict[str, IotaWattSensorEntityDescription] = {
50 native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
51 state_class=SensorStateClass.MEASUREMENT,
52 device_class=SensorDeviceClass.CURRENT,
53 entity_registry_enabled_default=
False,
57 native_unit_of_measurement=UnitOfFrequency.HERTZ,
58 state_class=SensorStateClass.MEASUREMENT,
59 device_class=SensorDeviceClass.FREQUENCY,
61 entity_registry_enabled_default=
False,
65 native_unit_of_measurement=PERCENTAGE,
66 state_class=SensorStateClass.MEASUREMENT,
67 device_class=SensorDeviceClass.POWER_FACTOR,
68 value=
lambda value: value * 100,
69 entity_registry_enabled_default=
False,
73 native_unit_of_measurement=UnitOfPower.WATT,
74 state_class=SensorStateClass.MEASUREMENT,
75 device_class=SensorDeviceClass.POWER,
79 native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
80 state_class=SensorStateClass.TOTAL,
81 device_class=SensorDeviceClass.ENERGY,
85 native_unit_of_measurement=UnitOfApparentPower.VOLT_AMPERE,
86 state_class=SensorStateClass.MEASUREMENT,
87 device_class=SensorDeviceClass.APPARENT_POWER,
88 entity_registry_enabled_default=
False,
92 native_unit_of_measurement=VOLT_AMPERE_REACTIVE,
93 state_class=SensorStateClass.MEASUREMENT,
95 entity_registry_enabled_default=
False,
99 native_unit_of_measurement=VOLT_AMPERE_REACTIVE_HOURS,
100 state_class=SensorStateClass.MEASUREMENT,
102 entity_registry_enabled_default=
False,
106 native_unit_of_measurement=UnitOfElectricPotential.VOLT,
107 state_class=SensorStateClass.MEASUREMENT,
108 device_class=SensorDeviceClass.VOLTAGE,
109 entity_registry_enabled_default=
False,
116 config_entry: ConfigEntry,
117 async_add_entities: AddEntitiesCallback,
119 """Add sensors for passed config_entry in HA."""
120 coordinator: IotawattUpdater = hass.data[DOMAIN][config_entry.entry_id]
124 def _create_entity(key: str) -> IotaWattSensor:
125 """Create a sensor entity."""
127 data = coordinator.data[
"sensors"][key]
128 description = ENTITY_DESCRIPTION_KEY_MAP.get(
133 coordinator=coordinator,
135 entity_description=description,
141 def new_data_received():
142 """Check for new sensors."""
145 for key
in coordinator.data[
"sensors"]
146 if key
not in created
150 coordinator.async_add_listener(new_data_received)
154 """Defines a IoTaWatt Energy Sensor."""
156 entity_description: IotaWattSensorEntityDescription
160 coordinator: IotawattUpdater,
162 entity_description: IotaWattSensorEntityDescription,
164 """Initialize the sensor."""
165 super().
__init__(coordinator=coordinator)
169 if data.getType() ==
"Input":
171 f
"{data.hub_mac_address}-input-{data.getChannel()}-{data.getUnit()}"
177 """Return sensor data."""
178 return self.coordinator.data[
"sensors"][self.
_key_key]
182 """Return name of the entity."""
187 """Return device info."""
188 return dr.DeviceInfo(
190 (dr.CONNECTION_NETWORK_MAC, self.
_sensor_data_sensor_data.hub_mac_address)
192 manufacturer=
"IoTaWatt",
198 """Handle updated data from the coordinator."""
199 if self.
_key_key
not in self.coordinator.data[
"sensors"]:
206 if (begin := self.
_sensor_data_sensor_data.getBegin())
and (
207 last_reset := dt_util.parse_datetime(begin)
215 """Return the extra state attributes of the entity."""
217 attrs = {
"type": data.getType()}
218 if attrs[
"type"] ==
"Input":
219 attrs[
"channel"] = data.getChannel()
225 """Return the state of the sensor."""
StateType native_value(self)
dr.DeviceInfo device_info(self)
None _handle_coordinator_update(self)
dict[str, str] extra_state_attributes(self)
None __init__(self, IotawattUpdater coordinator, str key, IotaWattSensorEntityDescription entity_description)
Sensor _sensor_data(self)
None async_remove(self, *bool force_remove=False)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)