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
pull/80946/head
Erik Montnemery 2022-10-25 14:53:59 +02:00 committed by GitHub
parent 93d4d02aac
commit b07e1281da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 0 deletions

View File

@ -83,6 +83,7 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
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 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
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,

View File

@ -27,6 +27,8 @@ from homeassistant.const import (
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 @@ METRIC_SYSTEM = UnitSystem(
("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 @@ US_CUSTOMARY_SYSTEM = UnitSystem(
("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,

View File

@ -16,7 +16,11 @@ from homeassistant.const import (
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(