Skip to content

Commit a09d8bb

Browse files
authored
Merge pull request #40 from tcfranks/main
Add Missing Type Annotations
2 parents bce17f2 + 49ca754 commit a09d8bb

File tree

1 file changed

+33
-23
lines changed

1 file changed

+33
-23
lines changed

adafruit_sgp30.py

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
2929
from adafruit_bus_device.i2c_device import I2CDevice
3030
from micropython import const
3131

32+
try:
33+
from typing import List, Tuple
34+
from busio import I2C
35+
except ImportError:
36+
pass
37+
3238
__version__ = "0.0.0+auto.0"
3339
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SGP30.git"
3440

@@ -80,7 +86,7 @@ class Adafruit_SGP30:
8086
8187
"""
8288

83-
def __init__(self, i2c, address=_SGP30_DEFAULT_I2C_ADDR):
89+
def __init__(self, i2c: I2C, address: int = _SGP30_DEFAULT_I2C_ADDR) -> None:
8490
"""Initialize the sensor, get the serial # and verify that we found a proper SGP30"""
8591
self._device = I2CDevice(i2c, address)
8692

@@ -94,61 +100,63 @@ def __init__(self, i2c, address=_SGP30_DEFAULT_I2C_ADDR):
94100

95101
@property
96102
# pylint: disable=invalid-name
97-
def TVOC(self):
103+
def TVOC(self) -> int:
98104
"""Total Volatile Organic Compound in parts per billion."""
99105
return self.iaq_measure()[1]
100106

101107
@property
102108
# pylint: disable=invalid-name
103-
def baseline_TVOC(self):
109+
def baseline_TVOC(self) -> int:
104110
"""Total Volatile Organic Compound baseline value"""
105111
return self.get_iaq_baseline()[1]
106112

107113
@property
108114
# pylint: disable=invalid-name
109-
def eCO2(self):
115+
def eCO2(self) -> int:
110116
"""Carbon Dioxide Equivalent in parts per million"""
111117
return self.iaq_measure()[0]
112118

113119
@property
114120
# pylint: disable=invalid-name
115-
def baseline_eCO2(self):
121+
def baseline_eCO2(self) -> int:
116122
"""Carbon Dioxide Equivalent baseline value"""
117123
return self.get_iaq_baseline()[0]
118124

119125
@property
120126
# pylint: disable=invalid-name
121-
def Ethanol(self):
127+
def Ethanol(self) -> int:
122128
"""Ethanol Raw Signal in ticks"""
123129
return self.raw_measure()[1]
124130

125131
@property
126132
# pylint: disable=invalid-name
127-
def H2(self):
133+
def H2(self) -> int:
128134
"""H2 Raw Signal in ticks"""
129135
return self.raw_measure()[0]
130136

131-
def iaq_init(self):
137+
def iaq_init(self) -> List[int]:
132138
"""Initialize the IAQ algorithm"""
133139
# name, command, signals, delay
134-
self._run_profile(["iaq_init", [0x20, 0x03], 0, 0.01])
140+
self._run_profile(("iaq_init", [0x20, 0x03], 0, 0.01))
135141

136-
def iaq_measure(self):
142+
def iaq_measure(self) -> List[int]:
137143
"""Measure the eCO2 and TVOC"""
138144
# name, command, signals, delay
139-
return self._run_profile(["iaq_measure", [0x20, 0x08], 2, 0.05])
145+
return self._run_profile(("iaq_measure", [0x20, 0x08], 2, 0.05))
140146

141-
def raw_measure(self):
147+
def raw_measure(self) -> List[int]:
142148
"""Measure H2 and Ethanol (Raw Signals)"""
143149
# name, command, signals, delay
144-
return self._run_profile(["raw_measure", [0x20, 0x50], 2, 0.025])
150+
return self._run_profile(("raw_measure", [0x20, 0x50], 2, 0.025))
145151

