diff --git a/CHANGES.md b/CHANGES.md index ecd343f..86137cf 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -7,6 +7,8 @@ @bee-mois. - Data Model: Make MQTT settings optional, to allow running the data logger without configuring them. +- Allow defining per-sensor offsets within the configuration + file. Thanks, @bee-mois. ## v0.0.3 - 2024-04-20 - Tests: Make sensor tests work, using a fake sysfs filesystem diff --git a/ds18b20_datalogger/core.py b/ds18b20_datalogger/core.py index a5ee853..a8db447 100755 --- a/ds18b20_datalogger/core.py +++ b/ds18b20_datalogger/core.py @@ -73,5 +73,7 @@ def read_ds18b20_sensor_matrix(devicemap: DeviceMap) -> Reading: reading = Reading() for device in devicemap.devices: value = read_temp(device.path) + if value is not None and device.offset is not None: + value += device.offset reading.add_measurement(name=device.name, value=value) return reading diff --git a/ds18b20_datalogger/datalogger.yaml b/ds18b20_datalogger/datalogger.yaml index bcb70a5..ae6f3be 100644 --- a/ds18b20_datalogger/datalogger.yaml +++ b/ds18b20_datalogger/datalogger.yaml @@ -19,13 +19,19 @@ mqtt: one-wire: - name: temp-ir-1-1 path: /sys/bus/w1/devices/28-0346d4430b06 + offset: null - name: temp-ir-1-2 path: /sys/bus/w1/devices/28-0cf3d443ba40 + offset: null - name: temp-ir-1-3 path: /sys/bus/w1/devices/28-0e49d44343bd + offset: null - name: temp-ir-2-1 path: /sys/bus/w1/devices/28-2231d443d266 + offset: null - name: temp-ir-2-2 path: /sys/bus/w1/devices/28-282bd4430f5e + offset: null - name: temp-ir-2-3 path: /sys/bus/w1/devices/28-2846d443e4f2 + offset: null diff --git a/ds18b20_datalogger/model.py b/ds18b20_datalogger/model.py index f3e89a1..b495e92 100644 --- a/ds18b20_datalogger/model.py +++ b/ds18b20_datalogger/model.py @@ -16,6 +16,7 @@ class Device: name: str path: str + offset: t.Union[float, None] = None class DeviceMap: diff --git a/tests/datalogger-offset.yaml b/tests/datalogger-offset.yaml new file mode 100644 index 0000000..e1289f4 --- /dev/null +++ b/tests/datalogger-offset.yaml @@ -0,0 +1,11 @@ +# Test configuration file for validating per-sensor offsets. + +one-wire: + - name: temp-ir-1-1 + path: /sys/bus/w1/devices/28-0346d4430b06 + offset: -0.5 + - name: temp-ir-1-2 + path: /sys/bus/w1/devices/28-0cf3d443ba40 + offset: null + - name: temp-ir-1-3 + path: /sys/bus/w1/devices/28-0e49d44343bd diff --git a/tests/test_core.py b/tests/test_core.py index 578f9e7..f632a9f 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -109,3 +109,18 @@ def test_sensors_none(settings): def test_telemetry(settings): reading = read_ds18b20_sensor_matrix(settings.devicemap) send_measurement_mqtt(settings.mqtt, reading) + + +@pytest.fixture +def offset_settings() -> Settings: + configfile = Path("tests") / "datalogger-offset.yaml" + return Settings.from_file(configfile) + + +def test_sensors_offset(offset_settings, fake_hardware_success): + reading = read_ds18b20_sensor_matrix(offset_settings.devicemap) + assert reading.to_dict() == { + "temp-ir-1-1": -0.499, + "temp-ir-1-2": 0.002, + "temp-ir-1-3": 0.003, + }