Skip to content

Add type hints #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 1, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions adafruit_am2320.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@
from adafruit_bus_device.i2c_device import I2CDevice
from micropython import const

try:
# Used only for typing
import typing # pylint: disable=unused-import
from busio import I2C
except ImportError:
pass

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_am2320.git"

Expand All @@ -47,7 +54,7 @@
AM2320_REG_HUM_H = const(0x00)


def _crc16(data):
def _crc16(data: bytearray) -> int:
crc = 0xFFFF
for byte in data:
crc ^= byte
Expand Down Expand Up @@ -94,7 +101,7 @@ class AM2320:

"""

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

def _read_register(self, register, length):
def _read_register(self, register: int, length: int) -> bytearray:
with self._i2c as i2c:
# wake up sensor
try:
Expand Down Expand Up @@ -133,15 +140,15 @@ def _read_register(self, register, length):
return result[2:-2]

@property
def temperature(self):
def temperature(self) -> float:
"""The measured temperature in Celsius."""
temperature = struct.unpack(">H", self._read_register(AM2320_REG_TEMP_H, 2))[0]
if temperature >= 32768:
temperature = 32768 - temperature
return temperature / 10.0

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