diff --git a/homeassistant/util/unit_conversion.py b/homeassistant/util/unit_conversion.py index 6d502ee6e6d8..9aa3084887ec 100644 --- a/homeassistant/util/unit_conversion.py +++ b/homeassistant/util/unit_conversion.py @@ -71,6 +71,10 @@ _DAYS_TO_SECS = 24 * _HRS_TO_SECS # 1 day = 24 hours = 86400 seconds _POUND_TO_G = 453.59237 _OUNCE_TO_G = _POUND_TO_G / 16 +# Pressure conversion constants +_STANDARD_GRAVITY = 9.80665 +_MERCURY_DENSITY = 13.5951 + # Volume conversion constants _L_TO_CUBIC_METER = 0.001 # 1 L = 0.001 m³ _ML_TO_CUBIC_METER = 0.001 * _L_TO_CUBIC_METER # 1 mL = 0.001 L @@ -211,9 +215,9 @@ class PressureConverter(BaseUnitConverter): PRESSURE_BAR: 1 / 100000, PRESSURE_CBAR: 1 / 1000, PRESSURE_MBAR: 1 / 100, - PRESSURE_INHG: 1 / 3386.389, + PRESSURE_INHG: 1 / (_IN_TO_M * 1000 * _STANDARD_GRAVITY * _MERCURY_DENSITY), PRESSURE_PSI: 1 / 6894.757, - PRESSURE_MMHG: 1 / 133.322, + PRESSURE_MMHG: 1 / (_MM_TO_M * 1000 * _STANDARD_GRAVITY * _MERCURY_DENSITY), } VALID_UNITS = { PRESSURE_PA, diff --git a/tests/util/test_pressure.py b/tests/util/test_pressure.py index 769c5aaf8016..f87b89df3f76 100644 --- a/tests/util/test_pressure.py +++ b/tests/util/test_pressure.py @@ -118,7 +118,7 @@ def test_convert_from_inhg(): 101.59167 ) assert pressure_util.convert(inhg, PRESSURE_INHG, PRESSURE_MMHG) == pytest.approx( - 762.002 + 762 ) @@ -126,23 +126,23 @@ def test_convert_from_mmhg(): """Test conversion from mmHg to other units.""" inhg = 30 assert pressure_util.convert(inhg, PRESSURE_MMHG, PRESSURE_PSI) == pytest.approx( - 0.580102 + 0.580103 ) assert pressure_util.convert(inhg, PRESSURE_MMHG, PRESSURE_KPA) == pytest.approx( - 3.99966 + 3.99967 ) assert pressure_util.convert(inhg, PRESSURE_MMHG, PRESSURE_HPA) == pytest.approx( - 39.9966 + 39.9967 ) assert pressure_util.convert(inhg, PRESSURE_MMHG, PRESSURE_PA) == pytest.approx( - 3999.66 + 3999.67 ) assert pressure_util.convert(inhg, PRESSURE_MMHG, PRESSURE_MBAR) == pytest.approx( - 39.9966 + 39.9967 ) assert pressure_util.convert(inhg, PRESSURE_MMHG, PRESSURE_CBAR) == pytest.approx( - 3.99966 + 3.99967 ) assert pressure_util.convert(inhg, PRESSURE_MMHG, PRESSURE_INHG) == pytest.approx( - 1.181099 + 1.181102 ) diff --git a/tests/util/test_unit_conversion.py b/tests/util/test_unit_conversion.py index ec839a6575c9..d74bacc66f8c 100644 --- a/tests/util/test_unit_conversion.py +++ b/tests/util/test_unit_conversion.py @@ -361,14 +361,14 @@ def test_power_convert( (30, PRESSURE_INHG, pytest.approx(101591.67), PRESSURE_PA), (30, PRESSURE_INHG, pytest.approx(1015.9167), PRESSURE_MBAR), (30, PRESSURE_INHG, pytest.approx(101.59167), PRESSURE_CBAR), - (30, PRESSURE_INHG, pytest.approx(762.002), PRESSURE_MMHG), - (30, PRESSURE_MMHG, pytest.approx(0.580102), PRESSURE_PSI), - (30, PRESSURE_MMHG, pytest.approx(3.99966), PRESSURE_KPA), - (30, PRESSURE_MMHG, pytest.approx(39.9966), PRESSURE_HPA), - (30, PRESSURE_MMHG, pytest.approx(3999.66), PRESSURE_PA), - (30, PRESSURE_MMHG, pytest.approx(39.9966), PRESSURE_MBAR), - (30, PRESSURE_MMHG, pytest.approx(3.99966), PRESSURE_CBAR), - (30, PRESSURE_MMHG, pytest.approx(1.181099), PRESSURE_INHG), + (30, PRESSURE_INHG, pytest.approx(762), PRESSURE_MMHG), + (30, PRESSURE_MMHG, pytest.approx(0.580103), PRESSURE_PSI), + (30, PRESSURE_MMHG, pytest.approx(3.99967), PRESSURE_KPA), + (30, PRESSURE_MMHG, pytest.approx(39.9967), PRESSURE_HPA), + (30, PRESSURE_MMHG, pytest.approx(3999.67), PRESSURE_PA), + (30, PRESSURE_MMHG, pytest.approx(39.9967), PRESSURE_MBAR), + (30, PRESSURE_MMHG, pytest.approx(3.99967), PRESSURE_CBAR), + (30, PRESSURE_MMHG, pytest.approx(1.181102), PRESSURE_INHG), ], ) def test_pressure_convert(