From b07e1281da06ac006cd84713fe719238bcc5145f Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Tue, 25 Oct 2022 14:53:59 +0200 Subject: [PATCH] Add rules for converting speeds (#80943) * Add rules for converting speeds * Update metoffice wind speed sensors to prefer mph * Don't convert speeds measured in knots --- homeassistant/components/metoffice/sensor.py | 2 ++ homeassistant/util/unit_system.py | 8 ++++++++ tests/util/test_unit_system.py | 20 ++++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/homeassistant/components/metoffice/sensor.py b/homeassistant/components/metoffice/sensor.py index 82a6712af9097d..ef9643be96ae05 100644 --- a/homeassistant/components/metoffice/sensor.py +++ b/homeassistant/components/metoffice/sensor.py @@ -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, @@ -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, diff --git a/homeassistant/util/unit_system.py b/homeassistant/util/unit_system.py index 72779fe8c8405e..9e1c20022b221e 100644 --- a/homeassistant/util/unit_system.py +++ b/homeassistant/util/unit_system.py @@ -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, @@ -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, @@ -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, diff --git a/tests/util/test_unit_system.py b/tests/util/test_unit_system.py index dd584a8dd3b8a6..82d13ae6baa08f 100644 --- a/tests/util/test_unit_system.py +++ b/tests/util/test_unit_system.py @@ -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, @@ -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(