Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unit inference for ITEMP field for APCUPSD integration #93724

Merged
merged 2 commits into from
May 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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