diff --git a/ecowitt2mqtt/data.py b/ecowitt2mqtt/data.py index 0f8174cc..89678b7e 100644 --- a/ecowitt2mqtt/data.py +++ b/ecowitt2mqtt/data.py @@ -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 ( @@ -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, @@ -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, diff --git a/ecowitt2mqtt/helpers/calculator/precipitation.py b/ecowitt2mqtt/helpers/calculator/precipitation.py index 24d9fac3..8a324959 100644 --- a/ecowitt2mqtt/helpers/calculator/precipitation.py +++ b/ecowitt2mqtt/helpers/calculator/precipitation.py @@ -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) diff --git a/tests/test_data.py b/tests/test_data.py index 54618528..2f2786ad 100644 --- a/tests/test_data.py +++ b/tests/test_data.py @@ -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, @@ -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, @@ -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,