Skip to content

Commit 95bfb54

Browse files
committed
fix(bme280): Use single read() in dew_point and clamp RH to avoid log(0).
1 parent fe4d515 commit 95bfb54

2 files changed

Lines changed: 7 additions & 9 deletions

File tree

lib/bme280/bme280/device.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,11 +414,12 @@ def dew_point(self):
414414
"""Return dew point temperature in degrees Celsius.
415415
416416
Uses the Magnus formula (Alduchov & Eskridge, 1996) with the
417-
current temperature and relative humidity readings.
417+
current temperature and relative humidity readings. Both values
418+
come from a single ``read()`` call to ensure consistency.
418419
"""
419420
from math import log
420421

421-
t = self.temperature()
422-
rh = self.humidity()
422+
t, _, rh = self.read()
423+
rh = max(rh, 0.01)
423424
gamma = log(rh / 100.0) + 17.625 * t / (243.04 + t)
424425
return 243.04 * gamma / (17.625 - gamma)

tests/scenarios/bme280.yaml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -642,15 +642,12 @@ tests:
642642
expect_true: true
643643
mode: [mock]
644644

645-
- name: "dew_point() equals temperature at 100% humidity"
645+
- name: "dew_point() matches Magnus formula for mock readings"
646646
action: script
647647
script: |
648-
# Override humidity compensation to return ~100%RH
649-
# At 100% RH, dew point should equal temperature
650648
from math import log
651-
t = dev.temperature()
652-
rh = dev.humidity()
653-
# Compute expected dew point from actual mock values
649+
t, _, rh = dev.read()
650+
# Compute expected dew point from Magnus formula
654651
gamma = log(rh / 100.0) + 17.625 * t / (243.04 + t)
655652
expected = 243.04 * gamma / (17.625 - gamma)
656653
dp = dev.dew_point()

0 commit comments

Comments
 (0)