Skip to content

Commit cbb99e2

Browse files
Added type annotations to adafruit_ahtx0.py
1 parent 0d991d2 commit cbb99e2

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

adafruit_ahtx0.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,20 @@
3030
"""
3131

3232
import time
33+
import busio
34+
3335
from adafruit_bus_device.i2c_device import I2CDevice
3436
from micropython import const
3537

36-
__version__ = "0.0.0-auto.0"
37-
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_AHTx0.git"
38+
__version__: str = "0.0.0-auto.0"
39+
__repo__: str = "https://github.com/adafruit/Adafruit_CircuitPython_AHTx0.git"
3840

39-
AHTX0_I2CADDR_DEFAULT = const(0x38) # Default I2C address
40-
AHTX0_CMD_CALIBRATE = const(0xE1) # Calibration command
41-
AHTX0_CMD_TRIGGER = const(0xAC) # Trigger reading command
42-
AHTX0_CMD_SOFTRESET = const(0xBA) # Soft reset command
43-
AHTX0_STATUS_BUSY = const(0x80) # Status bit for busy
44-
AHTX0_STATUS_CALIBRATED = const(0x08) # Status bit for calibrated
41+
AHTX0_I2CADDR_DEFAULT: int = const(0x38) # Default I2C address
42+
AHTX0_CMD_CALIBRATE: int = const(0xE1) # Calibration command
43+
AHTX0_CMD_TRIGGER: int = const(0xAC) # Trigger reading command
44+
AHTX0_CMD_SOFTRESET: int = const(0xBA) # Soft reset command
45+
AHTX0_STATUS_BUSY: int = const(0x80) # Status bit for busy
46+
AHTX0_STATUS_CALIBRATED: int = const(0x08) # Status bit for calibrated
4547

4648

4749
class AHTx0:
@@ -78,7 +80,7 @@ class AHTx0:
7880
7981
"""
8082

81-
def __init__(self, i2c_bus, address=AHTX0_I2CADDR_DEFAULT):
83+
def __init__(self, i2c_bus: busio.I2C, address: int = AHTX0_I2CADDR_DEFAULT):
8284
time.sleep(0.02) # 20ms delay to wake up
8385
self.i2c_device = I2CDevice(i2c_bus, address)
8486
self._buf = bytearray(6)
@@ -88,14 +90,14 @@ def __init__(self, i2c_bus, address=AHTX0_I2CADDR_DEFAULT):
8890
self._temp = None
8991
self._humidity = None
9092

91-
def reset(self):
93+
def reset(self) -> None:
9294
"""Perform a soft-reset of the AHT"""
9395
self._buf[0] = AHTX0_CMD_SOFTRESET
9496
with self.i2c_device as i2c:
9597
i2c.write(self._buf, start=0, end=1)
9698
time.sleep(0.02) # 20ms delay to wake up
9799

98-
def calibrate(self):
100+
def calibrate(self) -> bool:
99101
"""Ask the sensor to self-calibrate. Returns True on success, False otherwise"""
100102
self._buf[0] = AHTX0_CMD_CALIBRATE
101103
self._buf[1] = 0x08
@@ -109,26 +111,26 @@ def calibrate(self):
109111
return True
110112

111113
@property
112-
def status(self):
114+
def status(self) -> int:
113115
"""The status byte initially returned from the sensor, see datasheet for details"""
114116
with self.i2c_device as i2c:
115117
i2c.readinto(self._buf, start=0, end=1)
116118
# print("status: "+hex(self._buf[0]))
117119
return self._buf[0]
118120

119121
@property
120-
def relative_humidity(self):
122+
def relative_humidity(self) -> int:
121123
"""The measured relative humidity in percent."""
122124
self._readdata()
123125
return self._humidity
124126

125127
@property
126-
def temperature(self):
128+
def temperature(self) -> int:
127129
"""The measured temperature in degrees Celsius."""
128130
self._readdata()
129131
return self._temp
130132

131-
def _readdata(self):
133+
def _readdata(self) -> None:
132134
"""Internal function for triggering the AHT to read temp/humidity"""
133135
self._buf[0] = AHTX0_CMD_TRIGGER
134136
self._buf[1] = 0x33

0 commit comments

Comments
 (0)