Skip to content

Commit a9ae07d

Browse files
authored
Merge pull request #14 from tcfranks/main
Add Missing Type Annotations
2 parents 6e0a8dc + 9ab17bc commit a9ae07d

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

adafruit_scd4x.py

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
3131
from adafruit_bus_device import i2c_device
3232
from micropython import const
3333

34+
try:
35+
from typing import Tuple, Union
36+
from busio import I2C
37+
except ImportError:
38+
pass
3439

3540
__version__ = "0.0.0+auto.0"
3641
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SCD4X.git"
@@ -93,7 +98,7 @@ class SCD4X:
9398
9499
"""
95100

96-
def __init__(self, i2c_bus, address=SCD4X_DEFAULT_ADDR):
101+
def __init__(self, i2c_bus: I2C, address: int = SCD4X_DEFAULT_ADDR) -> None:
97102
self.i2c_device = i2c_device.I2CDevice(i2c_bus, address)
98103
self._buffer = bytearray(18)
99104
self._cmd = bytearray(2)
@@ -107,7 +112,7 @@ def __init__(self, i2c_bus, address=SCD4X_DEFAULT_ADDR):
107112
self.stop_periodic_measurement()
108113

109114
@property
110-
def CO2(self): # pylint:disable=invalid-name
115+
def CO2(self) -> int: # pylint:disable=invalid-name
111116
"""Returns the CO2 concentration in PPM (parts per million)
112117
113118
.. note::
@@ -119,7 +124,7 @@ def CO2(self): # pylint:disable=invalid-name
119124
return self._co2
120125

121126
@property
122-
def temperature(self):
127+
def temperature(self) -> float:
123128
"""Returns the current temperature in degrees Celsius
124129
125130
.. note::
@@ -131,7 +136,7 @@ def temperature(self):
131136
return self._temperature
132137

133138
@property
134-
def relative_humidity(self):
139+
def relative_humidity(self) -> float:
135140
"""Returns the current relative humidity in %rH.
136141
137142
.. note::
@@ -142,18 +147,18 @@ def relative_humidity(self):
142147
self._read_data()
143148
return self._relative_humidity
144149

145-
def reinit(self):
150+
def reinit(self) -> None:
146151
"""Reinitializes the sensor by reloading user settings from EEPROM."""
147152
self.stop_periodic_measurement()
148153
self._send_command(_SCD4X_REINIT, cmd_delay=0.02)
149154

150-
def factory_reset(self):
155+
def factory_reset(self) -> None:
151156
"""Resets all configuration settings stored in the EEPROM and erases the
152157
FRC and ASC algorithm history."""
153158
self.stop_periodic_measurement()
154159
self._send_command(_SCD4X_FACTORYRESET, cmd_delay=1.2)
155160

156-
def force_calibration(self, target_co2):
161+
def force_calibration(self, target_co2: int) -> None:
157162
"""Forces the sensor to recalibrate with a given current CO2"""
158163
self.stop_periodic_measurement()
159164
self._set_command_value(_SCD4X_FORCEDRECAL, target_co2)
@@ -167,7 +172,7 @@ def force_calibration(self, target_co2):
167172
)
168173

