Skip to content

Correct Missing Type Annotations #10

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 6 commits into from
Aug 23, 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
28 changes: 17 additions & 11 deletions adafruit_sht4x.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
from adafruit_bus_device import i2c_device
from micropython import const

try:
from typing import Tuple
from busio import I2C
except ImportError:
pass

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

Expand All @@ -46,7 +52,7 @@ class CV:
"""struct helper"""

@classmethod
def add_values(cls, value_tuples):
def add_values(cls, value_tuples: Tuple[str, int, str, float]) -> None:
"""Add CV values to the class"""
cls.string = {}
cls.delay = {}
Expand All @@ -58,7 +64,7 @@ def add_values(cls, value_tuples):
cls.delay[value] = delay

@classmethod
def is_valid(cls, value):
def is_valid(cls, value: int) -> bool:
"""Validate that a given value is a member"""
return value in cls.string

Expand Down Expand Up @@ -126,14 +132,14 @@ class SHT4x:

"""

def __init__(self, i2c_bus, address=_SHT4X_DEFAULT_ADDR):
def __init__(self, i2c_bus: I2C, address: int = _SHT4X_DEFAULT_ADDR) -> None:
self.i2c_device = i2c_device.I2CDevice(i2c_bus, address)
self._buffer = bytearray(6)
self.reset()
self._mode = Mode.NOHEAT_HIGHPRECISION # pylint: disable=no-member

@property
def serial_number(self):
def serial_number(self) -> int:
"""The unique 32-bit serial number"""
self._buffer[0] = _SHT4X_READSERIAL
with self.i2c_device as i2c:
Expand All @@ -153,37 +159,37 @@ def serial_number(self):
serial = (ser1[0] << 24) + (ser1[1] << 16) + (ser2[0] << 8) + ser2[1]
return serial

def reset(self):
def reset(self) -> None:
"""Perform a soft reset of the sensor, resetting all settings to their power-on defaults"""
self._buffer[0] = _SHT4X_SOFTRESET
with self.i2c_device as i2c:
i2c.write(self._buffer, end=1)
time.sleep(0.001)

@property
def mode(self):
def mode(self) -> int:
"""The current sensor reading mode (heater and precision)"""
return self._mode

@mode.setter
def mode(self, new_mode):
def mode(self, new_mode: int) -> None:

if not Mode.is_valid(new_mode):
raise AttributeError("mode must be a Mode")
self._mode = new_mode

@property
def relative_humidity(self):
def relative_humidity(self) -> float:
"""The current relative humidity in % rH. This is a value from 0-100%."""
return self.measurements[1]

@property
def temperature(self):
def temperature(self) -> float:
"""The current temperature in degrees Celsius"""
return self.measurements[0]

@property
def measurements(self):
def measurements(self) -> Tuple[float, float]:
"""both `temperature` and `relative_humidity`, read simultaneously"""

temperature = None
Expand Down Expand Up @@ -226,7 +232,7 @@ def measurements(self):
# Test data [0xBE, 0xEF] should yield 0x92

@staticmethod
def _crc8(buffer):
def _crc8(buffer) -> int:
"""verify the crc8 checksum"""
crc = 0xFF
for byte in buffer:
Expand Down