Skip to content

Correct Missing Type Annotations #32

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 3 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
65 changes: 35 additions & 30 deletions adafruit_sht31d.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
from micropython import const
from adafruit_bus_device.i2c_device import I2CDevice

try:
from typing import List, Tuple, Union
from busio import I2C
except ImportError:
pass


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

Expand Down Expand Up @@ -108,7 +115,7 @@
_DELAY = ((REP_LOW, 0.0045), (REP_MED, 0.0065), (REP_HIGH, 0.0155))


def _crc(data):
def _crc(data) -> int:
crc = 0xFF
for byte in data:
crc ^= byte
Expand All @@ -121,7 +128,7 @@ def _crc(data):
return crc & 0xFF


def _unpack(data):
def _unpack(data) -> float:
length = len(data)
crc = [None] * (length // 3)
word = [None] * (length // 3)
Expand Down Expand Up @@ -172,9 +179,9 @@ class SHT31D:

"""

def __init__(self, i2c_bus, address=_SHT31_DEFAULT_ADDRESS):
def __init__(self, i2c_bus: I2C, address: int = _SHT31_DEFAULT_ADDRESS) -> None:
if address not in _SHT31_ADDRESSES:
raise ValueError("Invalid address: 0x%x" % (address))
raise ValueError(f"Invalid address: {hex(address)}")
self.i2c_device = I2CDevice(i2c_bus, address)
self._mode = MODE_SINGLE
self._repeatability = REP_HIGH
Expand All @@ -186,11 +193,11 @@ def __init__(self, i2c_bus, address=_SHT31_DEFAULT_ADDRESS):
self._cached_humidity = None
self._reset()

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

def _reset(self):
def _reset(self) -> None:
"""
Soft reset the device
The reset command is preceded by a break command as the
Expand All @@ -201,7 +208,7 @@ def _reset(self):
self._command(_SHT31_SOFTRESET)
time.sleep(0.0015)

def _periodic(self):
def _periodic(self) -> None:
for command in _PERIODIC_COMMANDS:
if self.art == command[0] or (
self.repeatability == command[0] and self.frequency == command[1]
Expand All @@ -210,7 +217,7 @@ def _periodic(self):
time.sleep(0.001)
self._last_read = 0

def _data(self):
def _data(self) -> Tuple[float, float]:
if self.mode == MODE_PERIODIC:
data = bytearray(48)
data[0] = 0xFF
Expand Down Expand Up @@ -244,7 +251,7 @@ def _data(self):
return temperature[0], humidity[0]
return temperature, humidity

def _read(self):
def _read(self) -> Tuple[float, float]:
if (
self.mode == MODE_PERIODIC
and time.time() > self._last_read + 1 / self.frequency
Expand All @@ -256,7 +263,7 @@ def _read(self):
return self._cached_temperature, self._cached_humidity

@property
def mode(self):
def mode(self) -> str:
"""
Operation mode
Allowed values are the constants MODE_*
Expand All @@ -265,9 +272,9 @@ def mode(self):
return self._mode

@mode.setter
def mode(self, value):
def mode(self, value: str):
if not value in _SHT31_MODES:
raise ValueError("Mode '%s' not supported" % (value))
raise ValueError(f"Mode '{value}' not supported")
if self._mode == MODE_PERIODIC and value != MODE_PERIODIC:
self._command(_SHT31_PERIODIC_BREAK)
time.sleep(0.001)
Expand All @@ -276,45 +283,45 @@ def mode(self, value):
self._mode = value

@property
def repeatability(self):
def repeatability(self) -> Tuple[str, bool, int]:
"""
Repeatability
Allowed values are the constants REP_*
"""
return self._repeatability

@repeatability.setter
def repeatability(self, value):
def repeatability(self, value: Tuple[str, bool, int]) -> None:
if not value in _SHT31_REP:
raise ValueError("Repeatability '%s' not supported" % (value))
raise ValueError("Repeatability '{value}' not supported")
if self.mode == MODE_PERIODIC and not self._repeatability == value:
self._repeatability = value
self._periodic()
else:
self._repeatability = value

@property
def clock_stretching(self):
def clock_stretching(self) -> bool:
"""
Control clock stretching.
This feature only affects 'Single' mode.
"""
return self._clock_stretching

@clock_stretching.setter
def clock_stretching(self, value):
def clock_stretching(self, value: bool) -> None:
self._clock_stretching = bool(value)

@property
def art(self):
def art(self) -> bool:
"""
Control accelerated response time
This feature only affects 'Periodic' mode.
"""
return self._art

@art.setter
def art(self, value):
def art(self, value: bool) -> None:
if value:
self.frequency = FREQUENCY_4
if self.mode == MODE_PERIODIC and not self._art == value:
Expand All @@ -324,7 +331,7 @@ def art(self, value):
self._art = bool(value)

@property
def frequency(self):
def frequency(self) -> float:
"""
Periodic data acquisition frequency
Allowed values are the constants FREQUENCY_*
Expand All @@ -333,21 +340,19 @@ def frequency(self):
return self._frequency

@frequency.setter
def frequency(self, value):
def frequency(self, value: float) -> None:
if self.art:
raise RuntimeError("Frequency locked to '4 Hz' when ART enabled")
if not value in _SHT31_FREQUENCIES:
raise ValueError(
"Data acquisition frequency '%s Hz' not supported" % (value)
)
raise ValueError("Data acquisition frequency '{value} Hz' not supported")
if self.mode == MODE_PERIODIC and not self._frequency == value:
self._frequency = value
self._periodic()
else:
self._frequency = value

@property
def temperature(self):
def temperature(self) -> Union[float, List[float]]:
"""
The measured temperature in degrees Celsius.
'Single' mode reads and returns the current temperature as a float.
Expand All @@ -360,7 +365,7 @@ def temperature(self):
return temperature

@property
def relative_humidity(self):
def relative_humidity(self) -> Union[float, List[float]]:
"""
The measured relative humidity in percent.
'Single' mode reads and returns the current humidity as a float.
Expand All @@ -373,12 +378,12 @@ def relative_humidity(self):
return humidity

@property
def heater(self):
def heater(self) -> bool:
"""Control device's internal heater."""
return (self.status & 0x2000) != 0

@heater.setter
def heater(self, value=False):
def heater(self, value: bool = False) -> None:
if value:
self._command(_SHT31_HEATER_ENABLE)
time.sleep(0.001)
Expand All @@ -387,7 +392,7 @@ def heater(self, value=False):
time.sleep(0.001)

@property
def status(self):
def status(self) -> int:
"""Device status."""
data = bytearray(2)
self._command(_SHT31_READSTATUS)
Expand All @@ -398,7 +403,7 @@ def status(self):
return status

@property
def serial_number(self):
def serial_number(self) -> int:
"""Device serial number."""
data = bytearray(6)
data[0] = 0xFF
Expand Down