1 """Sensor entity for a Rainforest RAVEn device."""
3 from __future__
import annotations
5 from dataclasses
import dataclass
11 SensorEntityDescription,
26 from .coordinator
import RAVEnConfigEntry, RAVEnDataCoordinator
29 @dataclass(frozen=True, kw_only=True)
31 """A class that describes RAVEn sensor entities."""
34 attribute_keys: list[str] |
None =
None
39 message_key=
"CurrentSummationDelivered",
40 translation_key=
"total_energy_delivered",
41 key=
"summation_delivered",
42 native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
43 device_class=SensorDeviceClass.ENERGY,
44 state_class=SensorStateClass.TOTAL_INCREASING,
47 message_key=
"CurrentSummationDelivered",
48 translation_key=
"total_energy_received",
49 key=
"summation_received",
50 native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
51 device_class=SensorDeviceClass.ENERGY,
52 state_class=SensorStateClass.TOTAL_INCREASING,
55 message_key=
"InstantaneousDemand",
56 translation_key=
"power_demand",
58 native_unit_of_measurement=UnitOfPower.KILO_WATT,
59 device_class=SensorDeviceClass.POWER,
60 state_class=SensorStateClass.MEASUREMENT,
67 message_key=
"NetworkInfo",
68 translation_key=
"signal_strength",
70 native_unit_of_measurement=PERCENTAGE,
71 state_class=SensorStateClass.MEASUREMENT,
72 entity_category=EntityCategory.DIAGNOSTIC,
82 entry: RAVEnConfigEntry,
83 async_add_entities: AddEntitiesCallback,
85 """Set up a config entry."""
86 coordinator = entry.runtime_data
87 entities: list[RAVEnSensor] = [
88 RAVEnSensor(coordinator, description)
for description
in DIAGNOSTICS
91 for meter_mac_addr
in entry.data[CONF_MAC]:
94 for description
in SENSORS
97 meter_data = coordinator.data.get(
"Meters", {}).
get(meter_mac_addr)
or {}
98 if meter_data.get(
"PriceCluster", {}).
get(
"currency"):
103 message_key=
"PriceCluster",
104 translation_key=
"meter_price",
106 native_unit_of_measurement=f
"{meter_data['PriceCluster']['currency'].value}/{UnitOfEnergy.KILO_WATT_HOUR}",
107 state_class=SensorStateClass.MEASUREMENT,
121 """Rainforest RAVEn Sensor."""
123 _attr_has_entity_name =
True
124 entity_description: RAVEnSensorEntityDescription
128 coordinator: RAVEnDataCoordinator,
129 entity_description: RAVEnSensorEntityDescription,
131 """Initialize the sensor."""
136 f
"{self.coordinator.device_mac_address}"
137 f
".{self.entity_description.message_key}.{self.entity_description.key}"
142 """Return the raw sensor data from the source."""
143 return self.coordinator.data.get(self.
entity_descriptionentity_description.message_key, {})
147 """Return entity specific state attributes."""
157 """Return native value of the sensor."""
162 """Rainforest RAVEn Meter Sensor."""
166 coordinator: RAVEnDataCoordinator,
167 entity_description: RAVEnSensorEntityDescription,
170 """Initialize the sensor."""
171 super().
__init__(coordinator, entity_description)
174 f
"{self._meter_mac_addr}"
175 f
".{self.entity_description.message_key}.{self.entity_description.key}"
180 """Return the raw sensor data from the source."""
182 self.coordinator.data.get(
"Meters", {})
None __init__(self, RAVEnDataCoordinator coordinator, RAVEnSensorEntityDescription entity_description, str meter_mac_addr)
dict[str, Any]|None extra_state_attributes(self)
StateType native_value(self)
None __init__(self, RAVEnDataCoordinator coordinator, RAVEnSensorEntityDescription entity_description)
web.Response get(self, web.Request request, str config_key)
None async_setup_entry(HomeAssistant hass, RAVEnConfigEntry entry, AddEntitiesCallback async_add_entities)