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

Replace unit system constants with an Enum #848

Merged
merged 2 commits into from
Jan 18, 2024
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
6 changes: 3 additions & 3 deletions ecowitt2mqtt/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
LEGACY_ENV_PORT,
LEGACY_ENV_RAW_DATA,
LOGGER,
UNIT_SYSTEM_IMPERIAL,
UnitSystem,
__version__,
)
from ecowitt2mqtt.core import Ecowitt
Expand Down Expand Up @@ -272,7 +272,7 @@ def get_cli_arguments(args: list[str]) -> dict[str, Any]:
dest=CONF_INPUT_UNIT_SYSTEM,
help=(
"The input unit system used by the gateway "
f"(default: {UNIT_SYSTEM_IMPERIAL})"
f"(default: {UnitSystem.IMPERIAL})"
),
metavar=CONF_INPUT_UNIT_SYSTEM,
)
Expand Down Expand Up @@ -327,7 +327,7 @@ def get_cli_arguments(args: list[str]) -> dict[str, Any]:
dest=CONF_OUTPUT_UNIT_SYSTEM,
help=(
"The output unit system used by the gateway "
f"(default: {UNIT_SYSTEM_IMPERIAL})"
f"(default: {UnitSystem.IMPERIAL})"
),
metavar=CONF_OUTPUT_UNIT_SYSTEM,
)
Expand Down
6 changes: 3 additions & 3 deletions ecowitt2mqtt/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
DEFAULT_MQTT_PORT,
DEFAULT_PORT,
ENV_BATTERY_OVERRIDES,
UNIT_SYSTEM_IMPERIAL,
UnitSystem,
)
from ecowitt2mqtt.errors import EcowittError
from ecowitt2mqtt.helpers.calculator.battery import BatteryStrategy
Expand Down Expand Up @@ -133,8 +133,8 @@ class Config(BaseModel):
output_unit_temperature: str | None = None

# Optional unit system parameters:
input_unit_system: str = UNIT_SYSTEM_IMPERIAL
output_unit_system: str = UNIT_SYSTEM_IMPERIAL
input_unit_system: UnitSystem = UnitSystem.IMPERIAL
output_unit_system: UnitSystem = UnitSystem.IMPERIAL

# Generated parameters:
uuid: str = Field(default_factory=lambda: uuid4().hex)
Expand Down
12 changes: 9 additions & 3 deletions ecowitt2mqtt/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import logging
from typing import Final

from ecowitt2mqtt.backports.enum import StrEnum

__version__ = "2024.01.0"

LOGGER = logging.getLogger(__package__)
Expand Down Expand Up @@ -193,10 +195,14 @@
LEGACY_ENV_PORT: Final = "PORT"
LEGACY_ENV_RAW_DATA: Final = "RAW_DATA"


# Unit systems:
UNIT_SYSTEM_IMPERIAL: Final = "imperial"
UNIT_SYSTEM_METRIC: Final = "metric"
UNIT_SYSTEMS: Final = [UNIT_SYSTEM_IMPERIAL, UNIT_SYSTEM_METRIC]
class UnitSystem(StrEnum):
"""Define unit systems."""

IMPERIAL = "imperial"
METRIC = "metric"


# Unit classes:
ACCUMULATED_PRECIPITATION: Final = "accumulated_precipitation"
Expand Down
4 changes: 2 additions & 2 deletions ecowitt2mqtt/helpers/calculator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from functools import wraps
from typing import TYPE_CHECKING, Any, TypeVar, cast

from ecowitt2mqtt.const import UNIT_SYSTEM_IMPERIAL
from ecowitt2mqtt.const import UnitSystem
from ecowitt2mqtt.errors import EcowittError
from ecowitt2mqtt.helpers.typing import CalculatedValueType, PreCalculatedValueType
from ecowitt2mqtt.util.unit_conversion import BaseUnitConverter
Expand Down Expand Up @@ -100,7 +100,7 @@ def output_unit(self) -> str | None:
)
) is not None:
return cast(str, override)
if self._config.output_unit_system == UNIT_SYSTEM_IMPERIAL:
if self._config.output_unit_system == UnitSystem.IMPERIAL:
return self.output_unit_imperial
return self.output_unit_metric