146-
def get_iaq_baseline(self):
152+
def get_iaq_baseline(self) -> List[int]:
147153
"""Retreive the IAQ algorithm baseline for eCO2 and TVOC"""
148154
# name, command, signals, delay
149-
return self._run_profile(["iaq_get_baseline", [0x20, 0x15], 2, 0.01])
155+
return self._run_profile(("iaq_get_baseline", [0x20, 0x15], 2, 0.01))
150156

151-
def set_iaq_baseline(self, eCO2, TVOC): # pylint: disable=invalid-name
157+
def set_iaq_baseline( # pylint: disable=invalid-name
158+
self, eCO2: int, TVOC: int
159+
) -> None:
152160
"""Set the previously recorded IAQ algorithm baseline for eCO2 and TVOC"""
153161
if eCO2 == 0 and TVOC == 0:
154162
raise RuntimeError("Invalid baseline")
@@ -157,19 +165,19 @@ def set_iaq_baseline(self, eCO2, TVOC): # pylint: disable=invalid-name
157165
arr = [value >> 8, value & 0xFF]
158166
arr.append(self._generate_crc(arr))
159167
buffer += arr
160-
self._run_profile(["iaq_set_baseline", [0x20, 0x1E] + buffer, 0, 0.01])
168+
self._run_profile(("iaq_set_baseline", [0x20, 0x1E] + buffer, 0, 0.01))
161169

162-
def set_iaq_humidity(self, gramsPM3): # pylint: disable=invalid-name
170+
def set_iaq_humidity(self, gramsPM3: float) -> None: # pylint: disable=invalid-name
163171
"""Set the humidity in g/m3 for eCO2 and TVOC compensation algorithm"""
164172
tmp = int(gramsPM3 * 256)
165173
buffer = []
166174
for value in [tmp]:
167175
arr = [value >> 8, value & 0xFF]
168176
arr.append(self._generate_crc(arr))
169177
buffer += arr
170-
self._run_profile(["iaq_set_humidity", [0x20, 0x61] + buffer, 0, 0.01])
178+
self._run_profile(("iaq_set_humidity", [0x20, 0x61] + buffer, 0, 0.01))
171179

172-
def set_iaq_relative_humidity(self, celsius, relative_humidity):
180+
def set_iaq_relative_humidity(self, celsius: float, relative_humidity: float):
173181
"""
174182
Set the humidity in g/m3 for eCo2 and TVOC compensation algorithm.
175183
The absolute humidity is calculated from the temperature (Celsius)
@@ -185,7 +193,7 @@ def set_iaq_relative_humidity(self, celsius, relative_humidity):
185193

186194
# Low level command functions
187195

188-
def _run_profile(self, profile):
196+
def _run_profile(self, profile: Tuple[str, List[int], int, float]) -> List[int]:
189197
"""Run an SGP 'profile' which is a named command set"""
190198
# pylint: disable=unused-variable
191199
name, command, signals, delay = profile
@@ -195,7 +203,9 @@ def _run_profile(self, profile):
195203
# (name, ["0x%02x" % i for i in command], signals, delay))
196204
return self._i2c_read_words_from_cmd(command, delay, signals)
197205

198-
def _i2c_read_words_from_cmd(self, command, delay, reply_size):
206+
def _i2c_read_words_from_cmd(
207+
self, command: List[int], delay: float, reply_size: int
208+
) -> List[int]:
199209
"""Run an SGP command query, get a reply and CRC results if necessary"""
200210
with self._device:
201211
self._device.write(bytes(command))
@@ -216,7 +226,7 @@ def _i2c_read_words_from_cmd(self, command, delay, reply_size):
216226
return result
217227

218228
# pylint: disable=no-self-use
219-
def _generate_crc(self, data):
229+
def _generate_crc(self, data: bytearray) -> int:
220230
"""8-bit CRC algorithm for checking data"""
221231
crc = _SGP30_CRC8_INIT
222232
# calculates 8-Bit checksum with given polynomial

0 commit comments

Comments
 (0)