Skip to content

Commit c1783db

Browse files
authored
Merge pull request #8 from tcfranks/main
Add Missing Type Annotations
2 parents 92cb2e3 + 253522f commit c1783db

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

adafruit_htu31d.py

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@
3535
from adafruit_bus_device import i2c_device
3636
from micropython import const
3737

38+
try:
39+
from typing import Tuple
40+
from typing_extensions import Literal
41+
from busio import I2C
42+
except ImportError:
43+
pass
44+
3845
__version__ = "0.0.0+auto.0"
3946
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_HTU31D.git"
4047

@@ -89,24 +96,24 @@ class HTU31D:
8996
9097
"""
9198

92-
def __init__(self, i2c_bus, address=_HTU31D_DEFAULT_ADDR):
99+
def __init__(self, i2c_bus: I2C, address: int = _HTU31D_DEFAULT_ADDR) -> None:
93100
if address not in _HTU31D_ADDRESSES:
94-
raise ValueError("Invalid address: 0x%x" % (address))
101+
raise ValueError(f"Invalid address: {address:#x}")
95102
self.i2c_device = i2c_device.I2CDevice(i2c_bus, address)
96103
self._conversion_command = _HTU31D_CONVERSION
97104
self._buffer = bytearray(6)
98105
self.reset()
99106

100107
@property
101-
def serial_number(self):
108+
def serial_number(self) -> int:
102109
"""The unique 32-bit serial number"""
103110
self._buffer[0] = _HTU31D_READSERIAL
104111
with self.i2c_device as i2c:
105112
i2c.write_then_readinto(self._buffer, self._buffer, out_end=1, in_end=4)
106113
ser = struct.unpack(">I", self._buffer[0:4])
107-
return ser
114+
return ser[0]
108115

109-
def reset(self):
116+
def reset(self) -> None:
110117
"""Perform a soft reset of the sensor, resetting all settings to their power-on defaults"""
111118
self._conversion_command = _HTU31D_CONVERSION
112119
self._buffer[0] = _HTU31D_SOFTRESET
@@ -115,14 +122,14 @@ def reset(self):
115122
time.sleep(0.015)
116123

117124
@property
118-
def heater(self):
125+
def heater(self) -> bool:
119126
"""The current sensor heater mode"""
120127
return self._heater
121128

122129
@heater.setter
123-
def heater(self, new_mode):
124-
# check its a boolean
125-
if not new_mode in (True, False):
130+
def heater(self, new_mode: bool) -> None:
131+
# check it is a boolean
132+
if not isinstance(new_mode, bool):
126133
raise AttributeError("Heater mode must be boolean")
127134
# cache the mode
128135
self._heater = new_mode
@@ -135,17 +142,17 @@ def heater(self, new_mode):
135142
i2c.write(self._buffer, end=1)
136143

137144
@property
138-
def relative_humidity(self):
145+
def relative_humidity(self) -> float:
139146
"""The current relative humidity in % rH"""
140147
return self.measurements[1]
141148

142149
@property
143-
def temperature(self):
150+
def temperature(self) -> float:
144151
"""The current temperature in degrees Celsius"""
145152
return self.measurements[0]
146153

147154
@property
148-
def measurements(self):
155+
def measurements(self) -> Tuple[float, float]:
149156
"""both `temperature` and `relative_humidity`, read simultaneously"""
150157

151158
temperature = None
@@ -183,7 +190,7 @@ def measurements(self):
183190
return (temperature, humidity)
184191

185192
@property
186-
def humidity_resolution(self):
193+
def humidity_resolution(self) -> Literal["0.020%", "0.014%", "0.010%", "0.007%"]:
187194
"""The current relative humidity resolution in % rH.
188195
189196
Possibles values:
@@ -198,17 +205,19 @@ def humidity_resolution(self):
198205
return _HTU31D_HUMIDITY_RES[self._conversion_command >> 4 & 3]
199206

200207
@humidity_resolution.setter
201-
def humidity_resolution(self, value):
208+
def humidity_resolution(
209+
self, value: Literal["0.020%", "0.014%", "0.010%", "0.007%"]
210+
) -> None:
202211
if value not in _HTU31D_HUMIDITY_RES:
203212
raise ValueError(
204-
"Humidity resolution must be one of: {}".format(_HTU31D_HUMIDITY_RES)
213+
f"Humidity resolution must be one of: {_HTU31D_HUMIDITY_RES}"
205214
)
206215
register = self._conversion_command & 0xCF
207216
hum_res = _HTU31D_HUMIDITY_RES.index(value)
208217
self._conversion_command = register | hum_res << 4
209218

210219
@property
211-
def temp_resolution(self):
220+
def temp_resolution(self) -> Literal["0.040", "0.025", "0.016", "0.012"]:
212221
"""The current temperature resolution in Celsius.
213222
214223
Possibles values:
@@ -223,17 +232,19 @@ def temp_resolution(self):
223232
return _HTU31D_TEMP_RES[self._conversion_command >> 2 & 3]
224233

225234
@temp_resolution.setter
226-
def temp_resolution(self, value):
235+
def temp_resolution(
236+
self, value: Literal["0.040", "0.025", "0.016", "0.012"]
237+
) -> None:
227238
if value not in _HTU31D_TEMP_RES:
228239
raise ValueError(
229-
"Temperature resolution must be one of: {}".format(_HTU31D_TEMP_RES)
240+
f"Temperature resolution must be one of: {_HTU31D_TEMP_RES}"
230241
)
231242
register = self._conversion_command & 0xF3
232243
temp_res = _HTU31D_TEMP_RES.index(value)
233244
self._conversion_command = register | temp_res << 2
234245

235246
@staticmethod
236-
def _crc(value):
247+
def _crc(value) -> int:
237248
polynom = 0x988000 # x^8 + x^5 + x^4 + 1
238249
msb = 0x800000
239250
mask = 0xFF8000

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44

55
Adafruit-Blinka
66
adafruit-circuitpython-busdevice
7+
typing-extensions~=4.0

0 commit comments

Comments
 (0)