Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for Oncue sensors."""
2 
3 from __future__ import annotations
4 
5 from aiooncue import OncueDevice, OncueSensor
6 
8  SensorDeviceClass,
9  SensorEntity,
10  SensorEntityDescription,
11  SensorStateClass,
12 )
13 from homeassistant.const import (
14  PERCENTAGE,
15  EntityCategory,
16  UnitOfElectricCurrent,
17  UnitOfElectricPotential,
18  UnitOfEnergy,
19  UnitOfFrequency,
20  UnitOfPower,
21  UnitOfPressure,
22  UnitOfTemperature,
23 )
24 from homeassistant.core import HomeAssistant
25 from homeassistant.helpers.entity_platform import AddEntitiesCallback
26 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
27 
28 from .entity import OncueEntity
29 from .types import OncueConfigEntry
30 
31 SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
33  key="LatestFirmware",
34  icon="mdi:update",
35  entity_category=EntityCategory.DIAGNOSTIC,
36  ),
38  key="EngineSpeed",
39  icon="mdi:speedometer",
40  entity_category=EntityCategory.DIAGNOSTIC,
41  ),
43  key="EngineTargetSpeed",
44  icon="mdi:speedometer",
45  entity_category=EntityCategory.DIAGNOSTIC,
46  ),
48  key="EngineOilPressure",
49  native_unit_of_measurement=UnitOfPressure.PSI,
50  device_class=SensorDeviceClass.PRESSURE,
51  state_class=SensorStateClass.MEASUREMENT,
52  entity_category=EntityCategory.DIAGNOSTIC,
53  ),
55  key="EngineCoolantTemperature",
56  device_class=SensorDeviceClass.TEMPERATURE,
57  state_class=SensorStateClass.MEASUREMENT,
58  entity_category=EntityCategory.DIAGNOSTIC,
59  ),
61  key="BatteryVoltage",
62  native_unit_of_measurement=UnitOfElectricPotential.VOLT,
63  device_class=SensorDeviceClass.VOLTAGE,
64  state_class=SensorStateClass.MEASUREMENT,
65  entity_category=EntityCategory.DIAGNOSTIC,
66  ),
68  key="LubeOilTemperature",
69  device_class=SensorDeviceClass.TEMPERATURE,
70  state_class=SensorStateClass.MEASUREMENT,
71  entity_category=EntityCategory.DIAGNOSTIC,
72  ),
74  key="GensetControllerTemperature",
75  device_class=SensorDeviceClass.TEMPERATURE,
76  state_class=SensorStateClass.MEASUREMENT,
77  entity_category=EntityCategory.DIAGNOSTIC,
78  ),
80  key="EngineCompartmentTemperature",
81  device_class=SensorDeviceClass.TEMPERATURE,
82  state_class=SensorStateClass.MEASUREMENT,
83  entity_category=EntityCategory.DIAGNOSTIC,
84  ),
86  key="GeneratorTrueTotalPower",
87  native_unit_of_measurement=UnitOfPower.WATT,
88  device_class=SensorDeviceClass.POWER,
89  state_class=SensorStateClass.MEASUREMENT,
90  entity_category=EntityCategory.DIAGNOSTIC,
91  ),
93  key="GeneratorTruePercentOfRatedPower",
94  native_unit_of_measurement=PERCENTAGE,
95  entity_category=EntityCategory.DIAGNOSTIC,
96  ),
98  key="GeneratorVoltageAverageLineToLine",
99  native_unit_of_measurement=UnitOfElectricPotential.VOLT,
100  device_class=SensorDeviceClass.VOLTAGE,
101  state_class=SensorStateClass.MEASUREMENT,
102  entity_category=EntityCategory.DIAGNOSTIC,
103  ),
105  key="GeneratorFrequency",
106  native_unit_of_measurement=UnitOfFrequency.HERTZ,
107  device_class=SensorDeviceClass.FREQUENCY,
108  state_class=SensorStateClass.MEASUREMENT,
109  entity_category=EntityCategory.DIAGNOSTIC,
110  ),
111  SensorEntityDescription(key="GensetState", icon="mdi:home-lightning-bolt"),
113  key="GensetControllerTotalOperationTime",
114  icon="mdi:hours-24",
115  entity_category=EntityCategory.DIAGNOSTIC,
116  ),
118  key="EngineTotalRunTime",
119  icon="mdi:hours-24",
120  entity_category=EntityCategory.DIAGNOSTIC,
121  ),
123  key="EngineTotalRunTimeLoaded",
124  icon="mdi:hours-24",
125  entity_category=EntityCategory.DIAGNOSTIC,
126  ),
127  SensorEntityDescription(key="AtsContactorPosition", icon="mdi:electric-switch"),
129  key="IPAddress",
130  icon="mdi:ip-network",
131  entity_category=EntityCategory.DIAGNOSTIC,
132  ),
134  key="ConnectedServerIPAddress",
135  icon="mdi:server-network",
136  entity_category=EntityCategory.DIAGNOSTIC,
137  ),
139  key="Source1VoltageAverageLineToLine",
140  native_unit_of_measurement=UnitOfElectricPotential.VOLT,
141  device_class=SensorDeviceClass.VOLTAGE,
142  state_class=SensorStateClass.MEASUREMENT,
143  entity_category=EntityCategory.DIAGNOSTIC,
144  ),
146  key="Source2VoltageAverageLineToLine",
147  native_unit_of_measurement=UnitOfElectricPotential.VOLT,
148  device_class=SensorDeviceClass.VOLTAGE,
149  state_class=SensorStateClass.MEASUREMENT,
150  entity_category=EntityCategory.DIAGNOSTIC,
151  ),
153  key="GensetTotalEnergy",
154  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
155  device_class=SensorDeviceClass.ENERGY,
156  state_class=SensorStateClass.TOTAL_INCREASING,
157  ),
159  key="EngineTotalNumberOfStarts",
160  icon="mdi:engine",
161  entity_category=EntityCategory.DIAGNOSTIC,
162  ),
164  key="GeneratorCurrentAverage",
165  native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
166  device_class=SensorDeviceClass.CURRENT,
167  state_class=SensorStateClass.MEASUREMENT,
168  entity_category=EntityCategory.DIAGNOSTIC,
169  ),
170 )
171 
172 SENSOR_MAP = {description.key: description for description in SENSOR_TYPES}
173 
174 UNIT_MAPPINGS = {
175  "C": UnitOfTemperature.CELSIUS,
176  "F": UnitOfTemperature.FAHRENHEIT,
177 }
178 
179 
181  hass: HomeAssistant,
182  config_entry: OncueConfigEntry,
183  async_add_entities: AddEntitiesCallback,
184 ) -> None:
185  """Set up sensors."""
186  coordinator = config_entry.runtime_data
187  devices = coordinator.data
189  OncueSensorEntity(coordinator, device_id, device, sensor, SENSOR_MAP[key])
190  for device_id, device in devices.items()
191  for key, sensor in device.sensors.items()
192  if key in SENSOR_MAP
193  )
194 
195 
197  """Representation of an Oncue sensor."""
198 
199  def __init__(
200  self,
201  coordinator: DataUpdateCoordinator[dict[str, OncueDevice]],
202  device_id: str,
203  device: OncueDevice,
204  sensor: OncueSensor,
205  description: SensorEntityDescription,
206  ) -> None:
207  """Initialize the sensor."""
208  super().__init__(coordinator, device_id, device, sensor, description)
209  if not description.native_unit_of_measurement and sensor.unit is not None:
210  self._attr_native_unit_of_measurement_attr_native_unit_of_measurement = UNIT_MAPPINGS.get(
211  sensor.unit, sensor.unit
212  )
213 
214  @property
215  def native_value(self) -> str:
216  """Return the sensors state."""
217  return self._oncue_value_oncue_value_oncue_value
None __init__(self, DataUpdateCoordinator[dict[str, OncueDevice]] coordinator, str device_id, OncueDevice device, OncueSensor sensor, SensorEntityDescription description)
Definition: sensor.py:206
None async_setup_entry(HomeAssistant hass, OncueConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:184