Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Representation of a sensorMultilevel."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 
8 from zwave_me_ws import ZWaveMeData
9 
11  SensorDeviceClass,
12  SensorEntity,
13  SensorEntityDescription,
14  SensorStateClass,
15 )
16 from homeassistant.config_entries import ConfigEntry
17 from homeassistant.const import (
18  LIGHT_LUX,
19  PERCENTAGE,
20  UnitOfElectricCurrent,
21  UnitOfElectricPotential,
22  UnitOfEnergy,
23  UnitOfPower,
24  UnitOfPressure,
25  UnitOfTemperature,
26 )
27 from homeassistant.core import HomeAssistant, callback
28 from homeassistant.helpers.dispatcher import async_dispatcher_connect
29 from homeassistant.helpers.entity_platform import AddEntitiesCallback
30 
31 from . import ZWaveMeController
32 from .const import DOMAIN, ZWaveMePlatform
33 from .entity import ZWaveMeEntity
34 
35 
36 @dataclass(frozen=True)
38  """Class describing ZWaveMeSensor sensor entities."""
39 
40  value: Callable = lambda value: value
41 
42 
43 SENSORS_MAP: dict[str, ZWaveMeSensorEntityDescription] = {
44  "barometer": ZWaveMeSensorEntityDescription(
45  key="barometer",
46  device_class=SensorDeviceClass.PRESSURE,
47  native_unit_of_measurement=UnitOfPressure.KPA,
48  state_class=SensorStateClass.MEASUREMENT,
49  ),
51  key="co",
52  device_class=SensorDeviceClass.CO,
53  native_unit_of_measurement="ppm",
54  state_class=SensorStateClass.MEASUREMENT,
55  ),
57  key="co2",
58  device_class=SensorDeviceClass.CO2,
59  native_unit_of_measurement="ppm",
60  state_class=SensorStateClass.MEASUREMENT,
61  ),
63  key="humidity",
64  device_class=SensorDeviceClass.HUMIDITY,
65  native_unit_of_measurement=PERCENTAGE,
66  state_class=SensorStateClass.MEASUREMENT,
67  ),
68  "luminosity": ZWaveMeSensorEntityDescription(
69  key="luminosity",
70  device_class=SensorDeviceClass.ILLUMINANCE,
71  native_unit_of_measurement=LIGHT_LUX,
72  state_class=SensorStateClass.MEASUREMENT,
73  ),
74  "meterElectric_ampere": ZWaveMeSensorEntityDescription(
75  key="meterElectric_ampere",
76  device_class=SensorDeviceClass.CURRENT,
77  native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
78  state_class=SensorStateClass.MEASUREMENT,
79  ),
80  "meterElectric_kilowatt_hour": ZWaveMeSensorEntityDescription(
81  key="meterElectric_kilowatt_hour",
82  device_class=SensorDeviceClass.ENERGY,
83  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
84  state_class=SensorStateClass.TOTAL_INCREASING,
85  ),
86  "meterElectric_power_factor": ZWaveMeSensorEntityDescription(
87  key="meterElectric_power_factor",
88  device_class=SensorDeviceClass.POWER_FACTOR,
89  native_unit_of_measurement=PERCENTAGE,
90  state_class=SensorStateClass.MEASUREMENT,
91  value=lambda value: float(value) * 100,
92  ),
93  "meterElectric_voltage": ZWaveMeSensorEntityDescription(
94  key="meterElectric_voltage",
95  device_class=SensorDeviceClass.VOLTAGE,
96  native_unit_of_measurement=UnitOfElectricPotential.VOLT,
97  state_class=SensorStateClass.MEASUREMENT,
98  ),
99  "meterElectric_watt": ZWaveMeSensorEntityDescription(
100  key="meterElectric_watt",
101  device_class=SensorDeviceClass.POWER,
102  native_unit_of_measurement=UnitOfPower.WATT,
103  state_class=SensorStateClass.MEASUREMENT,
104  ),
105  "temperature": ZWaveMeSensorEntityDescription(
106  key="temperature",
107  device_class=SensorDeviceClass.TEMPERATURE,
108  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
109  state_class=SensorStateClass.MEASUREMENT,
110  ),
112  key="generic",
113  ),
114 }
115 DEVICE_NAME = ZWaveMePlatform.SENSOR
116 
117 
119  hass: HomeAssistant,
120  config_entry: ConfigEntry,
121  async_add_entities: AddEntitiesCallback,
122 ) -> None:
123  """Set up the sensor platform."""
124 
125  @callback
126  def add_new_device(new_device: ZWaveMeData) -> None:
127  controller: ZWaveMeController = hass.data[DOMAIN][config_entry.entry_id]
128  description = SENSORS_MAP.get(new_device.probeType, SENSORS_MAP["generic"])
129  sensor = ZWaveMeSensor(controller, new_device, description)
130 
132  [
133  sensor,
134  ]
135  )
136 
137  config_entry.async_on_unload(
139  hass, f"ZWAVE_ME_NEW_{DEVICE_NAME.upper()}", add_new_device
140  )
141  )
142 
143 
145  """Representation of a ZWaveMe sensor."""
146 
147  entity_description: ZWaveMeSensorEntityDescription
148 
149  def __init__(
150  self,
151  controller: ZWaveMeController,
152  device: ZWaveMeData,
153  description: ZWaveMeSensorEntityDescription,
154  ) -> None:
155  """Initialize the device."""
156  super().__init__(controller=controller, device=device)
157  self.entity_descriptionentity_description = description
158 
159  @property
160  def native_value(self) -> str:
161  """Return the state of the sensor."""
162  return self.entity_descriptionentity_description.value(self.devicedevice.level)
None __init__(self, ZWaveMeController controller, ZWaveMeData device, ZWaveMeSensorEntityDescription description)
Definition: sensor.py:154
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:122
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103