Skip to content

Commit a0753ab

Browse files
committed
Correct Missing Type Annotations
1 parent 1e8a9ba commit a0753ab

File tree

1 file changed

+35
-28
lines changed

1 file changed

+35
-28
lines changed

adafruit_sht31d.py

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@
3333
from micropython import const
3434
from adafruit_bus_device.i2c_device import I2CDevice
3535

36+
try:
37+
from typing import List, Tuple, Union
38+
from busio import I2C
39+
except ImportError:
40+
pass
41+
42+
3643
__version__ = "0.0.0+auto.0"
3744
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SHT31D.git"
3845

@@ -108,7 +115,7 @@
108115
_DELAY = ((REP_LOW, 0.0045), (REP_MED, 0.0065), (REP_HIGH, 0.0155))
109116

110117

111-
def _crc(data):
118+
def _crc(data) -> int:
112119
crc = 0xFF
113120
for byte in data:
114121
crc ^= byte
@@ -121,7 +128,7 @@ def _crc(data):
121128
return crc & 0xFF
122129

123130

124-
def _unpack(data):
131+
def _unpack(data) -> float:
125132
length = len(data)
126133
crc = [None] * (length // 3)
127134
word = [None] * (length // 3)
@@ -172,9 +179,9 @@ class SHT31D:
172179
173180
"""
174181

175-
def __init__(self, i2c_bus, address=_SHT31_DEFAULT_ADDRESS):
182+
def __init__(self, i2c_bus: I2C, address: int = _SHT31_DEFAULT_ADDRESS) -> None:
176183
if address not in _SHT31_ADDRESSES:
177-
raise ValueError("Invalid address: 0x%x" % (address))
184+
raise ValueError(f"Invalid address: {hex(address)}")
178185
self.i2c_device = I2CDevice(i2c_bus, address)
179186
self._mode = MODE_SINGLE
180187
self._repeatability = REP_HIGH
@@ -186,11 +193,11 @@ def __init__(self, i2c_bus, address=_SHT31_DEFAULT_ADDRESS):
186193
self._cached_humidity = None
187194
self._reset()
188195

189-
def _command(self, command):
196+
def _command(self, command: int) -> None:
190197
with self.i2c_device as i2c:
191198
i2c.write(struct.pack(">H", command))
192199

193-
def _reset(self):
200+
def _reset(self) -> None:
194201
"""
195202
Soft reset the device
196203
The reset command is preceded by a break command as the
@@ -201,7 +208,7 @@ def _reset(self):
201208
self._command(_SHT31_SOFTRESET)
202209
time.sleep(0.0015)
203210

204-
def _periodic(self):
211+
def _periodic(self) -> None:
205212
for command in _PERIODIC_COMMANDS:
206213
if self.art == command[0] or (
207214
self.repeatability == command[0] and self.frequency == command[1]
@@ -210,7 +217,7 @@ def _periodic(self):
210217
time.sleep(0.001)
211218
self._last_read = 0
212219

213-
def _data(self):
220+
def _data(self) -> Tuple[float, float]:
214221
if self.mode == MODE_PERIODIC:
215222
data = bytearray(48)
216223
data[0] = 0xFF
@@ -244,7 +251,7 @@ def _data(self):
244251
return temperature[0], humidity[0]
245252
return temperature, humidity
246253

247-
def _read(self):
254+
def _read(self) -> Tuple[float, float]:
248255
if (
249256
self.mode == MODE_PERIODIC
250257
and time.time() > self._last_read + 1 / self.frequency
@@ -256,7 +263,7 @@ def _read(self):
256263
return self._cached_temperature, self._cached_humidity
257264

258265
@property
259-
def mode(self):
266+
def mode(self) -> str:
260267
"""
261268
Operation mode
262269
Allowed values are the constants MODE_*
@@ -265,9 +272,9 @@ def mode(self):
265272
return self._mode
266273

267274
@mode.setter
268-
def mode(self, value):
275+
def mode(self, value: str):
269276
if not value in _SHT31_MODES:
270-
raise ValueError("Mode '%s' not supported" % (value))
277+
raise ValueError(f"Mode '{value}' not supported")
271278
if self._mode == MODE_PERIODIC and value != MODE_PERIODIC:
272279
self._command(_SHT31_PERIODIC_BREAK)
273280
time.sleep(0.001)
@@ -276,45 +283,45 @@ def mode(self, value):
276283
self._mode = value
277284

278285
@property
279-
def repeatability(self):
286+
def repeatability(self) -> Tuple[str, bool, int]:
280287
"""
281288
Repeatability
282289
Allowed values are the constants REP_*
283290
"""
284291
return self._repeatability
285292

286293
@repeatability.setter
287-
def repeatability(self, value):
294+
def repeatability(self, value: Tuple[str, bool, int]) -> None:
288295
if not value in _SHT31_REP:
289-
raise ValueError("Repeatability '%s' not supported" % (value))
296+
raise ValueError("Repeatability '{value}' not supported")
290297
if self.mode == MODE_PERIODIC and not self._repeatability == value:
291298
self._repeatability = value
292299
self._periodic()
293300
else:
294301
self._repeatability = value
295302

296303
@property
297-
def clock_stretching(self):
304+
def clock_stretching(self) -> bool:
298305
"""
299306
Control clock stretching.
300307
This feature only affects 'Single' mode.
301308
"""
302309
return self._clock_stretching
303310

304311
@clock_stretching.setter
305-
def clock_stretching(self, value):
312+
def clock_stretching(self, value: bool) -> None:
306313
self._clock_stretching = bool(value)
307314

308315
@property
309-
def art(self):
316+
def art(self) -> bool:
310317
"""
311318
Control accelerated response time
312319
This feature only affects 'Periodic' mode.
313320
"""
314321
return self._art
315322

316323
@art.setter
317-
def art(self, value):
324+
def art(self, value: bool) -> None:
318325
if value:
319326
self.frequency = FREQUENCY_4
320327
if self.mode == MODE_PERIODIC and not self._art == value:
@@ -324,7 +331,7 @@ def art(self, value):
324331
self._art = bool(value)
325332

326333
@property
327-
def frequency(self):
334+
def frequency(self) -> float:
328335
"""
329336
Periodic data acquisition frequency
330337
Allowed values are the constants FREQUENCY_*
@@ -333,12 +340,12 @@ def frequency(self):
333340
return self._frequency
334341

335342
@frequency.setter
336-
def frequency(self, value):
343+
def frequency(self, value: float) -> None:
337344
if self.art:
338345
raise RuntimeError("Frequency locked to '4 Hz' when ART enabled")
339346
if not value in _SHT31_FREQUENCIES:
340347
raise ValueError(
341-
"Data acquisition frequency '%s Hz' not supported" % (value)
348+
"Data acquisition frequency '{value} Hz' not supported"
342349
)
343350
if self.mode == MODE_PERIODIC and not self._frequency == value:
344351
self._frequency = value
@@ -347,7 +354,7 @@ def frequency(self, value):
347354
self._frequency = value
348355

349356
@property
350-
def temperature(self):
357+
def temperature(self) -> Union(float, List[float]):
351358
"""
352359
The measured temperature in degrees Celsius.
353360
'Single' mode reads and returns the current temperature as a float.
@@ -360,7 +367,7 @@ def temperature(self):
360367
return temperature
361368

362369
@property
363-
def relative_humidity(self):
370+
def relative_humidity(self) -> Union(float, List[float]):
364371
"""
365372
The measured relative humidity in percent.
366373
'Single' mode reads and returns the current humidity as a float.
@@ -373,12 +380,12 @@ def relative_humidity(self):
373380
return humidity
374381

375382
@property
376-
def heater(self):
383+
def heater(self) -> bool:
377384
"""Control device's internal heater."""
378385
return (self.status & 0x2000) != 0
379386

380387
@heater.setter
381-
def heater(self, value=False):
388+
def heater(self, value: bool = False) -> None:
382389
if value:
383390
self._command(_SHT31_HEATER_ENABLE)
384391
time.sleep(0.001)
@@ -387,7 +394,7 @@ def heater(self, value=False):
387394
time.sleep(0.001)
388395

389396
@property
390-
def status(self):
397+
def status(self) -> int:
391398
"""Device status."""
392399
data = bytearray(2)
393400
self._command(_SHT31_READSTATUS)
@@ -398,7 +405,7 @@ def status(self):
398405
return status
399406

400407
@property
401-
def serial_number(self):
408+
def serial_number(self) -> int:
402409
"""Device serial number."""
403410
data = bytearray(6)
404411
data[0] = 0xFF

0 commit comments

Comments
 (0)