Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rules for converting speeds #80943

Merged
merged 3 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions homeassistant/components/metoffice/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
key="wind_speed",
name="Wind speed",
native_unit_of_measurement=SPEED_MILES_PER_HOUR,
suggested_unit_of_measurement=SPEED_MILES_PER_HOUR,
device_class=SensorDeviceClass.SPEED,
icon="mdi:weather-windy",
entity_registry_enabled_default=True,
Expand All @@ -98,6 +99,7 @@
key="wind_gust",
name="Wind gust",
native_unit_of_measurement=SPEED_MILES_PER_HOUR,
suggested_unit_of_measurement=SPEED_MILES_PER_HOUR,
device_class=SensorDeviceClass.SPEED,
icon="mdi:weather-windy",
entity_registry_enabled_default=False,
Expand Down
8 changes: 8 additions & 0 deletions homeassistant/util/unit_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
PRESSURE,
PRESSURE_PA,
PRESSURE_PSI,
SPEED_FEET_PER_SECOND,
SPEED_KILOMETERS_PER_HOUR,
SPEED_METERS_PER_SECOND,
SPEED_MILES_PER_HOUR,
TEMP_CELSIUS,
Expand Down Expand Up @@ -268,6 +270,9 @@ def _deprecated_unit_system(value: str) -> str:
("distance", LENGTH_INCHES): LENGTH_MILLIMETERS,
("distance", LENGTH_MILES): LENGTH_KILOMETERS,
("distance", LENGTH_YARD): LENGTH_METERS,
# Convert non-metric speeds except knots to km/h
("speed", SPEED_FEET_PER_SECOND): SPEED_KILOMETERS_PER_HOUR,
("speed", SPEED_MILES_PER_HOUR): SPEED_KILOMETERS_PER_HOUR,
},
length=LENGTH_KILOMETERS,
mass=MASS_GRAMS,
Expand All @@ -286,6 +291,9 @@ def _deprecated_unit_system(value: str) -> str:
("distance", LENGTH_KILOMETERS): LENGTH_MILES,
("distance", LENGTH_METERS): LENGTH_FEET,
("distance", LENGTH_MILLIMETERS): LENGTH_INCHES,
# Convert non-USCS speeds except knots to mph
("speed", SPEED_METERS_PER_SECOND): SPEED_MILES_PER_HOUR,
("speed", SPEED_KILOMETERS_PER_HOUR): SPEED_MILES_PER_HOUR,
},
length=LENGTH_MILES,
mass=MASS_POUNDS,
Expand Down
20 changes: 20 additions & 0 deletions tests/util/test_unit_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
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,
Expand Down Expand Up @@ -384,18 +388,34 @@ def test_get_unit_system_invalid(key: str) -> None:
@pytest.mark.parametrize(
"unit_system, device_class, original_unit, state_unit",
(
# Test distance conversion
(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),
# 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
(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),
# 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),
),
)
def test_get_converted_unit(
Expand Down