Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement unit conversion in pressure calculators #331

Merged
merged 1 commit into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions ecowitt2mqtt/helpers/calculator/pressure.py
Original file line number Diff line number Diff line change
@@ -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)."""
Expand All @@ -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)
12 changes: 6 additions & 6 deletions tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down