Skip to content

Commit 10050da

Browse files
committed
Add type hints
1 parent d54ca70 commit 10050da

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

adafruit_am2320.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
from adafruit_bus_device.i2c_device import I2CDevice
3838
from micropython import const
39+
from busio import I2C
3940

4041
__version__ = "0.0.0-auto.0"
4142
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_am2320.git"
@@ -47,7 +48,7 @@
4748
AM2320_REG_HUM_H = const(0x00)
4849

4950

50-
def _crc16(data):
51+
def _crc16(data: bytearray) -> int:
5152
crc = 0xFFFF
5253
for byte in data:
5354
crc ^= byte
@@ -94,7 +95,7 @@ class AM2320:
9495
9596
"""
9697

97-
def __init__(self, i2c_bus, address=AM2320_DEFAULT_ADDR):
98+
def __init__(self, i2c_bus: I2C, address: int = AM2320_DEFAULT_ADDR):
9899
for _ in range(3):
99100
# retry since we have to wake up the devices
100101
try:
@@ -105,7 +106,7 @@ def __init__(self, i2c_bus, address=AM2320_DEFAULT_ADDR):
105106
time.sleep(0.25)
106107
raise ValueError("AM2320 not found")
107108

108-
def _read_register(self, register, length):
109+
def _read_register(self, register: int, length: int) -> bytearray:
109110
with self._i2c as i2c:
110111
# wake up sensor
111112
try:
@@ -133,15 +134,15 @@ def _read_register(self, register, length):
133134
return result[2:-2]
134135

135136
@property
136-
def temperature(self):
137+
def temperature(self) -> float:
137138
"""The measured temperature in Celsius."""
138139
temperature = struct.unpack(">H", self._read_register(AM2320_REG_TEMP_H, 2))[0]
139140
if temperature >= 32768:
140141
temperature = 32768 - temperature
141142
return temperature / 10.0
142143

143144
@property
144-
def relative_humidity(self):
145+
def relative_humidity(self) -> float:
145146
"""The measured relative humidity in percent."""
146147
humidity = struct.unpack(">H", self._read_register(AM2320_REG_HUM_H, 2))[0]
147148
return humidity / 10.0

0 commit comments

Comments
 (0)