Fix unit inference for ITEMP field for APCUPSD integration (#93724)

This commit is contained in:
Yuxin Wang 2023-05-29 14:40:36 -04:00 committed by GitHub
parent 17fadbcf4a
commit a547181984
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 6 deletions

View File

@ -3,8 +3,6 @@ from __future__ import annotations
import logging
from apcaccess.status import ALL_UNITS
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
@ -427,7 +425,6 @@ SENSORS: dict[str, SensorEntityDescription] = {
),
}
SPECIFIC_UNITS = {"ITEMP": UnitOfTemperature.CELSIUS}
INFERRED_UNITS = {
" Minutes": UnitOfTime.MINUTES,
" Seconds": UnitOfTime.SECONDS,
@ -438,6 +435,10 @@ INFERRED_UNITS = {
" Watts": UnitOfPower.WATT,
" Hz": UnitOfFrequency.HERTZ,
" C": UnitOfTemperature.CELSIUS,
# APCUPSd reports data for "itemp" field (eventually represented by UPS Internal
# Temperature sensor in this integration) with a trailing "Internal", e.g.,
# "34.6 C Internal". Here we create a fake unit " C Internal" to handle this case.
" C Internal": UnitOfTemperature.CELSIUS,
" Percent Load Capacity": PERCENTAGE,
}
@ -466,15 +467,16 @@ async def async_setup_entry(
def infer_unit(value: str) -> tuple[str, str | None]:
"""If the value ends with any of the units from ALL_UNITS.
"""If the value ends with any of the units from supported units.
Split the unit off the end of the value and return the value, unit tuple
pair. Else return the original value and None as the unit.
"""
for unit in ALL_UNITS:
for unit, ha_unit in INFERRED_UNITS.items():
if value.endswith(unit):
return value.removesuffix(unit), INFERRED_UNITS.get(unit, unit.strip())
return value.removesuffix(unit), ha_unit
return value, None

View File

@ -26,6 +26,7 @@ MOCK_STATUS: Final = OrderedDict(
("LOADPCT", "14.0 Percent"),
("BCHARGE", "100.0 Percent"),
("TIMELEFT", "51.0 Minutes"),
("ITEMP", "34.6 C Internal"),
("MBATTCHG", "5 Percent"),
("MINTIMEL", "3 Minutes"),
("MAXTIME", "0 Seconds"),