mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
Add additional rules for converting distances (#80940)
* Add additional rules for converting distances * Convert in to mm * Adjust existing tests * Add test
This commit is contained in:
@@ -4,9 +4,14 @@ import pytest
|
||||
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,
|
||||
@@ -40,8 +45,8 @@ def test_invalid_units():
|
||||
UnitSystem(
|
||||
SYSTEM_NAME,
|
||||
accumulated_precipitation=LENGTH_MILLIMETERS,
|
||||
conversions={},
|
||||
length=LENGTH_METERS,
|
||||
length_conversions={},
|
||||
mass=MASS_GRAMS,
|
||||
pressure=PRESSURE_PA,
|
||||
temperature=INVALID_UNIT,
|
||||
@@ -53,8 +58,8 @@ def test_invalid_units():
|
||||
UnitSystem(
|
||||
SYSTEM_NAME,
|
||||
accumulated_precipitation=LENGTH_MILLIMETERS,
|
||||
conversions={},
|
||||
length=INVALID_UNIT,
|
||||
length_conversions={},
|
||||
mass=MASS_GRAMS,
|
||||
pressure=PRESSURE_PA,
|
||||
temperature=TEMP_CELSIUS,
|
||||
@@ -66,8 +71,8 @@ def test_invalid_units():
|
||||
UnitSystem(
|
||||
SYSTEM_NAME,
|
||||
accumulated_precipitation=LENGTH_MILLIMETERS,
|
||||
conversions={},
|
||||
length=LENGTH_METERS,
|
||||
length_conversions={},
|
||||
mass=MASS_GRAMS,
|
||||
pressure=PRESSURE_PA,
|
||||
temperature=TEMP_CELSIUS,
|
||||
@@ -79,8 +84,8 @@ def test_invalid_units():
|
||||
UnitSystem(
|
||||
SYSTEM_NAME,
|
||||
accumulated_precipitation=LENGTH_MILLIMETERS,
|
||||
conversions={},
|
||||
length=LENGTH_METERS,
|
||||
length_conversions={},
|
||||
mass=MASS_GRAMS,
|
||||
pressure=PRESSURE_PA,
|
||||
temperature=TEMP_CELSIUS,
|
||||
@@ -92,8 +97,8 @@ def test_invalid_units():
|
||||
UnitSystem(
|
||||
SYSTEM_NAME,
|
||||
accumulated_precipitation=LENGTH_MILLIMETERS,
|
||||
conversions={},
|
||||
length=LENGTH_METERS,
|
||||
length_conversions={},
|
||||
mass=INVALID_UNIT,
|
||||
pressure=PRESSURE_PA,
|
||||
temperature=TEMP_CELSIUS,
|
||||
@@ -105,8 +110,8 @@ def test_invalid_units():
|
||||
UnitSystem(
|
||||
SYSTEM_NAME,
|
||||
accumulated_precipitation=LENGTH_MILLIMETERS,
|
||||
conversions={},
|
||||
length=LENGTH_METERS,
|
||||
length_conversions={},
|
||||
mass=MASS_GRAMS,
|
||||
pressure=INVALID_UNIT,
|
||||
temperature=TEMP_CELSIUS,
|
||||
@@ -118,8 +123,8 @@ def test_invalid_units():
|
||||
UnitSystem(
|
||||
SYSTEM_NAME,
|
||||
accumulated_precipitation=INVALID_UNIT,
|
||||
conversions={},
|
||||
length=LENGTH_METERS,
|
||||
length_conversions={},
|
||||
mass=MASS_GRAMS,
|
||||
pressure=PRESSURE_PA,
|
||||
temperature=TEMP_CELSIUS,
|
||||
@@ -374,3 +379,27 @@ def test_get_unit_system_invalid(key: str) -> None:
|
||||
"""Test get_unit_system with an invalid key."""
|
||||
with pytest.raises(ValueError, match=f"`{key}` is not a valid unit system key"):
|
||||
_ = get_unit_system(key)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"unit_system, device_class, original_unit, state_unit",
|
||||
(
|
||||
(METRIC_SYSTEM, "distance", LENGTH_FEET, LENGTH_METERS),
|
||||
(METRIC_SYSTEM, "distance", LENGTH_INCHES, LENGTH_MILLIMETERS),
|
||||
(METRIC_SYSTEM, "distance", LENGTH_MILES, LENGTH_KILOMETERS),
|
||||
(METRIC_SYSTEM, "distance", LENGTH_YARD, LENGTH_METERS),
|
||||
(METRIC_SYSTEM, "distance", LENGTH_KILOMETERS, None),
|
||||
(METRIC_SYSTEM, "distance", "very_long", None),
|
||||
(US_CUSTOMARY_SYSTEM, "distance", LENGTH_CENTIMETERS, LENGTH_INCHES),
|
||||
(US_CUSTOMARY_SYSTEM, "distance", LENGTH_KILOMETERS, LENGTH_MILES),
|
||||
(US_CUSTOMARY_SYSTEM, "distance", LENGTH_METERS, LENGTH_FEET),
|
||||
(US_CUSTOMARY_SYSTEM, "distance", LENGTH_MILLIMETERS, LENGTH_INCHES),
|
||||
(US_CUSTOMARY_SYSTEM, "distance", LENGTH_MILES, None),
|
||||
(US_CUSTOMARY_SYSTEM, "distance", "very_long", None),
|
||||
),
|
||||
)
|
||||
def test_get_converted_unit(
|
||||
unit_system, device_class, original_unit, state_unit
|
||||
) -> None:
|
||||
"""Test unit conversion rules."""
|
||||
assert unit_system.get_converted_unit(device_class, original_unit) == state_unit
|
||||
|
||||
Reference in New Issue
Block a user