Add speed to units system (#58437)
* Use speed units in unit system * Use more obvious conversion factor for unit system speed test * Use wind_speed instead of speed, use m/s
This commit is contained in:
parent
3dc0b9537c
commit
7cc7bbb76d
4 changed files with 76 additions and 1 deletions
|
@ -9,10 +9,12 @@ from homeassistant.const import (
|
|||
MASS_GRAMS,
|
||||
PRESSURE,
|
||||
PRESSURE_PA,
|
||||
SPEED_METERS_PER_SECOND,
|
||||
TEMP_CELSIUS,
|
||||
TEMPERATURE,
|
||||
VOLUME,
|
||||
VOLUME_LITERS,
|
||||
WIND_SPEED,
|
||||
)
|
||||
from homeassistant.util.unit_system import IMPERIAL_SYSTEM, METRIC_SYSTEM, UnitSystem
|
||||
|
||||
|
@ -27,6 +29,7 @@ def test_invalid_units():
|
|||
SYSTEM_NAME,
|
||||
INVALID_UNIT,
|
||||
LENGTH_METERS,
|
||||
SPEED_METERS_PER_SECOND,
|
||||
VOLUME_LITERS,
|
||||
MASS_GRAMS,
|
||||
PRESSURE_PA,
|
||||
|
@ -37,6 +40,7 @@ def test_invalid_units():
|
|||
SYSTEM_NAME,
|
||||
TEMP_CELSIUS,
|
||||
INVALID_UNIT,
|
||||
SPEED_METERS_PER_SECOND,
|
||||
VOLUME_LITERS,
|
||||
MASS_GRAMS,
|
||||
PRESSURE_PA,
|
||||
|
@ -48,6 +52,7 @@ def test_invalid_units():
|
|||
TEMP_CELSIUS,
|
||||
LENGTH_METERS,
|
||||
INVALID_UNIT,
|
||||
VOLUME_LITERS,
|
||||
MASS_GRAMS,
|
||||
PRESSURE_PA,
|
||||
)
|
||||
|
@ -57,6 +62,18 @@ def test_invalid_units():
|
|||
SYSTEM_NAME,
|
||||
TEMP_CELSIUS,
|
||||
LENGTH_METERS,
|
||||
SPEED_METERS_PER_SECOND,
|
||||
INVALID_UNIT,
|
||||
MASS_GRAMS,
|
||||
PRESSURE_PA,
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
UnitSystem(
|
||||
SYSTEM_NAME,
|
||||
TEMP_CELSIUS,
|
||||
LENGTH_METERS,
|
||||
SPEED_METERS_PER_SECOND,
|
||||
VOLUME_LITERS,
|
||||
INVALID_UNIT,
|
||||
PRESSURE_PA,
|
||||
|
@ -67,6 +84,7 @@ def test_invalid_units():
|
|||
SYSTEM_NAME,
|
||||
TEMP_CELSIUS,
|
||||
LENGTH_METERS,
|
||||
SPEED_METERS_PER_SECOND,
|
||||
VOLUME_LITERS,
|
||||
MASS_GRAMS,
|
||||
INVALID_UNIT,
|
||||
|
@ -79,6 +97,8 @@ def test_invalid_value():
|
|||
METRIC_SYSTEM.length("25a", LENGTH_KILOMETERS)
|
||||
with pytest.raises(TypeError):
|
||||
METRIC_SYSTEM.temperature("50K", TEMP_CELSIUS)
|
||||
with pytest.raises(TypeError):
|
||||
METRIC_SYSTEM.wind_speed("50km/h", SPEED_METERS_PER_SECOND)
|
||||
with pytest.raises(TypeError):
|
||||
METRIC_SYSTEM.volume("50L", VOLUME_LITERS)
|
||||
with pytest.raises(TypeError):
|
||||
|
@ -89,6 +109,7 @@ 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,
|
||||
|
@ -142,6 +163,29 @@ def test_length_to_imperial():
|
|||
assert IMPERIAL_SYSTEM.length(5, METRIC_SYSTEM.length_unit) == 3.106855
|
||||
|
||||
|
||||
def test_wind_speed_unknown_unit():
|
||||
"""Test wind_speed conversion with unknown from unit."""
|
||||
with pytest.raises(ValueError):
|
||||
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)
|
||||
|
||||
|
||||
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
|
||||
|
@ -172,6 +216,7 @@ def test_pressure_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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue