Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Platform for sensor integration."""
2 
3 from collections.abc import Callable
4 from dataclasses import dataclass
5 
6 from weheat.abstractions.heat_pump import HeatPump
7 
9  SensorDeviceClass,
10  SensorEntity,
11  SensorEntityDescription,
12  SensorStateClass,
13 )
14 from homeassistant.const import UnitOfEnergy, UnitOfPower, UnitOfTemperature
15 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 from homeassistant.helpers.typing import StateType
18 
19 from . import WeheatConfigEntry
20 from .const import (
21  DISPLAY_PRECISION_COP,
22  DISPLAY_PRECISION_WATER_TEMP,
23  DISPLAY_PRECISION_WATTS,
24 )
25 from .coordinator import WeheatDataUpdateCoordinator
26 from .entity import WeheatEntity
27 
28 
29 @dataclass(frozen=True, kw_only=True)
31  """Describes Weheat sensor entity."""
32 
33  value_fn: Callable[[HeatPump], StateType]
34 
35 
36 SENSORS = [
38  translation_key="power_output",
39  key="power_output",
40  native_unit_of_measurement=UnitOfPower.WATT,
41  device_class=SensorDeviceClass.POWER,
42  state_class=SensorStateClass.MEASUREMENT,
43  suggested_display_precision=DISPLAY_PRECISION_WATTS,
44  value_fn=lambda status: status.power_output,
45  ),
47  translation_key="power_input",
48  key="power_input",
49  native_unit_of_measurement=UnitOfPower.WATT,
50  device_class=SensorDeviceClass.POWER,
51  state_class=SensorStateClass.MEASUREMENT,
52  suggested_display_precision=DISPLAY_PRECISION_WATTS,
53  value_fn=lambda status: status.power_input,
54  ),
56  translation_key="cop",
57  key="cop",
58  state_class=SensorStateClass.MEASUREMENT,
59  suggested_display_precision=DISPLAY_PRECISION_COP,
60  value_fn=lambda status: status.cop,
61  ),
63  translation_key="water_inlet_temperature",
64  key="water_inlet_temperature",
65  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
66  device_class=SensorDeviceClass.TEMPERATURE,
67  state_class=SensorStateClass.MEASUREMENT,
68  suggested_display_precision=DISPLAY_PRECISION_WATER_TEMP,
69  value_fn=lambda status: status.water_inlet_temperature,
70  ),
72  translation_key="water_outlet_temperature",
73  key="water_outlet_temperature",
74  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
75  device_class=SensorDeviceClass.TEMPERATURE,
76  state_class=SensorStateClass.MEASUREMENT,
77  suggested_display_precision=DISPLAY_PRECISION_WATER_TEMP,
78  value_fn=lambda status: status.water_outlet_temperature,
79  ),
81  translation_key="ch_inlet_temperature",
82  key="ch_inlet_temperature",
83  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
84  device_class=SensorDeviceClass.TEMPERATURE,
85  state_class=SensorStateClass.MEASUREMENT,
86  suggested_display_precision=DISPLAY_PRECISION_WATER_TEMP,
87  value_fn=lambda status: status.water_house_in_temperature,
88  ),
90  translation_key="outside_temperature",
91  key="outside_temperature",
92  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
93  device_class=SensorDeviceClass.TEMPERATURE,
94  state_class=SensorStateClass.MEASUREMENT,
95  suggested_display_precision=DISPLAY_PRECISION_WATER_TEMP,
96  value_fn=lambda status: status.air_inlet_temperature,
97  ),
99  translation_key="thermostat_water_setpoint",
100  key="thermostat_water_setpoint",
101  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
102  device_class=SensorDeviceClass.TEMPERATURE,
103  state_class=SensorStateClass.MEASUREMENT,
104  suggested_display_precision=DISPLAY_PRECISION_WATER_TEMP,
105  value_fn=lambda status: status.thermostat_water_setpoint,
106  ),
108  translation_key="thermostat_room_temperature",
109  key="thermostat_room_temperature",
110  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
111  device_class=SensorDeviceClass.TEMPERATURE,
112  state_class=SensorStateClass.MEASUREMENT,
113  suggested_display_precision=DISPLAY_PRECISION_WATER_TEMP,
114  value_fn=lambda status: status.thermostat_room_temperature,
115  ),
117  translation_key="thermostat_room_temperature_setpoint",
118  key="thermostat_room_temperature_setpoint",
119  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
120  device_class=SensorDeviceClass.TEMPERATURE,
121  state_class=SensorStateClass.MEASUREMENT,
122  suggested_display_precision=DISPLAY_PRECISION_WATER_TEMP,
123  value_fn=lambda status: status.thermostat_room_temperature_setpoint,
124  ),
126  translation_key="heat_pump_state",
127  key="heat_pump_state",
128  name=None,
129  device_class=SensorDeviceClass.ENUM,
130  options=[s.name.lower() for s in HeatPump.State],
131  value_fn=(
132  lambda status: status.heat_pump_state.name.lower()
133  if status.heat_pump_state
134  else None
135  ),
136  ),
138  translation_key="electricity_used",
139  key="electricity_used",
140  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
141  device_class=SensorDeviceClass.ENERGY,
142  state_class=SensorStateClass.TOTAL_INCREASING,
143  value_fn=lambda status: status.energy_total,
144  ),
145 ]
146 
147 
148 DHW_SENSORS = [
150  translation_key="dhw_top_temperature",
151  key="dhw_top_temperature",
152  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
153  device_class=SensorDeviceClass.TEMPERATURE,
154  state_class=SensorStateClass.MEASUREMENT,
155  suggested_display_precision=DISPLAY_PRECISION_WATER_TEMP,
156  value_fn=lambda status: status.dhw_top_temperature,
157  ),
159  translation_key="dhw_bottom_temperature",
160  key="dhw_bottom_temperature",
161  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
162  device_class=SensorDeviceClass.TEMPERATURE,
163  state_class=SensorStateClass.MEASUREMENT,
164  suggested_display_precision=DISPLAY_PRECISION_WATER_TEMP,
165  value_fn=lambda status: status.dhw_bottom_temperature,
166  ),
167 ]
168 
169 
171  hass: HomeAssistant,
172  entry: WeheatConfigEntry,
173  async_add_entities: AddEntitiesCallback,
174 ) -> None:
175  """Set up the sensors for weheat heat pump."""
176  entities = [
177  WeheatHeatPumpSensor(coordinator, entity_description)
178  for entity_description in SENSORS
179  for coordinator in entry.runtime_data
180  ]
181  entities.extend(
182  WeheatHeatPumpSensor(coordinator, entity_description)
183  for entity_description in DHW_SENSORS
184  for coordinator in entry.runtime_data
185  if coordinator.heat_pump_info.has_dhw
186  )
187 
188  async_add_entities(entities)
189 
190 
192  """Defines a Weheat heat pump sensor."""
193 
194  coordinator: WeheatDataUpdateCoordinator
195  entity_description: WeHeatSensorEntityDescription
196 
197  def __init__(
198  self,
199  coordinator: WeheatDataUpdateCoordinator,
200  entity_description: WeHeatSensorEntityDescription,
201  ) -> None:
202  """Pass coordinator to CoordinatorEntity."""
203  super().__init__(coordinator)
204 
205  self.entity_descriptionentity_description = entity_description
206 
207  self._attr_unique_id_attr_unique_id = f"{coordinator.heatpump_id}_{entity_description.key}"
208 
209  @property
210  def native_value(self) -> StateType:
211  """Return the state of the sensor."""
212  return self.entity_descriptionentity_description.value_fn(self.coordinator.data)
None __init__(self, WeheatDataUpdateCoordinator coordinator, WeHeatSensorEntityDescription entity_description)
Definition: sensor.py:201
None async_setup_entry(HomeAssistant hass, WeheatConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:174