diff --git a/homeassistant/components/apcupsd/sensor.py b/homeassistant/components/apcupsd/sensor.py index 4e0e46f6392..77d8933827d 100644 --- a/homeassistant/components/apcupsd/sensor.py +++ b/homeassistant/components/apcupsd/sensor.py @@ -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 diff --git a/tests/components/apcupsd/__init__.py b/tests/components/apcupsd/__init__.py index 64547822b42..7b58013ddbe 100644 --- a/tests/components/apcupsd/__init__.py +++ b/tests/components/apcupsd/__init__.py @@ -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"),