Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for HomeMatic sensors."""
2 
3 from __future__ import annotations
4 
5 from copy import copy
6 import logging
7 
9  SensorDeviceClass,
10  SensorEntity,
11  SensorEntityDescription,
12  SensorStateClass,
13 )
14 from homeassistant.const import (
15  ATTR_NAME,
16  CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
17  CONCENTRATION_PARTS_PER_MILLION,
18  DEGREE,
19  LIGHT_LUX,
20  PERCENTAGE,
21  UnitOfElectricCurrent,
22  UnitOfElectricPotential,
23  UnitOfEnergy,
24  UnitOfFrequency,
25  UnitOfPower,
26  UnitOfPrecipitationDepth,
27  UnitOfPressure,
28  UnitOfSpeed,
29  UnitOfTemperature,
30  UnitOfVolume,
31 )
32 from homeassistant.core import HomeAssistant
33 from homeassistant.helpers.entity_platform import AddEntitiesCallback
34 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
35 
36 from .const import ATTR_DISCOVER_DEVICES, ATTR_PARAM
37 from .entity import HMDevice
38 
39 _LOGGER = logging.getLogger(__name__)
40 
41 HM_STATE_HA_CAST = {
42  "IPGarage": {0: "closed", 1: "open", 2: "ventilation", 3: None},
43  "RotaryHandleSensor": {0: "closed", 1: "tilted", 2: "open"},
44  "RotaryHandleSensorIP": {0: "closed", 1: "tilted", 2: "open"},
45  "WaterSensor": {0: "dry", 1: "wet", 2: "water"},
46  "CO2Sensor": {0: "normal", 1: "added", 2: "strong"},
47  "IPSmoke": {0: "off", 1: "primary", 2: "intrusion", 3: "secondary"},
48  "RFSiren": {
49  0: "disarmed",
50  1: "extsens_armed",
51  2: "allsens_armed",
52  3: "alarm_blocked",
53  },
54  "IPLockDLD": {0: None, 1: "locked", 2: "unlocked"},
55 }
56 
57 
58 SENSOR_DESCRIPTIONS: dict[str, SensorEntityDescription] = {
59  "HUMIDITY": SensorEntityDescription(
60  key="HUMIDITY",
61  native_unit_of_measurement=PERCENTAGE,
62  device_class=SensorDeviceClass.HUMIDITY,
63  state_class=SensorStateClass.MEASUREMENT,
64  ),
65  "ACTUAL_TEMPERATURE": SensorEntityDescription(
66  key="ACTUAL_TEMPERATURE",
67  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
68  device_class=SensorDeviceClass.TEMPERATURE,
69  state_class=SensorStateClass.MEASUREMENT,
70  ),
71  "TEMPERATURE": SensorEntityDescription(
72  key="TEMPERATURE",
73  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
74  device_class=SensorDeviceClass.TEMPERATURE,
75  state_class=SensorStateClass.MEASUREMENT,
76  ),
78  key="LUX",
79  native_unit_of_measurement=LIGHT_LUX,
80  device_class=SensorDeviceClass.ILLUMINANCE,
81  state_class=SensorStateClass.MEASUREMENT,
82  ),
83  "CURRENT_ILLUMINATION": SensorEntityDescription(
84  key="CURRENT_ILLUMINATION",
85  native_unit_of_measurement=LIGHT_LUX,
86  device_class=SensorDeviceClass.ILLUMINANCE,
87  state_class=SensorStateClass.MEASUREMENT,
88  ),
89  "ILLUMINATION": SensorEntityDescription(
90  key="ILLUMINATION",
91  native_unit_of_measurement=LIGHT_LUX,
92  device_class=SensorDeviceClass.ILLUMINANCE,
93  state_class=SensorStateClass.MEASUREMENT,
94  ),
95  "AVERAGE_ILLUMINATION": SensorEntityDescription(
96  key="AVERAGE_ILLUMINATION",
97  native_unit_of_measurement=LIGHT_LUX,
98  device_class=SensorDeviceClass.ILLUMINANCE,
99  state_class=SensorStateClass.MEASUREMENT,
100  ),
101  "LOWEST_ILLUMINATION": SensorEntityDescription(
102  key="LOWEST_ILLUMINATION",
103  native_unit_of_measurement=LIGHT_LUX,
104  device_class=SensorDeviceClass.ILLUMINANCE,
105  state_class=SensorStateClass.MEASUREMENT,
106  ),
107  "HIGHEST_ILLUMINATION": SensorEntityDescription(
108  key="HIGHEST_ILLUMINATION",
109  native_unit_of_measurement=LIGHT_LUX,
110  device_class=SensorDeviceClass.ILLUMINANCE,
111  state_class=SensorStateClass.MEASUREMENT,
112  ),
113  "POWER": SensorEntityDescription(
114  key="POWER",
115  native_unit_of_measurement=UnitOfPower.WATT,
116  device_class=SensorDeviceClass.POWER,
117  state_class=SensorStateClass.MEASUREMENT,
118  ),
119  "IEC_POWER": SensorEntityDescription(
120  key="IEC_POWER",
121  native_unit_of_measurement=UnitOfPower.WATT,
122  device_class=SensorDeviceClass.POWER,
123  state_class=SensorStateClass.MEASUREMENT,
124  ),
125  "CURRENT": SensorEntityDescription(
126  key="CURRENT",
127  native_unit_of_measurement=UnitOfElectricCurrent.MILLIAMPERE,
128  device_class=SensorDeviceClass.CURRENT,
129  state_class=SensorStateClass.MEASUREMENT,
130  ),
131  "CONCENTRATION": SensorEntityDescription(
132  key="CONCENTRATION",
133  native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
134  device_class=SensorDeviceClass.CO2,
135  state_class=SensorStateClass.MEASUREMENT,
136  ),
137  "ENERGY_COUNTER": SensorEntityDescription(
138  key="ENERGY_COUNTER",
139  native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
140  device_class=SensorDeviceClass.ENERGY,
141  state_class=SensorStateClass.TOTAL_INCREASING,
142  ),
143  "IEC_ENERGY_COUNTER": SensorEntityDescription(
144  key="IEC_ENERGY_COUNTER",
145  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
146  device_class=SensorDeviceClass.ENERGY,
147  state_class=SensorStateClass.TOTAL_INCREASING,
148  ),
149  "VOLTAGE": SensorEntityDescription(
150  key="VOLTAGE",
151  native_unit_of_measurement=UnitOfElectricPotential.VOLT,
152  device_class=SensorDeviceClass.VOLTAGE,
153  state_class=SensorStateClass.MEASUREMENT,
154  ),
155  "GAS_POWER": SensorEntityDescription(
156  key="GAS_POWER",
157  native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
158  device_class=SensorDeviceClass.GAS,
159  ),
160  "GAS_ENERGY_COUNTER": SensorEntityDescription(
161  key="GAS_ENERGY_COUNTER",
162  native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
163  device_class=SensorDeviceClass.GAS,
164  state_class=SensorStateClass.TOTAL_INCREASING,
165  ),
166  "RAIN_COUNTER": SensorEntityDescription(
167  key="RAIN_COUNTER",
168  native_unit_of_measurement=UnitOfPrecipitationDepth.MILLIMETERS,
169  device_class=SensorDeviceClass.PRECIPITATION,
170  ),
171  "WIND_SPEED": SensorEntityDescription(
172  key="WIND_SPEED",
173  native_unit_of_measurement=UnitOfSpeed.KILOMETERS_PER_HOUR,
174  device_class=SensorDeviceClass.WIND_SPEED,
175  icon="mdi:weather-windy",
176  ),
177  "WIND_DIRECTION": SensorEntityDescription(
178  key="WIND_DIRECTION",
179  native_unit_of_measurement=DEGREE,
180  ),
181  "WIND_DIRECTION_RANGE": SensorEntityDescription(
182  key="WIND_DIRECTION_RANGE",
183  native_unit_of_measurement=DEGREE,
184  ),
185  "SUNSHINEDURATION": SensorEntityDescription(
186  key="SUNSHINEDURATION",
187  native_unit_of_measurement="#",
188  ),
189  "AIR_PRESSURE": SensorEntityDescription(
190  key="AIR_PRESSURE",
191  native_unit_of_measurement=UnitOfPressure.HPA,
192  device_class=SensorDeviceClass.PRESSURE,
193  state_class=SensorStateClass.MEASUREMENT,
194  ),
195  "FREQUENCY": SensorEntityDescription(
196  key="FREQUENCY",
197  native_unit_of_measurement=UnitOfFrequency.HERTZ,
198  device_class=SensorDeviceClass.FREQUENCY,
199  ),
200  "VALUE": SensorEntityDescription(
201  key="VALUE",
202  native_unit_of_measurement="#",
203  ),
204  "VALVE_STATE": SensorEntityDescription(
205  key="VALVE_STATE",
206  native_unit_of_measurement=PERCENTAGE,
207  ),
208  "CARRIER_SENSE_LEVEL": SensorEntityDescription(
209  key="CARRIER_SENSE_LEVEL",
210  native_unit_of_measurement=PERCENTAGE,
211  ),
212  "DUTY_CYCLE_LEVEL": SensorEntityDescription(
213  key="DUTY_CYCLE_LEVEL",
214  native_unit_of_measurement=PERCENTAGE,
215  ),
216  "BRIGHTNESS": SensorEntityDescription(
217  key="BRIGHTNESS",
218  native_unit_of_measurement="#",
219  icon="mdi:invert-colors",
220  ),
221  "MASS_CONCENTRATION_PM_1": SensorEntityDescription(
222  key="MASS_CONCENTRATION_PM_1",
223  native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
224  device_class=SensorDeviceClass.PM1,
225  state_class=SensorStateClass.MEASUREMENT,
226  ),
227  "MASS_CONCENTRATION_PM_2_5": SensorEntityDescription(
228  key="MASS_CONCENTRATION_PM_2_5",
229  native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
230  device_class=SensorDeviceClass.PM25,
231  state_class=SensorStateClass.MEASUREMENT,
232  ),
233  "MASS_CONCENTRATION_PM_10": SensorEntityDescription(
234  key="MASS_CONCENTRATION_PM_10",
235  native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
236  device_class=SensorDeviceClass.PM10,
237  state_class=SensorStateClass.MEASUREMENT,
238  ),
239  "MASS_CONCENTRATION_PM_1_24H_AVERAGE": SensorEntityDescription(
240  key="MASS_CONCENTRATION_PM_1_24H_AVERAGE",
241  native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
242  device_class=SensorDeviceClass.PM1,
243  state_class=SensorStateClass.MEASUREMENT,
244  ),
245  "MASS_CONCENTRATION_PM_2_5_24H_AVERAGE": SensorEntityDescription(
246  key="MASS_CONCENTRATION_PM_2_5_24H_AVERAGE",
247  native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
248  device_class=SensorDeviceClass.PM25,
249  state_class=SensorStateClass.MEASUREMENT,
250  ),
251  "MASS_CONCENTRATION_PM_10_24H_AVERAGE": SensorEntityDescription(
252  key="MASS_CONCENTRATION_PM_10_24H_AVERAGE",
253  native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
254  device_class=SensorDeviceClass.PM10,
255  state_class=SensorStateClass.MEASUREMENT,
256  ),
257  "STATE": SensorEntityDescription(
258  key="STATE",
259  ),
260  "SMOKE_DETECTOR_ALARM_STATUS": SensorEntityDescription(
261  key="SMOKE_DETECTOR_ALARM_STATUS",
262  ),
263  "WIND_DIR": SensorEntityDescription(
264  key="WIND_DIR",
265  ),
266  "WIND_DIR_RANGE": SensorEntityDescription(
267  key="WIND_DIR_RANGE",
268  ),
269  "CONCENTRATION_STATUS": SensorEntityDescription(
270  key="CONCENTRATION_STATUS",
271  ),
272  "PASSAGE_COUNTER_VALUE": SensorEntityDescription(
273  key="PASSAGE_COUNTER_VALUE",
274  ),
275  "LEVEL": SensorEntityDescription(
276  key="LEVEL",
277  ),
278  "LEVEL_2": SensorEntityDescription(
279  key="LEVEL_2",
280  ),
281  "DOOR_STATE": SensorEntityDescription(
282  key="DOOR_STATE",
283  ),
284  "FILLING_LEVEL": SensorEntityDescription(
285  key="FILLING_LEVEL",
286  ),
287 }
288 
289 DEFAULT_SENSOR_DESCRIPTION = SensorEntityDescription(
290  key="",
291  entity_registry_enabled_default=True,
292 )
293 
294 
296  hass: HomeAssistant,
297  config: ConfigType,
298  add_entities: AddEntitiesCallback,
299  discovery_info: DiscoveryInfoType | None = None,
300 ) -> None:
301  """Set up the HomeMatic sensor platform."""
302  if discovery_info is None:
303  return
304 
305  devices = []
306  for conf in discovery_info[ATTR_DISCOVER_DEVICES]:
307  state = conf.get(ATTR_PARAM)
308  if (entity_desc := SENSOR_DESCRIPTIONS.get(state)) is None:
309  name = conf.get(ATTR_NAME)
310  _LOGGER.warning(
311  (
312  "Sensor (%s) entity description is missing. Sensor state (%s) needs"
313  " to be maintained"
314  ),
315  name,
316  state,
317  )
318  entity_desc = copy(DEFAULT_SENSOR_DESCRIPTION)
319 
320  new_device = HMSensor(conf, entity_desc)
321  devices.append(new_device)
322 
323  add_entities(devices, True)
324 
325 
327  """Representation of a HomeMatic sensor."""
328 
329  @property
330  def native_value(self):
331  """Return the state of the sensor."""
332  # Does a cast exist for this class?
333  name = self._hmdevice_hmdevice.__class__.__name__
334  if name in HM_STATE_HA_CAST:
335  return HM_STATE_HA_CAST[name].get(self._hm_get_state_hm_get_state())
336 
337  # No cast, return original value
338  return self._hm_get_state_hm_get_state()
339 
340  def _init_data_struct(self):
341  """Generate a data dictionary (self._data) from metadata."""
342  if self._state_state:
343  self._data.update({self._state_state: None})
344  else:
345  _LOGGER.critical("Unable to initialize sensor: %s", self._name_name)
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: sensor.py:300
def add_entities(account, async_add_entities, tracked)
Definition: sensor.py:40