Add mm/s and in/s As Unit Of Speed (#125044)

Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
Trevor Schirmer 2024-09-22 10:01:46 -04:00 committed by GitHub
parent 7c5dc29981
commit 96b7fc9a75
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 38 additions and 2 deletions

View file

@ -350,8 +350,8 @@ class SensorDeviceClass(StrEnum):
"""Generic speed.
Unit of measurement: `SPEED_*` units or `UnitOfVolumetricFlux`
- SI /metric: `mm/d`, `mm/h`, `m/s`, `km/h`
- USCS / imperial: `in/d`, `in/h`, `ft/s`, `mph`
- SI /metric: `mm/d`, `mm/h`, `m/s`, `km/h`, `mm/s`
- USCS / imperial: `in/d`, `in/h`, `in/s`, `ft/s`, `mph`
- Nautical: `kn`
- Beaufort: `Beaufort`
"""

View file

@ -1273,10 +1273,12 @@ class UnitOfSpeed(StrEnum):
BEAUFORT = "Beaufort"
FEET_PER_SECOND = "ft/s"
INCHES_PER_SECOND = "in/s"
METERS_PER_SECOND = "m/s"
KILOMETERS_PER_HOUR = "km/h"
KNOTS = "kn"
MILES_PER_HOUR = "mph"
MILLIMETERS_PER_SECOND = "mm/s"
_DEPRECATED_SPEED_FEET_PER_SECOND: Final = DeprecatedConstantEnum(

View file

@ -337,9 +337,11 @@ class SpeedConverter(BaseUnitConverter):
UnitOfVolumetricFlux.MILLIMETERS_PER_DAY: _DAYS_TO_SECS / _MM_TO_M,
UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR: _HRS_TO_SECS / _MM_TO_M,
UnitOfSpeed.FEET_PER_SECOND: 1 / _FOOT_TO_M,
UnitOfSpeed.INCHES_PER_SECOND: 1 / _IN_TO_M,
UnitOfSpeed.KILOMETERS_PER_HOUR: _HRS_TO_SECS / _KM_TO_M,
UnitOfSpeed.KNOTS: _HRS_TO_SECS / _NAUTICAL_MILE_TO_M,
UnitOfSpeed.METERS_PER_SECOND: 1,
UnitOfSpeed.MILLIMETERS_PER_SECOND: 1 / _MM_TO_M,
UnitOfSpeed.MILES_PER_HOUR: _HRS_TO_SECS / _MILE_TO_M,
UnitOfSpeed.BEAUFORT: 1,
}
@ -348,11 +350,13 @@ class SpeedConverter(BaseUnitConverter):
UnitOfVolumetricFlux.INCHES_PER_HOUR,
UnitOfVolumetricFlux.MILLIMETERS_PER_DAY,
UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR,
UnitOfSpeed.INCHES_PER_SECOND,
UnitOfSpeed.FEET_PER_SECOND,
UnitOfSpeed.KILOMETERS_PER_HOUR,
UnitOfSpeed.KNOTS,
UnitOfSpeed.METERS_PER_SECOND,
UnitOfSpeed.MILES_PER_HOUR,
UnitOfSpeed.MILLIMETERS_PER_SECOND,
UnitOfSpeed.BEAUFORT,
}

View file

@ -258,6 +258,7 @@ METRIC_SYSTEM = UnitSystem(
("pressure", UnitOfPressure.INHG): UnitOfPressure.HPA,
# Convert non-metric speeds except knots to km/h
("speed", UnitOfSpeed.FEET_PER_SECOND): UnitOfSpeed.KILOMETERS_PER_HOUR,
("speed", UnitOfSpeed.INCHES_PER_SECOND): UnitOfSpeed.MILLIMETERS_PER_SECOND,
("speed", UnitOfSpeed.MILES_PER_HOUR): UnitOfSpeed.KILOMETERS_PER_HOUR,
(
"speed",
@ -330,6 +331,7 @@ US_CUSTOMARY_SYSTEM = UnitSystem(
("pressure", UnitOfPressure.MMHG): UnitOfPressure.INHG,
# Convert non-USCS speeds, except knots, to mph
("speed", UnitOfSpeed.METERS_PER_SECOND): UnitOfSpeed.MILES_PER_HOUR,
("speed", UnitOfSpeed.MILLIMETERS_PER_SECOND): UnitOfSpeed.INCHES_PER_SECOND,
("speed", UnitOfSpeed.KILOMETERS_PER_HOUR): UnitOfSpeed.MILES_PER_HOUR,
(
"speed",

View file

@ -36,11 +36,13 @@ async def test_device_class_units(
"ft/s",
"in/d",
"in/h",
"in/s",
"km/h",
"kn",
"m/s",
"mm/d",
"mm/h",
"mm/s",
"mph",
]
}

View file

@ -431,6 +431,20 @@ _CONVERTED_VALUE: dict[
708661.42,
UnitOfVolumetricFlux.INCHES_PER_HOUR,
),
# 5 m/s * 1000 = 5000 mm/s
(
5,
UnitOfSpeed.METERS_PER_SECOND,
5000,
UnitOfSpeed.MILLIMETERS_PER_SECOND,
),
# 5 m/s ÷ 0.0254 = 196.8503937 in/s
(
5,
UnitOfSpeed.METERS_PER_SECOND,
5 / 0.0254,
UnitOfSpeed.INCHES_PER_SECOND,
),
# 5000 in/h / 39.3701 in/m / 3600 s/h = 0.03528 m/s
(
5000,

View file

@ -413,6 +413,11 @@ def test_get_unit_system_invalid(key: str) -> None:
UnitOfSpeed.FEET_PER_SECOND,
UnitOfSpeed.KILOMETERS_PER_HOUR,
),
(
SensorDeviceClass.SPEED,
UnitOfSpeed.INCHES_PER_SECOND,
UnitOfSpeed.MILLIMETERS_PER_SECOND,
),
(
SensorDeviceClass.SPEED,
UnitOfSpeed.MILES_PER_HOUR,
@ -520,6 +525,7 @@ UNCONVERTED_UNITS_METRIC_SYSTEM = {
UnitOfSpeed.KILOMETERS_PER_HOUR,
UnitOfSpeed.KNOTS,
UnitOfSpeed.METERS_PER_SECOND,
UnitOfSpeed.MILLIMETERS_PER_SECOND,
UnitOfVolumetricFlux.MILLIMETERS_PER_DAY,
UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR,
),
@ -661,6 +667,11 @@ def test_metric_converted_units(device_class: SensorDeviceClass) -> None:
),
(SensorDeviceClass.SPEED, UnitOfVolumetricFlux.INCHES_PER_DAY, None),
(SensorDeviceClass.SPEED, UnitOfVolumetricFlux.INCHES_PER_HOUR, None),
(
SensorDeviceClass.SPEED,
UnitOfSpeed.MILLIMETERS_PER_SECOND,
UnitOfSpeed.INCHES_PER_SECOND,
),
(SensorDeviceClass.SPEED, "very_fast", None),
# Test volume conversion
(SensorDeviceClass.VOLUME, UnitOfVolume.CUBIC_METERS, UnitOfVolume.CUBIC_FEET),
@ -729,6 +740,7 @@ UNCONVERTED_UNITS_US_SYSTEM = {
UnitOfSpeed.FEET_PER_SECOND,
UnitOfSpeed.KNOTS,
UnitOfSpeed.MILES_PER_HOUR,
UnitOfSpeed.INCHES_PER_SECOND,
UnitOfVolumetricFlux.INCHES_PER_DAY,
UnitOfVolumetricFlux.INCHES_PER_HOUR,
),