Enable automatic conversion for pressures (#83525)

* Enable automatic conversion between bar and psi

* Fix tests

* Fix mazda tests

* Fix oncue tests

* Adjust US pressures

* Adjust metric pressures

* Adjust tests

* Adjust tests
This commit is contained in:
epenet 2022-12-12 12:42:35 +01:00 committed by GitHub
parent 52d4a358a0
commit 583b4aef07
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 10 deletions

View file

@ -265,6 +265,9 @@ METRIC_SYSTEM = UnitSystem(
("gas", UnitOfVolume.CUBIC_FEET): UnitOfVolume.CUBIC_METERS, ("gas", UnitOfVolume.CUBIC_FEET): UnitOfVolume.CUBIC_METERS,
# Convert non-metric precipitation # Convert non-metric precipitation
("precipitation", UnitOfLength.INCHES): UnitOfLength.MILLIMETERS, ("precipitation", UnitOfLength.INCHES): UnitOfLength.MILLIMETERS,
# Convert non-metric pressure
("pressure", UnitOfPressure.PSI): UnitOfPressure.KPA,
("pressure", UnitOfPressure.INHG): UnitOfPressure.HPA,
# Convert non-metric speeds except knots to km/h # Convert non-metric speeds except knots to km/h
("speed", UnitOfSpeed.FEET_PER_SECOND): UnitOfSpeed.KILOMETERS_PER_HOUR, ("speed", UnitOfSpeed.FEET_PER_SECOND): UnitOfSpeed.KILOMETERS_PER_HOUR,
("speed", UnitOfSpeed.MILES_PER_HOUR): UnitOfSpeed.KILOMETERS_PER_HOUR, ("speed", UnitOfSpeed.MILES_PER_HOUR): UnitOfSpeed.KILOMETERS_PER_HOUR,
@ -303,6 +306,14 @@ US_CUSTOMARY_SYSTEM = UnitSystem(
("gas", UnitOfVolume.CUBIC_METERS): UnitOfVolume.CUBIC_FEET, ("gas", UnitOfVolume.CUBIC_METERS): UnitOfVolume.CUBIC_FEET,
# Convert non-USCS precipitation # Convert non-USCS precipitation
("precipitation", UnitOfLength.MILLIMETERS): UnitOfLength.INCHES, ("precipitation", UnitOfLength.MILLIMETERS): UnitOfLength.INCHES,
# Convert non-USCS pressure
("pressure", UnitOfPressure.MBAR): UnitOfPressure.PSI,
("pressure", UnitOfPressure.CBAR): UnitOfPressure.PSI,
("pressure", UnitOfPressure.BAR): UnitOfPressure.PSI,
("pressure", UnitOfPressure.PA): UnitOfPressure.PSI,
("pressure", UnitOfPressure.HPA): UnitOfPressure.PSI,
("pressure", UnitOfPressure.KPA): UnitOfPressure.PSI,
("pressure", UnitOfPressure.MMHG): UnitOfPressure.INHG,
# Convert non-USCS speeds except knots to mph # Convert non-USCS speeds except knots to mph
("speed", UnitOfSpeed.METERS_PER_SECOND): UnitOfSpeed.MILES_PER_HOUR, ("speed", UnitOfSpeed.METERS_PER_SECOND): UnitOfSpeed.MILES_PER_HOUR,
("speed", UnitOfSpeed.KILOMETERS_PER_HOUR): UnitOfSpeed.MILES_PER_HOUR, ("speed", UnitOfSpeed.KILOMETERS_PER_HOUR): UnitOfSpeed.MILES_PER_HOUR,

View file

@ -13,7 +13,7 @@ from homeassistant.const import (
LENGTH_KILOMETERS, LENGTH_KILOMETERS,
LENGTH_MILES, LENGTH_MILES,
PERCENTAGE, PERCENTAGE,
PRESSURE_PSI, UnitOfPressure,
) )
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM
@ -76,9 +76,9 @@ async def test_sensors(hass):
) )
assert state.attributes.get(ATTR_ICON) == "mdi:car-tire-alert" assert state.attributes.get(ATTR_ICON) == "mdi:car-tire-alert"
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.PRESSURE assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.PRESSURE
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PRESSURE_PSI assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfPressure.KPA
assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT
assert state.state == "35" assert state.state == "241"
entry = entity_registry.async_get("sensor.my_mazda3_front_left_tire_pressure") entry = entity_registry.async_get("sensor.my_mazda3_front_left_tire_pressure")
assert entry assert entry
assert entry.unique_id == "JM000000000000000_front_left_tire_pressure" assert entry.unique_id == "JM000000000000000_front_left_tire_pressure"
@ -92,9 +92,9 @@ async def test_sensors(hass):
) )
assert state.attributes.get(ATTR_ICON) == "mdi:car-tire-alert" assert state.attributes.get(ATTR_ICON) == "mdi:car-tire-alert"
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.PRESSURE assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.PRESSURE
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PRESSURE_PSI assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfPressure.KPA
assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT
assert state.state == "35" assert state.state == "241"
entry = entity_registry.async_get("sensor.my_mazda3_front_right_tire_pressure") entry = entity_registry.async_get("sensor.my_mazda3_front_right_tire_pressure")
assert entry assert entry
assert entry.unique_id == "JM000000000000000_front_right_tire_pressure" assert entry.unique_id == "JM000000000000000_front_right_tire_pressure"
@ -107,9 +107,9 @@ async def test_sensors(hass):
) )
assert state.attributes.get(ATTR_ICON) == "mdi:car-tire-alert" assert state.attributes.get(ATTR_ICON) == "mdi:car-tire-alert"
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.PRESSURE assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.PRESSURE
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PRESSURE_PSI assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfPressure.KPA
assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT
assert state.state == "33" assert state.state == "228"
entry = entity_registry.async_get("sensor.my_mazda3_rear_left_tire_pressure") entry = entity_registry.async_get("sensor.my_mazda3_rear_left_tire_pressure")
assert entry assert entry
assert entry.unique_id == "JM000000000000000_rear_left_tire_pressure" assert entry.unique_id == "JM000000000000000_rear_left_tire_pressure"
@ -122,9 +122,9 @@ async def test_sensors(hass):
) )
assert state.attributes.get(ATTR_ICON) == "mdi:car-tire-alert" assert state.attributes.get(ATTR_ICON) == "mdi:car-tire-alert"
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.PRESSURE assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.PRESSURE
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PRESSURE_PSI assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfPressure.KPA
assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT
assert state.state == "33" assert state.state == "228"
entry = entity_registry.async_get("sensor.my_mazda3_rear_right_tire_pressure") entry = entity_registry.async_get("sensor.my_mazda3_rear_right_tire_pressure")
assert entry assert entry
assert entry.unique_id == "JM000000000000000_rear_right_tire_pressure" assert entry.unique_id == "JM000000000000000_rear_right_tire_pressure"

