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

Enable unit conversion for DATA_SIZE #84699

Merged
merged 2 commits into from
Dec 29, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions homeassistant/components/sensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
BaseUnitConverter,
DataRateConverter,
DistanceConverter,
InformationConverter,
MassConverter,
PressureConverter,
SpeedConverter,
Expand Down Expand Up @@ -468,6 +469,7 @@ class SensorStateClass(StrEnum):
# `entity-registry-settings.ts`
UNIT_CONVERTERS: dict[SensorDeviceClass | str | None, type[BaseUnitConverter]] = {
SensorDeviceClass.DATA_RATE: DataRateConverter,
SensorDeviceClass.DATA_SIZE: InformationConverter,
SensorDeviceClass.DISTANCE: DistanceConverter,
SensorDeviceClass.GAS: VolumeConverter,
SensorDeviceClass.PRECIPITATION: DistanceConverter,
Expand Down
33 changes: 33 additions & 0 deletions homeassistant/util/unit_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
UNIT_NOT_RECOGNIZED_TEMPLATE,
UnitOfDataRate,
UnitOfEnergy,
UnitOfInformation,
UnitOfLength,
UnitOfMass,
UnitOfPower,
Expand Down Expand Up @@ -155,6 +156,38 @@ class EnergyConverter(BaseUnitConverter):
}


class InformationConverter(BaseUnitConverter):
"""Utility to convert information values."""

UNIT_CLASS = "information"
NORMALIZED_UNIT = UnitOfInformation.BITS
# Units in terms of bits
_UNIT_CONVERSION: dict[str, float] = {
UnitOfInformation.BITS: 1,
UnitOfInformation.KILOBITS: 1 / 1e3,
UnitOfInformation.MEGABITS: 1 / 1e6,
UnitOfInformation.GIGABITS: 1 / 1e9,
UnitOfInformation.BYTES: 1 / 8,
UnitOfInformation.KILOBYTES: 1 / 8e3,
UnitOfInformation.MEGABYTES: 1 / 8e6,
UnitOfInformation.GIGABYTES: 1 / 8e9,
UnitOfInformation.TERABYTES: 1 / 8e12,
UnitOfInformation.PETABYTES: 1 / 8e15,
UnitOfInformation.EXABYTES: 1 / 8e18,
UnitOfInformation.ZETTABYTES: 1 / 8e21,
UnitOfInformation.YOTTABYTES: 1 / 8e24,
UnitOfInformation.KIBIBYTES: 1 / 2**13,
UnitOfInformation.MEBIBYTES: 1 / 2**23,
UnitOfInformation.GIBIBYTES: 1 / 2**33,
UnitOfInformation.TEBIBYTES: 1 / 2**43,
UnitOfInformation.PEBIBYTES: 1 / 2**53,
UnitOfInformation.EXBIBYTES: 1 / 2**63,
UnitOfInformation.ZEBIBYTES: 1 / 2**73,
UnitOfInformation.YOBIBYTES: 1 / 2**83,
}
VALID_UNITS = set(UnitOfInformation)


class MassConverter(BaseUnitConverter):
"""Utility to convert mass values."""

