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

Provide config option for overriding speed units #346

Merged
merged 1 commit into from
Oct 16, 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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,19 @@ Config Option: `--output-unit-pressure`
* `Pa`
* `psi`

#### Speed

Config Option: `--output-unit-speed`

* `ft/s`
* `in/d`
* `in/h`
* `km/h`
* `kn`
* `m/s`
* `mph`
* `mm/d`

#### Temperature

Config Option: `--output-unit-temperature`
Expand Down
12 changes: 12 additions & 0 deletions ecowitt2mqtt/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
CONF_OUTPUT_UNIT_ILLUMINANCE,
CONF_OUTPUT_UNIT_PRECIPITATION_RATE,
CONF_OUTPUT_UNIT_PRESSURE,
CONF_OUTPUT_UNIT_SPEED,
CONF_OUTPUT_UNIT_SYSTEM,
CONF_OUTPUT_UNIT_TEMPERATURE,
CONF_PORT,
Expand Down Expand Up @@ -65,6 +66,7 @@
ENV_OUTPUT_UNIT_ILLUMINANCE,
ENV_OUTPUT_UNIT_PRECIPITATION_RATE,
ENV_OUTPUT_UNIT_PRESSURE,
ENV_OUTPUT_UNIT_SPEED,
ENV_OUTPUT_UNIT_SYSTEM,
ENV_OUTPUT_UNIT_TEMPERATURE,
ENV_PORT,
Expand Down Expand Up @@ -134,6 +136,7 @@
ENV_OUTPUT_UNIT_ILLUMINANCE: CONF_OUTPUT_UNIT_ILLUMINANCE,
ENV_OUTPUT_UNIT_PRECIPITATION_RATE: CONF_OUTPUT_UNIT_PRECIPITATION_RATE,
ENV_OUTPUT_UNIT_PRESSURE: CONF_OUTPUT_UNIT_PRESSURE,
ENV_OUTPUT_UNIT_SPEED: CONF_OUTPUT_UNIT_SPEED,
ENV_OUTPUT_UNIT_SYSTEM: CONF_OUTPUT_UNIT_SYSTEM,
ENV_OUTPUT_UNIT_TEMPERATURE: CONF_OUTPUT_UNIT_TEMPERATURE,
ENV_PORT: CONF_PORT,
Expand Down Expand Up @@ -390,6 +393,15 @@ def get_cli_arguments(args: list[str]) -> dict[str, Any]:
),
metavar=CONF_OUTPUT_UNIT_PRESSURE,
)
parser.add_argument(
"--output-unit-speed",
dest=CONF_OUTPUT_UNIT_SPEED,
help=(
"The output unit to use for speed data points "
"(default: the default used by the output unit system)"
),
metavar=CONF_OUTPUT_UNIT_SPEED,
)
parser.add_argument(
"--output-unit-temperature",
dest=CONF_OUTPUT_UNIT_TEMPERATURE,
Expand Down
10 changes: 10 additions & 0 deletions ecowitt2mqtt/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
CONF_OUTPUT_UNIT_ILLUMINANCE,
CONF_OUTPUT_UNIT_PRECIPITATION_RATE,
CONF_OUTPUT_UNIT_PRESSURE,
CONF_OUTPUT_UNIT_SPEED,
CONF_OUTPUT_UNIT_SYSTEM,
CONF_OUTPUT_UNIT_TEMPERATURE,
CONF_PORT,
Expand All @@ -54,6 +55,7 @@
IlluminanceConverter,
PrecipitationRateConverter,
PressureConverter,
SpeedConverter,
TemperatureConverter,
VolumeConverter,
)
Expand Down Expand Up @@ -124,6 +126,9 @@
vol.Optional(CONF_OUTPUT_UNIT_PRESSURE): vol.All(
str, vol.In(PressureConverter.VALID_UNITS)
),
vol.Optional(CONF_OUTPUT_UNIT_SPEED): vol.All(
str, vol.In(SpeedConverter.VALID_UNITS)
),
vol.Optional(CONF_OUTPUT_UNIT_TEMPERATURE): vol.All(
str, vol.In(TemperatureConverter.VALID_UNITS)
),
Expand Down Expand Up @@ -302,6 +307,11 @@ def output_unit_pressure(self) -> str | None:
"""Return the output unit for pressure."""
return self._config.get(CONF_OUTPUT_UNIT_PRESSURE)

@property
def output_unit_speed(self) -> str | None:
"""Return the output unit for speed."""
return self._config.get(CONF_OUTPUT_UNIT_SPEED)

@property
def output_unit_temperature(self) -> str | None:
"""Return the output unit for temperature."""
Expand Down
2 changes: 2 additions & 0 deletions ecowitt2mqtt/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
CONF_OUTPUT_UNIT_ILLUMINANCE: Final = "output_unit_illuminance"
CONF_OUTPUT_UNIT_PRECIPITATION_RATE: Final = "output_unit_precipitation_rate"
CONF_OUTPUT_UNIT_PRESSURE: Final = "output_unit_pressure"
CONF_OUTPUT_UNIT_SPEED: Final = "output_unit_speed"
CONF_OUTPUT_UNIT_SYSTEM: Final = "output_unit_system"
CONF_OUTPUT_UNIT_TEMPERATURE: Final = "output_unit_temperature"
CONF_PORT: Final = "port"
Expand Down Expand Up @@ -158,6 +159,7 @@
"ECOWITT2MQTT_OUTPUT_UNIT_PRECIPITATION_RATE"
)
ENV_OUTPUT_UNIT_PRESSURE: Final = "ECOWITT2MQTT_OUTPUT_UNIT_PRESSURE"
ENV_OUTPUT_UNIT_SPEED: Final = "ECOWITT2MQTT_OUTPUT_UNIT_SPEED"
ENV_OUTPUT_UNIT_SYSTEM: Final = "ECOWITT2MQTT_OUTPUT_UNIT_SYSTEM"
ENV_OUTPUT_UNIT_TEMPERATURE: Final = "ECOWITT2MQTT_OUTPUT_UNIT_TEMPERATURE"
ENV_PORT: Final = "ECOWITT2MQTT_PORT"
Expand Down
2 changes: 2 additions & 0 deletions ecowitt2mqtt/helpers/calculator/wind.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import cast

