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

Add PrecipitationConverter helper #314

Merged
merged 1 commit into from
Oct 10, 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
5 changes: 3 additions & 2 deletions ecowitt2mqtt/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,11 @@
CONCENTRATION_PARTS_PER_MILLION: Final = "ppm"

# Precipitation units:
PRECIPITATION_MILLIMETERS: Final = "mm"
PRECIPITATION_MILLIMETERS_PER_HOUR: Final = "mm/h"
PRECIPITATION_INCHES: Final = "in"
PRECIPITATION_MILLIMETERS: Final = "mm"

PRECIPITATION_INCHES_PER_HOUR: Final = "in/h"
PRECIPITATION_MILLIMETERS_PER_HOUR: Final = "mm/h"

# Pressure units:
PRESSURE_BAR: Final = "bar"
Expand Down
25 changes: 25 additions & 0 deletions ecowitt2mqtt/util/unit_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
LENGTH_MILES,
LENGTH_MILLIMETERS,
LENGTH_YARD,
PRECIPITATION_INCHES,
PRECIPITATION_INCHES_PER_HOUR,
PRECIPITATION_MILLIMETERS,
PRECIPITATION_MILLIMETERS_PER_HOUR,
PRESSURE_BAR,
PRESSURE_CBAR,
PRESSURE_HPA,
Expand Down Expand Up @@ -120,6 +124,27 @@ class DistanceConverter(BaseUnitConverter):
}


class PrecipitationConverter(BaseUnitConverter):
"""Utility to convert precipitation values."""

UNIT_CLASS = "precipitation"
# Note that we can accept either mm or mm/h as the normalized unit:
NORMALIZED_UNIT = PRECIPITATION_MILLIMETERS
VALID_UNITS = {
PRECIPITATION_MILLIMETERS,
PRECIPITATION_MILLIMETERS_PER_HOUR,
PRECIPITATION_INCHES,
PRECIPITATION_INCHES_PER_HOUR,
}

_UNIT_CONVERSION = {
PRECIPITATION_MILLIMETERS: 1,
PRECIPITATION_MILLIMETERS_PER_HOUR: 1,
PRECIPITATION_INCHES: 1 * _MM_TO_M / _IN_TO_M,
PRECIPITATION_INCHES_PER_HOUR: 1 * _MM_TO_M / _IN_TO_M,
}


class PressureConverter(BaseUnitConverter):
"""Define a utility to convert pressure values."""

Expand Down
19 changes: 19 additions & 0 deletions tests/util/test_unit_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from ecowitt2mqtt.util.unit_conversion import (
DistanceConverter,
PrecipitationConverter,
PressureConverter,
SpeedConverter,
TemperatureConverter,
Expand All @@ -15,6 +16,8 @@
[
("distance", DistanceConverter, "miles", "ft"),
("distance", DistanceConverter, "m", "dm"),
("precipitation", PrecipitationConverter, "mm/s", "mm/h"),
("precipitation", PrecipitationConverter, "in/h", "yd/yr"),
("pressure", PressureConverter, "hPa", "hPa/s"),
("pressure", PressureConverter, "units", "hPa"),
("speed", SpeedConverter, "mph", "km/s"),
Expand Down Expand Up @@ -48,6 +51,22 @@ def test_distance_conversion(converted_value, from_unit, to_unit, value):
assert DistanceConverter.convert(value, from_unit, to_unit) == converted_value


@pytest.mark.parametrize(
"value,from_unit,to_unit,converted_value",
[
(10, "mm", "mm", 10.0),
(10, "in", "mm", 254.0),
(10, "mm", "in", 0.39370078740157477),
(10, "mm/h", "mm/h", 10.0),
(10, "in/h", "mm/h", 254.0),
(10, "mm/h", "in/h", 0.39370078740157477),
],
)
def test_precipitation_conversion(converted_value, from_unit, to_unit, value):
"""Test precipitation conversions."""
assert PrecipitationConverter.convert(value, from_unit, to_unit) == converted_value


@pytest.mark.parametrize(
"value,from_unit,to_unit,converted_value",
[
Expand Down