Skip to content

Commit

Permalink
Add converter
Browse files Browse the repository at this point in the history
  • Loading branch information
SukramJ committed Dec 29, 2022
1 parent eafea88 commit dd5d4c3
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
2 changes: 1 addition & 1 deletion homeassistant/components/number/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class NumberDeviceClass(StrEnum):
CURRENT = "current"
"""Current.
Unit of measurement: `A`
Unit of measurement: `A`, `mA`
"""

DATA_RATE = "data_rate"
Expand Down
7 changes: 3 additions & 4 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,
ElectricCurrentConverter,
InformationConverter,
MassConverter,
PressureConverter,
Expand Down Expand Up @@ -471,6 +472,7 @@ class SensorStateClass(StrEnum):
SensorDeviceClass.DATA_RATE: DataRateConverter,
SensorDeviceClass.DATA_SIZE: InformationConverter,
SensorDeviceClass.DISTANCE: DistanceConverter,
SensorDeviceClass.CURRENT: ElectricCurrentConverter,
SensorDeviceClass.GAS: VolumeConverter,
SensorDeviceClass.PRECIPITATION: DistanceConverter,
SensorDeviceClass.PRESSURE: PressureConverter,
Expand All @@ -489,10 +491,7 @@ class SensorStateClass(StrEnum):
SensorDeviceClass.BATTERY: {PERCENTAGE},
SensorDeviceClass.CO: {CONCENTRATION_PARTS_PER_MILLION},
SensorDeviceClass.CO2: {CONCENTRATION_PARTS_PER_MILLION},
SensorDeviceClass.CURRENT: {
UnitOfElectricCurrent.AMPERE,
UnitOfElectricCurrent.MILLIAMPERE,
},
SensorDeviceClass.CURRENT: set(UnitOfElectricCurrent),
SensorDeviceClass.DATA_RATE: set(UnitOfDataRate),
SensorDeviceClass.DATA_SIZE: set(UnitOfInformation),
SensorDeviceClass.DISTANCE: set(UnitOfLength),
Expand Down
13 changes: 13 additions & 0 deletions homeassistant/util/unit_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from homeassistant.const import (
UNIT_NOT_RECOGNIZED_TEMPLATE,
UnitOfDataRate,
UnitOfElectricCurrent,
UnitOfEnergy,
UnitOfInformation,
UnitOfLength,
Expand Down Expand Up @@ -137,6 +138,18 @@ class DistanceConverter(BaseUnitConverter):
}


class ElectricCurrentConverter(BaseUnitConverter):
"""Utility to convert electric current values."""

UNIT_CLASS = "electric_current"
NORMALIZED_UNIT = UnitOfElectricCurrent.AMPERE
_UNIT_CONVERSION: dict[str, float] = {
UnitOfElectricCurrent.AMPERE: 1,
UnitOfElectricCurrent.MILLIAMPERE: 1 / 1e3,
}
VALID_UNITS = set(UnitOfElectricCurrent)


class EnergyConverter(BaseUnitConverter):
"""Utility to convert energy values."""

Expand Down
28 changes: 28 additions & 0 deletions tests/util/test_unit_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from homeassistant.const import (
UnitOfDataRate,
UnitOfElectricCurrent,
UnitOfEnergy,
UnitOfInformation,
UnitOfLength,
Expand All @@ -19,6 +20,7 @@
BaseUnitConverter,
DataRateConverter,
DistanceConverter,
ElectricCurrentConverter,
EnergyConverter,
InformationConverter,
MassConverter,
Expand All @@ -44,6 +46,8 @@
(DistanceConverter, UnitOfLength.YARDS),
(DistanceConverter, UnitOfLength.FEET),
(DistanceConverter, UnitOfLength.INCHES),
(ElectricCurrentConverter, UnitOfElectricCurrent.AMPERE),
(ElectricCurrentConverter, UnitOfElectricCurrent.MILLIAMPERE),
(EnergyConverter, UnitOfEnergy.WATT_HOUR),
(EnergyConverter, UnitOfEnergy.KILO_WATT_HOUR),
(EnergyConverter, UnitOfEnergy.MEGA_WATT_HOUR),
Expand Down Expand Up @@ -93,6 +97,7 @@ def test_convert_same_unit(converter: type[BaseUnitConverter], valid_unit: str)
[
(DataRateConverter, UnitOfDataRate.GIBIBYTES_PER_SECOND),
(DistanceConverter, UnitOfLength.KILOMETERS),
(ElectricCurrentConverter, UnitOfElectricCurrent.AMPERE),
(EnergyConverter, UnitOfEnergy.KILO_WATT_HOUR),
(InformationConverter, UnitOfInformation.GIBIBYTES),
(MassConverter, UnitOfMass.GRAMS),
Expand Down Expand Up @@ -157,6 +162,12 @@ def test_convert_nonnumeric_value(
8,
),
(DistanceConverter, UnitOfLength.KILOMETERS, UnitOfLength.METERS, 1 / 1000),
(
ElectricCurrentConverter,
UnitOfElectricCurrent.AMPERE,
UnitOfElectricCurrent.MILLIAMPERE,
1 / 1000,
),
(EnergyConverter, UnitOfEnergy.WATT_HOUR, UnitOfEnergy.KILO_WATT_HOUR, 1000),
(InformationConverter, UnitOfInformation.BITS, UnitOfInformation.BYTES, 8),
(PowerConverter, UnitOfPower.WATT, UnitOfPower.KILO_WATT, 1000),
Expand Down Expand Up @@ -235,6 +246,23 @@ def test_data_rate_convert(
)


@pytest.mark.parametrize(
"value,from_unit,expected,to_unit",
[
(5, UnitOfElectricCurrent.AMPERE, 5000, UnitOfElectricCurrent.MILLIAMPERE),
(5, UnitOfElectricCurrent.MILLIAMPERE, 0.005, UnitOfElectricCurrent.AMPERE),
],
)
def test_electric_current_convert(
value: float,
from_unit: str,
expected: float,
to_unit: str,
) -> None:
"""Test conversion to other units."""
assert ElectricCurrentConverter.convert(value, from_unit, to_unit) == expected


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

0 comments on commit dd5d4c3

Please sign in to comment.