Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for Fibaro sensors."""
2 
3 from __future__ import annotations
4 
5 from contextlib import suppress
6 
7 from pyfibaro.fibaro_device import DeviceModel
8 
10  ENTITY_ID_FORMAT,
11  SensorDeviceClass,
12  SensorEntity,
13  SensorEntityDescription,
14  SensorStateClass,
15 )
16 from homeassistant.config_entries import ConfigEntry
17 from homeassistant.const import (
18  CONCENTRATION_PARTS_PER_MILLION,
19  LIGHT_LUX,
20  PERCENTAGE,
21  Platform,
22  UnitOfEnergy,
23  UnitOfPower,
24  UnitOfTemperature,
25 )
26 from homeassistant.core import HomeAssistant
27 from homeassistant.helpers.entity_platform import AddEntitiesCallback
28 from homeassistant.util import convert
29 
30 from . import FibaroController
31 from .const import DOMAIN
32 from .entity import FibaroEntity
33 
34 # List of known sensors which represents a fibaro device
35 MAIN_SENSOR_TYPES: dict[str, SensorEntityDescription] = {
36  "com.fibaro.temperatureSensor": SensorEntityDescription(
37  key="com.fibaro.temperatureSensor",
38  name="Temperature",
39  device_class=SensorDeviceClass.TEMPERATURE,
40  state_class=SensorStateClass.MEASUREMENT,
41  ),
42  "com.fibaro.smokeSensor": SensorEntityDescription(
43  key="com.fibaro.smokeSensor",
44  name="Smoke",
45  native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
46  icon="mdi:fire",
47  ),
49  key="CO2",
50  name="CO2",
51  native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
52  device_class=SensorDeviceClass.CO2,
53  state_class=SensorStateClass.MEASUREMENT,
54  ),
55  "com.fibaro.humiditySensor": SensorEntityDescription(
56  key="com.fibaro.humiditySensor",
57  name="Humidity",
58  native_unit_of_measurement=PERCENTAGE,
59  device_class=SensorDeviceClass.HUMIDITY,
60  state_class=SensorStateClass.MEASUREMENT,
61  ),
62  "com.fibaro.lightSensor": SensorEntityDescription(
63  key="com.fibaro.lightSensor",
64  name="Light",
65  native_unit_of_measurement=LIGHT_LUX,
66  device_class=SensorDeviceClass.ILLUMINANCE,
67  state_class=SensorStateClass.MEASUREMENT,
68  ),
69  "com.fibaro.energyMeter": SensorEntityDescription(
70  key="com.fibaro.energyMeter",
71  name="Energy",
72  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
73  device_class=SensorDeviceClass.ENERGY,
74  state_class=SensorStateClass.TOTAL_INCREASING,
75  ),
76 }
77 
78 # List of additional sensors which are created based on a property
79 # The key is the property name
80 ADDITIONAL_SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
82  key="energy",
83  name="Energy",
84  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
85  device_class=SensorDeviceClass.ENERGY,
86  state_class=SensorStateClass.TOTAL_INCREASING,
87  ),
89  key="power",
90  name="Power",
91  native_unit_of_measurement=UnitOfPower.WATT,
92  device_class=SensorDeviceClass.POWER,
93  state_class=SensorStateClass.MEASUREMENT,
94  ),
95 )
96 
97 FIBARO_TO_HASS_UNIT: dict[str, str] = {
98  "lux": LIGHT_LUX,
99  "C": UnitOfTemperature.CELSIUS,
100  "F": UnitOfTemperature.FAHRENHEIT,
101 }
102 
103 
105  hass: HomeAssistant,
106  entry: ConfigEntry,
107  async_add_entities: AddEntitiesCallback,
108 ) -> None:
109  """Set up the Fibaro controller devices."""
110 
111  controller: FibaroController = hass.data[DOMAIN][entry.entry_id]
112  entities: list[SensorEntity] = [
113  FibaroSensor(device, MAIN_SENSOR_TYPES.get(device.type))
114  for device in controller.fibaro_devices[Platform.SENSOR]
115  # Some sensor devices do not have a value but report power or energy.
116  # These sensors are added to the sensor list but need to be excluded
117  # here as the FibaroSensor expects a value. One example is the
118  # Qubino 3 phase power meter.
119  if device.value.has_value
120  ]
121 
122  entities.extend(
123  FibaroAdditionalSensor(device, entity_description)
124  for platform in (
125  Platform.BINARY_SENSOR,
126  Platform.CLIMATE,
127  Platform.COVER,
128  Platform.LIGHT,
129  Platform.LOCK,
130  Platform.SENSOR,
131  Platform.SWITCH,
132  )
133  for device in controller.fibaro_devices[platform]
134  for entity_description in ADDITIONAL_SENSOR_TYPES
135  if entity_description.key in device.properties
136  )
137 
138  async_add_entities(entities, True)
139 
140 
142  """Representation of a Fibaro Sensor."""
143 
144  def __init__(
145  self,
146  fibaro_device: DeviceModel,
147  entity_description: SensorEntityDescription | None,
148  ) -> None:
149  """Initialize the sensor."""
150  super().__init__(fibaro_device)
151  if entity_description is not None:
152  self.entity_descriptionentity_description = entity_description
153  self.entity_identity_identity_id = ENTITY_ID_FORMAT.format(self.ha_idha_id)
154 
155  # Map unit if it was not defined in the entity description
156  # or there is no entity description at all
157  with suppress(KeyError, ValueError):
158  if not self.native_unit_of_measurementnative_unit_of_measurement:
159  self._attr_native_unit_of_measurement_attr_native_unit_of_measurement = FIBARO_TO_HASS_UNIT.get(
160  fibaro_device.unit, fibaro_device.unit
161  )
162 
163  def update(self) -> None:
164  """Update the state."""
165  super().update()
166  with suppress(TypeError):
167  self._attr_native_value_attr_native_value = self.fibaro_devicefibaro_device.value.float_value()
168 
169 
171  """Representation of a Fibaro Additional Sensor."""
172 
173  def __init__(
174  self, fibaro_device: DeviceModel, entity_description: SensorEntityDescription
175  ) -> None:
176  """Initialize the sensor."""
177  super().__init__(fibaro_device)
178  self.entity_descriptionentity_description = entity_description
179 
180  # To differentiate additional sensors from main sensors they need
181  # to get different names and ids
182  self.entity_identity_identity_id = ENTITY_ID_FORMAT.format(
183  f"{self.ha_id}_{entity_description.key}"
184  )
185  self._attr_name_attr_name_attr_name = f"{fibaro_device.friendly_name} {entity_description.name}"
186  self._attr_unique_id_attr_unique_id_attr_unique_id = f"{fibaro_device.unique_id_str}_{entity_description.key}"
187 
188  def update(self) -> None:
189  """Update the state."""
190  super().update()
191  with suppress(KeyError, ValueError):
192  self._attr_native_value_attr_native_value = convert(
193  self.fibaro_devicefibaro_device.properties[self.entity_descriptionentity_description.key],
194  float,
195  )
None __init__(self, DeviceModel fibaro_device, SensorEntityDescription entity_description)
Definition: sensor.py:175
None __init__(self, DeviceModel fibaro_device, SensorEntityDescription|None entity_description)
Definition: sensor.py:148
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:108