From 583b4aef070eb6a1735cd5045ac1638a86a8f10a Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 12 Dec 2022 12:42:35 +0100 Subject: [PATCH] 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 --- homeassistant/util/unit_system.py | 11 +++++++++++ tests/components/mazda/test_sensor.py | 18 +++++++++--------- tests/components/tomorrowio/test_sensor.py | 2 +- tests/util/test_unit_system.py | 8 ++++++++ 4 files changed, 29 insertions(+), 10 deletions(-) diff --git a/homeassistant/util/unit_system.py b/homeassistant/util/unit_system.py index 24bcee2ed93..a76be7e761e 100644 --- a/homeassistant/util/unit_system.py +++ b/homeassistant/util/unit_system.py @@ -265,6 +265,9 @@ METRIC_SYSTEM = UnitSystem( ("gas", UnitOfVolume.CUBIC_FEET): UnitOfVolume.CUBIC_METERS, # Convert non-metric precipitation ("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 ("speed", UnitOfSpeed.FEET_PER_SECOND): 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, # Convert non-USCS precipitation ("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 ("speed", UnitOfSpeed.METERS_PER_SECOND): UnitOfSpeed.MILES_PER_HOUR, ("speed", UnitOfSpeed.KILOMETERS_PER_HOUR): UnitOfSpeed.MILES_PER_HOUR, diff --git a/tests/components/mazda/test_sensor.py b/tests/components/mazda/test_sensor.py index 4484e6b7783..24ade7e0485 100644 --- a/tests/components/mazda/test_sensor.py +++ b/tests/components/mazda/test_sensor.py @@ -13,7 +13,7 @@ from homeassistant.const import ( LENGTH_KILOMETERS, LENGTH_MILES, PERCENTAGE, - PRESSURE_PSI, + UnitOfPressure, ) from homeassistant.helpers import entity_registry as er 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_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.state == "35" + assert state.state == "241" entry = entity_registry.async_get("sensor.my_mazda3_front_left_tire_pressure") assert entry 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_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.state == "35" + assert state.state == "241" entry = entity_registry.async_get("sensor.my_mazda3_front_right_tire_pressure") assert entry 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_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.state == "33" + assert state.state == "228" entry = entity_registry.async_get("sensor.my_mazda3_rear_left_tire_pressure") assert entry 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_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.state == "33" + assert state.state == "228" entry = entity_registry.async_get("sensor.my_mazda3_rear_right_tire_pressure") assert entry assert entry.unique_id == "JM000000000000000_rear_right_tire_pressure" diff --git a/tests/components/tomorrowio/test_sensor.py b/tests/components/tomorrowio/test_sensor.py index 7721e5d36ac..a93a551ae03 100644 --- a/tests/components/tomorrowio/test_sensor.py +++ b/tests/components/tomorrowio/test_sensor.py @@ -192,7 +192,7 @@ async def test_v4_sensor_imperial(hass: HomeAssistant) -> None: check_sensor_state(hass, TREE_POLLEN, "none") check_sensor_state(hass, FEELS_LIKE, "214.3") 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, CLOUD_BASE, "0.46") check_sensor_state(hass, CLOUD_COVER, "100") diff --git a/tests/util/test_unit_system.py b/tests/util/test_unit_system.py index 44e045a5c40..4813bc34a3a 100644 --- a/tests/util/test_unit_system.py +++ b/tests/util/test_unit_system.py @@ -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_METERS, 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 ( SensorDeviceClass.SPEED, @@ -482,6 +486,10 @@ def test_get_metric_converted_unit_( (SensorDeviceClass.GAS, UnitOfVolume.CUBIC_METERS, UnitOfVolume.CUBIC_FEET), (SensorDeviceClass.GAS, UnitOfVolume.CUBIC_FEET, 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 ( SensorDeviceClass.SPEED,