From abcb571882f3f3fec8757dae67390b2c0e287ccd Mon Sep 17 00:00:00 2001 From: Lennart Weller Date: Fri, 27 Dec 2024 19:06:00 +0000 Subject: [PATCH 1/4] feat: remove conversions to imperial --- aiocloudweather/conversion.py | 36 +--------------- aiocloudweather/station.py | 77 +++++++---------------------------- tests/test_sensor.py | 24 +++++------ 3 files changed, 27 insertions(+), 110 deletions(-) diff --git a/aiocloudweather/conversion.py b/aiocloudweather/conversion.py index be789f4..21f65bd 100644 --- a/aiocloudweather/conversion.py +++ b/aiocloudweather/conversion.py @@ -52,38 +52,4 @@ def lux_to_wm2(lux: float) -> float: @unit(UnitOfSpeed.METERS_PER_SECOND) def mph_to_ms(speed: float) -> float: """Convert miles per hour (mph) to meters per second (m/s).""" - return speed * 0.44704 - - -@unit(UnitOfPressure.INHG) -def hpa_to_inhg(pressure: float) -> float: - """Convert hectopascals (hPa) to inches of mercury (inHg).""" - return pressure * 0.02953 - - -@unit(UnitOfTemperature.FAHRENHEIT) -def celsius_to_fahrenheit(temp_c: float) -> float: - """Convert Celsius to Fahrenheit.""" - return temp_c * 9.0 / 5.0 + 32 - - -@unit(UnitOfPrecipitationDepth.INCHES) -def mm_to_in(length: float) -> float: - """Convert millimeters (mm) to inches.""" - return length * 0.0393701 - - -@unit(LIGHT_LUX) -def wm2_to_lux(lux: float) -> float: - """Convert watts per square meter (W/m²) to lux. - For natural daylight, the luminous efficacy varies - but is typically in the range of 90 to 120 lm/W. - A commonly used average value is around 93 lm/W - """ - return lux * 93 - - -@unit(UnitOfSpeed.MILES_PER_HOUR) -def ms_to_mph(speed: float) -> float: - """Convert meters per second (m/s) to miles per hour (mph).""" - return speed * 2.23694 + return speed * 0.44704 \ No newline at end of file diff --git a/aiocloudweather/station.py b/aiocloudweather/station.py index c07c240..449c5dc 100644 --- a/aiocloudweather/station.py +++ b/aiocloudweather/station.py @@ -187,7 +187,6 @@ class WeathercloudRawSensor: default=None, metadata={ "unit": UnitOfIrradiance.WATTS_PER_SQUARE_METER, - "keep": True, "arg": "solarrad", }, ) @@ -202,26 +201,14 @@ class WeathercloudRawSensor: UnitOfSpeed.MILES_PER_HOUR: mph_to_ms, } -METRIC_TO_IMPERIAL: Final = { - UnitOfPressure.HPA: hpa_to_inhg, - UnitOfTemperature.CELSIUS: celsius_to_fahrenheit, - UnitOfPrecipitationDepth.MILLIMETERS: mm_to_in, - UnitOfIrradiance.WATTS_PER_SQUARE_METER: wm2_to_lux, - UnitOfSpeed.METERS_PER_SECOND: ms_to_mph, -} - - @dataclass class Sensor: """Represents a weather sensor.""" name: str - metric: float - metric_unit: str - imperial: float - imperial_unit: str - + value: float + unit: str @dataclass class WeatherStation: @@ -305,7 +292,6 @@ def from_wunderground(data: WundergroundRawSensor) -> "WeatherStation": value = sensor_field.type(value) * sensor_field.metadata.get("factor", 1) unit = sensor_field.metadata.get("unit") - keep_original = sensor_field.metadata.get("keep", False) conversion_func = IMPERIAL_TO_METRIC.get(unit) if conversion_func: @@ -324,23 +310,14 @@ def from_wunderground(data: WundergroundRawSensor) -> "WeatherStation": continue sensor_data[sensor_field.name] = Sensor( name=sensor_field.name, - metric=converted_value, - metric_unit=conversion_func.unit, - imperial=value, - imperial_unit=unit, - ) - if not conversion_func or keep_original: - field_name = ( - sensor_field.name - if not keep_original - else f"{sensor_field.name}raw" + value=converted_value, + unit=conversion_func.unit, ) - sensor_data[field_name] = Sensor( - name=field_name, - metric=value, - metric_unit=unit, - imperial=value, - imperial_unit=unit, + else: + sensor_data[sensor_field.name] = Sensor( + name=sensor_field.name, + value=value, + unit=unit, ) return WeatherStation( station_id=data.station_id, @@ -374,38 +351,12 @@ def from_weathercloud(data: WeathercloudRawSensor) -> "WeatherStation": value = sensor_field.type(value) # No idea why this is needed unit = sensor_field.metadata.get("unit") - conversion_func = METRIC_TO_IMPERIAL.get(unit) - if conversion_func: - value: float = float(value) / 10 # All values are shifted by 10 - try: - converted_value = conversion_func(value) - except TypeError as e: - _LOGGER.error( - "Failed to convert %s from %s to %s: %s -> %s", - sensor_field, - unit, - conversion_func.unit, - value, - e, - ) - continue - converted_value = conversion_func(value) - sensor_data[sensor_field.name] = Sensor( - name=sensor_field.name, - metric=float(value), - metric_unit=unit, - imperial=converted_value, - imperial_unit=conversion_func.unit, - ) - else: - sensor_data[sensor_field.name] = Sensor( - name=sensor_field.name, - metric=value, - metric_unit=unit, - imperial=value, - imperial_unit=unit, - ) + sensor_data[sensor_field.name] = Sensor( + name=sensor_field.name, + value=value, + unit=unit, + ) return WeatherStation( station_id=str(data.station_id), diff --git a/tests/test_sensor.py b/tests/test_sensor.py index c676910..be275f7 100644 --- a/tests/test_sensor.py +++ b/tests/test_sensor.py @@ -25,16 +25,16 @@ def test_weather_station_from_wunderground(): assert weather_station.station_id == "12345" assert weather_station.station_key == "12345" - assert round(weather_station.barometer.metric, 2) == 1013.21 - assert weather_station.barometer.metric_unit == "hPa" + assert round(weather_station.barometer.value, 2) == 1013.21 + assert weather_station.barometer.unit == "hPa" assert weather_station.barometer.imperial == 29.92 assert weather_station.barometer.imperial_unit == "inHg" - assert weather_station.temperature.metric == 22.5 - assert weather_station.temperature.metric_unit == "°C" + assert weather_station.temperature.value == 22.5 + assert weather_station.temperature.unit == "°C" assert weather_station.temperature.imperial == 72.5 assert weather_station.temperature.imperial_unit == "°F" - assert weather_station.humidity.metric == 44 - assert weather_station.humidity.metric_unit == "%" + assert weather_station.humidity.value == 44 + assert weather_station.humidity.unit == "%" assert weather_station.humidity.imperial == 44 assert weather_station.humidity.imperial_unit == "%" @@ -59,15 +59,15 @@ def test_weather_station_from_weathercloud(): assert weather_station.station_id == "12345" assert weather_station.station_key == "12345" - assert weather_station.barometer.metric == 1013 - assert weather_station.barometer.metric_unit == "hPa" + assert weather_station.barometer.value == 1013 + assert weather_station.barometer.unit == "hPa" assert round(weather_station.barometer.imperial, 2) == 29.91 assert weather_station.barometer.imperial_unit == "inHg" - assert weather_station.temperature.metric == 16 - assert weather_station.temperature.metric_unit == "°C" + assert weather_station.temperature.value == 16 + assert weather_station.temperature.unit == "°C" assert weather_station.temperature.imperial == 60.8 assert weather_station.temperature.imperial_unit == "°F" - assert weather_station.humidity.metric == 80 - assert weather_station.humidity.metric_unit == "%" + assert weather_station.humidity.value == 80 + assert weather_station.humidity.unit == "%" assert weather_station.humidity.imperial == 80 assert weather_station.humidity.imperial_unit == "%" From 33506ac68efc1ae3e32076103f8bc7262da090b7 Mon Sep 17 00:00:00 2001 From: Lennart Weller Date: Fri, 27 Dec 2024 19:41:24 +0000 Subject: [PATCH 2/4] fixed conversions/test --- aiocloudweather/conversion.py | 3 +-- aiocloudweather/station.py | 9 ++++----- tests/test_conversion.py | 35 ----------------------------------- tests/test_sensor.py | 12 ------------ 4 files changed, 5 insertions(+), 54 deletions(-) diff --git a/aiocloudweather/conversion.py b/aiocloudweather/conversion.py index 21f65bd..ffa6b0d 100644 --- a/aiocloudweather/conversion.py +++ b/aiocloudweather/conversion.py @@ -1,7 +1,6 @@ """ A list of all the unit conversions. Many are just approximations.""" from .const import ( - LIGHT_LUX, UnitOfIrradiance, UnitOfPrecipitationDepth, UnitOfPressure, @@ -52,4 +51,4 @@ def lux_to_wm2(lux: float) -> float: @unit(UnitOfSpeed.METERS_PER_SECOND) def mph_to_ms(speed: float) -> float: """Convert miles per hour (mph) to meters per second (m/s).""" - return speed * 0.44704 \ No newline at end of file + return speed * 0.44704 diff --git a/aiocloudweather/station.py b/aiocloudweather/station.py index 449c5dc..fdd028c 100644 --- a/aiocloudweather/station.py +++ b/aiocloudweather/station.py @@ -6,16 +6,11 @@ from typing import Final from aiocloudweather.conversion import ( - celsius_to_fahrenheit, fahrenheit_to_celsius, - hpa_to_inhg, in_to_mm, inhg_to_hpa, lux_to_wm2, - mm_to_in, mph_to_ms, - ms_to_mph, - wm2_to_lux, ) from .const import ( DEGREE, @@ -201,6 +196,7 @@ class WeathercloudRawSensor: UnitOfSpeed.MILES_PER_HOUR: mph_to_ms, } + @dataclass class Sensor: """Represents a weather sensor.""" @@ -210,6 +206,7 @@ class Sensor: value: float unit: str + @dataclass class WeatherStation: """ @@ -351,6 +348,8 @@ def from_weathercloud(data: WeathercloudRawSensor) -> "WeatherStation": value = sensor_field.type(value) # No idea why this is needed unit = sensor_field.metadata.get("unit") + if unit != PERCENTAGE: + value: float = float(value) / 10 # All values are shifted by 10 sensor_data[sensor_field.name] = Sensor( name=sensor_field.name, diff --git a/tests/test_conversion.py b/tests/test_conversion.py index c3f109f..3c5bbf4 100644 --- a/tests/test_conversion.py +++ b/tests/test_conversion.py @@ -1,14 +1,9 @@ from aiocloudweather.conversion import ( - celsius_to_fahrenheit, fahrenheit_to_celsius, - hpa_to_inhg, in_to_mm, inhg_to_hpa, lux_to_wm2, - mm_to_in, mph_to_ms, - ms_to_mph, - wm2_to_lux, ) @@ -40,33 +35,3 @@ def test_mph_to_ms(): assert round(mph_to_ms(10), 4) == 4.4704 assert round(mph_to_ms(30), 4) == 13.4112 assert round(mph_to_ms(5), 4) == 2.2352 - - -def test_hpa_to_inhg(): - assert round(hpa_to_inhg(1013.25), 2) == 29.92 - assert round(hpa_to_inhg(1000), 2) == 29.53 - assert round(hpa_to_inhg(950), 2) == 28.05 - - -def test_celsius_to_fahrenheit(): - assert round(celsius_to_fahrenheit(0)) == 32 - assert round(celsius_to_fahrenheit(100)) == 212 - assert round(celsius_to_fahrenheit(10)) == 50 - - -def test_mm_to_in(): - assert round(mm_to_in(25.4), 2) == 1 - assert round(mm_to_in(63.5), 2) == 2.5 - assert round(mm_to_in(12.7), 2) == 0.5 - - -def test_wm2_to_lux(): - assert round(wm2_to_lux(1)) == 93 - assert round(wm2_to_lux(5)) == 465 - assert round(wm2_to_lux(10)) == 930 - - -def test_ms_to_mph(): - assert round(ms_to_mph(4.4704), 4) == 10 - assert round(ms_to_mph(13.4112), 4) == 30 - assert round(ms_to_mph(2.2352), 4) == 5 diff --git a/tests/test_sensor.py b/tests/test_sensor.py index be275f7..9dcf7d2 100644 --- a/tests/test_sensor.py +++ b/tests/test_sensor.py @@ -27,16 +27,10 @@ def test_weather_station_from_wunderground(): assert weather_station.station_key == "12345" assert round(weather_station.barometer.value, 2) == 1013.21 assert weather_station.barometer.unit == "hPa" - assert weather_station.barometer.imperial == 29.92 - assert weather_station.barometer.imperial_unit == "inHg" assert weather_station.temperature.value == 22.5 assert weather_station.temperature.unit == "°C" - assert weather_station.temperature.imperial == 72.5 - assert weather_station.temperature.imperial_unit == "°F" assert weather_station.humidity.value == 44 assert weather_station.humidity.unit == "%" - assert weather_station.humidity.imperial == 44 - assert weather_station.humidity.imperial_unit == "%" def test_weather_station_from_weathercloud(): @@ -61,13 +55,7 @@ def test_weather_station_from_weathercloud(): assert weather_station.station_key == "12345" assert weather_station.barometer.value == 1013 assert weather_station.barometer.unit == "hPa" - assert round(weather_station.barometer.imperial, 2) == 29.91 - assert weather_station.barometer.imperial_unit == "inHg" assert weather_station.temperature.value == 16 assert weather_station.temperature.unit == "°C" - assert weather_station.temperature.imperial == 60.8 - assert weather_station.temperature.imperial_unit == "°F" assert weather_station.humidity.value == 80 assert weather_station.humidity.unit == "%" - assert weather_station.humidity.imperial == 80 - assert weather_station.humidity.imperial_unit == "%" From e329024ff0a9a8d7a9d27c2483ddc8b6954f2dad Mon Sep 17 00:00:00 2001 From: Lennart Weller Date: Fri, 27 Dec 2024 20:05:11 +0000 Subject: [PATCH 3/4] at least check for these --- tests/test_sensor.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/test_sensor.py b/tests/test_sensor.py index 9dcf7d2..48add39 100644 --- a/tests/test_sensor.py +++ b/tests/test_sensor.py @@ -41,8 +41,8 @@ def test_weather_station_from_weathercloud(): temperature=160, humidity=80, dewpoint=129, - rain=0, - dailyrain=0, + rain=109, + dailyrain=25, winddirection=288, windspeed=0, windgustspeed=0, @@ -59,3 +59,7 @@ def test_weather_station_from_weathercloud(): assert weather_station.temperature.unit == "°C" assert weather_station.humidity.value == 80 assert weather_station.humidity.unit == "%" + assert weather_station.rain.value == 10.9 + assert weather_station.rain.unit == "mm/h" + assert weather_station.dailyrain.value == 2.5 + assert weather_station.dailyrain.unit == "mm" \ No newline at end of file From fcc2a386c692750c2c5bc9fcc5f34609ee71c890 Mon Sep 17 00:00:00 2001 From: Lennart Weller Date: Fri, 27 Dec 2024 20:15:28 +0000 Subject: [PATCH 4/4] newline --- tests/test_sensor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_sensor.py b/tests/test_sensor.py index 48add39..9cd3bb7 100644 --- a/tests/test_sensor.py +++ b/tests/test_sensor.py @@ -62,4 +62,4 @@ def test_weather_station_from_weathercloud(): assert weather_station.rain.value == 10.9 assert weather_station.rain.unit == "mm/h" assert weather_station.dailyrain.value == 2.5 - assert weather_station.dailyrain.unit == "mm" \ No newline at end of file + assert weather_station.dailyrain.unit == "mm"