Skip to content

Commit cc89d00

Browse files
committed
Updating to use try/except
1 parent 29c027a commit cc89d00

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

adafruit_ahtx0.py

+15-9
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,21 @@ def reset(self) -> None:
106106
time.sleep(0.02) # 20ms delay to wake up
107107

108108
def calibrate(self) -> bool:
109-
"""Ask the sensor to self-calibrate. May not 'succeed' on newer AHT20s."""
110-
self._buf[0] = AHTX0_CMD_CALIBRATE
111-
self._buf[1] = 0x08
112-
self._buf[2] = 0x00
113-
with self.i2c_device as i2c:
114-
i2c.write(self._buf, start=0, end=3)
115-
while self.status & AHTX0_STATUS_BUSY:
116-
time.sleep(0.01)
117-
return True
109+
"""Ask the sensor to self-calibrate. Returns True on success, False otherwise"""
110+
"""Newer AHT20's may not succeed, so wrapping in try/except"""
111+
try:
112+
self._buf[0] = AHTX0_CMD_CALIBRATE
113+
self._buf[1] = 0x08
114+
self._buf[2] = 0x00
115+
with self.i2c_device as i2c:
116+
i2c.write(self._buf, start=0, end=3)
117+
while self.status & AHTX0_STATUS_BUSY:
118+
time.sleep(0.01)
119+
if not self.status & AHTX0_STATUS_CALIBRATED:
120+
return False
121+
return True
122+
except:
123+
pass
118124

119125
@property
120126
def status(self) -> int:

0 commit comments

Comments
 (0)