From 48a26e1ff22ebe930ff98a420f7c8b1c0faa9d9d Mon Sep 17 00:00:00 2001 From: Thomas Franks Date: Mon, 10 Oct 2022 16:45:35 -0400 Subject: [PATCH 1/3] Add Missing Type Annotations --- adafruit_sgp30.py | 44 +++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/adafruit_sgp30.py b/adafruit_sgp30.py index 0b68b3f..7f20599 100755 --- a/adafruit_sgp30.py +++ b/adafruit_sgp30.py @@ -29,6 +29,12 @@ from adafruit_bus_device.i2c_device import I2CDevice from micropython import const +try: + from typing import List + from busio import I2C +except ImportError: + pass + __version__ = "0.0.0+auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SGP30.git" @@ -80,7 +86,7 @@ class Adafruit_SGP30: """ - def __init__(self, i2c, address=_SGP30_DEFAULT_I2C_ADDR): + def __init__(self, i2c: I2C, address: int = _SGP30_DEFAULT_I2C_ADDR) -> None: """Initialize the sensor, get the serial # and verify that we found a proper SGP30""" self._device = I2CDevice(i2c, address) @@ -94,61 +100,63 @@ def __init__(self, i2c, address=_SGP30_DEFAULT_I2C_ADDR): @property # pylint: disable=invalid-name - def TVOC(self): + def TVOC(self) -> int: """Total Volatile Organic Compound in parts per billion.""" return self.iaq_measure()[1] @property # pylint: disable=invalid-name - def baseline_TVOC(self): + def baseline_TVOC(self) -> int: """Total Volatile Organic Compound baseline value""" return self.get_iaq_baseline()[1] @property # pylint: disable=invalid-name - def eCO2(self): + def eCO2(self) -> int: """Carbon Dioxide Equivalent in parts per million""" return self.iaq_measure()[0] @property # pylint: disable=invalid-name - def baseline_eCO2(self): + def baseline_eCO2(self) -> int: """Carbon Dioxide Equivalent baseline value""" return self.get_iaq_baseline()[0] @property # pylint: disable=invalid-name - def Ethanol(self): + def Ethanol(self) -> int: """Ethanol Raw Signal in ticks""" return self.raw_measure()[1] @property # pylint: disable=invalid-name - def H2(self): + def H2(self) -> int: """H2 Raw Signal in ticks""" return self.raw_measure()[0] - def iaq_init(self): + def iaq_init(self) -> List[int]: """Initialize the IAQ algorithm""" # name, command, signals, delay self._run_profile(["iaq_init", [0x20, 0x03], 0, 0.01]) - def iaq_measure(self): + def iaq_measure(self) -> List[int]: """Measure the eCO2 and TVOC""" # name, command, signals, delay return self._run_profile(["iaq_measure", [0x20, 0x08], 2, 0.05]) - def raw_measure(self): + def raw_measure(self) -> List[int]: """Measure H2 and Ethanol (Raw Signals)""" # name, command, signals, delay return self._run_profile(["raw_measure", [0x20, 0x50], 2, 0.025]) - def get_iaq_baseline(self): + def get_iaq_baseline(self) -> List[int]: """Retreive the IAQ algorithm baseline for eCO2 and TVOC""" # name, command, signals, delay return self._run_profile(["iaq_get_baseline", [0x20, 0x15], 2, 0.01]) - def set_iaq_baseline(self, eCO2, TVOC): # pylint: disable=invalid-name + def set_iaq_baseline( # pylint: disable=invalid-name + self, eCO2: int, TVOC: int + ) -> None: """Set the previously recorded IAQ algorithm baseline for eCO2 and TVOC""" if eCO2 == 0 and TVOC == 0: raise RuntimeError("Invalid baseline") @@ -159,7 +167,7 @@ def set_iaq_baseline(self, eCO2, TVOC): # pylint: disable=invalid-name buffer += arr self._run_profile(["iaq_set_baseline", [0x20, 0x1E] + buffer, 0, 0.01]) - def set_iaq_humidity(self, gramsPM3): # pylint: disable=invalid-name + def set_iaq_humidity(self, gramsPM3: float) -> None: # pylint: disable=invalid-name """Set the humidity in g/m3 for eCO2 and TVOC compensation algorithm""" tmp = int(gramsPM3 * 256) buffer = [] @@ -169,7 +177,7 @@ def set_iaq_humidity(self, gramsPM3): # pylint: disable=invalid-name buffer += arr self._run_profile(["iaq_set_humidity", [0x20, 0x61] + buffer, 0, 0.01]) - def set_iaq_relative_humidity(self, celsius, relative_humidity): + def set_iaq_relative_humidity(self, celsius: float, relative_humidity: float): """ Set the humidity in g/m3 for eCo2 and TVOC compensation algorithm. The absolute humidity is calculated from the temperature (Celsius) @@ -185,7 +193,7 @@ def set_iaq_relative_humidity(self, celsius, relative_humidity): # Low level command functions - def _run_profile(self, profile): + def _run_profile(self, profile: List[str, List[int], int, float]) -> List[int]: """Run an SGP 'profile' which is a named command set""" # pylint: disable=unused-variable name, command, signals, delay = profile @@ -195,7 +203,9 @@ def _run_profile(self, profile): # (name, ["0x%02x" % i for i in command], signals, delay)) return self._i2c_read_words_from_cmd(command, delay, signals) - def _i2c_read_words_from_cmd(self, command, delay, reply_size): + def _i2c_read_words_from_cmd( + self, command: List[int], delay: float, reply_size: int + ) -> List[int]: """Run an SGP command query, get a reply and CRC results if necessary""" with self._device: self._device.write(bytes(command)) @@ -216,7 +226,7 @@ def _i2c_read_words_from_cmd(self, command, delay, reply_size): return result # pylint: disable=no-self-use - def _generate_crc(self, data): + def _generate_crc(self, data: bytearray) -> int: """8-bit CRC algorithm for checking data""" crc = _SGP30_CRC8_INIT # calculates 8-Bit checksum with given polynomial From 05f595f6f0a776c3e365b815843b90ed8ae923e5 Mon Sep 17 00:00:00 2001 From: Thomas Franks Date: Mon, 10 Oct 2022 16:54:07 -0400 Subject: [PATCH 2/3] fix job run error - Replace a List with Tuple --- adafruit_sgp30.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_sgp30.py b/adafruit_sgp30.py index 7f20599..8c53f0b 100755 --- a/adafruit_sgp30.py +++ b/adafruit_sgp30.py @@ -30,7 +30,7 @@ from micropython import const try: - from typing import List + from typing import List, Tuple from busio import I2C except ImportError: pass @@ -193,7 +193,7 @@ def set_iaq_relative_humidity(self, celsius: float, relative_humidity: float): # Low level command functions - def _run_profile(self, profile: List[str, List[int], int, float]) -> List[int]: + def _run_profile(self, profile: Tuple[str, List[int], int, float]) -> List[int]: """Run an SGP 'profile' which is a named command set""" # pylint: disable=unused-variable name, command, signals, delay = profile From 49ca754f15779d08a4b567b5f6885fdd5f9c8c90 Mon Sep 17 00:00:00 2001 From: Thomas Franks Date: Mon, 17 Oct 2022 16:12:42 -0400 Subject: [PATCH 3/3] fix job run error - Replace a List with Tuple --- adafruit_sgp30.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/adafruit_sgp30.py b/adafruit_sgp30.py index 8c53f0b..d0ca369 100755 --- a/adafruit_sgp30.py +++ b/adafruit_sgp30.py @@ -137,22 +137,22 @@ def H2(self) -> int: def iaq_init(self) -> List[int]: """Initialize the IAQ algorithm""" # name, command, signals, delay - self._run_profile(["iaq_init", [0x20, 0x03], 0, 0.01]) + self._run_profile(("iaq_init", [0x20, 0x03], 0, 0.01)) def iaq_measure(self) -> List[int]: """Measure the eCO2 and TVOC""" # name, command, signals, delay - return self._run_profile(["iaq_measure", [0x20, 0x08], 2, 0.05]) + return self._run_profile(("iaq_measure", [0x20, 0x08], 2, 0.05)) def raw_measure(self) -> List[int]: """Measure H2 and Ethanol (Raw Signals)""" # name, command, signals, delay - return self._run_profile(["raw_measure", [0x20, 0x50], 2, 0.025]) + return self._run_profile(("raw_measure", [0x20, 0x50], 2, 0.025)) def get_iaq_baseline(self) -> List[int]: """Retreive the IAQ algorithm baseline for eCO2 and TVOC""" # name, command, signals, delay - return self._run_profile(["iaq_get_baseline", [0x20, 0x15], 2, 0.01]) + return self._run_profile(("iaq_get_baseline", [0x20, 0x15], 2, 0.01)) def set_iaq_baseline( # pylint: disable=invalid-name self, eCO2: int, TVOC: int @@ -165,7 +165,7 @@ def set_iaq_baseline( # pylint: disable=invalid-name arr = [value >> 8, value & 0xFF] arr.append(self._generate_crc(arr)) buffer += arr - self._run_profile(["iaq_set_baseline", [0x20, 0x1E] + buffer, 0, 0.01]) + self._run_profile(("iaq_set_baseline", [0x20, 0x1E] + buffer, 0, 0.01)) def set_iaq_humidity(self, gramsPM3: float) -> None: # pylint: disable=invalid-name """Set the humidity in g/m3 for eCO2 and TVOC compensation algorithm""" @@ -175,7 +175,7 @@ def set_iaq_humidity(self, gramsPM3: float) -> None: # pylint: disable=invalid- arr = [value >> 8, value & 0xFF] arr.append(self._generate_crc(arr)) buffer += arr - self._run_profile(["iaq_set_humidity", [0x20, 0x61] + buffer, 0, 0.01]) + self._run_profile(("iaq_set_humidity", [0x20, 0x61] + buffer, 0, 0.01)) def set_iaq_relative_humidity(self, celsius: float, relative_humidity: float): """