Add speed conversion function & add speed to units system (#53846)

* Add speed conversion function

* Add test for speed utility functions

* Update unit system tests

* Fix incorrect unit conversions in tests

* Fix some test errors

* Calculate speed units from smaller set of constants

* Fix typo in speed test

* Use pytest.approx for checking floating point values

* Change other instance of speeds needing to be pytest.approx

* Revert changes to unit system

* Fix oopsie in defining in/day and in/hr

* Parametrize test

* Add comments describing calculations & remove duplicate test
This commit is contained in:
rianadon 2021-11-08 23:12:28 -08:00 committed by GitHub
parent f46ba2b38b
commit a102c425a9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 179 additions and 19 deletions

View file

@ -4,12 +4,12 @@ from homeassistant.const import (
ATTR_ATTRIBUTION,
CONF_LATITUDE,
CONF_LONGITUDE,
LENGTH_KILOMETERS,
LENGTH_METERS,
LENGTH_MILES,
PERCENTAGE,
PRESSURE_INHG,
PRESSURE_PA,
SPEED_KILOMETERS_PER_HOUR,
SPEED_MILES_PER_HOUR,
TEMP_CELSIUS,
)
@ -19,6 +19,7 @@ from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.util.distance import convert as convert_distance
from homeassistant.util.dt import utcnow
from homeassistant.util.pressure import convert as convert_pressure
from homeassistant.util.speed import convert as convert_speed
from . import base_unique_id, device_info
from .const import (
@ -86,7 +87,9 @@ class NWSSensor(CoordinatorEntity, SensorEntity):
# Set alias to unit property -> prevent unnecessary hasattr calls
unit_of_measurement = self.native_unit_of_measurement
if unit_of_measurement == SPEED_MILES_PER_HOUR:
return round(convert_distance(value, LENGTH_KILOMETERS, LENGTH_MILES))
return round(
convert_speed(value, SPEED_KILOMETERS_PER_HOUR, SPEED_MILES_PER_HOUR)
)
if unit_of_measurement == LENGTH_MILES:
return round(convert_distance(value, LENGTH_METERS, LENGTH_MILES))
if unit_of_measurement == PRESSURE_INHG: