feat(bme280): Add measurement modes and sensor configuration#313
feat(bme280): Add measurement modes and sensor configuration#313
Conversation
There was a problem hiding this comment.
Pull request overview
Adds configuration/mode-control APIs to the BME280 driver to support sleep/normal operation and tuning measurement behavior (oversampling, IIR filter, standby), with accompanying mock scenario coverage.
Changes:
- Add power/mode helpers:
power_off(),power_on(), andset_continuous(standby=None) - Add configuration helpers:
set_oversampling(...),set_iir_filter(...),set_standby(...) - Extend
tests/scenarios/bme280.yamlwith mock scenarios for mode/config register writes and bit preservation
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| lib/bme280/bme280/device.py | Introduces new public APIs for power/mode control and sensor configuration via register RMW. |
| tests/scenarios/bme280.yaml | Adds mock scenarios asserting expected register writes for the new APIs. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if humidity is not None: | ||
| self._write_reg(REG_CTRL_HUM, humidity) | ||
| if temperature is not None or pressure is not None: |
There was a problem hiding this comment.
set_oversampling() writes REG_CTRL_HUM when humidity is provided, but if only humidity is changed (temperature/pressure are None) it never writes REG_CTRL_MEAS. On BME280, ctrl_hum updates take effect only after a subsequent write to ctrl_meas (your own _configure_default() comment notes this), so humidity-only changes won't actually apply. Fix by performing a (read-modify-)write to REG_CTRL_MEAS after writing REG_CTRL_HUM even when temperature/pressure are unchanged (preserving the existing mode/oversampling bits).
| if humidity is not None: | |
| self._write_reg(REG_CTRL_HUM, humidity) | |
| if temperature is not None or pressure is not None: | |
| update_ctrl_meas = False | |
| if humidity is not None: | |
| # On BME280, changes to ctrl_hum take effect only after | |
| # a subsequent write to ctrl_meas. | |
| self._write_reg(REG_CTRL_HUM, humidity) | |
| update_ctrl_meas = True | |
| if temperature is not None or pressure is not None or update_ctrl_meas: |
There was a problem hiding this comment.
Already fixed in f63099a: set_oversampling() now always rewrites ctrl_meas after ctrl_hum, even when only humidity is changed. The comment in the code references datasheet section 5.4.3.
| hum_writes = [data[0] for reg, data in log if reg == 0xF2] | ||
| result = len(hum_writes) == 1 and hum_writes[0] == OSRS_X8 |
There was a problem hiding this comment.
This test asserts set_oversampling(humidity=...) only writes REG_CTRL_HUM (0xF2). However, BME280 latches ctrl_hum changes only after a write to REG_CTRL_MEAS (0xF4), so the correct behavior should include a REG_CTRL_MEAS write as well. Update the expectation to also require (at least) one write to 0xF4 following the 0xF2 write.
| hum_writes = [data[0] for reg, data in log if reg == 0xF2] | |
| result = len(hum_writes) == 1 and hum_writes[0] == OSRS_X8 | |
| hum_index = None | |
| for idx, (reg, data) in enumerate(log): | |
| if reg == 0xF2 and data[0] == OSRS_X8: | |
| hum_index = idx | |
| break | |
| if hum_index is None: | |
| result = False | |
| else: | |
| wrote_ctrl_after = any(reg == 0xF4 for reg, data in log[hum_index + 1:]) | |
| result = wrote_ctrl_after |
There was a problem hiding this comment.
Already fixed in f63099a: the test now verifies that both ctrl_hum (0xF2) and ctrl_meas (0xF4) are written, and that 0xF2 comes before 0xF4 in the write log.
# [0.6.0](v0.5.0...v0.6.0) (2026-03-29) ### Features * **bme280:** Add measurement modes and sensor configuration ([#313](#313)) ([2af3fd9](2af3fd9))
|
🎉 This PR is included in version 0.6.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
Closes #306
Summary
power_off()(sleep mode),power_on()(normal mode)set_continuous(standby=None)— enters normal mode with optional standby timeset_oversampling(temperature=, pressure=, humidity=)— per-channel config viaOSRS_SKIP..OSRS_X16set_iir_filter(coefficient)—FILTER_OFF..FILTER_16set_standby(standby)—STANDBY_0_5_MS..STANDBY_1000_MSNote:
trigger_one_shot()andread_one_shot()were already implemented in #312 (issue #305).Test plan
make test-bme280)