Skip to content

Commit f3c7e47

Browse files
authored
Merge pull request #17 from tcfranks/main
Fix Annotations
2 parents 88b3408 + c3ab4bf commit f3c7e47

File tree

3 files changed

+77
-55
lines changed

3 files changed

+77
-55
lines changed

adafruit_sgp40/__init__.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@
3333
from struct import unpack_from
3434
from adafruit_bus_device import i2c_device
3535

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

@@ -107,15 +114,15 @@ class SGP40:
107114
108115
"""
109116

110-
def __init__(self, i2c, address=0x59):
117+
def __init__(self, i2c: I2C, address: int = 0x59) -> None:
111118
self.i2c_device = i2c_device.I2CDevice(i2c, address)
112119
self._command_buffer = bytearray(2)
113120
self._measure_command = _READ_CMD
114121
self._voc_algorithm = None
115122

116123
self.initialize()
117124

118-
def initialize(self):
125+
def initialize(self) -> None:
119126
"""Reset the sensor to it's initial unconfigured state and configure it with sensible
120127
defaults so it can be used"""
121128
# check serial number
@@ -132,7 +139,7 @@ def initialize(self):
132139
featureset = self._read_word_from_command()
133140
if featureset[0] != 0x3220:
134141

135-
raise RuntimeError("Feature set does not match: %s" % hex(featureset[0]))
142+
raise RuntimeError(f"Feature set does not match: {featureset[0]:#x}")
136143

137144
# Self Test
138145
self._command_buffer[0] = 0x28
@@ -142,7 +149,7 @@ def initialize(self):
142149
raise RuntimeError("Self test failed")
143150
self._reset()
144151

145-
def _reset(self):
152+
def _reset(self) -> None:
146153
# This is a general call Reset. Several sensors may see this and it doesn't appear to
147154
# ACK before resetting
148155
self._command_buffer[0] = 0x00
@@ -156,7 +163,7 @@ def _reset(self):
156163
sleep(1)
157164

158165
@staticmethod
159-
def _celsius_to_ticks(temperature):
166+
def _celsius_to_ticks(temperature: float) -> List[int]:
160167
"""
161168
Converts Temperature in Celsius to 'ticks' which are an input parameter
162169
the sgp40 can use
@@ -175,7 +182,7 @@ def _celsius_to_ticks(temperature):
175182
return [most_sig_temp_ticks, least_sig_temp_ticks]
176183

177184
@staticmethod
178-
def _relative_humidity_to_ticks(humidity):
185+
def _relative_humidity_to_ticks(humidity: float) -> List[int]:
179186
"""
180187
Converts Relative Humidity in % to 'ticks' which are an input parameter
181188
the sgp40 can use
@@ -194,15 +201,15 @@ def _relative_humidity_to_ticks(humidity):
194201
return [most_sig_rhumidity_ticks, least_sig_rhumidity_ticks]
195202

196203
@property
197-
def raw(self):
204+
def raw(self) -> int:
198205
"""The raw gas value"""
199206
# recycle a single buffer
200207
self._command_buffer = self._measure_command
201208
read_value = self._read_word_from_command(delay_ms=250)
202209
self._command_buffer = bytearray(2)
203210
return read_value[0]
204211

205-
def measure_raw(self, temperature=25, relative_humidity=50):
212+
def measure_raw(self, temperature: float = 25, relative_humidity: float = 50):
206213
"""
207214
A humidity and temperature compensated raw gas value which helps
208215
address fluctuations in readings due to changing humidity.
@@ -225,7 +232,9 @@ def measure_raw(self, temperature=25, relative_humidity=50):
225232
self._measure_command = bytearray(_cmd)
226233
return self.raw
227234

228-
def measure_index(self, temperature=25, relative_humidity=50):
235+
def measure_index(
236+
self, temperature: float = 25, relative_humidity: float = 50
237+
) -> int:
229238
"""Measure VOC index after humidity compensation
230239
:param float temperature: The temperature in degrees Celsius, defaults to :const:`25`
231240
:param float relative_humidity: The relative humidity in percentage, defaults to :const:`50`
@@ -234,7 +243,7 @@ def measure_index(self, temperature=25, relative_humidity=50):
234243
:note 0-100, no need to ventilate, purify
235244
:note 100-200, no need to ventilate, purify
236245
:note 200-400, ventilate, purify
237-
:note 00-500, ventilate, purify intensely
246+
:note 400-500, ventilate, purify intensely
238247
:return int The VOC index measured, ranged from 0 to 500
239248
"""
240249
# import/setup algorithm only on use of index
@@ -255,9 +264,9 @@ def measure_index(self, temperature=25, relative_humidity=50):
255264

256265
def _read_word_from_command(
257266
self,
258-
delay_ms=10,
259-
readlen=1,
260-
):
267+
delay_ms: int = 10,
268+
readlen: Optional[int] = 1,
269+
) -> Optional[List[int]]:
261270
"""_read_word_from_command - send a given command code and read the result back
262271
263272
Args:
@@ -291,15 +300,15 @@ def _read_word_from_command(
291300

292301
return readdata_buffer
293302

294-
def _check_crc8(self, crc_buffer, crc_value):
303+
def _check_crc8(self, crc_buffer: ReadableBuffer, crc_value: int) -> bool:
295304
"""
296305
Checks that the 8 bit CRC Checksum value from the sensor matches the
297306
received data
298307
"""
299308
return crc_value == self._generate_crc(crc_buffer)
300309

301310
@staticmethod
302-
def _generate_crc(crc_buffer):
311+
def _generate_crc(crc_buffer: ReadableBuffer) -> int:
303312
"""
304313
Generates an 8 bit CRC Checksum from the input buffer.
305314

0 commit comments

Comments
 (0)