2016-07-31 20:24:49 +00:00
|
|
|
"""Test the unit system helper."""
|
2019-04-30 16:20:38 +00:00
|
|
|
import pytest
|
2016-07-31 20:24:49 +00:00
|
|
|
|
|
|
|
from homeassistant.const import (
|
2021-11-19 08:18:44 +00:00
|
|
|
ACCUMULATED_PRECIPITATION,
|
2019-12-09 15:52:24 +00:00
|
|
|
LENGTH,
|
2022-10-25 10:51:23 +00:00
|
|
|
LENGTH_CENTIMETERS,
|
|
|
|
LENGTH_FEET,
|
|
|
|
LENGTH_INCHES,
|
2016-07-31 20:24:49 +00:00
|
|
|
LENGTH_KILOMETERS,
|
2019-12-09 15:52:24 +00:00
|
|
|
LENGTH_METERS,
|
2022-10-25 10:51:23 +00:00
|
|
|
LENGTH_MILES,
|
2021-11-19 08:18:44 +00:00
|
|
|
LENGTH_MILLIMETERS,
|
2022-10-25 10:51:23 +00:00
|
|
|
LENGTH_YARD,
|
2019-12-09 15:52:24 +00:00
|
|
|
MASS,
|
2016-07-31 20:24:49 +00:00
|
|
|
MASS_GRAMS,
|
2019-12-09 15:52:24 +00:00
|
|
|
PRESSURE,
|
2019-03-24 17:37:31 +00:00
|
|
|
PRESSURE_PA,
|
2022-10-25 12:53:59 +00:00
|
|
|
SPEED_FEET_PER_SECOND,
|
|
|
|
SPEED_KILOMETERS_PER_HOUR,
|
|
|
|
SPEED_KNOTS,
|
2021-11-18 15:08:42 +00:00
|
|
|
SPEED_METERS_PER_SECOND,
|
2022-10-25 12:53:59 +00:00
|
|
|
SPEED_MILES_PER_HOUR,
|
2016-07-31 20:24:49 +00:00
|
|
|
TEMP_CELSIUS,
|
|
|
|
TEMPERATURE,
|
2019-07-31 19:25:30 +00:00
|
|
|
VOLUME,
|
2019-12-09 15:52:24 +00:00
|
|
|
VOLUME_LITERS,
|
2021-11-18 15:08:42 +00:00
|
|
|
WIND_SPEED,
|
2016-07-31 20:24:49 +00:00
|
|
|
)
|
2022-09-28 08:58:04 +00:00
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
2022-10-14 14:50:04 +00:00
|
|
|
from homeassistant.util.unit_system import (
|
|
|
|
_CONF_UNIT_SYSTEM_IMPERIAL,
|
|
|
|
_CONF_UNIT_SYSTEM_METRIC,
|
2022-10-19 11:31:08 +00:00
|
|
|
_CONF_UNIT_SYSTEM_US_CUSTOMARY,
|
2022-10-14 14:50:04 +00:00
|
|
|
IMPERIAL_SYSTEM,
|
|
|
|
METRIC_SYSTEM,
|
2022-10-19 11:31:08 +00:00
|
|
|
US_CUSTOMARY_SYSTEM,
|
2022-10-14 14:50:04 +00:00
|
|
|
UnitSystem,
|
|
|
|
get_unit_system,
|
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
|
|
|
|
SYSTEM_NAME = "TEST"
|
|
|
|
INVALID_UNIT = "INVALID"
|
2016-07-31 20:24:49 +00:00
|
|
|
|
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
def test_invalid_units():
|
|
|
|
"""Test errors are raised when invalid units are passed in."""
|
|
|
|
with pytest.raises(ValueError):
|
2019-07-31 19:25:30 +00:00
|
|
|
UnitSystem(
|
|
|
|
SYSTEM_NAME,
|
2022-10-24 14:08:02 +00:00
|
|
|
accumulated_precipitation=LENGTH_MILLIMETERS,
|
2022-10-25 10:51:23 +00:00
|
|
|
conversions={},
|
2022-10-20 08:43:32 +00:00
|
|
|
length=LENGTH_METERS,
|
|
|
|
mass=MASS_GRAMS,
|
|
|
|
pressure=PRESSURE_PA,
|
2022-10-24 14:08:02 +00:00
|
|
|
temperature=INVALID_UNIT,
|
|
|
|
volume=VOLUME_LITERS,
|
|
|
|
wind_speed=SPEED_METERS_PER_SECOND,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
2019-07-31 19:25:30 +00:00
|
|
|
UnitSystem(
|
|
|
|
SYSTEM_NAME,
|
2022-10-24 14:08:02 +00:00
|
|
|
accumulated_precipitation=LENGTH_MILLIMETERS,
|
2022-10-25 10:51:23 +00:00
|
|
|
conversions={},
|
2022-10-20 08:43:32 +00:00
|
|
|
length=INVALID_UNIT,
|
|
|
|
mass=MASS_GRAMS,
|
|
|
|
pressure=PRESSURE_PA,
|
2022-10-24 14:08:02 +00:00
|
|
|
temperature=TEMP_CELSIUS,
|
|
|
|
volume=VOLUME_LITERS,
|
|
|
|
wind_speed=SPEED_METERS_PER_SECOND,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
2019-07-31 19:25:30 +00:00
|
|
|
UnitSystem(
|
|
|
|
SYSTEM_NAME,
|
2022-10-24 14:08:02 +00:00
|
|
|
accumulated_precipitation=LENGTH_MILLIMETERS,
|
2022-10-25 10:51:23 +00:00
|
|
|
conversions={},
|
2022-10-20 08:43:32 +00:00
|
|
|
length=LENGTH_METERS,
|
|
|
|
mass=MASS_GRAMS,
|
|
|
|
pressure=PRESSURE_PA,
|
2022-10-24 14:08:02 +00:00
|
|
|
temperature=TEMP_CELSIUS,
|
|
|
|
volume=VOLUME_LITERS,
|
|
|
|
wind_speed=INVALID_UNIT,
|
2021-11-18 15:08:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
UnitSystem(
|
|
|
|
SYSTEM_NAME,
|
2022-10-24 14:08:02 +00:00
|
|
|
accumulated_precipitation=LENGTH_MILLIMETERS,
|
2022-10-25 10:51:23 +00:00
|
|
|
conversions={},
|
2022-10-20 08:43:32 +00:00
|
|
|
length=LENGTH_METERS,
|
|
|
|
mass=MASS_GRAMS,
|
|
|
|
pressure=PRESSURE_PA,
|
2022-10-24 14:08:02 +00:00
|
|
|
temperature=TEMP_CELSIUS,
|
|
|
|
volume=INVALID_UNIT,
|
|
|
|
wind_speed=SPEED_METERS_PER_SECOND,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
2019-07-31 19:25:30 +00:00
|
|
|
UnitSystem(
|
|
|
|
SYSTEM_NAME,
|
2022-10-24 14:08:02 +00:00
|
|
|
accumulated_precipitation=LENGTH_MILLIMETERS,
|
2022-10-25 10:51:23 +00:00
|
|
|
conversions={},
|
2022-10-20 08:43:32 +00:00
|
|
|
length=LENGTH_METERS,
|
|
|
|
mass=INVALID_UNIT,
|
|
|
|
pressure=PRESSURE_PA,
|
2022-10-24 14:08:02 +00:00
|
|
|
temperature=TEMP_CELSIUS,
|
|
|
|
volume=VOLUME_LITERS,
|
|
|
|
wind_speed=SPEED_METERS_PER_SECOND,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
2019-07-31 19:25:30 +00:00
|
|
|
UnitSystem(
|
|
|
|
SYSTEM_NAME,
|
2022-10-24 14:08:02 +00:00
|
|
|
accumulated_precipitation=LENGTH_MILLIMETERS,
|
2022-10-25 10:51:23 +00:00
|
|
|
conversions={},
|
2022-10-20 08:43:32 +00:00
|
|
|
length=LENGTH_METERS,
|
|
|
|
mass=MASS_GRAMS,
|
|
|
|
pressure=INVALID_UNIT,
|
2022-10-24 14:08:02 +00:00
|
|
|
temperature=TEMP_CELSIUS,
|
|
|
|
volume=VOLUME_LITERS,
|
|
|
|
wind_speed=SPEED_METERS_PER_SECOND,
|
2021-11-19 08:18:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
UnitSystem(
|
|
|
|
SYSTEM_NAME,
|
2022-10-24 14:08:02 +00:00
|
|
|
accumulated_precipitation=INVALID_UNIT,
|
2022-10-25 10:51:23 +00:00
|
|
|
conversions={},
|
2022-10-20 08:43:32 +00:00
|
|
|
length=LENGTH_METERS,
|
|
|
|
mass=MASS_GRAMS,
|
|
|
|
pressure=PRESSURE_PA,
|
2022-10-24 14:08:02 +00:00
|
|
|
temperature=TEMP_CELSIUS,
|
|
|
|
volume=VOLUME_LITERS,
|
|
|
|
wind_speed=SPEED_METERS_PER_SECOND,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_invalid_value():
|
|
|
|
"""Test no conversion happens if value is non-numeric."""
|
|
|
|
with pytest.raises(TypeError):
|
2019-07-31 19:25:30 +00:00
|
|
|
METRIC_SYSTEM.length("25a", LENGTH_KILOMETERS)
|
2019-04-30 16:20:38 +00:00
|
|
|
with pytest.raises(TypeError):
|
2019-07-31 19:25:30 +00:00
|
|
|
METRIC_SYSTEM.temperature("50K", TEMP_CELSIUS)
|
2021-11-18 15:08:42 +00:00
|
|
|
with pytest.raises(TypeError):
|
|
|
|
METRIC_SYSTEM.wind_speed("50km/h", SPEED_METERS_PER_SECOND)
|
2019-04-30 16:20:38 +00:00
|
|
|
with pytest.raises(TypeError):
|
2019-07-31 19:25:30 +00:00
|
|
|
METRIC_SYSTEM.volume("50L", VOLUME_LITERS)
|
2019-04-30 16:20:38 +00:00
|
|
|
with pytest.raises(TypeError):
|
2019-07-31 19:25:30 +00:00
|
|
|
METRIC_SYSTEM.pressure("50Pa", PRESSURE_PA)
|
2021-11-19 08:18:44 +00:00
|
|
|
with pytest.raises(TypeError):
|
|
|
|
METRIC_SYSTEM.accumulated_precipitation("50mm", LENGTH_MILLIMETERS)
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_as_dict():
|
|
|
|
"""Test that the as_dict() method returns the expected dictionary."""
|
|
|
|
expected = {
|
|
|
|
LENGTH: LENGTH_KILOMETERS,
|
2021-11-18 15:08:42 +00:00
|
|
|
WIND_SPEED: SPEED_METERS_PER_SECOND,
|
2019-04-30 16:20:38 +00:00
|
|
|
TEMPERATURE: TEMP_CELSIUS,
|
|
|
|
VOLUME: VOLUME_LITERS,
|
|
|
|
MASS: MASS_GRAMS,
|
2019-07-31 19:25:30 +00:00
|
|
|
PRESSURE: PRESSURE_PA,
|
2021-11-19 08:18:44 +00:00
|
|
|
ACCUMULATED_PRECIPITATION: LENGTH_MILLIMETERS,
|
2019-04-30 16:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assert expected == METRIC_SYSTEM.as_dict()
|
|
|
|
|
|
|
|
|
|
|
|
def test_temperature_same_unit():
|
|
|
|
"""Test no conversion happens if to unit is same as from unit."""
|
|
|
|
assert METRIC_SYSTEM.temperature(5, METRIC_SYSTEM.temperature_unit) == 5
|
|
|
|
|
|
|
|
|
|
|
|
def test_temperature_unknown_unit():
|
|
|
|
"""Test no conversion happens if unknown unit."""
|
2022-09-28 08:58:04 +00:00
|
|
|
with pytest.raises(HomeAssistantError, match="is not a recognized .* unit"):
|
2021-06-30 12:17:58 +00:00
|
|
|
METRIC_SYSTEM.temperature(5, "abc")
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_temperature_to_metric():
|
|
|
|
"""Test temperature conversion to metric system."""
|
|
|
|
assert METRIC_SYSTEM.temperature(25, METRIC_SYSTEM.temperature_unit) == 25
|
2019-07-31 19:25:30 +00:00
|
|
|
assert (
|
|
|
|
round(METRIC_SYSTEM.temperature(80, IMPERIAL_SYSTEM.temperature_unit), 1)
|
|
|
|
== 26.7
|
|
|
|
)
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_temperature_to_imperial():
|
|
|
|
"""Test temperature conversion to imperial system."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert IMPERIAL_SYSTEM.temperature(77, IMPERIAL_SYSTEM.temperature_unit) == 77
|
|
|
|
assert IMPERIAL_SYSTEM.temperature(25, METRIC_SYSTEM.temperature_unit) == 77
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_length_unknown_unit():
|
|
|
|
"""Test length conversion with unknown from unit."""
|
2022-09-28 08:58:04 +00:00
|
|
|
with pytest.raises(HomeAssistantError, match="is not a recognized .* unit"):
|
2019-07-31 19:25:30 +00:00
|
|
|
METRIC_SYSTEM.length(5, "fr")
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_length_to_metric():
|
|
|
|
"""Test length conversion to metric system."""
|
|
|
|
assert METRIC_SYSTEM.length(100, METRIC_SYSTEM.length_unit) == 100
|
2022-09-07 14:18:00 +00:00
|
|
|
assert METRIC_SYSTEM.length(5, IMPERIAL_SYSTEM.length_unit) == pytest.approx(
|
|
|
|
8.04672
|
|
|
|
)
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_length_to_imperial():
|
|
|
|
"""Test length conversion to imperial system."""
|
|
|
|
assert IMPERIAL_SYSTEM.length(100, IMPERIAL_SYSTEM.length_unit) == 100
|
2022-09-07 14:18:00 +00:00
|
|
|
assert IMPERIAL_SYSTEM.length(5, METRIC_SYSTEM.length_unit) == pytest.approx(
|
|
|
|
3.106855
|
|
|
|
)
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
|
2021-11-18 15:08:42 +00:00
|
|
|
def test_wind_speed_unknown_unit():
|
|
|
|
"""Test wind_speed conversion with unknown from unit."""
|
2022-09-28 08:58:04 +00:00
|
|
|
with pytest.raises(HomeAssistantError, match="is not a recognized .* unit"):
|
2021-11-18 15:08:42 +00:00
|
|
|
METRIC_SYSTEM.length(5, "turtles")
|
|
|
|
|
|
|
|
|
|
|
|
def test_wind_speed_to_metric():
|
|
|
|
"""Test length conversion to metric system."""
|
|
|
|
assert METRIC_SYSTEM.wind_speed(100, METRIC_SYSTEM.wind_speed_unit) == 100
|
|
|
|
# 1 m/s is about 2.237 mph
|
|
|
|
assert METRIC_SYSTEM.wind_speed(
|
|
|
|
2237, IMPERIAL_SYSTEM.wind_speed_unit
|
|
|
|
) == pytest.approx(1000, abs=0.1)
|
|
|
|
|
|
|
|
|
|
|
|
def test_wind_speed_to_imperial():
|
|
|
|
"""Test wind_speed conversion to imperial system."""
|
|
|
|
assert IMPERIAL_SYSTEM.wind_speed(100, IMPERIAL_SYSTEM.wind_speed_unit) == 100
|
|
|
|
assert IMPERIAL_SYSTEM.wind_speed(
|
|
|
|
1000, METRIC_SYSTEM.wind_speed_unit
|
|
|
|
) == pytest.approx(2237, abs=0.1)
|
|
|
|
|
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
def test_pressure_same_unit():
|
|
|
|
"""Test no conversion happens if to unit is same as from unit."""
|
|
|
|
assert METRIC_SYSTEM.pressure(5, METRIC_SYSTEM.pressure_unit) == 5
|
|
|
|
|
|
|
|
|
|
|
|
def test_pressure_unknown_unit():
|
|
|
|
"""Test no conversion happens if unknown unit."""
|
2022-09-28 08:58:04 +00:00
|
|
|
with pytest.raises(HomeAssistantError, match="is not a recognized .* unit"):
|
2019-07-31 19:25:30 +00:00
|
|
|
METRIC_SYSTEM.pressure(5, "K")
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_pressure_to_metric():
|
|
|
|
"""Test pressure conversion to metric system."""
|
|
|
|
assert METRIC_SYSTEM.pressure(25, METRIC_SYSTEM.pressure_unit) == 25
|
2019-07-31 19:25:30 +00:00
|
|
|
assert METRIC_SYSTEM.pressure(14.7, IMPERIAL_SYSTEM.pressure_unit) == pytest.approx(
|
|
|
|
101352.932, abs=1e-1
|
|
|
|
)
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_pressure_to_imperial():
|
|
|
|
"""Test pressure conversion to imperial system."""
|
|
|
|
assert IMPERIAL_SYSTEM.pressure(77, IMPERIAL_SYSTEM.pressure_unit) == 77
|
|
|
|
assert IMPERIAL_SYSTEM.pressure(
|
2019-07-31 19:25:30 +00:00
|
|
|
101352.932, METRIC_SYSTEM.pressure_unit
|
|
|
|
) == pytest.approx(14.7, abs=1e-4)
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
|
2021-11-19 08:18:44 +00:00
|
|
|
def test_accumulated_precipitation_same_unit():
|
|
|
|
"""Test no conversion happens if to unit is same as from unit."""
|
|
|
|
assert (
|
|
|
|
METRIC_SYSTEM.accumulated_precipitation(
|
|
|
|
5, METRIC_SYSTEM.accumulated_precipitation_unit
|
|
|
|
)
|
|
|
|
== 5
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_accumulated_precipitation_unknown_unit():
|
|
|
|
"""Test no conversion happens if unknown unit."""
|
2022-09-28 08:58:04 +00:00
|
|
|
with pytest.raises(HomeAssistantError, match="is not a recognized .* unit"):
|
2021-11-19 08:18:44 +00:00
|
|
|
METRIC_SYSTEM.accumulated_precipitation(5, "K")
|
|
|
|
|
|
|
|
|
|
|
|
def test_accumulated_precipitation_to_metric():
|
|
|
|
"""Test accumulated_precipitation conversion to metric system."""
|
|
|
|
assert (
|
|
|
|
METRIC_SYSTEM.accumulated_precipitation(
|
|
|
|
25, METRIC_SYSTEM.accumulated_precipitation_unit
|
|
|
|
)
|
|
|
|
== 25
|
|
|
|
)
|
|
|
|
assert METRIC_SYSTEM.accumulated_precipitation(
|
|
|
|
10, IMPERIAL_SYSTEM.accumulated_precipitation_unit
|
|
|
|
) == pytest.approx(254, abs=1e-4)
|
|
|
|
|
|
|
|
|
|
|
|
def test_accumulated_precipitation_to_imperial():
|
|
|
|
"""Test accumulated_precipitation conversion to imperial system."""
|
|
|
|
assert (
|
|
|
|
IMPERIAL_SYSTEM.accumulated_precipitation(
|
|
|
|
10, IMPERIAL_SYSTEM.accumulated_precipitation_unit
|
|
|
|
)
|
|
|
|
== 10
|
|
|
|
)
|
|
|
|
assert IMPERIAL_SYSTEM.accumulated_precipitation(
|
|
|
|
254, METRIC_SYSTEM.accumulated_precipitation_unit
|
|
|
|
) == pytest.approx(10, abs=1e-4)
|
|
|
|
|
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
def test_properties():
|
|
|
|
"""Test the unit properties are returned as expected."""
|
2021-03-20 12:55:10 +00:00
|
|
|
assert METRIC_SYSTEM.length_unit == LENGTH_KILOMETERS
|
2021-11-18 15:08:42 +00:00
|
|
|
assert METRIC_SYSTEM.wind_speed_unit == SPEED_METERS_PER_SECOND
|
2021-03-20 12:55:10 +00:00
|
|
|
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
|
2021-11-19 08:18:44 +00:00
|
|
|
assert METRIC_SYSTEM.accumulated_precipitation_unit == LENGTH_MILLIMETERS
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
|
2022-10-14 11:44:18 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"unit_system, expected_flag",
|
|
|
|
[
|
|
|
|
(METRIC_SYSTEM, True),
|
|
|
|
(IMPERIAL_SYSTEM, False),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_is_metric(
|
|
|
|
caplog: pytest.LogCaptureFixture, unit_system: UnitSystem, expected_flag: bool
|
|
|
|
):
|
2019-04-30 16:20:38 +00:00
|
|
|
"""Test the is metric flag."""
|
2022-10-14 11:44:18 +00:00
|
|
|
assert unit_system.is_metric == expected_flag
|
|
|
|
assert (
|
|
|
|
"Detected code that accesses the `is_metric` property of the unit system."
|
|
|
|
in caplog.text
|
|
|
|
)
|
2022-10-14 10:06:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2022-10-19 11:31:08 +00:00
|
|
|
"unit_system, expected_name, expected_private_name",
|
2022-10-14 10:06:14 +00:00
|
|
|
[
|
2022-10-19 11:31:08 +00:00
|
|
|
(METRIC_SYSTEM, _CONF_UNIT_SYSTEM_METRIC, _CONF_UNIT_SYSTEM_METRIC),
|
|
|
|
(IMPERIAL_SYSTEM, _CONF_UNIT_SYSTEM_IMPERIAL, _CONF_UNIT_SYSTEM_US_CUSTOMARY),
|
|
|
|
(
|
|
|
|
US_CUSTOMARY_SYSTEM,
|
|
|
|
_CONF_UNIT_SYSTEM_IMPERIAL,
|
|
|
|
_CONF_UNIT_SYSTEM_US_CUSTOMARY,
|
|
|
|
),
|
2022-10-14 10:06:14 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_deprecated_name(
|
2022-10-19 11:31:08 +00:00
|
|
|
caplog: pytest.LogCaptureFixture,
|
|
|
|
unit_system: UnitSystem,
|
|
|
|
expected_name: str,
|
|
|
|
expected_private_name: str,
|
2022-10-14 10:06:14 +00:00
|
|
|
) -> None:
|
|
|
|
"""Test the name is deprecated."""
|
|
|
|
assert unit_system.name == expected_name
|
2022-10-19 11:31:08 +00:00
|
|
|
assert unit_system._name == expected_private_name
|
2022-10-14 10:06:14 +00:00
|
|
|
assert (
|
|
|
|
"Detected code that accesses the `name` property of the unit system."
|
|
|
|
in caplog.text
|
|
|
|
)
|
2022-10-14 14:50:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"key, expected_system",
|
|
|
|
[
|
|
|
|
(_CONF_UNIT_SYSTEM_METRIC, METRIC_SYSTEM),
|
2022-10-19 11:31:08 +00:00
|
|
|
(_CONF_UNIT_SYSTEM_US_CUSTOMARY, US_CUSTOMARY_SYSTEM),
|
2022-10-14 14:50:04 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_get_unit_system(key: str, expected_system: UnitSystem) -> None:
|
|
|
|
"""Test get_unit_system."""
|
|
|
|
assert get_unit_system(key) is expected_system
|
|
|
|
|
|
|
|
|
2022-10-19 11:31:08 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"key", [None, "", "invalid_custom", _CONF_UNIT_SYSTEM_IMPERIAL]
|
|
|
|
)
|
2022-10-14 14:50:04 +00:00
|
|
|
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)
|
2022-10-25 10:51:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"unit_system, device_class, original_unit, state_unit",
|
|
|
|
(
|
2022-10-25 12:53:59 +00:00
|
|
|
# Test distance conversion
|
2022-10-25 10:51:23 +00:00
|
|
|
(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),
|
2022-10-25 12:53:59 +00:00
|
|
|
# Test speed conversion
|
|
|
|
(METRIC_SYSTEM, "speed", SPEED_FEET_PER_SECOND, SPEED_KILOMETERS_PER_HOUR),
|
|
|
|
(METRIC_SYSTEM, "speed", SPEED_MILES_PER_HOUR, SPEED_KILOMETERS_PER_HOUR),
|
|
|
|
(METRIC_SYSTEM, "speed", SPEED_KILOMETERS_PER_HOUR, None),
|
|
|
|
(METRIC_SYSTEM, "speed", SPEED_KNOTS, None),
|
|
|
|
(METRIC_SYSTEM, "speed", SPEED_METERS_PER_SECOND, None),
|
|
|
|
(METRIC_SYSTEM, "speed", "very_fast", None),
|
|
|
|
# Test distance conversion
|
2022-10-25 10:51:23 +00:00
|
|
|
(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),
|
2022-10-25 12:53:59 +00:00
|
|
|
# Test speed conversion
|
|
|
|
(US_CUSTOMARY_SYSTEM, "speed", SPEED_METERS_PER_SECOND, SPEED_MILES_PER_HOUR),
|
|
|
|
(US_CUSTOMARY_SYSTEM, "speed", SPEED_KILOMETERS_PER_HOUR, SPEED_MILES_PER_HOUR),
|
|
|
|
(US_CUSTOMARY_SYSTEM, "speed", SPEED_FEET_PER_SECOND, None),
|
|
|
|
(US_CUSTOMARY_SYSTEM, "speed", SPEED_KNOTS, None),
|
|
|
|
(US_CUSTOMARY_SYSTEM, "speed", SPEED_MILES_PER_HOUR, None),
|
|
|
|
(US_CUSTOMARY_SYSTEM, "speed", "very_fast", None),
|
2022-10-25 10:51:23 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
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
|