Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for Broadlink sensors."""
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 (
13  PERCENTAGE,
14  UnitOfElectricCurrent,
15  UnitOfElectricPotential,
16  UnitOfEnergy,
17  UnitOfPower,
18  UnitOfTemperature,
19 )
20 from homeassistant.core import HomeAssistant
21 from homeassistant.helpers.entity_platform import AddEntitiesCallback
22 
23 from .const import DOMAIN
24 from .entity import BroadlinkEntity
25 
26 SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
28  key="temperature",
29  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
30  device_class=SensorDeviceClass.TEMPERATURE,
31  state_class=SensorStateClass.MEASUREMENT,
32  ),
34  key="air_quality",
35  device_class=SensorDeviceClass.AQI,
36  ),
38  key="humidity",
39  native_unit_of_measurement=PERCENTAGE,
40  device_class=SensorDeviceClass.HUMIDITY,
41  state_class=SensorStateClass.MEASUREMENT,
42  ),
44  key="light",
45  translation_key="light",
46  ),
48  key="noise",
49  translation_key="noise",
50  ),
52  key="power",
53  native_unit_of_measurement=UnitOfPower.WATT,
54  device_class=SensorDeviceClass.POWER,
55  state_class=SensorStateClass.MEASUREMENT,
56  ),
58  key="volt",
59  native_unit_of_measurement=UnitOfElectricPotential.VOLT,
60  device_class=SensorDeviceClass.VOLTAGE,
61  state_class=SensorStateClass.MEASUREMENT,
62  ),
64  key="current",
65  native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
66  device_class=SensorDeviceClass.CURRENT,
67  state_class=SensorStateClass.MEASUREMENT,
68  ),
70  key="overload",
71  translation_key="overload",
72  native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
73  device_class=SensorDeviceClass.CURRENT,
74  state_class=SensorStateClass.MEASUREMENT,
75  ),
77  key="totalconsum",
78  translation_key="total_consumption",
79  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
80  device_class=SensorDeviceClass.ENERGY,
81  state_class=SensorStateClass.TOTAL_INCREASING,
82  ),
83 )
84 
85 
87  hass: HomeAssistant,
88  config_entry: ConfigEntry,
89  async_add_entities: AddEntitiesCallback,
90 ) -> None:
91  """Set up the Broadlink sensor."""
92  device = hass.data[DOMAIN].devices[config_entry.entry_id]
93  sensor_data = device.update_manager.coordinator.data
94  sensors = [
95  BroadlinkSensor(device, description)
96  for description in SENSOR_TYPES
97  if description.key in sensor_data
98  and (
99  # These devices have optional sensors.
100  # We don't create entities if the value is 0.
101  sensor_data[description.key] != 0
102  or device.api.type not in {"RM4PRO", "RM4MINI"}
103  )
104  ]
105  async_add_entities(sensors)
106 
107 
109  """Representation of a Broadlink sensor."""
110 
111  _attr_has_entity_name = True
112 
113  def __init__(self, device, description: SensorEntityDescription) -> None:
114  """Initialize the sensor."""
115  super().__init__(device)
116  self.entity_descriptionentity_description = description
117 
118  self._attr_native_value_attr_native_value = self._coordinator_coordinator.data[description.key]
119  self._attr_unique_id_attr_unique_id = f"{device.unique_id}-{description.key}"
120 
121  def _update_state(self, data):
122  """Update the state of the entity."""
123  self._attr_native_value_attr_native_value = data[self.entity_descriptionentity_description.key]