3434from adafruit_bus_device .i2c_device import I2CDevice
3535from micropython import const
3636
37+ try :
38+ import typing # pylint: disable=unused-import
39+ from typing_extensions import Literal
40+ from busio import I2C
41+ except ImportError :
42+ pass
43+
3744__version__ = "0.0.0+auto.0"
3845__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_HTU21D.git"
3946
5057_TEMP_RH_RES = (0 , 1 , 128 , 129 )
5158
5259
53- def _crc (data ) :
60+ def _crc (data : bytearray ) -> int :
5461 crc = 0
5562 for byte in data :
5663 crc ^= byte
@@ -97,18 +104,18 @@ class HTU21D:
97104
98105 """
99106
100- def __init__ (self , i2c_bus , address = 0x40 ):
107+ def __init__ (self , i2c_bus : I2C , address : int = 0x40 ) -> None :
101108 self .i2c_device = I2CDevice (i2c_bus , address )
102109 self ._command (_RESET )
103110 self ._measurement = 0
104111 self ._buffer = bytearray (3 )
105112 time .sleep (0.01 )
106113
107- def _command (self , command ) :
114+ def _command (self , command : int ) -> None :
108115 with self .i2c_device as i2c :
109116 i2c .write (struct .pack ("B" , command ))
110117
111- def _data (self ):
118+ def _data (self ) -> int :
112119 data = bytearray (3 )
113120 while True :
114121 # While busy, the sensor doesn't respond to reads.
@@ -125,22 +132,22 @@ def _data(self):
125132 return value
126133
127134 @property
128- def relative_humidity (self ):
135+ def relative_humidity (self ) -> float :
129136 """The measured relative humidity in percent."""
130137 self .measurement (HUMIDITY )
131138 self ._measurement = 0
132139 time .sleep (0.016 )
133140 return self ._data () * 125.0 / 65536.0 - 6.0
134141
135142 @property
136- def temperature (self ):
143+ def temperature (self ) -> float :
137144 """The measured temperature in degrees Celsius."""
138145 self .measurement (TEMPERATURE )
139146 self ._measurement = 0
140147 time .sleep (0.050 )
141148 return self ._data () * 175.72 / 65536.0 - 46.85
142149
143- def measurement (self , what ) :
150+ def measurement (self , what : Literal [ 0xF5 , 0xF3 ]) -> float :
144151 """
145152 Starts a measurement.
146153 Starts a measurement of either ``HUMIDITY`` or ``TEMPERATURE``
@@ -163,7 +170,7 @@ def measurement(self, what):
163170 self ._measurement = what
164171
165172 @property
166- def temp_rh_resolution (self ):
173+ def temp_rh_resolution (self ) -> int :
167174 """The temperature and relative humidity resolution
168175
169176 Have one of the following values: [#f1]_
@@ -189,7 +196,7 @@ def temp_rh_resolution(self):
189196 return self ._buffer [0 ]
190197
191198 @temp_rh_resolution .setter
192- def temp_rh_resolution (self , value ) :
199+ def temp_rh_resolution (self , value : int ) -> None :
193200 self ._buffer [0 ] = _READ_USER1
194201 with self .i2c_device as i2c :
195202 i2c .write_then_readinto (self ._buffer , self ._buffer , out_end = 1 )
0 commit comments