View file

@ -192,7 +192,7 @@ async def test_v4_sensor_imperial(hass: HomeAssistant) -> None:
check_sensor_state(hass, TREE_POLLEN, "none") check_sensor_state(hass, TREE_POLLEN, "none")
check_sensor_state(hass, FEELS_LIKE, "214.3") check_sensor_state(hass, FEELS_LIKE, "214.3")
check_sensor_state(hass, DEW_POINT, "163.08") check_sensor_state(hass, DEW_POINT, "163.08")
check_sensor_state(hass, PRESSURE_SURFACE_LEVEL, "29.47") check_sensor_state(hass, PRESSURE_SURFACE_LEVEL, "0.427")
check_sensor_state(hass, GHI, "0.0") check_sensor_state(hass, GHI, "0.0")
check_sensor_state(hass, CLOUD_BASE, "0.46") check_sensor_state(hass, CLOUD_BASE, "0.46")
check_sensor_state(hass, CLOUD_COVER, "100") check_sensor_state(hass, CLOUD_COVER, "100")

View file

@ -409,6 +409,10 @@ def test_get_unit_system_invalid(key: str) -> None:
(SensorDeviceClass.GAS, UnitOfVolume.CUBIC_FEET, UnitOfVolume.CUBIC_METERS), (SensorDeviceClass.GAS, UnitOfVolume.CUBIC_FEET, UnitOfVolume.CUBIC_METERS),
(SensorDeviceClass.GAS, UnitOfVolume.CUBIC_METERS, None), (SensorDeviceClass.GAS, UnitOfVolume.CUBIC_METERS, None),
(SensorDeviceClass.GAS, "very_much", None), (SensorDeviceClass.GAS, "very_much", None),
# Test pressure conversion
(SensorDeviceClass.PRESSURE, UnitOfPressure.PSI, UnitOfPressure.KPA),
(SensorDeviceClass.PRESSURE, UnitOfPressure.BAR, None),
(SensorDeviceClass.PRESSURE, "very_much", None),
# Test speed conversion # Test speed conversion
( (
SensorDeviceClass.SPEED, SensorDeviceClass.SPEED,
@ -482,6 +486,10 @@ def test_get_metric_converted_unit_(
(SensorDeviceClass.GAS, UnitOfVolume.CUBIC_METERS, UnitOfVolume.CUBIC_FEET), (SensorDeviceClass.GAS, UnitOfVolume.CUBIC_METERS, UnitOfVolume.CUBIC_FEET),
(SensorDeviceClass.GAS, UnitOfVolume.CUBIC_FEET, None), (SensorDeviceClass.GAS, UnitOfVolume.CUBIC_FEET, None),
(SensorDeviceClass.GAS, "very_much", None), (SensorDeviceClass.GAS, "very_much", None),
# Test pressure conversion
(SensorDeviceClass.PRESSURE, UnitOfPressure.BAR, UnitOfPressure.PSI),
(SensorDeviceClass.PRESSURE, UnitOfPressure.PSI, None),
(SensorDeviceClass.PRESSURE, "very_much", None),
# Test speed conversion # Test speed conversion
( (
SensorDeviceClass.SPEED, SensorDeviceClass.SPEED,