1 """Platform for sensor integration."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
9 from boschshcpy
import SHCSession
10 from boschshcpy.device
import SHCDevice
15 SensorEntityDescription,
20 CONCENTRATION_PARTS_PER_MILLION,
30 from .const
import DATA_SESSION, DOMAIN
31 from .entity
import SHCEntity
34 @dataclass(frozen=True, kw_only=True)
36 """Describes a SHC sensor."""
38 value_fn: Callable[[SHCDevice], StateType]
39 attributes_fn: Callable[[SHCDevice], dict[str, Any]] |
None =
None
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"
54 SENSOR_DESCRIPTIONS: dict[str, 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,
64 device_class=SensorDeviceClass.HUMIDITY,
65 native_unit_of_measurement=PERCENTAGE,
66 value_fn=
lambda device: device.humidity,
70 translation_key=PURITY_SENSOR,
71 native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
72 value_fn=
lambda device: device.purity,
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,
83 key=TEMPERATURE_RATING_SENSOR,
84 translation_key=TEMPERATURE_RATING_SENSOR,
85 value_fn=
lambda device: device.temperature_rating.name,
88 key=COMMUNICATION_QUALITY_SENSOR,
89 translation_key=COMMUNICATION_QUALITY_SENSOR,
90 value_fn=
lambda device: device.communicationquality.name,
93 key=HUMIDITY_RATING_SENSOR,
94 translation_key=HUMIDITY_RATING_SENSOR,
95 value_fn=
lambda device: device.humidity_rating.name,
98 key=PURITY_RATING_SENSOR,
99 translation_key=PURITY_RATING_SENSOR,
100 value_fn=
lambda device: device.purity_rating.name,
104 device_class=SensorDeviceClass.POWER,
105 native_unit_of_measurement=UnitOfPower.WATT,
106 value_fn=
lambda device: device.powerconsumption,
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,
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,
130 config_entry: ConfigEntry,
131 async_add_entities: AddEntitiesCallback,
133 """Set up the SHC sensor platform."""
134 session: SHCSession = hass.data[DOMAIN][config_entry.entry_id][DATA_SESSION]
136 entities: list[SensorEntity] = [
139 SENSOR_DESCRIPTIONS[sensor_type],
140 session.information.unique_id,
141 config_entry.entry_id,
143 for device
in session.device_helper.thermostats
144 for sensor_type
in (TEMPERATURE_SENSOR, VALVE_TAPPET_SENSOR)
150 SENSOR_DESCRIPTIONS[sensor_type],
151 session.information.unique_id,
152 config_entry.entry_id,
154 for device
in session.device_helper.wallthermostats
155 for sensor_type
in (TEMPERATURE_SENSOR, HUMIDITY_SENSOR)
161 SENSOR_DESCRIPTIONS[sensor_type],
162 session.information.unique_id,
163 config_entry.entry_id,
165 for device
in session.device_helper.twinguards
171 TEMPERATURE_RATING_SENSOR,
172 HUMIDITY_RATING_SENSOR,
173 PURITY_RATING_SENSOR,
180 SENSOR_DESCRIPTIONS[sensor_type],
181 session.information.unique_id,
182 config_entry.entry_id,
185 session.device_helper.smart_plugs + session.device_helper.light_switches_bsm
187 for sensor_type
in (POWER_SENSOR, ENERGY_SENSOR)
193 SENSOR_DESCRIPTIONS[sensor_type],
194 session.information.unique_id,
195 config_entry.entry_id,
197 for device
in session.device_helper.smart_plugs_compact
198 for sensor_type
in (POWER_SENSOR, ENERGY_SENSOR, COMMUNICATION_QUALITY_SENSOR)
205 """Representation of a SHC sensor."""
207 entity_description: SHCSensorEntityDescription
212 entity_description: SHCSensorEntityDescription,
216 """Initialize sensor."""
217 super().
__init__(device, parent_id, entry_id)
223 """Return the state of the sensor."""
228 """Return the state attributes."""
dict[str, Any]|None extra_state_attributes(self)
StateType native_value(self)
None __init__(self, SHCDevice device, SHCSensorEntityDescription entity_description, str parent_id, str entry_id)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)