Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """The Nibe Heat Pump sensors."""
2 
3 from __future__ import annotations
4 
5 from nibe.coil import Coil, CoilData
6 
8  ENTITY_ID_FORMAT,
9  SensorDeviceClass,
10  SensorEntity,
11  SensorEntityDescription,
12  SensorStateClass,
13 )
14 from homeassistant.config_entries import ConfigEntry
15 from homeassistant.const import (
16  EntityCategory,
17  UnitOfElectricCurrent,
18  UnitOfElectricPotential,
19  UnitOfEnergy,
20  UnitOfFrequency,
21  UnitOfPower,
22  UnitOfTemperature,
23  UnitOfTime,
24 )
25 from homeassistant.core import HomeAssistant
26 from homeassistant.helpers.entity_platform import AddEntitiesCallback
27 
28 from .const import DOMAIN
29 from .coordinator import CoilCoordinator
30 from .entity import CoilEntity
31 
32 UNIT_DESCRIPTIONS = {
34  key="°C",
35  entity_category=EntityCategory.DIAGNOSTIC,
36  device_class=SensorDeviceClass.TEMPERATURE,
37  state_class=SensorStateClass.MEASUREMENT,
38  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
39  ),
41  key="°F",
42  entity_category=EntityCategory.DIAGNOSTIC,
43  device_class=SensorDeviceClass.TEMPERATURE,
44  state_class=SensorStateClass.MEASUREMENT,
45  native_unit_of_measurement=UnitOfTemperature.FAHRENHEIT,
46  ),
48  key="A",
49  entity_category=EntityCategory.DIAGNOSTIC,
50  device_class=SensorDeviceClass.CURRENT,
51  state_class=SensorStateClass.MEASUREMENT,
52  native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
53  ),
55  key="mA",
56  entity_category=EntityCategory.DIAGNOSTIC,
57  device_class=SensorDeviceClass.CURRENT,
58  state_class=SensorStateClass.MEASUREMENT,
59  native_unit_of_measurement=UnitOfElectricCurrent.MILLIAMPERE,
60  ),
62  key="V",
63  entity_category=EntityCategory.DIAGNOSTIC,
64  device_class=SensorDeviceClass.VOLTAGE,
65  state_class=SensorStateClass.MEASUREMENT,
66  native_unit_of_measurement=UnitOfElectricPotential.VOLT,
67  ),
69  key="mV",
70  entity_category=EntityCategory.DIAGNOSTIC,
71  device_class=SensorDeviceClass.VOLTAGE,
72  state_class=SensorStateClass.MEASUREMENT,
73  native_unit_of_measurement=UnitOfElectricPotential.MILLIVOLT,
74  ),
76  key="W",
77  entity_category=EntityCategory.DIAGNOSTIC,
78  device_class=SensorDeviceClass.POWER,
79  state_class=SensorStateClass.MEASUREMENT,
80  native_unit_of_measurement=UnitOfPower.WATT,
81  ),
83  key="kW",
84  entity_category=EntityCategory.DIAGNOSTIC,
85  device_class=SensorDeviceClass.POWER,
86  state_class=SensorStateClass.MEASUREMENT,
87  native_unit_of_measurement=UnitOfPower.KILO_WATT,
88  ),
90  key="Wh",
91  entity_category=EntityCategory.DIAGNOSTIC,
92  device_class=SensorDeviceClass.ENERGY,
93  state_class=SensorStateClass.TOTAL_INCREASING,
94  native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
95  ),
97  key="kWh",
98  entity_category=EntityCategory.DIAGNOSTIC,
99  device_class=SensorDeviceClass.ENERGY,
100  state_class=SensorStateClass.TOTAL_INCREASING,
101  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
102  ),
104  key="MWh",
105  entity_category=EntityCategory.DIAGNOSTIC,
106  device_class=SensorDeviceClass.ENERGY,
107  state_class=SensorStateClass.TOTAL_INCREASING,
108  native_unit_of_measurement=UnitOfEnergy.MEGA_WATT_HOUR,
109  ),
111  key="h",
112  entity_category=EntityCategory.DIAGNOSTIC,
113  device_class=SensorDeviceClass.DURATION,
114  state_class=SensorStateClass.TOTAL_INCREASING,
115  native_unit_of_measurement=UnitOfTime.HOURS,
116  ),
118  key="Hz",
119  entity_category=EntityCategory.DIAGNOSTIC,
120  device_class=SensorDeviceClass.FREQUENCY,
121  state_class=SensorStateClass.MEASUREMENT,
122  native_unit_of_measurement=UnitOfFrequency.HERTZ,
123  ),
124 }
125 
126 
128  hass: HomeAssistant,
129  config_entry: ConfigEntry,
130  async_add_entities: AddEntitiesCallback,
131 ) -> None:
132  """Set up platform."""
133 
134  coordinator: CoilCoordinator = hass.data[DOMAIN][config_entry.entry_id]
135 
137  Sensor(coordinator, coil, UNIT_DESCRIPTIONS.get(coil.unit))
138  for coil in coordinator.coils
139  if not coil.is_writable and not coil.is_boolean
140  )
141 
142 
144  """Sensor entity."""
145 
146  def __init__(
147  self,
148  coordinator: CoilCoordinator,
149  coil: Coil,
150  entity_description: SensorEntityDescription | None,
151  ) -> None:
152  """Initialize entity."""
153  super().__init__(coordinator, coil, ENTITY_ID_FORMAT)
154  if entity_description:
155  self.entity_descriptionentity_description = entity_description
156  else:
157  self._attr_native_unit_of_measurement_attr_native_unit_of_measurement = coil.unit
158  self._attr_entity_category_attr_entity_category = EntityCategory.DIAGNOSTIC
159 
160  def _async_read_coil(self, data: CoilData):
161  self._attr_native_value_attr_native_value = data.value
None __init__(self, CoilCoordinator coordinator, Coil coil, SensorEntityDescription|None entity_description)
Definition: sensor.py:151
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:131