@@ -274,3 +274,145 @@ tests:
274274 result = triggered and abs(t - 25.08) < 0.1
275275 expect_true : true
276276 mode : [mock]
277+
278+ # ----- Power and mode control -----
279+
280+ - name : " power_off sets sleep mode"
281+ action : script
282+ script : |
283+ i2c.clear_write_log()
284+ dev.power_off()
285+ log = i2c.get_write_log()
286+ wrote_sleep = any(reg == 0xF4 and (data[0] & 0x03) == 0x00 for reg, data in log)
287+ result = wrote_sleep
288+ expect_true : true
289+ mode : [mock]
290+
291+ - name : " power_on sets normal mode"
292+ action : script
293+ script : |
294+ i2c.clear_write_log()
295+ dev.power_on()
296+ log = i2c.get_write_log()
297+ wrote_normal = any(reg == 0xF4 and (data[0] & 0x03) == 0x03 for reg, data in log)
298+ result = wrote_normal
299+ expect_true : true
300+ mode : [mock]
301+
302+ - name : " power_off preserves oversampling bits"
303+ action : script
304+ script : |
305+ from bme280.const import OSRS_X4, OSRS_T_SHIFT, OSRS_P_SHIFT
306+ dev.set_oversampling(temperature=OSRS_X4, pressure=OSRS_X4)
307+ i2c.clear_write_log()
308+ dev.power_off()
309+ log = i2c.get_write_log()
310+ ctrl = [data[0] for reg, data in log if reg == 0xF4][-1]
311+ osrs_t = (ctrl >> 5) & 0x07
312+ osrs_p = (ctrl >> 2) & 0x07
313+ result = osrs_t == OSRS_X4 and osrs_p == OSRS_X4 and (ctrl & 0x03) == 0x00
314+ expect_true : true
315+ mode : [mock]
316+
317+ - name : " set_continuous enters normal mode"
318+ action : script
319+ script : |
320+ dev.power_off()
321+ i2c.clear_write_log()
322+ dev.set_continuous()
323+ log = i2c.get_write_log()
324+ wrote_normal = any(reg == 0xF4 and (data[0] & 0x03) == 0x03 for reg, data in log)
325+ result = wrote_normal
326+ expect_true : true
327+ mode : [mock]
328+
329+ - name : " set_continuous with standby configures both"
330+ action : script
331+ script : |
332+ from bme280.const import STANDBY_500_MS, STANDBY_SHIFT
333+ i2c.clear_write_log()
334+ dev.set_continuous(standby=STANDBY_500_MS)
335+ log = i2c.get_write_log()
336+ wrote_config = any(reg == 0xF5 for reg, data in log)
337+ wrote_normal = any(reg == 0xF4 and (data[0] & 0x03) == 0x03 for reg, data in log)
338+ result = wrote_config and wrote_normal
339+ expect_true : true
340+ mode : [mock]
341+
342+ # ----- Configuration -----
343+
344+ - name : " set_oversampling configures temperature and pressure"
345+ action : script
346+ script : |
347+ from bme280.const import OSRS_X2, OSRS_X4, OSRS_T_SHIFT, OSRS_P_SHIFT
348+ i2c.clear_write_log()
349+ dev.set_oversampling(temperature=OSRS_X2, pressure=OSRS_X4)
350+ log = i2c.get_write_log()
351+ ctrl_writes = [data[0] for reg, data in log if reg == 0xF4]
352+ ctrl = ctrl_writes[-1]
353+ osrs_t = (ctrl >> OSRS_T_SHIFT) & 0x07
354+ osrs_p = (ctrl >> OSRS_P_SHIFT) & 0x07
355+ result = osrs_t == OSRS_X2 and osrs_p == OSRS_X4
356+ expect_true : true
357+ mode : [mock]
358+
359+ - name : " set_oversampling configures humidity and latches via ctrl_meas"
360+ action : script
361+ script : |
362+ from bme280.const import OSRS_X8
363+ i2c.clear_write_log()
364+ dev.set_oversampling(humidity=OSRS_X8)
365+ log = i2c.get_write_log()
366+ hum_writes = [data[0] for reg, data in log if reg == 0xF2]
367+ # ctrl_meas must be rewritten after ctrl_hum for the change to take effect
368+ ctrl_writes = [data[0] for reg, data in log if reg == 0xF4]
369+ # Verify write order: ctrl_hum (0xF2) must come before ctrl_meas (0xF4)
370+ hum_idx = next(i for i, (reg, _) in enumerate(log) if reg == 0xF2)
371+ meas_idx = next(i for i, (reg, _) in enumerate(log) if reg == 0xF4)
372+ result = (
373+ len(hum_writes) == 1
374+ and hum_writes[0] == OSRS_X8
375+ and len(ctrl_writes) == 1
376+ and hum_idx < meas_idx
377+ )
378+ expect_true : true
379+ mode : [mock]
380+
381+ - name : " set_iir_filter writes config register"
382+ action : script
383+ script : |
384+ from bme280.const import FILTER_16, FILTER_SHIFT
385+ i2c.clear_write_log()
386+ dev.set_iir_filter(FILTER_16)
387+ log = i2c.get_write_log()
388+ config_writes = [data[0] for reg, data in log if reg == 0xF5]
389+ result = len(config_writes) == 1 and ((config_writes[0] >> FILTER_SHIFT) & 0x07) == FILTER_16
390+ expect_true : true
391+ mode : [mock]
392+
393+ - name : " set_standby writes config register"
394+ action : script
395+ script : |
396+ from bme280.const import STANDBY_250_MS, STANDBY_SHIFT
397+ i2c.clear_write_log()
398+ dev.set_standby(STANDBY_250_MS)
399+ log = i2c.get_write_log()
400+ config_writes = [data[0] for reg, data in log if reg == 0xF5]
401+ result = len(config_writes) == 1 and ((config_writes[0] >> STANDBY_SHIFT) & 0x07) == STANDBY_250_MS
402+ expect_true : true
403+ mode : [mock]
404+
405+ - name : " set_iir_filter preserves standby bits"
406+ action : script
407+ script : |
408+ from bme280.const import STANDBY_500_MS, STANDBY_SHIFT, FILTER_4, FILTER_SHIFT
409+ dev.set_standby(STANDBY_500_MS)
410+ i2c.clear_write_log()
411+ dev.set_iir_filter(FILTER_4)
412+ log = i2c.get_write_log()
413+ config = [data[0] for reg, data in log if reg == 0xF5][-1]
414+ standby = (config >> STANDBY_SHIFT) & 0x07
415+ filt = (config >> FILTER_SHIFT) & 0x07
416+ result = standby == STANDBY_500_MS and filt == FILTER_4
417+ expect_true : true
418+ mode : [mock]
0 commit comments