Expand Down
47 changes: 47 additions & 0 deletions tests/util/test_unit_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from homeassistant.const import (
UnitOfDataRate,
UnitOfEnergy,
UnitOfInformation,
UnitOfLength,
UnitOfMass,
UnitOfPower,
Expand All @@ -19,6 +20,7 @@
DataRateConverter,
DistanceConverter,
EnergyConverter,
InformationConverter,
MassConverter,
PowerConverter,
PressureConverter,
Expand Down Expand Up @@ -46,6 +48,7 @@
(EnergyConverter, UnitOfEnergy.KILO_WATT_HOUR),
(EnergyConverter, UnitOfEnergy.MEGA_WATT_HOUR),
(EnergyConverter, UnitOfEnergy.GIGA_JOULE),
(InformationConverter, UnitOfInformation.GIGABYTES),
(MassConverter, UnitOfMass.GRAMS),
(MassConverter, UnitOfMass.KILOGRAMS),
(MassConverter, UnitOfMass.MICROGRAMS),
Expand Down Expand Up @@ -91,6 +94,7 @@ def test_convert_same_unit(converter: type[BaseUnitConverter], valid_unit: str)
(DataRateConverter, UnitOfDataRate.GIBIBYTES_PER_SECOND),
(DistanceConverter, UnitOfLength.KILOMETERS),
(EnergyConverter, UnitOfEnergy.KILO_WATT_HOUR),
(InformationConverter, UnitOfInformation.GIBIBYTES),
(MassConverter, UnitOfMass.GRAMS),
(PowerConverter, UnitOfPower.WATT),
(PressureConverter, UnitOfPressure.PA),
Expand Down Expand Up @@ -122,6 +126,11 @@ def test_convert_invalid_unit(
),
(DistanceConverter, UnitOfLength.KILOMETERS, UnitOfLength.METERS),
(EnergyConverter, UnitOfEnergy.WATT_HOUR, UnitOfEnergy.KILO_WATT_HOUR),
(
InformationConverter,
UnitOfInformation.GIBIBYTES,
UnitOfInformation.GIGABYTES,
),
(MassConverter, UnitOfMass.GRAMS, UnitOfMass.KILOGRAMS),
(PowerConverter, UnitOfPower.WATT, UnitOfPower.KILO_WATT),
(PressureConverter, UnitOfPressure.HPA, UnitOfPressure.INHG),
Expand Down Expand Up @@ -149,6 +158,7 @@ def test_convert_nonnumeric_value(
),
(DistanceConverter, UnitOfLength.KILOMETERS, UnitOfLength.METERS, 1 / 1000),
(EnergyConverter, UnitOfEnergy.WATT_HOUR, UnitOfEnergy.KILO_WATT_HOUR, 1000),
(InformationConverter, UnitOfInformation.BITS, UnitOfInformation.BYTES, 8),
(PowerConverter, UnitOfPower.WATT, UnitOfPower.KILO_WATT, 1000),
(
PressureConverter,
Expand Down Expand Up @@ -364,6 +374,43 @@ def test_energy_convert(
assert EnergyConverter.convert(value, from_unit, to_unit) == expected


@pytest.mark.parametrize(
"value,from_unit,expected,to_unit",
[
(8e3, UnitOfInformation.BITS, 8, UnitOfInformation.KILOBITS),
(8e6, UnitOfInformation.BITS, 8, UnitOfInformation.MEGABITS),
(8e9, UnitOfInformation.BITS, 8, UnitOfInformation.GIGABITS),
(8, UnitOfInformation.BITS, 1, UnitOfInformation.BYTES),
(8e3, UnitOfInformation.BITS, 1, UnitOfInformation.KILOBYTES),
(8e6, UnitOfInformation.BITS, 1, UnitOfInformation.MEGABYTES),
(8e9, UnitOfInformation.BITS, 1, UnitOfInformation.GIGABYTES),
(8e12, UnitOfInformation.BITS, 1, UnitOfInformation.TERABYTES),
(8e15, UnitOfInformation.BITS, 1, UnitOfInformation.PETABYTES),
(8e18, UnitOfInformation.BITS, 1, UnitOfInformation.EXABYTES),
(8e21, UnitOfInformation.BITS, 1, UnitOfInformation.ZETTABYTES),
(8e24, UnitOfInformation.BITS, 1, UnitOfInformation.YOTTABYTES),
(8 * 2**10, UnitOfInformation.BITS, 1, UnitOfInformation.KIBIBYTES),
(8 * 2**20, UnitOfInformation.BITS, 1, UnitOfInformation.MEBIBYTES),
(8 * 2**30, UnitOfInformation.BITS, 1, UnitOfInformation.GIBIBYTES),
(8 * 2**40, UnitOfInformation.BITS, 1, UnitOfInformation.TEBIBYTES),
(8 * 2**50, UnitOfInformation.BITS, 1, UnitOfInformation.PEBIBYTES),
(8 * 2**60, UnitOfInformation.BITS, 1, UnitOfInformation.EXBIBYTES),
(8 * 2**70, UnitOfInformation.BITS, 1, UnitOfInformation.ZEBIBYTES),
(8 * 2**80, UnitOfInformation.BITS, 1, UnitOfInformation.YOBIBYTES),
],
)
def test_information_convert(
value: float,
from_unit: str,
expected: float,
to_unit: str,
) -> None:
"""Test conversion to other units."""
assert InformationConverter.convert(value, from_unit, to_unit) == pytest.approx(
expected
)


@pytest.mark.parametrize(
"value,from_unit,expected,to_unit",
[
Expand Down