From c0987e3bf41c3eb28197f34e84ac96a59e48fa1d Mon Sep 17 00:00:00 2001 From: Aaron Bach Date: Fri, 14 Oct 2022 15:17:04 -0600 Subject: [PATCH] Implement unit conversion in pressure calculators --- ecowitt2mqtt/helpers/calculator/pressure.py | 16 ++++++---------- tests/test_data.py | 12 ++++++------ 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/ecowitt2mqtt/helpers/calculator/pressure.py b/ecowitt2mqtt/helpers/calculator/pressure.py index ca88bca7..a02b9735 100644 --- a/ecowitt2mqtt/helpers/calculator/pressure.py +++ b/ecowitt2mqtt/helpers/calculator/pressure.py @@ -1,14 +1,17 @@ """Define pressure calculators.""" from __future__ import annotations -from ecowitt2mqtt.const import PRESSURE_HPA, PRESSURE_INHG, UNIT_SYSTEM_IMPERIAL +from ecowitt2mqtt.const import PRESSURE_HPA, PRESSURE_INHG from ecowitt2mqtt.helpers.calculator import CalculatedDataPoint, Calculator from ecowitt2mqtt.helpers.typing import PreCalculatedValueType +from ecowitt2mqtt.util.unit_conversion import PressureConverter class PressureCalculator(Calculator): """Define a pressure calculator.""" + DEFAULT_INPUT_UNIT = PRESSURE_INHG + @property def output_unit_imperial(self) -> str: """Get the default unit (imperial).""" @@ -24,12 +27,5 @@ def calculate_from_value( ) -> CalculatedDataPoint: """Perform the calculation.""" assert isinstance(value, float) - - if self._config.input_unit_system == self._config.output_unit_system: - final_value = value - elif self._config.output_unit_system == UNIT_SYSTEM_IMPERIAL: - final_value = round(value / 33.8639, 3) - else: - final_value = round(value * 33.8639, 3) - - return self.get_calculated_data_point(final_value) + converted_value = self.convert_value(PressureConverter, value) + return self.get_calculated_data_point(converted_value) diff --git a/tests/test_data.py b/tests/test_data.py index 2f2786ad..04b15234 100644 --- a/tests/test_data.py +++ b/tests/test_data.py @@ -5003,14 +5003,14 @@ def test_unit_conversion_to_imperial(device_data, ecowitt): ), "baromrel": CalculatedDataPoint( "barom", - 24.74, + 837.793, unit=PRESSURE_INHG, attributes={}, data_type=DataPointType.NON_BOOLEAN, ), "baromabs": CalculatedDataPoint( "barom", - 24.74, + 837.793, unit=PRESSURE_INHG, attributes={}, data_type=DataPointType.NON_BOOLEAN, @@ -5373,14 +5373,14 @@ def test_unit_conversion_to_imperial(device_data, ecowitt): ), "baromrel": CalculatedDataPoint( "barom", - 837.793, + 837.7925496203633, unit=PRESSURE_HPA, attributes={}, data_type=DataPointType.NON_BOOLEAN, ), "baromabs": CalculatedDataPoint( "barom", - 837.793, + 837.7925496203633, unit=PRESSURE_HPA, attributes={}, data_type=DataPointType.NON_BOOLEAN, @@ -5725,14 +5725,14 @@ def test_unit_conversion_to_imperial(device_data, ecowitt): ), "baromrel": CalculatedDataPoint( "barom", - 833.187, + 833.1870610694995, unit=PRESSURE_HPA, attributes={}, data_type=DataPointType.NON_BOOLEAN, ), "baromabs": CalculatedDataPoint( "barom", - 833.187, + 833.1870610694995, unit=PRESSURE_HPA, attributes={}, data_type=DataPointType.NON_BOOLEAN,