34
34
from adafruit_register .i2c_bits import RWBits , ROBits
35
35
from adafruit_register .i2c_bit import RWBit , ROBit
36
36
37
+ try :
38
+ from typing import Union , Sequence , Tuple
39
+ from busio import I2C
40
+ except ImportError :
41
+ pass
42
+
37
43
_WHO_AM_I = const (0x0F )
38
44
39
45
_CTRL_REG1 = const (0x20 )
@@ -65,21 +71,21 @@ class CV:
65
71
"""struct helper"""
66
72
67
73
@classmethod
68
- def add_values (cls , value_tuples ):
74
+ def add_values (
75
+ cls , value_tuples : Sequence [Tuple [str , int , Union [int , float ]]]
76
+ ) -> None :
69
77
"""creates CV entries"""
70
- cls .string = {}
71
- cls .lsb = {}
78
+ cls .label = {}
72
79
73
80
for value_tuple in value_tuples :
74
- name , value , string , lsb = value_tuple
81
+ name , value , label = value_tuple
75
82
setattr (cls , name , value )
76
- cls .string [value ] = string
77
- cls .lsb [value ] = lsb
83
+ cls .label [value ] = label
78
84
79
85
@classmethod
80
- def is_valid (cls , value ) :
86
+ def is_valid (cls , value : int ) -> bool :
81
87
"""Returns true if the given value is a member of the CV"""
82
- return value in cls .string
88
+ return value in cls .label
83
89
84
90
85
91
class Rate (CV ):
@@ -105,10 +111,10 @@ class Rate(CV):
105
111
106
112
Rate .add_values (
107
113
(
108
- ("ONE_SHOT" , 0 , 0 , None ),
109
- ("RATE_1_HZ" , 1 , 1 , None ),
110
- ("RATE_7_HZ" , 2 , 7 , None ),
111
- ("RATE_12_5_HZ" , 3 , 12.5 , None ),
114
+ ("ONE_SHOT" , 0 , 0 ),
115
+ ("RATE_1_HZ" , 1 , 1 ),
116
+ ("RATE_7_HZ" , 2 , 7 ),
117
+ ("RATE_12_5_HZ" , 3 , 12.5 ),
112
118
)
113
119
)
114
120
@@ -170,7 +176,7 @@ class HTS221: # pylint: disable=too-many-instance-attributes
170
176
_h0_t0_out = ROUnaryStruct (_H0_T0_OUT , "<h" )
171
177
_h1_t0_out = ROUnaryStruct (_H1_T1_OUT , "<h" )
172
178
173
- def __init__ (self , i2c_bus ) :
179
+ def __init__ (self , i2c_bus : I2C ) -> None :
174
180
self .i2c_device = i2cdevice .I2CDevice (i2c_bus , _HTS221_DEFAULT_ADDRESS )
175
181
if not self ._chip_id in [_HTS221_CHIP_ID ]:
176
182
raise RuntimeError (
@@ -203,14 +209,14 @@ def __init__(self, i2c_bus):
203
209
self .calib_hum_meas_1 = self ._h1_t0_out
204
210
205
211
# This is the closest thing to a software reset. It re-loads the calibration values from flash
206
- def _boot (self ):
212
+ def _boot (self ) -> None :
207
213
self ._boot_bit = True
208
214
# wait for the reset to finish
209
215
while self ._boot_bit :
210
216
pass
211
217
212
218
@property
213
- def relative_humidity (self ):
219
+ def relative_humidity (self ) -> float :
214
220
"""The current relative humidity measurement in %rH"""
215
221
calibrated_value_delta = self .calib_hum_value_1 - self .calib_hum_value_0
216
222
calibrated_measurement_delta = self .calib_hum_meas_1 - self .calib_hum_meas_0
@@ -228,7 +234,7 @@ def relative_humidity(self):
228
234
return adjusted_humidity
229
235
230
236
@property
231
- def temperature (self ):
237
+ def temperature (self ) -> float :
232
238
"""The current temperature measurement in degrees Celsius"""
233
239
234
240
calibrated_value_delta = self .calibrated_value_1 - self .calib_temp_value_0
@@ -247,7 +253,7 @@ def temperature(self):
247
253
return adjusted_temp
248
254
249
255
@property
250
- def data_rate (self ):
256
+ def data_rate (self ) -> int :
251
257
"""The rate at which the sensor measures :attr:`relative_humidity` and :attr:`temperature`.
252
258
:attr:`data_rate` should be set to one of the values of :class:`adafruit_hts221.Rate`.
253
259
Note that setting :attr:`data_rate` to ``Rate.ONE_SHOT`` will cause
@@ -256,23 +262,23 @@ def data_rate(self):
256
262
return self ._data_rate
257
263
258
264
@data_rate .setter
259
- def data_rate (self , value ) :
265
+ def data_rate (self , value : int ) -> None :
260
266
if not Rate .is_valid (value ):
261
267
raise AttributeError ("data_rate must be a `Rate`" )
262
268
263
269
self ._data_rate = value
264
270
265
271
@property
266
- def humidity_data_ready (self ):
272
+ def humidity_data_ready (self ) -> bool :
267
273
"""Returns true if a new relative humidity measurement is available to be read"""
268
274
return self ._humidity_status_bit
269
275
270
276
@property
271
- def temperature_data_ready (self ):
277
+ def temperature_data_ready (self ) -> bool :
272
278
"""Returns true if a new temperature measurement is available to be read"""
273
279
return self ._temperature_status_bit
274
280
275
- def take_measurements (self ):
281
+ def take_measurements (self ) -> None :
276
282
"""Update the value of :attr:`relative_humidity` and :attr:`temperature` by taking a single
277
283
measurement. Only meaningful if :attr:`data_rate` is set to ``ONE_SHOT``"""
278
284
self ._one_shot_bit = True
0 commit comments