Skip to content

Commit

Permalink
Validate temperature (#18)
Browse files Browse the repository at this point in the history
* Add new max temperature constant

* Revert "Add new max temperature constant"

This reverts commit ac750e5.

* Add max temperature constant

* Add function to validate min and max values

* Add test for validating values

* Ignore coverage file

* Remove coverage file

* Simplify validator

* Adjust tests

* Add more tests

* Add tests to ci workflow

* Fix linting issues

* Fix linting issues

* Fix tests

* Remove duplicated temperature

* Remove duplicated radon

* Validate all temperatures

* Fix linting issue
  • Loading branch information
LaStrada authored Oct 26, 2023
1 parent 1bf3157 commit 3cb17e0
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 16 deletions.
Binary file removed .coverage
Binary file not shown.
4 changes: 3 additions & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ jobs:
- name: Install Dependencies
run: poetry install
- name: Run linters
run: poetry run make lint
run: poetry run make lint
- name: Run tests
run: poetry run pytest
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ MANIFEST
pip-log.txt
pip-delete-this-directory.txt


# pyenv
.python-version

Expand All @@ -40,3 +39,6 @@ pip-delete-this-directory.txt
# Misc
.vscode
.idea

# Tests
.coverage
1 change: 1 addition & 0 deletions airthings_ble/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
VOC_MAX = 65534
HUMIDITY_MAX = 100
RADON_MAX = 16383
TEMPERATURE_MAX = 100

DEVICE_TYPE = {
"2900": "Wave gen. 1",
Expand Down
41 changes: 27 additions & 14 deletions airthings_ble/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
LOW,
MODERATE,
RADON_MAX,
TEMPERATURE_MAX,
VERY_LOW,
VOC_MAX,
)
Expand Down Expand Up @@ -114,13 +115,15 @@ def handler(raw_data: bytearray) -> dict[str, float | None | str]:
val = vals[name]
data: dict[str, float | None | str] = {}
data["date_time"] = str(datetime.isoformat(datetime.now()))
data["humidity"] = val[1] / 2.0 if 0 <= val[1] / 2.0 <= HUMIDITY_MAX else None
data["radon_1day_avg"] = val[4] if 0 <= val[4] <= RADON_MAX else None
data["radon_longterm_avg"] = val[5] if 0 <= val[5] <= RADON_MAX else None
data["temperature"] = val[6] / 100.0
data["humidity"] = validate_value(value=val[1] / 2.0, max_value=HUMIDITY_MAX)
data["radon_1day_avg"] = validate_value(value=val[4], max_value=RADON_MAX)
data["radon_longterm_avg"] = validate_value(value=val[5], max_value=RADON_MAX)
data["temperature"] = validate_value(
value=val[6] / 100.0, max_value=TEMPERATURE_MAX
)
data["rel_atm_pressure"] = val[7] / 50.0
data["co2"] = val[8] * 1.0 if 0 <= val[8] * 1.0 <= CO2_MAX else None
data["voc"] = val[9] * 1.0 if 0 <= val[9] * 1.0 <= VOC_MAX else None
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

return handler
Expand All @@ -134,10 +137,12 @@ def handler(raw_data: bytearray) -> dict[str, float | None | str]:
val = vals[name]
data: dict[str, float | None | str] = {}
data["date_time"] = str(datetime.isoformat(datetime.now()))
data["humidity"] = val[1] / 2.0 if 0 <= val[1] / 2.0 <= HUMIDITY_MAX else None
data["radon_1day_avg"] = val[4] if 0 <= val[4] <= RADON_MAX else None
data["radon_longterm_avg"] = val[5] if 0 <= val[5] <= RADON_MAX else None
data["temperature"] = val[6] / 100.0
data["humidity"] = validate_value(value=val[1] / 2.0, max_value=HUMIDITY_MAX)
data["radon_1day_avg"] = validate_value(value=val[4], max_value=RADON_MAX)
data["radon_longterm_avg"] = validate_value(value=val[5], max_value=RADON_MAX)
data["temperature"] = validate_value(
value=val[6] / 100.0, max_value=TEMPERATURE_MAX
)
return data

return handler
Expand All @@ -151,11 +156,11 @@ def handler(raw_data: bytearray) -> dict[str, float | None | str]:
val = vals[name]
data: dict[str, float | None | str] = {}
data["date_time"] = str(datetime.isoformat(datetime.now()))
data["temperature"] = round(val[1] / 100.0 - 273.15, 2)
data["humidity"] = (
val[3] / 100.0 if 0 <= val[3] / 100.0 <= HUMIDITY_MAX else None
data["temperature"] = validate_value(
value=round(val[1] / 100.0 - 273.15, 2), max_value=TEMPERATURE_MAX
)
data["voc"] = val[4] * 1.0 if 0 <= val[4] * 1.0 <= VOC_MAX else None
data["humidity"] = validate_value(value=val[3] / 100.0, max_value=HUMIDITY_MAX)
data["voc"] = validate_value(value=val[4] * 1.0, max_value=VOC_MAX)
return data

return handler
Expand Down Expand Up @@ -198,6 +203,14 @@ def handler(raw_data: bytearray) -> dict[str, float | None | str]:
return handler


def validate_value(value: float, max_value: float) -> Optional[float]:
"""Validate if the given 'value' is within the specified range [min, max]"""
min_value = 0
if min_value <= value <= max_value:
return value
return None


# pylint: disable=too-few-public-methods
class CommandDecode:
"""Decoder for the command response"""
Expand Down
34 changes: 34 additions & 0 deletions tests/test_validate_value.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import pytest

from airthings_ble.parser import validate_value
from airthings_ble.const import CO2_MAX, HUMIDITY_MAX, RADON_MAX


def test_validate_value_humidity():
valid_humidity_values = [0, 50, 100.0]
for value in valid_humidity_values:
assert validate_value(value=value, max_value=HUMIDITY_MAX) == value

invalid_humidity_values = [-1, 100.1, 101]
for value in invalid_humidity_values:
assert validate_value(value=value, max_value=HUMIDITY_MAX) is None


def test_validate_value_radon():
valid_radon_values = [0, 100, 1000.0, 16383]
for value in valid_radon_values:
assert validate_value(value=value, max_value=RADON_MAX) == value

invalid_radon_values = [-1, 16384, 65535]
for value in invalid_radon_values:
assert validate_value(value=value, max_value=RADON_MAX) is None


def test_validate_value_co2():
valid_co2_values = [0, 100, 1000.0, 65534]
for value in valid_co2_values:
assert validate_value(value=value, max_value=CO2_MAX) == value

invalid_co2_values = [-1, 65535]
for value in invalid_co2_values:
assert validate_value(value=value, max_value=CO2_MAX) is None

0 comments on commit 3cb17e0

Please sign in to comment.