Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Platform for sensor integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from typing import Any
8 
9 from boschshcpy import SHCSession
10 from boschshcpy.device import SHCDevice
11 
13  SensorDeviceClass,
14  SensorEntity,
15  SensorEntityDescription,
16  SensorStateClass,
17 )
18 from homeassistant.config_entries import ConfigEntry
19 from homeassistant.const import (
20  CONCENTRATION_PARTS_PER_MILLION,
21  PERCENTAGE,
22  UnitOfEnergy,
23  UnitOfPower,
24  UnitOfTemperature,
25 )
26 from homeassistant.core import HomeAssistant
27 from homeassistant.helpers.entity_platform import AddEntitiesCallback
28 from homeassistant.helpers.typing import StateType
29 
30 from .const import DATA_SESSION, DOMAIN
31 from .entity import SHCEntity
32 
33 
34 @dataclass(frozen=True, kw_only=True)
36  """Describes a SHC sensor."""
37 
38  value_fn: Callable[[SHCDevice], StateType]
39  attributes_fn: Callable[[SHCDevice], dict[str, Any]] | None = None
40 
41 
42 TEMPERATURE_SENSOR = "temperature"
43 HUMIDITY_SENSOR = "humidity"
44 VALVE_TAPPET_SENSOR = "valvetappet"
45 PURITY_SENSOR = "purity"
46 AIR_QUALITY_SENSOR = "airquality"
47 TEMPERATURE_RATING_SENSOR = "temperature_rating"
48 HUMIDITY_RATING_SENSOR = "humidity_rating"
49 PURITY_RATING_SENSOR = "purity_rating"
50 POWER_SENSOR = "power"
51 ENERGY_SENSOR = "energy"
52 COMMUNICATION_QUALITY_SENSOR = "communication_quality"
53 
54 SENSOR_DESCRIPTIONS: dict[str, SHCSensorEntityDescription] = {
55  TEMPERATURE_SENSOR: SHCSensorEntityDescription(
56  key=TEMPERATURE_SENSOR,
57  device_class=SensorDeviceClass.TEMPERATURE,
58  state_class=SensorStateClass.MEASUREMENT,
59  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
60  value_fn=lambda device: device.temperature,
61  ),
62  HUMIDITY_SENSOR: SHCSensorEntityDescription(
63  key=HUMIDITY_SENSOR,
64  device_class=SensorDeviceClass.HUMIDITY,
65  native_unit_of_measurement=PERCENTAGE,
66  value_fn=lambda device: device.humidity,
67  ),
68  PURITY_SENSOR: SHCSensorEntityDescription(
69  key=PURITY_SENSOR,
70  translation_key=PURITY_SENSOR,
71  native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
72  value_fn=lambda device: device.purity,
73  ),
74  AIR_QUALITY_SENSOR: SHCSensorEntityDescription(
75  key=AIR_QUALITY_SENSOR,
76  translation_key="air_quality",
77  value_fn=lambda device: device.combined_rating.name,
78  attributes_fn=lambda device: {
79  "rating_description": device.description,
80  },
81  ),
82  TEMPERATURE_RATING_SENSOR: SHCSensorEntityDescription(
83  key=TEMPERATURE_RATING_SENSOR,
84  translation_key=TEMPERATURE_RATING_SENSOR,
85  value_fn=lambda device: device.temperature_rating.name,
86  ),
87  COMMUNICATION_QUALITY_SENSOR: SHCSensorEntityDescription(
88  key=COMMUNICATION_QUALITY_SENSOR,
89  translation_key=COMMUNICATION_QUALITY_SENSOR,
90  value_fn=lambda device: device.communicationquality.name,
91  ),
92  HUMIDITY_RATING_SENSOR: SHCSensorEntityDescription(
93  key=HUMIDITY_RATING_SENSOR,
94  translation_key=HUMIDITY_RATING_SENSOR,
95  value_fn=lambda device: device.humidity_rating.name,
96  ),
97  PURITY_RATING_SENSOR: SHCSensorEntityDescription(
98  key=PURITY_RATING_SENSOR,
99  translation_key=PURITY_RATING_SENSOR,
100  value_fn=lambda device: device.purity_rating.name,
101  ),
102  POWER_SENSOR: SHCSensorEntityDescription(
103  key=POWER_SENSOR,
104  device_class=SensorDeviceClass.POWER,
105  native_unit_of_measurement=UnitOfPower.WATT,
106  value_fn=lambda device: device.powerconsumption,
107  ),
108  ENERGY_SENSOR: SHCSensorEntityDescription(
109  key=ENERGY_SENSOR,
110  device_class=SensorDeviceClass.ENERGY,
111  state_class=SensorStateClass.TOTAL_INCREASING,
112  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
113  value_fn=lambda device: device.energyconsumption / 1000.0,
114  ),
115  VALVE_TAPPET_SENSOR: SHCSensorEntityDescription(
116  key=VALVE_TAPPET_SENSOR,
117  translation_key=VALVE_TAPPET_SENSOR,
118  state_class=SensorStateClass.MEASUREMENT,
119  native_unit_of_measurement=PERCENTAGE,
120  value_fn=lambda device: device.position,
121  attributes_fn=lambda device: {
122  "valve_tappet_state": device.valvestate.name,
123  },
124  ),
125 }
126 
127 
129  hass: HomeAssistant,
130  config_entry: ConfigEntry,
131  async_add_entities: AddEntitiesCallback,
132 ) -> None:
133  """Set up the SHC sensor platform."""
134  session: SHCSession = hass.data[DOMAIN][config_entry.entry_id][DATA_SESSION]
135 
136  entities: list[SensorEntity] = [
137  SHCSensor(
138  device,
139  SENSOR_DESCRIPTIONS[sensor_type],
140  session.information.unique_id,
141  config_entry.entry_id,
142  )
143  for device in session.device_helper.thermostats
144  for sensor_type in (TEMPERATURE_SENSOR, VALVE_TAPPET_SENSOR)
145  ]
146 
147  entities.extend(
148  SHCSensor(
149  device,
150  SENSOR_DESCRIPTIONS[sensor_type],
151  session.information.unique_id,
152  config_entry.entry_id,
153  )
154  for device in session.device_helper.wallthermostats
155  for sensor_type in (TEMPERATURE_SENSOR, HUMIDITY_SENSOR)
156  )
157 
158  entities.extend(
159  SHCSensor(
160  device,
161  SENSOR_DESCRIPTIONS[sensor_type],
162  session.information.unique_id,
163  config_entry.entry_id,
164  )
165  for device in session.device_helper.twinguards
166  for sensor_type in (
167  TEMPERATURE_SENSOR,
168  HUMIDITY_SENSOR,
169  PURITY_SENSOR,
170  AIR_QUALITY_SENSOR,
171  TEMPERATURE_RATING_SENSOR,
172  HUMIDITY_RATING_SENSOR,
173  PURITY_RATING_SENSOR,
174  )
175  )
176 
177  entities.extend(
178  SHCSensor(
179  device,
180  SENSOR_DESCRIPTIONS[sensor_type],
181  session.information.unique_id,
182  config_entry.entry_id,
183  )
184  for device in (
185  session.device_helper.smart_plugs + session.device_helper.light_switches_bsm
186  )
187  for sensor_type in (POWER_SENSOR, ENERGY_SENSOR)
188  )
189 
190  entities.extend(
191  SHCSensor(
192  device,
193  SENSOR_DESCRIPTIONS[sensor_type],
194  session.information.unique_id,
195  config_entry.entry_id,
196  )
197  for device in session.device_helper.smart_plugs_compact
198  for sensor_type in (POWER_SENSOR, ENERGY_SENSOR, COMMUNICATION_QUALITY_SENSOR)
199  )
200 
201  async_add_entities(entities)
202 
203 
205  """Representation of a SHC sensor."""
206 
207  entity_description: SHCSensorEntityDescription
208 
209  def __init__(
210  self,
211  device: SHCDevice,
212  entity_description: SHCSensorEntityDescription,
213  parent_id: str,
214  entry_id: str,
215  ) -> None:
216  """Initialize sensor."""
217  super().__init__(device, parent_id, entry_id)
218  self.entity_descriptionentity_description = entity_description
219  self._attr_unique_id_attr_unique_id_attr_unique_id = f"{device.serial}_{entity_description.key}"
220 
221  @property
222  def native_value(self) -> StateType:
223  """Return the state of the sensor."""
224  return self.entity_descriptionentity_description.value_fn(self._device_device)
225 
226  @property
227  def extra_state_attributes(self) -> dict[str, Any] | None:
228  """Return the state attributes."""
229  if self.entity_descriptionentity_description.attributes_fn is not None:
230  return self.entity_descriptionentity_description.attributes_fn(self._device_device)
231  return None
dict[str, Any]|None extra_state_attributes(self)
Definition: sensor.py:227
None __init__(self, SHCDevice device, SHCSensorEntityDescription entity_description, str parent_id, str entry_id)
Definition: sensor.py:215
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:132