33
33
from micropython import const
34
34
from adafruit_bus_device .i2c_device import I2CDevice
35
35
36
+ try :
37
+ from typing import List , Tuple , Union
38
+ from typing_extensions import Literal
39
+ from circuitpython_typing import ReadableBuffer
40
+ from busio import I2C
41
+ except ImportError :
42
+ pass
43
+
44
+
36
45
__version__ = "0.0.0+auto.0"
37
46
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SHT31D.git"
38
47
108
117
_DELAY = ((REP_LOW , 0.0045 ), (REP_MED , 0.0065 ), (REP_HIGH , 0.0155 ))
109
118
110
119
111
- def _crc (data ):
120
+ def _crc (data ) -> int :
112
121
crc = 0xFF
113
122
for byte in data :
114
123
crc ^= byte
@@ -121,7 +130,7 @@ def _crc(data):
121
130
return crc & 0xFF
122
131
123
132
124
- def _unpack (data ) :
133
+ def _unpack (data : ReadableBuffer ) -> List [ int ] :
125
134
length = len (data )
126
135
crc = [None ] * (length // 3 )
127
136
word = [None ] * (length // 3 )
@@ -172,9 +181,9 @@ class SHT31D:
172
181
173
182
"""
174
183
175
- def __init__ (self , i2c_bus , address = _SHT31_DEFAULT_ADDRESS ):
184
+ def __init__ (self , i2c_bus : I2C , address : int = _SHT31_DEFAULT_ADDRESS ) -> None :
176
185
if address not in _SHT31_ADDRESSES :
177
- raise ValueError ("Invalid address: 0x%x" % (address ))
186
+ raise ValueError (f "Invalid address: { hex (address )} " )
178
187
self .i2c_device = I2CDevice (i2c_bus , address )
179
188
self ._mode = MODE_SINGLE
180
189
self ._repeatability = REP_HIGH
@@ -186,11 +195,11 @@ def __init__(self, i2c_bus, address=_SHT31_DEFAULT_ADDRESS):
186
195
self ._cached_humidity = None
187
196
self ._reset ()
188
197
189
- def _command (self , command ) :
198
+ def _command (self , command : int ) -> None :
190
199
with self .i2c_device as i2c :
191
200
i2c .write (struct .pack (">H" , command ))
192
201
193
- def _reset (self ):
202
+ def _reset (self ) -> None :
194
203
"""
195
204
Soft reset the device
196
205
The reset command is preceded by a break command as the
@@ -201,7 +210,7 @@ def _reset(self):
201
210
self ._command (_SHT31_SOFTRESET )
202
211
time .sleep (0.0015 )
203
212
204
- def _periodic (self ):
213
+ def _periodic (self ) -> None :
205
214
for command in _PERIODIC_COMMANDS :
206
215
if self .art == command [0 ] or (
207
216
self .repeatability == command [0 ] and self .frequency == command [1 ]
@@ -210,7 +219,7 @@ def _periodic(self):
210
219
time .sleep (0.001 )
211
220
self ._last_read = 0
212
221
213
- def _data (self ):
222
+ def _data (self ) -> Union [ Tuple [ float , float ], Tuple [ List [ float ], List [ float ]]] :
214
223
if self .mode == MODE_PERIODIC :
215
224
data = bytearray (48 )
216
225
data [0 ] = 0xFF
@@ -244,7 +253,7 @@ def _data(self):
244
253
return temperature [0 ], humidity [0 ]
245
254
return temperature , humidity
246
255
247
- def _read (self ):
256
+ def _read (self ) -> Union [ Tuple [ float , float ], Tuple [ List [ float ], List [ float ]]] :
248
257
if (
249
258
self .mode == MODE_PERIODIC
250
259
and time .time () > self ._last_read + 1 / self .frequency
@@ -256,7 +265,7 @@ def _read(self):
256
265
return self ._cached_temperature , self ._cached_humidity
257
266
258
267
@property
259
- def mode (self ):
268
+ def mode (self ) -> Literal [ "Single" , "Periodic" ] :
260
269
"""
261
270
Operation mode
262
271
Allowed values are the constants MODE_*
@@ -265,9 +274,9 @@ def mode(self):
265
274
return self ._mode
266
275
267
276
@mode .setter
268
- def mode (self , value ) :
277
+ def mode (self , value : Literal [ "Single" , "Periodic" ]) -> None :
269
278
if not value in _SHT31_MODES :
270
- raise ValueError ("Mode '%s ' not supported" % ( value ) )
279
+ raise ValueError (f "Mode '{ value } ' not supported" )
271
280
if self ._mode == MODE_PERIODIC and value != MODE_PERIODIC :
272
281
self ._command (_SHT31_PERIODIC_BREAK )
273
282
time .sleep (0.001 )
@@ -276,45 +285,45 @@ def mode(self, value):
276
285
self ._mode = value
277
286
278
287
@property
279
- def repeatability (self ):
288
+ def repeatability (self ) -> Literal [ "High" , "Medium" , "Low" ] :
280
289
"""
281
290
Repeatability
282
291
Allowed values are the constants REP_*
283
292
"""
284
293
return self ._repeatability
285
294
286
295
@repeatability .setter
287
- def repeatability (self , value ) :
296
+ def repeatability (self , value : Literal [ "High" , "Medium" , "Low" ]) -> None :
288
297
if not value in _SHT31_REP :
289
- raise ValueError ("Repeatability '%s ' not supported" % ( value ) )
298
+ raise ValueError ("Repeatability '{value} ' not supported" )
290
299
if self .mode == MODE_PERIODIC and not self ._repeatability == value :
291
300
self ._repeatability = value
292
301
self ._periodic ()
293
302
else :
294
303
self ._repeatability = value
295
304
296
305
@property
297
- def clock_stretching (self ):
306
+ def clock_stretching (self ) -> bool :
298
307
"""
299
308
Control clock stretching.
300
309
This feature only affects 'Single' mode.
301
310
"""
302
311
return self ._clock_stretching
303
312
304
313
@clock_stretching .setter
305
- def clock_stretching (self , value ) :
314
+ def clock_stretching (self , value : bool ) -> None :
306
315
self ._clock_stretching = bool (value )
307
316
308
317
@property
309
- def art (self ):
318
+ def art (self ) -> bool :
310
319
"""
311
320
Control accelerated response time
312
321
This feature only affects 'Periodic' mode.
313
322
"""
314
323
return self ._art
315
324
316
325
@art .setter
317
- def art (self , value ) :
326
+ def art (self , value : bool ) -> None :
318
327
if value :
319
328
self .frequency = FREQUENCY_4
320
329
if self .mode == MODE_PERIODIC and not self ._art == value :
@@ -324,7 +333,7 @@ def art(self, value):
324
333
self ._art = bool (value )
325
334
326
335
@property
327
- def frequency (self ):
336
+ def frequency (self ) -> float :
328
337
"""
329
338
Periodic data acquisition frequency
330
339
Allowed values are the constants FREQUENCY_*
@@ -333,21 +342,19 @@ def frequency(self):
333
342
return self ._frequency
334
343
335
344
@frequency .setter
336
- def frequency (self , value ) :
345
+ def frequency (self , value : float ) -> None :
337
346
if self .art :
338
347
raise RuntimeError ("Frequency locked to '4 Hz' when ART enabled" )
339
348
if not value in _SHT31_FREQUENCIES :
340
- raise ValueError (
341
- "Data acquisition frequency '%s Hz' not supported" % (value )
342
- )
349
+ raise ValueError ("Data acquisition frequency '{value} Hz' not supported" )
343
350
if self .mode == MODE_PERIODIC and not self ._frequency == value :
344
351
self ._frequency = value
345
352
self ._periodic ()
346
353
else :
347
354
self ._frequency = value
348
355
349
356
@property
350
- def temperature (self ):
357
+ def temperature (self ) -> Union [ float , List [ float ]] :
351
358
"""
352
359
The measured temperature in degrees Celsius.
353
360
'Single' mode reads and returns the current temperature as a float.
@@ -360,7 +367,7 @@ def temperature(self):
360
367
return temperature
361
368
362
369
@property
363
- def relative_humidity (self ):
370
+ def relative_humidity (self ) -> Union [ float , List [ float ]] :
364
371
"""
365
372
The measured relative humidity in percent.
366
373
'Single' mode reads and returns the current humidity as a float.
@@ -373,12 +380,12 @@ def relative_humidity(self):
373
380
return humidity
374
381
375
382
@property
376
- def heater (self ):
383
+ def heater (self ) -> bool :
377
384
"""Control device's internal heater."""
378
385
return (self .status & 0x2000 ) != 0
379
386
380
387
@heater .setter
381
- def heater (self , value = False ):
388
+ def heater (self , value : bool = False ) -> None :
382
389
if value :
383
390
self ._command (_SHT31_HEATER_ENABLE )
384
391
time .sleep (0.001 )
@@ -387,7 +394,7 @@ def heater(self, value=False):
387
394
time .sleep (0.001 )
388
395
389
396
@property
390
- def status (self ):
397
+ def status (self ) -> int :
391
398
"""Device status."""
392
399
data = bytearray (2 )
393
400
self ._command (_SHT31_READSTATUS )
@@ -398,7 +405,7 @@ def status(self):
398
405
return status
399
406
400
407
@property
401
- def serial_number (self ):
408
+ def serial_number (self ) -> int :
402
409
"""Device serial number."""
403
410
data = bytearray (6 )
404
411
data [0 ] = 0xFF
0 commit comments