Skip to content

Add Missing Type Annotations #21

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 1 commit into from
Sep 13, 2022
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
25 changes: 16 additions & 9 deletions adafruit_htu21d.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@
from adafruit_bus_device.i2c_device import I2CDevice
from micropython import const

try:
import typing # pylint: disable=unused-import
from typing_extensions import Literal
from busio import I2C
except ImportError:
pass

__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_HTU21D.git"

Expand All @@ -50,7 +57,7 @@
_TEMP_RH_RES = (0, 1, 128, 129)


def _crc(data):
def _crc(data: bytearray) -> int:
crc = 0
for byte in data:
crc ^= byte
Expand Down Expand Up @@ -97,18 +104,18 @@ class HTU21D:

"""

def __init__(self, i2c_bus, address=0x40):
def __init__(self, i2c_bus: I2C, address: int = 0x40) -> None:
self.i2c_device = I2CDevice(i2c_bus, address)
self._command(_RESET)
self._measurement = 0
self._buffer = bytearray(3)
time.sleep(0.01)

def _command(self, command):
def _command(self, command: int) -> None:
with self.i2c_device as i2c:
i2c.write(struct.pack("B", command))

def _data(self):
def _data(self) -> int:
data = bytearray(3)
while True:
# While busy, the sensor doesn't respond to reads.
Expand All @@ -125,22 +132,22 @@ def _data(self):
return value

@property
def relative_humidity(self):
def relative_humidity(self) -> float:
"""The measured relative humidity in percent."""
self.measurement(HUMIDITY)
self._measurement = 0
time.sleep(0.016)
return self._data() * 125.0 / 65536.0 - 6.0

@property
def temperature(self):
def temperature(self) -> float:
"""The measured temperature in degrees Celsius."""
self.measurement(TEMPERATURE)
self._measurement = 0
time.sleep(0.050)
return self._data() * 175.72 / 65536.0 - 46.85

def measurement(self, what):
def measurement(self, what: Literal[0xF5, 0xF3]) -> float:
"""
Starts a measurement.
Starts a measurement of either ``HUMIDITY`` or ``TEMPERATURE``
Expand All @@ -163,7 +170,7 @@ def measurement(self, what):
self._measurement = what

@property
def temp_rh_resolution(self):
def temp_rh_resolution(self) -> int:
"""The temperature and relative humidity resolution

Have one of the following values: [#f1]_
Expand All @@ -189,7 +196,7 @@ def temp_rh_resolution(self):
return self._buffer[0]

@temp_rh_resolution.setter
def temp_rh_resolution(self, value):
def temp_rh_resolution(self, value: int) -> None:
self._buffer[0] = _READ_USER1
with self.i2c_device as i2c:
i2c.write_then_readinto(self._buffer, self._buffer, out_end=1)
Expand Down