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

Lay groundwork for dynamic unit selection #324

Merged
merged 2 commits 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
24 changes: 23 additions & 1 deletion ecowitt2mqtt/helpers/calculator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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."""
Expand All @@ -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:
Expand Down
20 changes: 14 additions & 6 deletions ecowitt2mqtt/helpers/calculator/humidity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
27 changes: 21 additions & 6 deletions ecowitt2mqtt/helpers/calculator/illuminance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
21 changes: 14 additions & 7 deletions ecowitt2mqtt/helpers/calculator/lightning.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
LENGTH_MILES,
LOGGER,
STRIKES,
UNIT_SYSTEM_IMPERIAL,
UNIT_SYSTEM_METRIC,
)
from ecowitt2mqtt.helpers.calculator import (
Expand All @@ -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


Expand All @@ -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(
Expand Down
22 changes: 14 additions & 8 deletions ecowitt2mqtt/helpers/calculator/precipitation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down
11 changes: 7 additions & 4 deletions ecowitt2mqtt/helpers/calculator/pressure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
11 changes: 7 additions & 4 deletions ecowitt2mqtt/helpers/calculator/temperature.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
9 changes: 7 additions & 2 deletions ecowitt2mqtt/helpers/calculator/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 14 additions & 4 deletions ecowitt2mqtt/helpers/calculator/uv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
20 changes: 14 additions & 6 deletions ecowitt2mqtt/helpers/calculator/wind.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,19 +255,27 @@ 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


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(
Expand Down