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 busio import I2C
39
+ except ImportError :
40
+ pass
41
+
42
+
36
43
__version__ = "0.0.0+auto.0"
37
44
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SHT31D.git"
38
45
108
115
_DELAY = ((REP_LOW , 0.0045 ), (REP_MED , 0.0065 ), (REP_HIGH , 0.0155 ))
109
116
110
117
111
- def _crc (data ):
118
+ def _crc (data ) -> int :
112
119
crc = 0xFF
113
120
for byte in data :
114
121
crc ^= byte
@@ -121,7 +128,7 @@ def _crc(data):
121
128
return crc & 0xFF
122
129
123
130
124
- def _unpack (data ):
131
+ def _unpack (data ) -> float :
125
132
length = len (data )
126
133
crc = [None ] * (length // 3 )
127
134
word = [None ] * (length // 3 )
@@ -172,9 +179,9 @@ class SHT31D:
172
179
173
180
"""
174
181
175
- def __init__ (self , i2c_bus , address = _SHT31_DEFAULT_ADDRESS ):
182
+ def __init__ (self , i2c_bus : I2C , address : int = _SHT31_DEFAULT_ADDRESS ) -> None :
176
183
if address not in _SHT31_ADDRESSES :
177
- raise ValueError ("Invalid address: 0x%x" % (address ))
184
+ raise ValueError (f "Invalid address: { hex (address )} " )
178
185
self .i2c_device = I2CDevice (i2c_bus , address )
179
186
self ._mode = MODE_SINGLE
180
187
self ._repeatability = REP_HIGH
@@ -186,11 +193,11 @@ def __init__(self, i2c_bus, address=_SHT31_DEFAULT_ADDRESS):
186
193
self ._cached_humidity = None
187
194
self ._reset ()
188
195
189
- def _command (self , command ) :
196
+ def _command (self , command : int ) -> None :
190
197
with self .i2c_device as i2c :
191
198
i2c .write (struct .pack (">H" , command ))
192
199
193
- def _reset (self ):
200
+ def _reset (self ) -> None :
194
201
"""
195
202
Soft reset the device
196
203
The reset command is preceded by a break command as the
@@ -201,7 +208,7 @@ def _reset(self):
201
208
self ._command (_SHT31_SOFTRESET )
202
209
time .sleep (0.0015 )
203
210
204
- def _periodic (self ):
211
+ def _periodic (self ) -> None :
205
212
for command in _PERIODIC_COMMANDS :
206
213
if self .art == command [0 ] or (
207
214
self .repeatability == command [0 ] and self .frequency == command [1 ]
@@ -210,7 +217,7 @@ def _periodic(self):
210
217
time .sleep (0.001 )
211
218
self ._last_read = 0
212
219
213
- def _data (self ):
220
+ def _data (self ) -> Tuple [ float , float ] :
214
221
if self .mode == MODE_PERIODIC :
215
222
data = bytearray (48 )
216
223
data [0 ] = 0xFF
@@ -244,7 +251,7 @@ def _data(self):
244
251
return temperature [0 ], humidity [0 ]
245
252
return temperature , humidity
246
253
247
- def _read (self ):
254
+ def _read (self ) -> Tuple [ float , float ] :
248
255
if (
249
256
self .mode == MODE_PERIODIC
250
257
and time .time () > self ._last_read + 1 / self .frequency
@@ -256,7 +263,7 @@ def _read(self):
256
263
return self ._cached_temperature , self ._cached_humidity
257
264
258
265
@property
259
- def mode (self ):
266
+ def mode (self ) -> str :
260
267
"""
261
268
Operation mode
262
269
Allowed values are the constants MODE_*
@@ -265,9 +272,9 @@ def mode(self):
265
272
return self ._mode
266
273
267
274
@mode .setter
268
- def mode (self , value ):
275
+ def mode (self , value : str ):
269
276
if not value in _SHT31_MODES :
270
- raise ValueError ("Mode '%s ' not supported" % ( value ) )
277
+ raise ValueError (f "Mode '{ value } ' not supported" )
271
278
if self ._mode == MODE_PERIODIC and value != MODE_PERIODIC :
272
279
self ._command (_SHT31_PERIODIC_BREAK )
273
280
time .sleep (0.001 )
@@ -276,45 +283,45 @@ def mode(self, value):
276
283
self ._mode = value
277
284
278
285
@property
279
- def repeatability (self ):
286
+ def repeatability (self ) -> Tuple [ str , bool , int ] :
280
287
"""
281
288
Repeatability
282
289
Allowed values are the constants REP_*
283
290
"""
284
291
return self ._repeatability
285
292
286
293
@repeatability .setter
287
- def repeatability (self , value ) :
294
+ def repeatability (self , value : Tuple [ str , bool , int ]) -> None :
288
295
if not value in _SHT31_REP :
289
- raise ValueError ("Repeatability '%s ' not supported" % ( value ) )
296
+ raise ValueError ("Repeatability '{value} ' not supported" )
290
297
if self .mode == MODE_PERIODIC and not self ._repeatability == value :
291
298
self ._repeatability = value
292
299
self ._periodic ()
293
300
else :
294
301
self ._repeatability = value
295
302
296
303
@property
297
- def clock_stretching (self ):
304
+ def clock_stretching (self ) -> bool :
298
305
"""
299
306
Control clock stretching.
300
307
This feature only affects 'Single' mode.
301
308
"""
302
309
return self ._clock_stretching
303
310
304
311
@clock_stretching .setter
305
- def clock_stretching (self , value ) :
312
+ def clock_stretching (self , value : bool ) -> None :
306
313
self ._clock_stretching = bool (value )
307
314
308
315
@property
309
- def art (self ):
316
+ def art (self ) -> bool :
310
317
"""
311
318
Control accelerated response time
312
319
This feature only affects 'Periodic' mode.
313
320
"""
314
321
return self ._art
315
322
316
323
@art .setter
317
- def art (self , value ) :
324
+ def art (self , value : bool ) -> None :
318
325
if value :
319
326
self .frequency = FREQUENCY_4
320
327
if self .mode == MODE_PERIODIC and not self ._art == value :
@@ -324,7 +331,7 @@ def art(self, value):
324
331
self ._art = bool (value )
325
332
326
333
@property
327
- def frequency (self ):
334
+ def frequency (self ) -> float :
328
335
"""
329
336
Periodic data acquisition frequency
330
337
Allowed values are the constants FREQUENCY_*
@@ -333,12 +340,12 @@ def frequency(self):
333
340
return self ._frequency
334
341
335
342
@frequency .setter
336
- def frequency (self , value ) :
343
+ def frequency (self , value : float ) -> None :
337
344
if self .art :
338
345
raise RuntimeError ("Frequency locked to '4 Hz' when ART enabled" )
339
346
if not value in _SHT31_FREQUENCIES :
340
347
raise ValueError (
341
- "Data acquisition frequency '%s Hz' not supported" % ( value )
348
+ "Data acquisition frequency '{value} Hz' not supported"
342
349
)
343
350
if self .mode == MODE_PERIODIC and not self ._frequency == value :
344
351
self ._frequency = value
@@ -347,7 +354,7 @@ def frequency(self, value):
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