Skip to content

Commit

Permalink
Allow defining per-sensor offsets within the configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Apr 21, 2024
1 parent 9a29dd7 commit e4b0b69
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions ds18b20_datalogger/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions ds18b20_datalogger/datalogger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions ds18b20_datalogger/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Device:

name: str
path: str
offset: t.Union[float, None] = None


class DeviceMap:
Expand Down
11 changes: 11 additions & 0 deletions tests/datalogger-offset.yaml
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

0 comments on commit e4b0b69

Please sign in to comment.