Use unit enums in unit utilities (#81030)

This commit is contained in:
epenet 2022-10-26 20:47:17 +02:00 committed by GitHub
parent 37cfa3e19b
commit 2a2e097e17
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 690 additions and 591 deletions

View file

@ -7,33 +7,17 @@ from homeassistant.components.sensor import SensorDeviceClass
from homeassistant.const import (
ACCUMULATED_PRECIPITATION,
LENGTH,
LENGTH_CENTIMETERS,
LENGTH_FEET,
LENGTH_INCHES,
LENGTH_KILOMETERS,
LENGTH_METERS,
LENGTH_MILES,
LENGTH_MILLIMETERS,
LENGTH_YARD,
MASS,
MASS_GRAMS,
PRESSURE,
PRESSURE_PA,
SPEED_FEET_PER_SECOND,
SPEED_KILOMETERS_PER_HOUR,
SPEED_KNOTS,
SPEED_METERS_PER_SECOND,
SPEED_MILES_PER_HOUR,
TEMP_CELSIUS,
TEMPERATURE,
VOLUME,
VOLUME_CUBIC_FEET,
VOLUME_CUBIC_METERS,
VOLUME_FLUID_OUNCE,
VOLUME_GALLONS,
VOLUME_LITERS,
VOLUME_MILLILITERS,
WIND_SPEED,
UnitOfLength,
UnitOfMass,
UnitOfPressure,
UnitOfSpeed,
UnitOfTemperature,
UnitOfVolume,
)
from homeassistant.exceptions import HomeAssistantError
from homeassistant.util.unit_system import (
@ -56,79 +40,79 @@ def test_invalid_units():
with pytest.raises(ValueError):
UnitSystem(
SYSTEM_NAME,
accumulated_precipitation=LENGTH_MILLIMETERS,
accumulated_precipitation=UnitOfLength.MILLIMETERS,
conversions={},
length=LENGTH_METERS,
mass=MASS_GRAMS,
pressure=PRESSURE_PA,
length=UnitOfLength.METERS,
mass=UnitOfMass.GRAMS,
pressure=UnitOfPressure.PA,
temperature=INVALID_UNIT,
volume=VOLUME_LITERS,
wind_speed=SPEED_METERS_PER_SECOND,
volume=UnitOfVolume.LITERS,
wind_speed=UnitOfSpeed.METERS_PER_SECOND,
)
with pytest.raises(ValueError):
UnitSystem(
SYSTEM_NAME,
accumulated_precipitation=LENGTH_MILLIMETERS,
accumulated_precipitation=UnitOfLength.MILLIMETERS,
conversions={},
length=INVALID_UNIT,
mass=MASS_GRAMS,
pressure=PRESSURE_PA,
temperature=TEMP_CELSIUS,
volume=VOLUME_LITERS,
wind_speed=SPEED_METERS_PER_SECOND,
mass=UnitOfMass.GRAMS,
pressure=UnitOfPressure.PA,
temperature=UnitOfTemperature.CELSIUS,
volume=UnitOfVolume.LITERS,
wind_speed=UnitOfSpeed.METERS_PER_SECOND,
)
with pytest.raises(ValueError):
UnitSystem(
SYSTEM_NAME,
accumulated_precipitation=LENGTH_MILLIMETERS,
accumulated_precipitation=UnitOfLength.MILLIMETERS,
conversions={},
length=LENGTH_METERS,
mass=MASS_GRAMS,
pressure=PRESSURE_PA,
temperature=TEMP_CELSIUS,
volume=VOLUME_LITERS,
length=UnitOfLength.METERS,
mass=UnitOfMass.GRAMS,
pressure=UnitOfPressure.PA,
temperature=UnitOfTemperature.CELSIUS,
volume=UnitOfVolume.LITERS,
wind_speed=INVALID_UNIT,
)
with pytest.raises(ValueError):
UnitSystem(
SYSTEM_NAME,
accumulated_precipitation=LENGTH_MILLIMETERS,
accumulated_precipitation=UnitOfLength.MILLIMETERS,
conversions={},
length=LENGTH_METERS,
mass=MASS_GRAMS,
pressure=PRESSURE_PA,
temperature=TEMP_CELSIUS,
length=UnitOfLength.METERS,
mass=UnitOfMass.GRAMS,
pressure=UnitOfPressure.PA,
temperature=UnitOfTemperature.CELSIUS,
volume=INVALID_UNIT,
wind_speed=SPEED_METERS_PER_SECOND,
wind_speed=UnitOfSpeed.METERS_PER_SECOND,
)
with pytest.raises(ValueError):
UnitSystem(
SYSTEM_NAME,
accumulated_precipitation=LENGTH_MILLIMETERS,
accumulated_precipitation=UnitOfLength.MILLIMETERS,
conversions={},
length=LENGTH_METERS,
length=UnitOfLength.METERS,
mass=INVALID_UNIT,
pressure=PRESSURE_PA,
temperature=TEMP_CELSIUS,
volume=VOLUME_LITERS,
wind_speed=SPEED_METERS_PER_SECOND,
pressure=UnitOfPressure.PA,
temperature=UnitOfTemperature.CELSIUS,
volume=UnitOfVolume.LITERS,
wind_speed=UnitOfSpeed.METERS_PER_SECOND,
)
with pytest.raises(ValueError):
UnitSystem(
SYSTEM_NAME,
accumulated_precipitation=LENGTH_MILLIMETERS,
accumulated_precipitation=UnitOfLength.MILLIMETERS,
conversions={},
length=LENGTH_METERS,
mass=MASS_GRAMS,
length=UnitOfLength.METERS,
mass=UnitOfMass.GRAMS,
pressure=INVALID_UNIT,
temperature=TEMP_CELSIUS,
volume=VOLUME_LITERS,
wind_speed=SPEED_METERS_PER_SECOND,
temperature=UnitOfTemperature.CELSIUS,
volume=UnitOfVolume.LITERS,
wind_speed=UnitOfSpeed.METERS_PER_SECOND,
)
with pytest.raises(ValueError):
@ -136,41 +120,41 @@ def test_invalid_units():
SYSTEM_NAME,
accumulated_precipitation=INVALID_UNIT,
conversions={},
length=LENGTH_METERS,
mass=MASS_GRAMS,
pressure=PRESSURE_PA,
temperature=TEMP_CELSIUS,
volume=VOLUME_LITERS,
wind_speed=SPEED_METERS_PER_SECOND,
length=UnitOfLength.METERS,
mass=UnitOfMass.GRAMS,
pressure=UnitOfPressure.PA,
temperature=UnitOfTemperature.CELSIUS,
volume=UnitOfVolume.LITERS,
wind_speed=UnitOfSpeed.METERS_PER_SECOND,
)
def test_invalid_value():
"""Test no conversion happens if value is non-numeric."""
with pytest.raises(TypeError):
METRIC_SYSTEM.length("25a", LENGTH_KILOMETERS)
METRIC_SYSTEM.length("25a", UnitOfLength.KILOMETERS)
with pytest.raises(TypeError):
METRIC_SYSTEM.temperature("50K", TEMP_CELSIUS)
METRIC_SYSTEM.temperature("50K", UnitOfTemperature.CELSIUS)
with pytest.raises(TypeError):
METRIC_SYSTEM.wind_speed("50km/h", SPEED_METERS_PER_SECOND)
METRIC_SYSTEM.wind_speed("50km/h", UnitOfSpeed.METERS_PER_SECOND)
with pytest.raises(TypeError):
METRIC_SYSTEM.volume("50L", VOLUME_LITERS)
METRIC_SYSTEM.volume("50L", UnitOfVolume.LITERS)
with pytest.raises(TypeError):
METRIC_SYSTEM.pressure("50Pa", PRESSURE_PA)
METRIC_SYSTEM.pressure("50Pa", UnitOfPressure.PA)
with pytest.raises(TypeError):
METRIC_SYSTEM.accumulated_precipitation("50mm", LENGTH_MILLIMETERS)
METRIC_SYSTEM.accumulated_precipitation("50mm", UnitOfLength.MILLIMETERS)
def test_as_dict():
"""Test that the as_dict() method returns the expected dictionary."""
expected = {
LENGTH: LENGTH_KILOMETERS,
WIND_SPEED: SPEED_METERS_PER_SECOND,
TEMPERATURE: TEMP_CELSIUS,
VOLUME: VOLUME_LITERS,
MASS: MASS_GRAMS,
PRESSURE: PRESSURE_PA,
ACCUMULATED_PRECIPITATION: LENGTH_MILLIMETERS,
LENGTH: UnitOfLength.KILOMETERS,
WIND_SPEED: UnitOfSpeed.METERS_PER_SECOND,
TEMPERATURE: UnitOfTemperature.CELSIUS,
VOLUME: UnitOfVolume.LITERS,
MASS: UnitOfMass.GRAMS,
PRESSURE: UnitOfPressure.PA,
ACCUMULATED_PRECIPITATION: UnitOfLength.MILLIMETERS,
}
assert expected == METRIC_SYSTEM.as_dict()
@ -318,13 +302,13 @@ def test_accumulated_precipitation_to_imperial():
def test_properties():
"""Test the unit properties are returned as expected."""
assert METRIC_SYSTEM.length_unit == LENGTH_KILOMETERS
assert METRIC_SYSTEM.wind_speed_unit == SPEED_METERS_PER_SECOND
assert METRIC_SYSTEM.temperature_unit == TEMP_CELSIUS
assert METRIC_SYSTEM.mass_unit == MASS_GRAMS
assert METRIC_SYSTEM.volume_unit == VOLUME_LITERS
assert METRIC_SYSTEM.pressure_unit == PRESSURE_PA
assert METRIC_SYSTEM.accumulated_precipitation_unit == LENGTH_MILLIMETERS
assert METRIC_SYSTEM.length_unit == UnitOfLength.KILOMETERS
assert METRIC_SYSTEM.wind_speed_unit == UnitOfSpeed.METERS_PER_SECOND
assert METRIC_SYSTEM.temperature_unit == UnitOfTemperature.CELSIUS
assert METRIC_SYSTEM.mass_unit == UnitOfMass.GRAMS
assert METRIC_SYSTEM.volume_unit == UnitOfVolume.LITERS
assert METRIC_SYSTEM.pressure_unit == UnitOfPressure.PA
assert METRIC_SYSTEM.accumulated_precipitation_unit == UnitOfLength.MILLIMETERS
@pytest.mark.parametrize(
@ -397,36 +381,44 @@ def test_get_unit_system_invalid(key: str) -> None:
"device_class, original_unit, state_unit",
(
# Test distance conversion
(SensorDeviceClass.DISTANCE, LENGTH_FEET, LENGTH_METERS),
(SensorDeviceClass.DISTANCE, LENGTH_INCHES, LENGTH_MILLIMETERS),
(SensorDeviceClass.DISTANCE, LENGTH_MILES, LENGTH_KILOMETERS),
(SensorDeviceClass.DISTANCE, LENGTH_YARD, LENGTH_METERS),
(SensorDeviceClass.DISTANCE, LENGTH_KILOMETERS, None),
(SensorDeviceClass.DISTANCE, UnitOfLength.FEET, UnitOfLength.METERS),
(SensorDeviceClass.DISTANCE, UnitOfLength.INCHES, UnitOfLength.MILLIMETERS),
(SensorDeviceClass.DISTANCE, UnitOfLength.MILES, UnitOfLength.KILOMETERS),
(SensorDeviceClass.DISTANCE, UnitOfLength.YARDS, UnitOfLength.METERS),
(SensorDeviceClass.DISTANCE, UnitOfLength.KILOMETERS, None),
(SensorDeviceClass.DISTANCE, "very_long", None),
# Test gas meter conversion
(SensorDeviceClass.GAS, VOLUME_CUBIC_FEET, VOLUME_CUBIC_METERS),
(SensorDeviceClass.GAS, VOLUME_CUBIC_METERS, None),
(SensorDeviceClass.GAS, UnitOfVolume.CUBIC_FEET, UnitOfVolume.CUBIC_METERS),
(SensorDeviceClass.GAS, UnitOfVolume.CUBIC_METERS, None),
(SensorDeviceClass.GAS, "very_much", None),
# Test speed conversion
(SensorDeviceClass.SPEED, SPEED_FEET_PER_SECOND, SPEED_KILOMETERS_PER_HOUR),
(SensorDeviceClass.SPEED, SPEED_MILES_PER_HOUR, SPEED_KILOMETERS_PER_HOUR),
(SensorDeviceClass.SPEED, SPEED_KILOMETERS_PER_HOUR, None),
(SensorDeviceClass.SPEED, SPEED_KNOTS, None),
(SensorDeviceClass.SPEED, SPEED_METERS_PER_SECOND, None),
(
SensorDeviceClass.SPEED,
UnitOfSpeed.FEET_PER_SECOND,
UnitOfSpeed.KILOMETERS_PER_HOUR,
),
(
SensorDeviceClass.SPEED,
UnitOfSpeed.MILES_PER_HOUR,
UnitOfSpeed.KILOMETERS_PER_HOUR,
),
(SensorDeviceClass.SPEED, UnitOfSpeed.KILOMETERS_PER_HOUR, None),
(SensorDeviceClass.SPEED, UnitOfSpeed.KNOTS, None),
(SensorDeviceClass.SPEED, UnitOfSpeed.METERS_PER_SECOND, None),
(SensorDeviceClass.SPEED, "very_fast", None),
# Test volume conversion
(SensorDeviceClass.VOLUME, VOLUME_CUBIC_FEET, VOLUME_CUBIC_METERS),
(SensorDeviceClass.VOLUME, VOLUME_FLUID_OUNCE, VOLUME_MILLILITERS),
(SensorDeviceClass.VOLUME, VOLUME_GALLONS, VOLUME_LITERS),
(SensorDeviceClass.VOLUME, VOLUME_CUBIC_METERS, None),
(SensorDeviceClass.VOLUME, VOLUME_LITERS, None),
(SensorDeviceClass.VOLUME, VOLUME_MILLILITERS, None),
(SensorDeviceClass.VOLUME, UnitOfVolume.CUBIC_FEET, UnitOfVolume.CUBIC_METERS),
(SensorDeviceClass.VOLUME, UnitOfVolume.FLUID_OUNCES, UnitOfVolume.MILLILITERS),
(SensorDeviceClass.VOLUME, UnitOfVolume.GALLONS, UnitOfVolume.LITERS),
(SensorDeviceClass.VOLUME, UnitOfVolume.CUBIC_METERS, None),
(SensorDeviceClass.VOLUME, UnitOfVolume.LITERS, None),
(SensorDeviceClass.VOLUME, UnitOfVolume.MILLILITERS, None),
(SensorDeviceClass.VOLUME, "very_much", None),
# Test water meter conversion
(SensorDeviceClass.WATER, VOLUME_CUBIC_FEET, VOLUME_CUBIC_METERS),
(SensorDeviceClass.WATER, VOLUME_GALLONS, VOLUME_LITERS),
(SensorDeviceClass.WATER, VOLUME_CUBIC_METERS, None),
(SensorDeviceClass.WATER, VOLUME_LITERS, None),
(SensorDeviceClass.WATER, UnitOfVolume.CUBIC_FEET, UnitOfVolume.CUBIC_METERS),
(SensorDeviceClass.WATER, UnitOfVolume.GALLONS, UnitOfVolume.LITERS),
(SensorDeviceClass.WATER, UnitOfVolume.CUBIC_METERS, None),
(SensorDeviceClass.WATER, UnitOfVolume.LITERS, None),
(SensorDeviceClass.WATER, "very_much", None),
),
)
@ -444,36 +436,44 @@ def test_get_metric_converted_unit_(
"device_class, original_unit, state_unit",
(
# Test distance conversion
(SensorDeviceClass.DISTANCE, LENGTH_CENTIMETERS, LENGTH_INCHES),
(SensorDeviceClass.DISTANCE, LENGTH_KILOMETERS, LENGTH_MILES),
(SensorDeviceClass.DISTANCE, LENGTH_METERS, LENGTH_FEET),
(SensorDeviceClass.DISTANCE, LENGTH_MILLIMETERS, LENGTH_INCHES),
(SensorDeviceClass.DISTANCE, LENGTH_MILES, None),
(SensorDeviceClass.DISTANCE, UnitOfLength.CENTIMETERS, UnitOfLength.INCHES),
(SensorDeviceClass.DISTANCE, UnitOfLength.KILOMETERS, UnitOfLength.MILES),
(SensorDeviceClass.DISTANCE, UnitOfLength.METERS, UnitOfLength.FEET),
(SensorDeviceClass.DISTANCE, UnitOfLength.MILLIMETERS, UnitOfLength.INCHES),
(SensorDeviceClass.DISTANCE, UnitOfLength.MILES, None),
(SensorDeviceClass.DISTANCE, "very_long", None),
# Test gas meter conversion
(SensorDeviceClass.GAS, VOLUME_CUBIC_METERS, VOLUME_CUBIC_FEET),
(SensorDeviceClass.GAS, VOLUME_CUBIC_FEET, None),
(SensorDeviceClass.GAS, UnitOfVolume.CUBIC_METERS, UnitOfVolume.CUBIC_FEET),
(SensorDeviceClass.GAS, UnitOfVolume.CUBIC_FEET, None),
(SensorDeviceClass.GAS, "very_much", None),
# Test speed conversion
(SensorDeviceClass.SPEED, SPEED_METERS_PER_SECOND, SPEED_MILES_PER_HOUR),
(SensorDeviceClass.SPEED, SPEED_KILOMETERS_PER_HOUR, SPEED_MILES_PER_HOUR),
(SensorDeviceClass.SPEED, SPEED_FEET_PER_SECOND, None),
(SensorDeviceClass.SPEED, SPEED_KNOTS, None),
(SensorDeviceClass.SPEED, SPEED_MILES_PER_HOUR, None),
(
SensorDeviceClass.SPEED,
UnitOfSpeed.METERS_PER_SECOND,
UnitOfSpeed.MILES_PER_HOUR,
),
(
SensorDeviceClass.SPEED,
UnitOfSpeed.KILOMETERS_PER_HOUR,
UnitOfSpeed.MILES_PER_HOUR,
),
(SensorDeviceClass.SPEED, UnitOfSpeed.FEET_PER_SECOND, None),
(SensorDeviceClass.SPEED, UnitOfSpeed.KNOTS, None),
(SensorDeviceClass.SPEED, UnitOfSpeed.MILES_PER_HOUR, None),
(SensorDeviceClass.SPEED, "very_fast", None),
# Test volume conversion
(SensorDeviceClass.VOLUME, VOLUME_CUBIC_METERS, VOLUME_CUBIC_FEET),
(SensorDeviceClass.VOLUME, VOLUME_LITERS, VOLUME_GALLONS),
(SensorDeviceClass.VOLUME, VOLUME_MILLILITERS, VOLUME_FLUID_OUNCE),
(SensorDeviceClass.VOLUME, VOLUME_CUBIC_FEET, None),
(SensorDeviceClass.VOLUME, VOLUME_FLUID_OUNCE, None),
(SensorDeviceClass.VOLUME, VOLUME_GALLONS, None),
(SensorDeviceClass.VOLUME, UnitOfVolume.CUBIC_METERS, UnitOfVolume.CUBIC_FEET),
(SensorDeviceClass.VOLUME, UnitOfVolume.LITERS, UnitOfVolume.GALLONS),
(SensorDeviceClass.VOLUME, UnitOfVolume.MILLILITERS, UnitOfVolume.FLUID_OUNCES),
(SensorDeviceClass.VOLUME, UnitOfVolume.CUBIC_FEET, None),
(SensorDeviceClass.VOLUME, UnitOfVolume.FLUID_OUNCES, None),
(SensorDeviceClass.VOLUME, UnitOfVolume.GALLONS, None),
(SensorDeviceClass.VOLUME, "very_much", None),
# Test water meter conversion
(SensorDeviceClass.WATER, VOLUME_CUBIC_METERS, VOLUME_CUBIC_FEET),
(SensorDeviceClass.WATER, VOLUME_LITERS, VOLUME_GALLONS),
(SensorDeviceClass.WATER, VOLUME_CUBIC_FEET, None),
(SensorDeviceClass.WATER, VOLUME_GALLONS, None),
(SensorDeviceClass.WATER, UnitOfVolume.CUBIC_METERS, UnitOfVolume.CUBIC_FEET),
(SensorDeviceClass.WATER, UnitOfVolume.LITERS, UnitOfVolume.GALLONS),
(SensorDeviceClass.WATER, UnitOfVolume.CUBIC_FEET, None),
(SensorDeviceClass.WATER, UnitOfVolume.GALLONS, None),
(SensorDeviceClass.WATER, "very_much", None),
),
)