1 """Support for APCUPSd sensors."""
3 from __future__
import annotations
10 SensorEntityDescription,
16 UnitOfElectricCurrent,
17 UnitOfElectricPotential,
27 from .
import APCUPSdConfigEntry
28 from .const
import LAST_S_TEST
29 from .coordinator
import APCUPSdCoordinator
33 _LOGGER = logging.getLogger(__name__)
35 SENSORS: dict[str, SensorEntityDescription] = {
38 translation_key=
"alarm_delay",
42 translation_key=
"ambient_temperature",
43 native_unit_of_measurement=UnitOfTemperature.CELSIUS,
44 device_class=SensorDeviceClass.TEMPERATURE,
45 state_class=SensorStateClass.MEASUREMENT,
49 translation_key=
"apc_status",
50 entity_registry_enabled_default=
False,
54 translation_key=
"apc_model",
55 entity_registry_enabled_default=
False,
59 translation_key=
"bad_batteries",
63 translation_key=
"battery_replacement_date",
67 translation_key=
"battery_status",
71 translation_key=
"battery_voltage",
72 native_unit_of_measurement=UnitOfElectricPotential.VOLT,
73 device_class=SensorDeviceClass.VOLTAGE,
74 state_class=SensorStateClass.MEASUREMENT,
78 native_unit_of_measurement=PERCENTAGE,
79 device_class=SensorDeviceClass.BATTERY,
80 state_class=SensorStateClass.MEASUREMENT,
84 translation_key=
"cable_type",
85 entity_registry_enabled_default=
False,
89 translation_key=
"total_time_on_battery",
90 native_unit_of_measurement=UnitOfTime.SECONDS,
91 state_class=SensorStateClass.TOTAL_INCREASING,
92 device_class=SensorDeviceClass.DURATION,
96 translation_key=
"date",
97 entity_registry_enabled_default=
False,
101 translation_key=
"dip_switch_settings",
105 translation_key=
"low_battery_signal",
109 translation_key=
"driver",
110 entity_registry_enabled_default=
False,
114 translation_key=
"shutdown_delay",
118 translation_key=
"wake_delay",
122 translation_key=
"date_and_time",
123 entity_registry_enabled_default=
False,
127 translation_key=
"external_batteries",
131 translation_key=
"firmware_version",
132 entity_registry_enabled_default=
False,
136 translation_key=
"transfer_high",
137 native_unit_of_measurement=UnitOfElectricPotential.VOLT,
138 device_class=SensorDeviceClass.VOLTAGE,
142 translation_key=
"hostname",
143 entity_registry_enabled_default=
False,
147 translation_key=
"humidity",
148 native_unit_of_measurement=PERCENTAGE,
149 device_class=SensorDeviceClass.HUMIDITY,
150 state_class=SensorStateClass.MEASUREMENT,
154 translation_key=
"internal_temperature",
155 native_unit_of_measurement=UnitOfTemperature.CELSIUS,
156 device_class=SensorDeviceClass.TEMPERATURE,
157 state_class=SensorStateClass.MEASUREMENT,
161 translation_key=
"last_self_test",
165 translation_key=
"last_transfer",
166 entity_registry_enabled_default=
False,
170 translation_key=
"line_failure",
174 translation_key=
"line_frequency",
175 native_unit_of_measurement=UnitOfFrequency.HERTZ,
176 device_class=SensorDeviceClass.FREQUENCY,
177 state_class=SensorStateClass.MEASUREMENT,
181 translation_key=
"line_voltage",
182 native_unit_of_measurement=UnitOfElectricPotential.VOLT,
183 device_class=SensorDeviceClass.VOLTAGE,
184 state_class=SensorStateClass.MEASUREMENT,
188 translation_key=
"load_capacity",
189 native_unit_of_measurement=PERCENTAGE,
190 state_class=SensorStateClass.MEASUREMENT,
194 translation_key=
"apparent_power",
195 native_unit_of_measurement=PERCENTAGE,
199 translation_key=
"transfer_low",
200 native_unit_of_measurement=UnitOfElectricPotential.VOLT,
201 device_class=SensorDeviceClass.VOLTAGE,
205 translation_key=
"manufacture_date",
206 entity_registry_enabled_default=
False,
210 translation_key=
"master_update",
214 translation_key=
"input_voltage_high",
215 native_unit_of_measurement=UnitOfElectricPotential.VOLT,
216 device_class=SensorDeviceClass.VOLTAGE,
220 translation_key=
"max_time",
224 translation_key=
"max_battery_charge",
225 native_unit_of_measurement=PERCENTAGE,
229 translation_key=
"input_voltage_low",
230 native_unit_of_measurement=UnitOfElectricPotential.VOLT,
231 device_class=SensorDeviceClass.VOLTAGE,
235 translation_key=
"min_time",
239 translation_key=
"model",
240 entity_registry_enabled_default=
False,
244 translation_key=
"battery_nominal_voltage",
245 native_unit_of_measurement=UnitOfElectricPotential.VOLT,
246 device_class=SensorDeviceClass.VOLTAGE,
250 translation_key=
"nominal_input_voltage",
251 native_unit_of_measurement=UnitOfElectricPotential.VOLT,
252 device_class=SensorDeviceClass.VOLTAGE,
256 translation_key=
"nominal_output_voltage",
257 native_unit_of_measurement=UnitOfElectricPotential.VOLT,
258 device_class=SensorDeviceClass.VOLTAGE,
262 translation_key=
"nominal_output_power",
263 native_unit_of_measurement=UnitOfPower.WATT,
264 device_class=SensorDeviceClass.POWER,
268 translation_key=
"nominal_apparent_power",
269 native_unit_of_measurement=UnitOfApparentPower.VOLT_AMPERE,
270 device_class=SensorDeviceClass.APPARENT_POWER,
274 translation_key=
"transfer_count",
275 state_class=SensorStateClass.TOTAL_INCREASING,
279 translation_key=
"output_current",
280 native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
281 device_class=SensorDeviceClass.CURRENT,
282 state_class=SensorStateClass.MEASUREMENT,
286 translation_key=
"output_voltage",
287 native_unit_of_measurement=UnitOfElectricPotential.VOLT,
288 device_class=SensorDeviceClass.VOLTAGE,
289 state_class=SensorStateClass.MEASUREMENT,
293 translation_key=
"register_1_fault",
294 entity_registry_enabled_default=
False,
298 translation_key=
"register_2_fault",
299 entity_registry_enabled_default=
False,
303 translation_key=
"register_3_fault",
304 entity_registry_enabled_default=
False,
308 translation_key=
"restore_capacity",
309 native_unit_of_measurement=PERCENTAGE,
313 translation_key=
"self_test_result",
317 translation_key=
"sensitivity",
318 entity_registry_enabled_default=
False,
322 translation_key=
"serial_number",
323 entity_registry_enabled_default=
False,
327 translation_key=
"startup_time",
331 translation_key=
"online_status",
332 entity_registry_enabled_default=
False,
336 translation_key=
"status",
340 translation_key=
"self_test_interval",
344 translation_key=
"time_left",
345 native_unit_of_measurement=UnitOfTime.MINUTES,
346 state_class=SensorStateClass.MEASUREMENT,
347 device_class=SensorDeviceClass.DURATION,
351 translation_key=
"time_on_battery",
352 native_unit_of_measurement=UnitOfTime.SECONDS,
353 state_class=SensorStateClass.TOTAL_INCREASING,
354 device_class=SensorDeviceClass.DURATION,
358 translation_key=
"ups_mode",
362 translation_key=
"ups_name",
363 entity_registry_enabled_default=
False,
367 translation_key=
"version",
368 entity_registry_enabled_default=
False,
372 translation_key=
"transfer_from_battery",
376 translation_key=
"transfer_from_battery",
380 translation_key=
"transfer_to_battery",
385 " Minutes": UnitOfTime.MINUTES,
386 " Seconds": UnitOfTime.SECONDS,
387 " Percent": PERCENTAGE,
388 " Volts": UnitOfElectricPotential.VOLT,
389 " Ampere": UnitOfElectricCurrent.AMPERE,
390 " Amps": UnitOfElectricCurrent.AMPERE,
391 " Volt-Ampere": UnitOfApparentPower.VOLT_AMPERE,
392 " VA": UnitOfApparentPower.VOLT_AMPERE,
393 " Watts": UnitOfPower.WATT,
394 " Hz": UnitOfFrequency.HERTZ,
395 " C": UnitOfTemperature.CELSIUS,
399 " C Internal": UnitOfTemperature.CELSIUS,
400 " Percent Load Capacity": PERCENTAGE,
403 " days": UnitOfTime.DAYS,
409 config_entry: APCUPSdConfigEntry,
410 async_add_entities: AddEntitiesCallback,
412 """Set up the APCUPSd sensors from config entries."""
413 coordinator = config_entry.runtime_data
417 available_resources: set[str] = {k.lower()
for k, _
in coordinator.data.items()}
425 for resource
in available_resources | {LAST_S_TEST}:
426 if resource
not in SENSORS:
427 _LOGGER.warning(
"Invalid resource from APCUPSd: %s", resource.upper())
430 entities.append(
APCUPSdSensor(coordinator, SENSORS[resource]))
436 """If the value ends with any of the units from supported units.
438 Split the unit off the end of the value and return the value, unit tuple
439 pair. Else return the original value and None as the unit.
442 for unit, ha_unit
in INFERRED_UNITS.items():
443 if value.endswith(unit):
444 return value.removesuffix(unit), ha_unit
450 """Representation of a sensor entity for APCUPSd status values."""
452 _attr_has_entity_name =
True
456 coordinator: APCUPSdCoordinator,
457 description: SensorEntityDescription,
459 """Initialize the sensor."""
460 super().
__init__(coordinator=coordinator, context=description.key.upper())
463 if (serial_no := coordinator.data.serial_no)
is not None:
474 """Handle updated data from the coordinator."""
479 """Update sensor attributes based on coordinator data."""
485 if key
not in self.coordinator.data:
None __init__(self, APCUPSdCoordinator coordinator, SensorEntityDescription description)
_attr_native_unit_of_measurement
None _handle_coordinator_update(self)
str|None native_unit_of_measurement(self)
None async_write_ha_state(self)
None async_setup_entry(HomeAssistant hass, APCUPSdConfigEntry config_entry, AddEntitiesCallback async_add_entities)
tuple[str, str|None] infer_unit(str value)