169174
@property
170-
def self_calibration_enabled(self):
175+
def self_calibration_enabled(self) -> bool:
171176
"""Enables or disables automatic self calibration (ASC). To work correctly, the sensor must
172177
be on and active for 7 days after enabling ASC, and exposed to fresh air for at least 1 hour
173178
per day. Consult the manufacturer's documentation for more information.
@@ -182,18 +187,18 @@ def self_calibration_enabled(self):
182187
return self._buffer[1] == 1
183188

184189
@self_calibration_enabled.setter
185-
def self_calibration_enabled(self, enabled):
190+
def self_calibration_enabled(self, enabled: bool) -> None:
186191
self._set_command_value(_SCD4X_SETASCE, enabled)
187192

188-
def self_test(self):
193+
def self_test(self) -> None:
189194
"""Performs a self test, takes up to 10 seconds"""
190195
self.stop_periodic_measurement()
191196
self._send_command(_SCD4X_SELFTEST, cmd_delay=10)
192197
self._read_reply(self._buffer, 3)
193198
if (self._buffer[0] != 0) or (self._buffer[1] != 0):
194199
raise RuntimeError("Self test failed")
195200

196-
def _read_data(self):
201+
def _read_data(self) -> None:
197202
"""Reads the temp/hum/co2 from the sensor and caches it"""
198203
self._send_command(_SCD4X_READMEASUREMENT, cmd_delay=0.001)
199204
self._read_reply(self._buffer, 9)
@@ -204,14 +209,14 @@ def _read_data(self):
204209
self._relative_humidity = 100 * (humi / 2**16)
205210

206211
@property
207-
def data_ready(self):
212+
def data_ready(self) -> bool:
208213
"""Check the sensor to see if new data is available"""
209214
self._send_command(_SCD4X_DATAREADY, cmd_delay=0.001)
210215
self._read_reply(self._buffer, 3)
211216
return not ((self._buffer[0] & 0x07 == 0) and (self._buffer[1] == 0))
212217

213218
@property
214-
def serial_number(self):
219+
def serial_number(self) -> Tuple[int, int, int, int, int, int]:
215220
"""Request a 6-tuple containing the unique serial number for this sensor"""
216221
self._send_command(_SCD4X_SERIALNUMBER, cmd_delay=0.001)
217222
self._read_reply(self._buffer, 9)
@@ -224,11 +229,11 @@ def serial_number(self):
224229
self._buffer[7],
225230
)
226231

227-
def stop_periodic_measurement(self):
232+
def stop_periodic_measurement(self) -> None:
228233
"""Stop measurement mode"""
229234
self._send_command(_SCD4X_STOPPERIODICMEASUREMENT, cmd_delay=0.5)
230235

231-
def start_periodic_measurement(self):
236+
def start_periodic_measurement(self) -> None:
232237
"""Put sensor into working mode, about 5s per measurement
233238
234239
.. note::
@@ -247,25 +252,25 @@ def start_periodic_measurement(self):
247252
"""
248253
self._send_command(_SCD4X_STARTPERIODICMEASUREMENT)
249254

250-
def start_low_periodic_measurement(self):
255+
def start_low_periodic_measurement(self) -> None:
251256
"""Put sensor into low power working mode, about 30s per measurement. See
252257
:meth:`start_periodic_measurement() <adafruit_scd4x.SCD4X.start_perodic_measurement>`
253258
for more details.
254259
"""
255260
self._send_command(_SCD4X_STARTLOWPOWERPERIODICMEASUREMENT)
256261

257-
def persist_settings(self):
262+
def persist_settings(self) -> None:
258263
"""Save temperature offset, altitude offset, and selfcal enable settings to EEPROM"""
259264
self._send_command(_SCD4X_PERSISTSETTINGS, cmd_delay=0.8)
260265

261-
def set_ambient_pressure(self, ambient_pressure):
266+
def set_ambient_pressure(self, ambient_pressure: int) -> None:
262267
"""Set the ambient pressure in hPa at any time to adjust CO2 calculations"""
263268
if ambient_pressure < 0 or ambient_pressure > 65535:
264269
raise AttributeError("`ambient_pressure` must be from 0~65535 hPascals")
265270
self._set_command_value(_SCD4X_SETPRESSURE, ambient_pressure)
266271

267272
@property
268-
def temperature_offset(self):
273+
def temperature_offset(self) -> float:
269274
"""Specifies the offset to be added to the reported measurements to account for a bias in
270275
the measured signal. Value is in degrees Celsius with a resolution of 0.01 degrees and a
271276
maximum value of 374 C
@@ -281,7 +286,7 @@ def temperature_offset(self):
281286
return 175.0 * temp / 2**16
282287

283288
@temperature_offset.setter
284-
def temperature_offset(self, offset):
289+
def temperature_offset(self, offset: Union[int, float]) -> None:
285290
if offset > 374:
286291
raise AttributeError(
287292
"Offset value must be less than or equal to 374 degrees Celsius"
@@ -290,7 +295,7 @@ def temperature_offset(self, offset):
290295
self._set_command_value(_SCD4X_SETTEMPOFFSET, temp)
291296

292297
@property
293-
def altitude(self):
298+
def altitude(self) -> int:
294299
"""Specifies the altitude at the measurement location in meters above sea level. Setting
295300
this value adjusts the CO2 measurement calculations to account for the air pressure's effect
296301
on readings.
@@ -304,12 +309,12 @@ def altitude(self):
304309
return (self._buffer[0] << 8) | self._buffer[1]
305310

306311
@altitude.setter
307-
def altitude(self, height):
312+
def altitude(self, height: int) -> None:
308313
if height > 65535:
309314
raise AttributeError("Height must be less than or equal to 65535 meters")
310315
self._set_command_value(_SCD4X_SETALTITUDE, height)
311316

312-
def _check_buffer_crc(self, buf):
317+
def _check_buffer_crc(self, buf: bytearray) -> bool:
313318
for i in range(0, len(buf), 3):
314319
self._crc_buffer[0] = buf[i]
315320
self._crc_buffer[1] = buf[i + 1]
@@ -347,7 +352,7 @@ def _read_reply(self, buff, num):
347352
self._check_buffer_crc(self._buffer[0:num])
348353

349354
@staticmethod
350-
def _crc8(buffer):
355+
def _crc8(buffer: bytearray) -> int:
351356
crc = 0xFF
352357
for byte in buffer:
353358
crc ^= byte

0 commit comments

Comments
 (0)