Skip to content

Commit 3ee7746

Browse files
authored
Merge pull request #22 from tylercrumpton/add-type-hints
Add type hints
2 parents d54ca70 + a515c4e commit 3ee7746

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

adafruit_am2320.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@
3737
from adafruit_bus_device.i2c_device import I2CDevice
3838
from micropython import const
3939

40+
try:
41+
# Used only for typing
42+
import typing # pylint: disable=unused-import
43+
from busio import I2C
44+
except ImportError:
45+
pass
46+
4047
__version__ = "0.0.0-auto.0"
4148
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_am2320.git"
4249

@@ -47,7 +54,7 @@
4754
AM2320_REG_HUM_H = const(0x00)
4855

4956

50-
def _crc16(data):
57+
def _crc16(data: bytearray) -> int:
5158
crc = 0xFFFF
5259
for byte in data:
5360
crc ^= byte
@@ -94,7 +101,7 @@ class AM2320:
94101
95102
"""
96103

97-
def __init__(self, i2c_bus, address=AM2320_DEFAULT_ADDR):
104+
def __init__(self, i2c_bus: I2C, address: int = AM2320_DEFAULT_ADDR):
98105
for _ in range(3):
99106
# retry since we have to wake up the devices
100107
try:
@@ -105,7 +112,7 @@ def __init__(self, i2c_bus, address=AM2320_DEFAULT_ADDR):
105112
time.sleep(0.25)
106113
raise ValueError("AM2320 not found")
107114

108-
def _read_register(self, register, length):
115+
def _read_register(self, register: int, length: int) -> bytearray:
109116
with self._i2c as i2c:
110117
# wake up sensor
111118
try:
@@ -133,15 +140,15 @@ def _read_register(self, register, length):
133140
return result[2:-2]
134141

135142
@property
136-
def temperature(self):
143+
def temperature(self) -> float:
137144
"""The measured temperature in Celsius."""
138145
temperature = struct.unpack(">H", self._read_register(AM2320_REG_TEMP_H, 2))[0]
139146
if temperature >= 32768:
140147
temperature = 32768 - temperature
141148
return temperature / 10.0
142149

143150
@property
144-
def relative_humidity(self):
151+
def relative_humidity(self) -> float:
145152
"""The measured relative humidity in percent."""
146153
humidity = struct.unpack(">H", self._read_register(AM2320_REG_HUM_H, 2))[0]
147154
return humidity / 10.0

0 commit comments

Comments
 (0)