Skip to content

Commit

Permalink
Fix unit inference for ITEMP field for APCUPSD integration (#93724)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuxincs committed May 29, 2023
1 parent 17fadbc commit a547181
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
14 changes: 8 additions & 6 deletions homeassistant/components/apcupsd/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

import logging

from apcaccess.status import ALL_UNITS

from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
Expand Down Expand Up @@ -427,7 +425,6 @@
),
}

SPECIFIC_UNITS = {"ITEMP": UnitOfTemperature.CELSIUS}
INFERRED_UNITS = {
" Minutes": UnitOfTime.MINUTES,
" Seconds": UnitOfTime.SECONDS,
Expand All @@ -438,6 +435,10 @@
" 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,
}

Expand Down Expand Up @@ -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


Expand Down
1 change: 1 addition & 0 deletions tests/components/apcupsd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
("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"),
Expand Down

0 comments on commit a547181

Please sign in to comment.