From ead88cc3f8e27169129af86cb6d1400156e68da8 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Thu, 30 Mar 2023 12:54:12 +0200 Subject: [PATCH] Add preferred wind speed unit to unit systems (#90504) * Add preferred wind speed unit to unit systems * Tweak * Update tests --- homeassistant/util/unit_system.py | 12 ++++++++++ tests/util/test_unit_system.py | 38 +++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/homeassistant/util/unit_system.py b/homeassistant/util/unit_system.py index 2a7af577769..c9da324e8a5 100644 --- a/homeassistant/util/unit_system.py +++ b/homeassistant/util/unit_system.py @@ -277,6 +277,12 @@ METRIC_SYSTEM = UnitSystem( ("water", UnitOfVolume.CENTUM_CUBIC_FEET): UnitOfVolume.CUBIC_METERS, ("water", UnitOfVolume.CUBIC_FEET): UnitOfVolume.CUBIC_METERS, ("water", UnitOfVolume.GALLONS): UnitOfVolume.LITERS, + # Convert wind speeds except knots to km/h + **{ + ("wind_speed", unit): UnitOfSpeed.KILOMETERS_PER_HOUR + for unit in UnitOfSpeed + if unit not in (UnitOfSpeed.KILOMETERS_PER_HOUR, UnitOfSpeed.KNOTS) + }, }, length=UnitOfLength.KILOMETERS, mass=UnitOfMass.GRAMS, @@ -341,6 +347,12 @@ US_CUSTOMARY_SYSTEM = UnitSystem( # Convert non-USCS volumes of water meters ("water", UnitOfVolume.CUBIC_METERS): UnitOfVolume.CUBIC_FEET, ("water", UnitOfVolume.LITERS): UnitOfVolume.GALLONS, + # Convert wind speeds except knots to mph + **{ + ("wind_speed", unit): UnitOfSpeed.MILES_PER_HOUR + for unit in UnitOfSpeed + if unit not in (UnitOfSpeed.KNOTS, UnitOfSpeed.MILES_PER_HOUR) + }, }, length=UnitOfLength.MILES, mass=UnitOfMass.POUNDS, diff --git a/tests/util/test_unit_system.py b/tests/util/test_unit_system.py index 01aa1256fd6..44b287bd05d 100644 --- a/tests/util/test_unit_system.py +++ b/tests/util/test_unit_system.py @@ -457,6 +457,25 @@ def test_get_unit_system_invalid(key: str) -> None: (SensorDeviceClass.WATER, UnitOfVolume.CUBIC_METERS, None), (SensorDeviceClass.WATER, UnitOfVolume.LITERS, None), (SensorDeviceClass.WATER, "very_much", None), + # Test wind speed conversion + ( + SensorDeviceClass.WIND_SPEED, + UnitOfSpeed.FEET_PER_SECOND, + UnitOfSpeed.KILOMETERS_PER_HOUR, + ), + ( + SensorDeviceClass.WIND_SPEED, + UnitOfSpeed.MILES_PER_HOUR, + UnitOfSpeed.KILOMETERS_PER_HOUR, + ), + (SensorDeviceClass.WIND_SPEED, UnitOfSpeed.KILOMETERS_PER_HOUR, None), + (SensorDeviceClass.WIND_SPEED, UnitOfSpeed.KNOTS, None), + ( + SensorDeviceClass.WIND_SPEED, + UnitOfSpeed.METERS_PER_SECOND, + UnitOfSpeed.KILOMETERS_PER_HOUR, + ), + (SensorDeviceClass.WIND_SPEED, "very_fast", None), ), ) def test_get_metric_converted_unit_( @@ -657,6 +676,25 @@ def test_metric_converted_units(device_class: SensorDeviceClass) -> None: (SensorDeviceClass.WATER, UnitOfVolume.CUBIC_FEET, None), (SensorDeviceClass.WATER, UnitOfVolume.GALLONS, None), (SensorDeviceClass.WATER, "very_much", None), + # Test wind speed conversion + ( + SensorDeviceClass.WIND_SPEED, + UnitOfSpeed.METERS_PER_SECOND, + UnitOfSpeed.MILES_PER_HOUR, + ), + ( + SensorDeviceClass.WIND_SPEED, + UnitOfSpeed.KILOMETERS_PER_HOUR, + UnitOfSpeed.MILES_PER_HOUR, + ), + ( + SensorDeviceClass.WIND_SPEED, + UnitOfSpeed.FEET_PER_SECOND, + UnitOfSpeed.MILES_PER_HOUR, + ), + (SensorDeviceClass.WIND_SPEED, UnitOfSpeed.KNOTS, None), + (SensorDeviceClass.WIND_SPEED, UnitOfSpeed.MILES_PER_HOUR, None), + (SensorDeviceClass.WIND_SPEED, "very_fast", None), ), ) def test_get_us_converted_unit(