diff --git a/ecowitt2mqtt/helpers/calculator/__init__.py b/ecowitt2mqtt/helpers/calculator/__init__.py index 4d18ffb8..4b85480a 100644 --- a/ecowitt2mqtt/helpers/calculator/__init__.py +++ b/ecowitt2mqtt/helpers/calculator/__init__.py @@ -7,6 +7,7 @@ from functools import wraps from typing import TYPE_CHECKING, Any, Callable, Dict, TypeVar +from ecowitt2mqtt.const import UNIT_SYSTEM_IMPERIAL from ecowitt2mqtt.errors import EcowittError from ecowitt2mqtt.helpers.typing import CalculatedValueType, PreCalculatedValueType @@ -52,6 +53,16 @@ def __init__(self, config: Config, payload_key: str, data_point_key: str) -> Non self._data_point_key = data_point_key self._payload_key = payload_key + @property + def default_imperial_unit(self) -> str | None: + """Get the default unit (imperial).""" + return None + + @property + def default_metric_unit(self) -> str | None: + """Get the default unit (metric).""" + return None + @property def output_unit(self) -> str | None: """Get the output unit of measurement for this calculation.""" @@ -75,8 +86,19 @@ def get_calculated_data_point( data_type: DataPointType | None = None, ) -> CalculatedDataPoint: """Get the output unit for this calculation.""" + output_unit: str | None + + # If the user explicitly sets self.output_unit, use it if it's truthy; + # otherwise, default to the standard output unit for the unit system: + if self.output_unit: + output_unit = self.output_unit + elif self._config.output_unit_system == UNIT_SYSTEM_IMPERIAL: + output_unit = self.default_imperial_unit + else: + output_unit = self.default_metric_unit + data_point = CalculatedDataPoint( - data_point_key=self._data_point_key, value=value, unit=self.output_unit + data_point_key=self._data_point_key, value=value, unit=output_unit ) if attributes: diff --git a/ecowitt2mqtt/helpers/calculator/humidity.py b/ecowitt2mqtt/helpers/calculator/humidity.py index 4c9f243b..b3c43920 100644 --- a/ecowitt2mqtt/helpers/calculator/humidity.py +++ b/ecowitt2mqtt/helpers/calculator/humidity.py @@ -25,10 +25,13 @@ class AbsoluteHumidityCalculator(Calculator): """Define an absolute humidity calculator.""" @property - def output_unit(self) -> str | None: - """Get the output unit of measurement for this calculation.""" - if self._config.output_unit_system == UNIT_SYSTEM_IMPERIAL: - return VOLUME_POUNDS_PER_CUBIC_FOOT + def default_imperial_unit(self) -> str: + """Get the default unit (imperial).""" + return VOLUME_POUNDS_PER_CUBIC_FOOT + + @property + def default_metric_unit(self) -> str: + """Get the default unit (metric).""" return VOLUME_GRAMS_PER_CUBIC_METER @Calculator.requires_keys(DATA_POINT_TEMP, DATA_POINT_HUMIDITY) @@ -54,6 +57,11 @@ class RelativeHumidityCalculator(SimpleCalculator): """Define a boolean leak calculator.""" @property - def output_unit(self) -> str | None: - """Get the output unit of measurement for this calculation.""" + def default_imperial_unit(self) -> str: + """Get the default unit (imperial).""" + return PERCENTAGE + + @property + def default_metric_unit(self) -> str: + """Get the default unit (metric).""" return PERCENTAGE diff --git a/ecowitt2mqtt/helpers/calculator/illuminance.py b/ecowitt2mqtt/helpers/calculator/illuminance.py index 2f460301..57cf64bb 100644 --- a/ecowitt2mqtt/helpers/calculator/illuminance.py +++ b/ecowitt2mqtt/helpers/calculator/illuminance.py @@ -21,8 +21,13 @@ class IlluminanceLuxCalculator(Calculator): """Define a illuminance calculator (lux).""" @property - def output_unit(self) -> str | None: - """Get the output unit of measurement for this calculation.""" + def default_imperial_unit(self) -> str: + """Get the default unit (imperial).""" + return LIGHT_LUX + + @property + def default_metric_unit(self) -> str: + """Get the default unit (metric).""" return LIGHT_LUX @Calculator.requires_keys(DATA_POINT_SOLARRADIATION) @@ -41,8 +46,13 @@ class IlluminancePerceivedCalculator(Calculator): """Define a illuminance calculator (perceived).""" @property - def output_unit(self) -> str | None: - """Get the output unit of measurement for this calculation.""" + def default_imperial_unit(self) -> str: + """Get the default unit (imperial).""" + return PERCENTAGE + + @property + def default_metric_unit(self) -> str: + """Get the default unit (metric).""" return PERCENTAGE @Calculator.requires_keys(DATA_POINT_SOLARRADIATION) @@ -68,6 +78,11 @@ class IlluminanceWM2Calculator(SimpleCalculator): """Define a illuminance calculator (W/m²).""" @property - def output_unit(self) -> str | None: - """Get the output unit of measurement for this calculation.""" + def default_imperial_unit(self) -> str: + """Get the default unit (imperial).""" + return ILLUMINANCE_WATTS_PER_SQUARE_METER + + @property + def default_metric_unit(self) -> str: + """Get the default unit (metric).""" return ILLUMINANCE_WATTS_PER_SQUARE_METER diff --git a/ecowitt2mqtt/helpers/calculator/lightning.py b/ecowitt2mqtt/helpers/calculator/lightning.py index 1e3f3c08..12649d52 100644 --- a/ecowitt2mqtt/helpers/calculator/lightning.py +++ b/ecowitt2mqtt/helpers/calculator/lightning.py @@ -6,7 +6,6 @@ LENGTH_MILES, LOGGER, STRIKES, - UNIT_SYSTEM_IMPERIAL, UNIT_SYSTEM_METRIC, ) from ecowitt2mqtt.helpers.calculator import ( @@ -21,8 +20,13 @@ class LightningStrikeCountCalculator(SimpleCalculator): """Define a lightning strike count calculator.""" @property - def output_unit(self) -> str | None: - """Get the output unit of measurement for this calculation.""" + def default_imperial_unit(self) -> str: + """Get the default unit (imperial).""" + return STRIKES + + @property + def default_metric_unit(self) -> str: + """Get the default unit (metric).""" return STRIKES @@ -33,10 +37,13 @@ class LightningStrikeDistanceCalculator(Calculator): """ @property - def output_unit(self) -> str | None: - """Get the output unit of measurement for this calculation.""" - if self._config.output_unit_system == UNIT_SYSTEM_IMPERIAL: - return LENGTH_MILES + def default_imperial_unit(self) -> str: + """Get the default unit (imperial).""" + return LENGTH_MILES + + @property + def default_metric_unit(self) -> str: + """Get the default unit (metric).""" return LENGTH_KILOMETERS def calculate_from_value( diff --git a/ecowitt2mqtt/helpers/calculator/precipitation.py b/ecowitt2mqtt/helpers/calculator/precipitation.py index 3b69823f..b4686a8b 100644 --- a/ecowitt2mqtt/helpers/calculator/precipitation.py +++ b/ecowitt2mqtt/helpers/calculator/precipitation.py @@ -16,10 +16,13 @@ class RainRateCalculator(Calculator): """Define a rain rate calculator.""" @property - def output_unit(self) -> str | None: - """Get the output unit of measurement for this calculation.""" - if self._config.output_unit_system == UNIT_SYSTEM_IMPERIAL: - return PRECIPITATION_INCHES_PER_HOUR + def default_imperial_unit(self) -> str: + """Get the default unit (imperial).""" + return PRECIPITATION_INCHES_PER_HOUR + + @property + def default_metric_unit(self) -> str: + """Get the default unit (metric).""" return PRECIPITATION_MILLIMETERS_PER_HOUR def calculate_from_value( @@ -41,10 +44,13 @@ class RainVolumeCalculator(Calculator): """Define a rain volume calculator.""" @property - def output_unit(self) -> str | None: - """Get the output unit of measurement for this calculation.""" - if self._config.output_unit_system == UNIT_SYSTEM_IMPERIAL: - return PRECIPITATION_INCHES + def default_imperial_unit(self) -> str: + """Get the default unit (imperial).""" + return PRECIPITATION_INCHES + + @property + def default_metric_unit(self) -> str: + """Get the default unit (metric).""" return PRECIPITATION_MILLIMETERS def calculate_from_value( diff --git a/ecowitt2mqtt/helpers/calculator/pressure.py b/ecowitt2mqtt/helpers/calculator/pressure.py index 59509a79..d3604c50 100644 --- a/ecowitt2mqtt/helpers/calculator/pressure.py +++ b/ecowitt2mqtt/helpers/calculator/pressure.py @@ -10,10 +10,13 @@ class PressureCalculator(Calculator): """Define a pressure calculator.""" @property - def output_unit(self) -> str | None: - """Get the output unit of measurement for this calculation.""" - if self._config.output_unit_system == UNIT_SYSTEM_IMPERIAL: - return PRESSURE_INHG + def default_imperial_unit(self) -> str: + """Get the default unit (imperial).""" + return PRESSURE_INHG + + @property + def default_metric_unit(self) -> str: + """Get the default unit (metric).""" return PRESSURE_HPA def calculate_from_value( diff --git a/ecowitt2mqtt/helpers/calculator/temperature.py b/ecowitt2mqtt/helpers/calculator/temperature.py index 263e0968..6bc9bd0c 100644 --- a/ecowitt2mqtt/helpers/calculator/temperature.py +++ b/ecowitt2mqtt/helpers/calculator/temperature.py @@ -177,10 +177,13 @@ class TemperatureUnitConverter(Calculator): """Define a base temperature calculator.""" @property - def output_unit(self) -> str | None: - """Get the output unit of measurement for this calculation.""" - if self._config.output_unit_system == UNIT_SYSTEM_IMPERIAL: - return TEMP_FAHRENHEIT + def default_imperial_unit(self) -> str: + """Get the default unit (imperial).""" + return TEMP_FAHRENHEIT + + @property + def default_metric_unit(self) -> str: + """Get the default unit (metric).""" return TEMP_CELSIUS diff --git a/ecowitt2mqtt/helpers/calculator/time.py b/ecowitt2mqtt/helpers/calculator/time.py index 613e01a9..68e2d172 100644 --- a/ecowitt2mqtt/helpers/calculator/time.py +++ b/ecowitt2mqtt/helpers/calculator/time.py @@ -31,6 +31,11 @@ class RuntimeCalculator(SimpleCalculator): """Define a runtime calculator.""" @property - def output_unit(self) -> str | None: - """Get the output unit of measurement for this calculation.""" + def default_imperial_unit(self) -> str: + """Get the default unit (imperial).""" + return TIME_SECONDS + + @property + def default_metric_unit(self) -> str: + """Get the default unit (metric).""" return TIME_SECONDS diff --git a/ecowitt2mqtt/helpers/calculator/uv.py b/ecowitt2mqtt/helpers/calculator/uv.py index 4333e0ec..178e9133 100644 --- a/ecowitt2mqtt/helpers/calculator/uv.py +++ b/ecowitt2mqtt/helpers/calculator/uv.py @@ -87,8 +87,13 @@ class SafeExposureCalculator(Calculator): """Define a safe exposure calculator.""" @property - def output_unit(self) -> str | None: - """Get the output unit of measurement for this calculation.""" + def default_imperial_unit(self) -> str: + """Get the default unit (imperial).""" + return TIME_MINUTES + + @property + def default_metric_unit(self) -> str: + """Get the default unit (metric).""" return TIME_MINUTES @Calculator.requires_keys(DATA_POINT_UV) @@ -126,6 +131,11 @@ class UVIndexCalculator(SimpleCalculator): """Define a UV index calculator.""" @property - def output_unit(self) -> str | None: - """Get the output unit of measurement for this calculation.""" + def default_imperial_unit(self) -> str: + """Get the default unit (imperial).""" + return UV_INDEX + + @property + def default_metric_unit(self) -> str: + """Get the default unit (metric).""" return UV_INDEX diff --git a/ecowitt2mqtt/helpers/calculator/wind.py b/ecowitt2mqtt/helpers/calculator/wind.py index 186ec143..809b0479 100644 --- a/ecowitt2mqtt/helpers/calculator/wind.py +++ b/ecowitt2mqtt/helpers/calculator/wind.py @@ -255,8 +255,13 @@ class WindDirCalculator(SimpleCalculator): """Define a wind direction calculator.""" @property - def output_unit(self) -> str | None: - """Get the output unit of measurement for this calculation.""" + def default_imperial_unit(self) -> str: + """Get the default unit (imperial).""" + return DEGREE + + @property + def default_metric_unit(self) -> str: + """Get the default unit (metric).""" return DEGREE @@ -264,10 +269,13 @@ class WindSpeedCalculator(Calculator): """Define a wind speed calculator.""" @property - def output_unit(self) -> str | None: - """Get the output unit of measurement for this calculation.""" - if self._config.output_unit_system == UNIT_SYSTEM_IMPERIAL: - return SPEED_MILES_PER_HOUR + def default_imperial_unit(self) -> str: + """Get the default unit (imperial).""" + return SPEED_MILES_PER_HOUR + + @property + def default_metric_unit(self) -> str: + """Get the default unit (metric).""" return SPEED_KILOMETERS_PER_HOUR def calculate_from_value(