Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for the Rainforest Eagle energy monitor."""
2 
3 from __future__ import annotations
4 
6  SensorDeviceClass,
7  SensorEntity,
8  SensorEntityDescription,
9  SensorStateClass,
10 )
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.const import UnitOfEnergy, UnitOfPower
13 from homeassistant.core import HomeAssistant
14 from homeassistant.helpers.device_registry import DeviceInfo
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 from homeassistant.helpers.typing import StateType
17 from homeassistant.helpers.update_coordinator import CoordinatorEntity
18 
19 from .const import DOMAIN
20 from .coordinator import EagleDataCoordinator
21 
22 SENSORS = (
24  key="zigbee:InstantaneousDemand",
25  translation_key="power_demand",
26  native_unit_of_measurement=UnitOfPower.KILO_WATT,
27  device_class=SensorDeviceClass.POWER,
28  state_class=SensorStateClass.MEASUREMENT,
29  ),
31  key="zigbee:CurrentSummationDelivered",
32  translation_key="total_energy_delivered",
33  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
34  device_class=SensorDeviceClass.ENERGY,
35  state_class=SensorStateClass.TOTAL_INCREASING,
36  ),
38  key="zigbee:CurrentSummationReceived",
39  translation_key="total_energy_received",
40  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
41  device_class=SensorDeviceClass.ENERGY,
42  state_class=SensorStateClass.TOTAL_INCREASING,
43  ),
44 )
45 
46 
48  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
49 ) -> None:
50  """Set up a config entry."""
51  coordinator = hass.data[DOMAIN][entry.entry_id]
52  entities = [EagleSensor(coordinator, description) for description in SENSORS]
53 
54  if coordinator.data.get("zigbee:Price") not in (None, "invalid"):
55  entities.append(
57  coordinator,
59  key="zigbee:Price",
60  translation_key="meter_price",
61  native_unit_of_measurement=f"{coordinator.data['zigbee:PriceCurrency']}/{UnitOfEnergy.KILO_WATT_HOUR}",
62  state_class=SensorStateClass.MEASUREMENT,
63  ),
64  )
65  )
66 
67  async_add_entities(entities)
68 
69 
70 class EagleSensor(CoordinatorEntity[EagleDataCoordinator], SensorEntity):
71  """Implementation of the Rainforest Eagle sensor."""
72 
73  _attr_has_entity_name = True
74 
75  def __init__(self, coordinator, entity_description):
76  """Initialize the sensor."""
77  super().__init__(coordinator)
78  self.entity_descriptionentity_description = entity_description
79  self._attr_unique_id_attr_unique_id = f"{coordinator.cloud_id}-${coordinator.hardware_address}-{entity_description.key}"
80  self._attr_device_info_attr_device_info = DeviceInfo(
81  identifiers={(DOMAIN, coordinator.cloud_id)},
82  manufacturer="Rainforest Automation",
83  model=coordinator.model,
84  name=coordinator.model,
85  )
86 
87  @property
88  def available(self) -> bool:
89  """Return if entity is available."""
90  return super().available and self.coordinator.is_connected
91 
92  @property
93  def native_value(self) -> StateType:
94  """Return native value of the sensor."""
95  return self.coordinator.data.get(self.entity_descriptionentity_description.key)
def __init__(self, coordinator, entity_description)
Definition: sensor.py:75
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:49