Skip to content

Commit

Permalink
Implement unit conversion in precipitation calculators (#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
bachya authored Oct 14, 2022
1 parent 92aacc6 commit 51b5a56
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 36 deletions.
10 changes: 5 additions & 5 deletions ecowitt2mqtt/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@
)
from ecowitt2mqtt.helpers.calculator.pollution import PollutantCalculator
from ecowitt2mqtt.helpers.calculator.precipitation import (
RainRateCalculator,
RainVolumeCalculator,
AccumulatedPrecipitationCalculator,
PrecipitationRateCalculator,
)
from ecowitt2mqtt.helpers.calculator.pressure import PressureCalculator
from ecowitt2mqtt.helpers.calculator.temperature import (
Expand Down Expand Up @@ -121,8 +121,8 @@
DATA_POINT_GLOB_MOISTURE: RelativeHumidityCalculator,
DATA_POINT_GLOB_PM10: PollutantCalculator,
DATA_POINT_GLOB_PM25: PollutantCalculator,
DATA_POINT_GLOB_RAIN: RainVolumeCalculator,
DATA_POINT_GLOB_R_RAIN: RainRateCalculator,
DATA_POINT_GLOB_RAIN: AccumulatedPrecipitationCalculator,
DATA_POINT_GLOB_R_RAIN: PrecipitationRateCalculator,
DATA_POINT_GLOB_TEMP: TemperatureCalculator,
DATA_POINT_GLOB_TF: TemperatureCalculator,
DATA_POINT_GLOB_VOLT: BatteryCalculator,
Expand All @@ -137,7 +137,7 @@
DATA_POINT_LIGHTNING: LightningStrikeDistanceCalculator,
DATA_POINT_LIGHTNING_NUM: LightningStrikeCountCalculator,
DATA_POINT_LIGHTNING_TIME: EpochCalculator,
DATA_POINT_RAIN_RATE: RainRateCalculator,
DATA_POINT_RAIN_RATE: PrecipitationRateCalculator,
DATA_POINT_RUNTIME: RuntimeCalculator,
DATA_POINT_SAFE_EXPOSURE_TIME_SKIN_TYPE_1: SafeExposureCalculator,
DATA_POINT_SAFE_EXPOSURE_TIME_SKIN_TYPE_2: SafeExposureCalculator,
Expand Down
44 changes: 19 additions & 25 deletions ecowitt2mqtt/helpers/calculator/precipitation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,58 @@
PRECIPITATION_INCHES_PER_HOUR,
PRECIPITATION_MILLIMETERS,
PRECIPITATION_MILLIMETERS_PER_HOUR,
UNIT_SYSTEM_IMPERIAL,
)
from ecowitt2mqtt.helpers.calculator import CalculatedDataPoint, Calculator
from ecowitt2mqtt.helpers.typing import PreCalculatedValueType
from ecowitt2mqtt.util.unit_conversion import (
AccumulatedPrecipitationConverter,
PrecipitationRateConverter,
)


class RainRateCalculator(Calculator):
"""Define a rain rate calculator."""
class AccumulatedPrecipitationCalculator(Calculator):
"""Define a rain volume calculator."""

DEFAULT_INPUT_UNIT = PRECIPITATION_INCHES

@property
def output_unit_imperial(self) -> str:
"""Get the default unit (imperial)."""
return PRECIPITATION_INCHES_PER_HOUR
return PRECIPITATION_INCHES

@property
def output_unit_metric(self) -> str:
"""Get the default unit (metric)."""
return PRECIPITATION_MILLIMETERS_PER_HOUR
return PRECIPITATION_MILLIMETERS

def calculate_from_value(
self, value: PreCalculatedValueType
) -> CalculatedDataPoint:
"""Perform the calculation."""
assert isinstance(value, float)
converted_value = self.convert_value(AccumulatedPrecipitationConverter, value)
return self.get_calculated_data_point(converted_value)

if self._config.input_unit_system == self._config.output_unit_system:
rain_volume = value
elif self._config.output_unit_system == UNIT_SYSTEM_IMPERIAL:
rain_volume = round(value / 25.4, 1)
else:
rain_volume = round(value * 25.4, 1)
return self.get_calculated_data_point(rain_volume)

class PrecipitationRateCalculator(Calculator):
"""Define a rain rate calculator."""

class RainVolumeCalculator(Calculator):
"""Define a rain volume calculator."""
DEFAULT_INPUT_UNIT = PRECIPITATION_INCHES_PER_HOUR

@property
def output_unit_imperial(self) -> str:
"""Get the default unit (imperial)."""
return PRECIPITATION_INCHES
return PRECIPITATION_INCHES_PER_HOUR

@property
def output_unit_metric(self) -> str:
"""Get the default unit (metric)."""
return PRECIPITATION_MILLIMETERS
return PRECIPITATION_MILLIMETERS_PER_HOUR

def calculate_from_value(
self, value: PreCalculatedValueType
) -> 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 / 25.4, 1)
else:
final_value = round(value * 25.4, 1)

return self.get_calculated_data_point(final_value)
converted_value = self.convert_value(PrecipitationRateConverter, 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 @@ -5202,14 +5202,14 @@ def test_unit_conversion_to_imperial(device_data, ecowitt):
),
"monthlyrain": CalculatedDataPoint(
"rain",
2.2,
55.3,
unit=PRECIPITATION_INCHES,
attributes={},
data_type=DataPointType.NON_BOOLEAN,
),
"yearlyrain": CalculatedDataPoint(
"rain",
4.4,
112.8,
unit=PRECIPITATION_INCHES,
attributes={},
data_type=DataPointType.NON_BOOLEAN,
Expand Down Expand Up @@ -5573,14 +5573,14 @@ def test_unit_conversion_to_imperial(device_data, ecowitt):
),
"monthlyrain": CalculatedDataPoint(
"rain",
55.3,
55.2958,
unit=PRECIPITATION_MILLIMETERS,
attributes={},
data_type=DataPointType.NON_BOOLEAN,
),
"yearlyrain": CalculatedDataPoint(
"rain",
112.8,
112.8014,
unit=PRECIPITATION_MILLIMETERS,
attributes={},
data_type=DataPointType.NON_BOOLEAN,
Expand Down Expand Up @@ -5893,14 +5893,14 @@ def test_unit_conversion_to_imperial(device_data, ecowitt):
),
"yearlyrain": CalculatedDataPoint(
"rain",
298.6,
298.6024,
unit=PRECIPITATION_MILLIMETERS,
attributes={},
data_type=DataPointType.NON_BOOLEAN,
),
"totalrain": CalculatedDataPoint(
"rain",
298.6,
298.6024,
unit=PRECIPITATION_MILLIMETERS,
attributes={},
data_type=DataPointType.NON_BOOLEAN,
Expand Down

0 comments on commit 51b5a56

Please sign in to comment.