From 1d37d5a1aecd8e8766ba4b60b53a44eabf21a18e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A5le=20Stor=C3=B8=20Hauknes?= Date: Thu, 17 Oct 2024 13:43:19 +0200 Subject: [PATCH 1/3] Fix max value for pressure --- airthings_ble/const.py | 1 + airthings_ble/parser.py | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/airthings_ble/const.py b/airthings_ble/const.py index 151a925..2607b13 100644 --- a/airthings_ble/const.py +++ b/airthings_ble/const.py @@ -50,6 +50,7 @@ CO2_MAX = 65534 VOC_MAX = 65534 PERCENTAGE_MAX = 100 +PRESSURE_MAX = 1310 RADON_MAX = 16383 TEMPERATURE_MAX = 100 diff --git a/airthings_ble/parser.py b/airthings_ble/parser.py index e48a288..0e4a52a 100644 --- a/airthings_ble/parser.py +++ b/airthings_ble/parser.py @@ -44,6 +44,7 @@ LOW, MODERATE, PERCENTAGE_MAX, + PRESSURE_MAX, RADON_MAX, TEMPERATURE_MAX, UPDATE_TIMEOUT, @@ -142,7 +143,7 @@ def handler(raw_data: bytearray) -> dict[str, float | None | str]: data["temperature"] = validate_value( value=val[6] / 100.0, max_value=TEMPERATURE_MAX ) - data["pressure"] = float(val[7] / 50.0) + data["pressure"] = validate_value(val[7] / 50.0, max_value=PRESSURE_MAX) data["co2"] = validate_value(value=val[8] * 1.0, max_value=CO2_MAX) data["voc"] = validate_value(value=val[9] * 1.0, max_value=VOC_MAX) return data From 28ad8fab6c10ce1e50c8ecc97fc95af9b2527e09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A5le=20Stor=C3=B8=20Hauknes?= Date: Thu, 17 Oct 2024 13:43:35 +0200 Subject: [PATCH 2/3] Add tests --- tests/test_validate_value.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/test_validate_value.py b/tests/test_validate_value.py index 7b9fc3e..bfd9a06 100644 --- a/tests/test_validate_value.py +++ b/tests/test_validate_value.py @@ -1,4 +1,4 @@ -from airthings_ble.const import CO2_MAX, PERCENTAGE_MAX, RADON_MAX +from airthings_ble.const import CO2_MAX, PERCENTAGE_MAX, PRESSURE_MAX, RADON_MAX from airthings_ble.parser import validate_value, illuminance_converter @@ -36,3 +36,11 @@ def test_validate_value_illuminance(): assert illuminance_converter(0) == 0 assert illuminance_converter(255) == 100 assert illuminance_converter(256) == None + + +def test_validata_value_pressure(): + assert validate_value(value=0.0, max_value=PRESSURE_MAX) == 0 + assert validate_value(value=1310.0, max_value=PRESSURE_MAX) == 1310 + assert validate_value(value=1311.0, max_value=PRESSURE_MAX) is None + assert validate_value(value=-1.0, max_value=PRESSURE_MAX) is None + assert validate_value(value=65535.0, max_value=PRESSURE_MAX) is None From 094376b136f52637baa78fc360e0218bdc716308 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A5le=20Stor=C3=B8=20Hauknes?= Date: Thu, 17 Oct 2024 13:43:42 +0200 Subject: [PATCH 3/3] Fix old test --- tests/test_validate_value.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_validate_value.py b/tests/test_validate_value.py index bfd9a06..b6d2af3 100644 --- a/tests/test_validate_value.py +++ b/tests/test_validate_value.py @@ -35,7 +35,7 @@ def test_validate_value_co2(): def test_validate_value_illuminance(): assert illuminance_converter(0) == 0 assert illuminance_converter(255) == 100 - assert illuminance_converter(256) == None + assert illuminance_converter(256) is None def test_validata_value_pressure():