Skip to content

Commit

Permalink
Add rules for converting speeds (home-assistant#80943)
Browse files Browse the repository at this point in the history
* Add rules for converting speeds

* Update metoffice wind speed sensors to prefer mph

* Don't convert speeds measured in knots
  • Loading branch information
emontnemery authored Oct 25, 2022
1 parent 93d4d02 commit b07e128
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
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

0 comments on commit b07e128

Please sign in to comment.