from ecowitt2mqtt.const import (
CONF_OUTPUT_UNIT_SPEED,
DATA_POINT_WINDSPEED,
DEGREE,
SPEED_KILOMETERS_PER_HOUR,
Expand Down Expand Up @@ -264,6 +265,7 @@ class WindSpeedCalculator(Calculator):
"""Define a wind speed calculator."""

DEFAULT_INPUT_UNIT = SPEED_MILES_PER_HOUR
UNIT_OVERRIDE_CONFIG_OPTION = CONF_OUTPUT_UNIT_SPEED

@property
def output_unit_imperial(self) -> str:
Expand Down
4 changes: 4 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
CONF_OUTPUT_UNIT_ILLUMINANCE,
CONF_OUTPUT_UNIT_PRECIPITATION_RATE,
CONF_OUTPUT_UNIT_PRESSURE,
CONF_OUTPUT_UNIT_SPEED,
CONF_OUTPUT_UNIT_TEMPERATURE,
CONF_VERBOSE,
ENV_BATTERY_OVERRIDES,
Expand All @@ -26,6 +27,7 @@
PRECIPITATION_MILLIMETERS,
PRECIPITATION_MILLIMETERS_PER_HOUR,
PRESSURE_HPA,
SPEED_KILOMETERS_PER_HOUR,
TEMP_CELSIUS,
VOLUME_GRAMS_PER_CUBIC_METER,
)
Expand Down Expand Up @@ -251,6 +253,7 @@ def test_invalid_boolean_config_validation(config):
(CONF_OUTPUT_UNIT_ILLUMINANCE, ILLUMINANCE_LUX),
(CONF_OUTPUT_UNIT_PRECIPITATION_RATE, PRECIPITATION_MILLIMETERS_PER_HOUR),
(CONF_OUTPUT_UNIT_PRESSURE, PRESSURE_HPA),
(CONF_OUTPUT_UNIT_SPEED, SPEED_KILOMETERS_PER_HOUR),
(CONF_OUTPUT_UNIT_TEMPERATURE, TEMP_CELSIUS),
],
)
Expand All @@ -270,6 +273,7 @@ def test_output_units(config_option, value):
CONF_OUTPUT_UNIT_ILLUMINANCE,
CONF_OUTPUT_UNIT_PRECIPITATION_RATE,
CONF_OUTPUT_UNIT_PRESSURE,
CONF_OUTPUT_UNIT_SPEED,
CONF_OUTPUT_UNIT_TEMPERATURE,
],
)
Expand Down
14 changes: 8 additions & 6 deletions tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
CONF_OUTPUT_UNIT_ILLUMINANCE,
CONF_OUTPUT_UNIT_PRECIPITATION_RATE,
CONF_OUTPUT_UNIT_PRESSURE,
CONF_OUTPUT_UNIT_SPEED,
CONF_OUTPUT_UNIT_SYSTEM,
CONF_OUTPUT_UNIT_TEMPERATURE,
DEGREE,
Expand Down Expand Up @@ -1755,6 +1756,7 @@ def test_nonnumeric_value(device_data, ecowitt):
CONF_OUTPUT_UNIT_ILLUMINANCE: ILLUMINANCE_LUX,
CONF_OUTPUT_UNIT_PRECIPITATION_RATE: PRECIPITATION_MILLIMETERS_PER_HOUR,
CONF_OUTPUT_UNIT_PRESSURE: PRESSURE_HPA,
CONF_OUTPUT_UNIT_SPEED: SPEED_KILOMETERS_PER_HOUR,
CONF_OUTPUT_UNIT_TEMPERATURE: TEMP_CELSIUS,
}
],
Expand Down Expand Up @@ -1821,22 +1823,22 @@ def test_output_units(device_data, ecowitt):
),
"windspeed": CalculatedDataPoint(
"wind",
20.89,
unit=SPEED_MILES_PER_HOUR,
33.619196159999994,
unit=SPEED_KILOMETERS_PER_HOUR,
attributes={},
data_type=DataPointType.NON_BOOLEAN,
),
"windgust": CalculatedDataPoint(
"gust",
1.12,
unit=SPEED_MILES_PER_HOUR,
1.8024652800000003,
unit=SPEED_KILOMETERS_PER_HOUR,
attributes={},
data_type=DataPointType.NON_BOOLEAN,
),
"maxdailygust": CalculatedDataPoint(
"gust",
8.05,
unit=SPEED_MILES_PER_HOUR,
12.9552192,
unit=SPEED_KILOMETERS_PER_HOUR,
attributes={},
data_type=DataPointType.NON_BOOLEAN,
),
Expand Down