Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/bme280/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,16 @@ sensor.sea_level_pressure_hpa = 1020.0

---

### Dew Point

```python
dp = sensor.dew_point()
```

Returns the dew point temperature in **degrees Celsius**, computed from the current temperature and humidity using the Magnus formula.

---

## Data-Ready Status

```python
Expand Down Expand Up @@ -313,7 +323,7 @@ Performs a soft reset, re-reads calibration data, and re-applies default configu
| Standby time | ✅ | ❌ | ✅ | ❌ | ⚠️ Fixed 500ms | ❌ |
| Altitude | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ |
| Sea-level pressure | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ |
| Dew point | | ✅ | ❌ | ❌ | ❌ | ❌ |
| Dew point | | ✅ | ❌ | ❌ | ❌ | ❌ |
| Soft reset | ✅ | ❌ | ❌ | ❌ | ✅ | ❌ |
| Full reset + recalibration | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ |
| power_off / power_on | ✅ | ❌ | ⚠️ Via mode | ❌ | ❌ | ❌ |
Expand Down
14 changes: 14 additions & 0 deletions lib/bme280/bme280/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,3 +409,17 @@ def altitude(self, pressure_hpa=None):
"""
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

altitude() divides by self.sea_level_pressure without validating it. Since this is a public attribute, it’s easy for users/tests to set it to 0 or a negative value and trigger a ZeroDivisionError or complex results. Consider validating sea_level_pressure > 0 (e.g., raise a clear exception) before computing altitude.

Suggested change
"""
"""
# Validate sea_level_pressure before using it as a divisor to avoid
# ZeroDivisionError or invalid results if it has been set to a
# non-positive value by user code.
if not hasattr(self, "sea_level_pressure") or self.sea_level_pressure <= 0:
raise ValueError(
"sea_level_pressure must be a positive value for altitude calculation"
)

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not addressed: same rationale as PR #321 — no validation on internal attributes per project convention. See discussion on PR #321.

p = self.pressure_hpa() if pressure_hpa is None else pressure_hpa
return 44330.0 * (1.0 - (p / self.sea_level_pressure_hpa) ** 0.1903)

def dew_point(self):
"""Return dew point temperature in degrees Celsius.

Uses the Magnus formula (Alduchov & Eskridge, 1996) with the
current temperature and relative humidity readings. Both values
come from a single ``read()`` call to ensure consistency.
"""
from math import log

t, _, rh = self.read()
rh = max(rh, 0.01)
gamma = log(rh / 100.0) + 17.625 * t / (243.04 + t)
return 243.04 * gamma / (17.625 - gamma)
24 changes: 24 additions & 0 deletions tests/scenarios/bme280.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -630,3 +630,27 @@ tests:
result = abs(alt - 33.7) < 1.0
expect_true: true
mode: [mock]

# ----- Dew point -----

- name: "dew_point() returns plausible value"
action: script
script: |
# Mock: ~25.08°C, ~50.57%RH → dew point ~14.11°C
dp = dev.dew_point()
result = abs(dp - 14.11) < 0.5
expect_true: true
mode: [mock]

- name: "dew_point() matches Magnus formula for mock readings"
action: script
script: |
from math import log
t, _, rh = dev.read()
# Compute expected dew point from Magnus formula
gamma = log(rh / 100.0) + 17.625 * t / (243.04 + t)
expected = 243.04 * gamma / (17.625 - gamma)
dp = dev.dew_point()
result = abs(dp - expected) < 0.01
expect_true: true
mode: [mock]
Loading