Expand Down
7 changes: 3 additions & 4 deletions ecowitt2mqtt/helpers/calculator/wind.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
DEGREE,
SPEED_KILOMETERS_PER_HOUR,
SPEED_MILES_PER_HOUR,
UNIT_SYSTEM_IMPERIAL,
UNIT_SYSTEM_METRIC,
UnitSystem,
)
from ecowitt2mqtt.helpers.calculator import (
CalculatedDataPoint,
Expand Down Expand Up @@ -240,11 +239,11 @@ def calculate_from_payload(
r
for r in BEAUFORT_SCALE_RATINGS
if (
self._config.input_unit_system == UNIT_SYSTEM_IMPERIAL
self._config.input_unit_system == UnitSystem.IMPERIAL
and r.minimum_mph <= wind_speed < r.maximum_mph
)
or (
self._config.input_unit_system == UNIT_SYSTEM_METRIC
self._config.input_unit_system == UnitSystem.METRIC
and r.minimum_kmh <= wind_speed < r.maximum_kmh
)
]
Expand Down
33 changes: 15 additions & 18 deletions ecowitt2mqtt/util/meteo.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@

import meteocalc

from ecowitt2mqtt.const import (
TEMP_CELSIUS,
TEMP_KELVIN,
UNIT_SYSTEM_IMPERIAL,
UNIT_SYSTEM_METRIC,
)
from ecowitt2mqtt.const import TEMP_CELSIUS, TEMP_KELVIN, UnitSystem
from ecowitt2mqtt.util.unit_conversion import TemperatureConverter


Expand Down Expand Up @@ -40,7 +35,7 @@ def get_absolute_humidity_in_metric(


def get_dew_point_meteocalc_object(
temperature: float, relative_humidity: float, unit_system: str
temperature: float, relative_humidity: float, unit_system: UnitSystem
) -> meteocalc.Temp:
"""Get a dew point meteocalc object.

Expand All @@ -60,7 +55,7 @@ def get_feels_like_meteocalc_object(
temperature: float,
relative_humidity: float,
wind_speed: float,
unit_system: str,
unit_system: UnitSystem,
) -> meteocalc.Temp:
"""Get a "feels like" meteocalc object.

Expand Down Expand Up @@ -107,14 +102,14 @@ def get_frost_point_meteocalc_object(
- absolute_temp_c
)
- 273.15,
UNIT_SYSTEM_METRIC,
UnitSystem.METRIC,
)


def get_heat_index_meteocalc_object(
temperature: float,
relative_humidity: float,
unit_system: str,
unit_system: UnitSystem,
) -> meteocalc.Temp:
"""Get a heat index meteocalc object.

Expand All @@ -130,7 +125,9 @@ def get_heat_index_meteocalc_object(
return meteocalc.heat_index(temp_obj, relative_humidity)


def get_humidex(temperature: float, relative_humidity: float, unit_system: str) -> int:
def get_humidex(
temperature: float, relative_humidity: float, unit_system: UnitSystem
) -> int:
"""Get a humidex.

Args:
Expand Down Expand Up @@ -176,7 +173,7 @@ def get_humidex(temperature: float, relative_humidity: float, unit_system: str)


def get_relative_strain_index(
temperature: float, relative_humidity: float, unit_system: str
temperature: float, relative_humidity: float, unit_system: UnitSystem
) -> float:
"""Get a simmer index meteocalc object.

Expand Down Expand Up @@ -217,7 +214,7 @@ def get_relative_strain_index(


def get_simmer_index_meteocalc_object(
temp_obj: meteocalc.Temp, relative_humidity: float, unit_system: str
temp_obj: meteocalc.Temp, relative_humidity: float, unit_system: UnitSystem
) -> meteocalc.Temp:
"""Get a simmer index meteocalc object.

Expand All @@ -235,7 +232,7 @@ def get_simmer_index_meteocalc_object(
if temp_obj.f < 70:
raise ValueError(
"Simmer Index is only valid for temperatures above "
f"{'70°F' if unit_system == UNIT_SYSTEM_IMPERIAL else '21.1°C'}"
f"{'70°F' if unit_system == UnitSystem.IMPERIAL else '21.1°C'}"
)

return get_temperature_meteocalc_object(
Expand All @@ -244,12 +241,12 @@ def get_simmer_index_meteocalc_object(
* (temp_obj.f - (0.55 - (0.0055 * relative_humidity)) * (temp_obj.f - 58.0))
- 56.83
),
UNIT_SYSTEM_IMPERIAL,
UnitSystem.IMPERIAL,
)


def get_temperature_meteocalc_object(
temperature: float, unit_system: str
temperature: float, unit_system: UnitSystem
) -> meteocalc.Temp:
"""Get a temperature meteocalc object.

Expand All @@ -260,15 +257,15 @@ def get_temperature_meteocalc_object(
Returns:
A meteocalc.Temp object.
"""
if unit_system == UNIT_SYSTEM_IMPERIAL:
if unit_system == UnitSystem.IMPERIAL:
unit = "f"
else:
unit = "c"
return meteocalc.Temp(temperature, unit)


def get_wind_chill_meteocalc_object(
temperature: float, wind_speed: float, unit_system: str
temperature: float, wind_speed: float, unit_system: UnitSystem
) -> meteocalc.Temp:
"""Get a wind chill meteocalc object.

Expand Down
10 changes: 5 additions & 5 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
CONF_PORT,
CONF_RAW_DATA,
CONF_VERBOSE,
UNIT_SYSTEM_IMPERIAL,
UnitSystem,
)
from ecowitt2mqtt.helpers.calculator.battery import BatteryStrategy
from ecowitt2mqtt.helpers.server import InputDataFormat
Expand All @@ -46,15 +46,15 @@
CONF_HASS_DISCOVERY_PREFIX: TEST_HASS_DISCOVERY_PREFIX,
CONF_HASS_ENTITY_ID_PREFIX: None,
CONF_INPUT_DATA_FORMAT: InputDataFormat.ECOWITT,
CONF_INPUT_UNIT_SYSTEM: UNIT_SYSTEM_IMPERIAL,
CONF_INPUT_UNIT_SYSTEM: UnitSystem.IMPERIAL,
CONF_MQTT_BROKER: TEST_MQTT_BROKER,
CONF_MQTT_PASSWORD: TEST_MQTT_PASSWORD,
CONF_MQTT_PORT: TEST_MQTT_PORT,
CONF_MQTT_RETAIN: False,
CONF_MQTT_TLS: False,
CONF_MQTT_TOPIC: TEST_MQTT_TOPIC,
CONF_MQTT_USERNAME: TEST_MQTT_USERNAME,
CONF_OUTPUT_UNIT_SYSTEM: UNIT_SYSTEM_IMPERIAL,
CONF_OUTPUT_UNIT_SYSTEM: UnitSystem.IMPERIAL,
CONF_PORT: TEST_PORT,
CONF_RAW_DATA: False,
CONF_VERBOSE: False,
Expand All @@ -70,15 +70,15 @@
{CONF_HASS_DISCOVERY_PREFIX}: {TEST_HASS_DISCOVERY_PREFIX}
{CONF_HASS_ENTITY_ID_PREFIX}: null
{CONF_INPUT_DATA_FORMAT}: {InputDataFormat.ECOWITT}
{CONF_INPUT_UNIT_SYSTEM}: {UNIT_SYSTEM_IMPERIAL}
{CONF_INPUT_UNIT_SYSTEM}: {UnitSystem.IMPERIAL}
{CONF_MQTT_BROKER}: {TEST_MQTT_BROKER}
{CONF_MQTT_PASSWORD}: {TEST_MQTT_PASSWORD}
{CONF_MQTT_PORT}: {TEST_MQTT_PORT}
{CONF_MQTT_RETAIN}: false
{CONF_MQTT_TLS}: false
{CONF_MQTT_TOPIC}: {TEST_MQTT_TOPIC}
{CONF_MQTT_USERNAME}: {TEST_MQTT_USERNAME}
{CONF_OUTPUT_UNIT_SYSTEM}: {UNIT_SYSTEM_IMPERIAL}
{CONF_OUTPUT_UNIT_SYSTEM}: {UnitSystem.IMPERIAL}
{CONF_PORT}: {TEST_PORT}
{CONF_RAW_DATA}: false
{CONF_VERBOSE}: false
Expand Down
4 changes: 2 additions & 2 deletions tests/data/test_data_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
TEMP_FAHRENHEIT,
TIME_MINUTES,
TIME_SECONDS,
UNIT_SYSTEM_METRIC,
UV_INDEX,
VOLUME_POUNDS_PER_CUBIC_FOOT,
UnitSystem,
)
from ecowitt2mqtt.core import Ecowitt
from ecowitt2mqtt.data import ProcessedData
Expand Down Expand Up @@ -4841,7 +4841,7 @@ def test_process(


@pytest.mark.parametrize(
"config", [TEST_CONFIG_JSON | {CONF_INPUT_UNIT_SYSTEM: UNIT_SYSTEM_METRIC}]
"config", [TEST_CONFIG_JSON | {CONF_INPUT_UNIT_SYSTEM: UnitSystem.METRIC}]
)
def test_suspcious_temperature_value(
caplog: Mock, device_data: dict[str, Any], ecowitt: Ecowitt
Expand Down
6 changes: 3 additions & 3 deletions tests/data/test_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
TEMP_FAHRENHEIT,
TIME_MINUTES,
TIME_SECONDS,
UNIT_SYSTEM_METRIC,
UV_INDEX,
VOLUME_GRAMS_PER_CUBIC_METER,
VOLUME_POUNDS_PER_CUBIC_FOOT,
UnitSystem,
)
from ecowitt2mqtt.core import Ecowitt
from ecowitt2mqtt.data import ProcessedData
Expand Down Expand Up @@ -461,7 +461,7 @@ def test_output_units(device_data: dict[str, Any], ecowitt: Ecowitt) -> None:
"config,device_data_filename",
[
(
TEST_CONFIG_JSON | {CONF_INPUT_UNIT_SYSTEM: UNIT_SYSTEM_METRIC},
TEST_CONFIG_JSON | {CONF_INPUT_UNIT_SYSTEM: UnitSystem.METRIC},
"payload_gw1000bpro_metric.json",
)
],
Expand Down Expand Up @@ -854,7 +854,7 @@ def test_unit_conversion_to_imperial(


@pytest.mark.parametrize(
"config", [TEST_CONFIG_JSON | {CONF_OUTPUT_UNIT_SYSTEM: UNIT_SYSTEM_METRIC}]
"config", [TEST_CONFIG_JSON | {CONF_OUTPUT_UNIT_SYSTEM: UnitSystem.METRIC}]
)
@pytest.mark.parametrize(
"device_data_filename,expected_output",
Expand Down
6 changes: 3 additions & 3 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
PRESSURE_HPA,
SPEED_KILOMETERS_PER_HOUR,
TEMP_CELSIUS,
UNIT_SYSTEM_IMPERIAL,
VOLUME_GRAMS_PER_CUBIC_METER,
UnitSystem,
)
from ecowitt2mqtt.helpers.calculator.battery import BatteryStrategy
from ecowitt2mqtt.helpers.server import InputDataFormat
Expand Down Expand Up @@ -181,15 +181,15 @@ def test_config_file(config_filepath: str) -> None:
assert configs.default_config.hass_discovery_prefix == TEST_HASS_DISCOVERY_PREFIX
assert configs.default_config.hass_entity_id_prefix is None
assert configs.default_config.input_data_format == InputDataFormat.ECOWITT
assert configs.default_config.input_unit_system == UNIT_SYSTEM_IMPERIAL
assert configs.default_config.input_unit_system == UnitSystem.IMPERIAL
assert configs.default_config.mqtt_broker == TEST_MQTT_BROKER
assert configs.default_config.mqtt_password == TEST_MQTT_PASSWORD
assert configs.default_config.mqtt_port == TEST_MQTT_PORT
assert configs.default_config.mqtt_retain is False
assert configs.default_config.mqtt_tls is False
assert configs.default_config.mqtt_topic == TEST_MQTT_TOPIC
assert configs.default_config.mqtt_username == TEST_MQTT_USERNAME
assert configs.default_config.output_unit_system == UNIT_SYSTEM_IMPERIAL
assert configs.default_config.output_unit_system == UnitSystem.IMPERIAL
assert configs.default_config.output_unit_accumulated_precipitation is None
assert configs.default_config.output_unit_distance is None
assert configs.default_config.output_unit_humidity is None
Expand Down