From 24a4c6c0e491c97d7f16e61ccea395343e848bbc Mon Sep 17 00:00:00 2001 From: Tyler Crumpton Date: Sun, 31 Oct 2021 16:58:53 -0500 Subject: [PATCH 1/5] Add type hints --- adafruit_dht.py | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/adafruit_dht.py b/adafruit_dht.py index d11881b..8a34d93 100644 --- a/adafruit_dht.py +++ b/adafruit_dht.py @@ -30,6 +30,7 @@ import time from os import uname from digitalio import DigitalInOut, Pull, Direction +from microcontroller import Pin _USE_PULSEIO = False try: @@ -49,7 +50,7 @@ class DHTBase: __hiLevel = 51 - def __init__(self, dht11, pin, trig_wait, use_pulseio): + def __init__(self, dht11: bool, pin: Pin, trig_wait: int, use_pulseio: bool): """ :param boolean dht11: True if device is DHT11, otherwise DHT22. :param ~board.Pin pin: digital pin used for communication @@ -59,25 +60,25 @@ def __init__(self, dht11, pin, trig_wait, use_pulseio): self._dht11 = dht11 self._pin = pin self._trig_wait = trig_wait - self._last_called = 0 - self._humidity = None - self._temperature = None + self._last_called: float = 0 + self._humidity: int | float | None = None + self._temperature: int | float | None = None self._use_pulseio = use_pulseio if "Linux" not in uname() and not self._use_pulseio: raise Exception("Bitbanging is not supported when using CircuitPython.") # We don't use a context because linux-based systems are sluggish # and we're better off having a running process if self._use_pulseio: - self.pulse_in = PulseIn(self._pin, 81, True) + self.pulse_in = PulseIn(self._pin, maxlen=81, idle_state=True) self.pulse_in.pause() - def exit(self): - """ Cleans up the PulseIn process. Must be called explicitly """ + def exit(self) -> None: + """Cleans up the PulseIn process. Must be called explicitly""" if self._use_pulseio: print("De-initializing self.pulse_in") self.pulse_in.deinit() - def _pulses_to_binary(self, pulses, start, stop): + def _pulses_to_binary(self, pulses: array.array[int], start: int, stop: int) -> int: """Takes pulses, a list of transition times, and converts them to a 1's or 0's. The pulses array contains the transition times. pulses starts with a low transition time followed by a high transistion time. @@ -106,7 +107,7 @@ def _pulses_to_binary(self, pulses, start, stop): return binary - def _get_pulses_pulseio(self): + def _get_pulses_pulseio(self) -> array.array[int]: """_get_pulses implements the communication protocol for DHT11 and DHT22 type devices. It sends a start signal of a specific length and listens and measures the @@ -134,7 +135,7 @@ def _get_pulses_pulseio(self): pulses.append(self.pulse_in.popleft()) return pulses - def _get_pulses_bitbang(self): + def _get_pulses_bitbang(self) -> array.array[int]: """_get_pulses implements the communication protcol for DHT11 and DHT22 type devices. It sends a start signal of a specific length and listens and measures the @@ -179,7 +180,7 @@ def _get_pulses_bitbang(self): pulses.append(min(pulses_micro_sec, 65535)) return pulses - def measure(self): + def measure(self) -> None: """measure runs the communications to the DHT11/22 type device. if successful, the class properties temperature and humidity will return the reading returned from the device. @@ -197,8 +198,8 @@ def measure(self): ): self._last_called = time.monotonic() - new_temperature = 0 - new_humidity = 0 + new_temperature: int | float = 0 + new_humidity: int | float = 0 if self._use_pulseio: pulses = self._get_pulses_pulseio() @@ -250,7 +251,7 @@ def measure(self): self._humidity = new_humidity @property - def temperature(self): + def temperature(self) -> int | float | None: """temperature current reading. It makes sure a reading is available Raises RuntimeError exception for checksum failure and for insufficient @@ -260,7 +261,7 @@ def temperature(self): return self._temperature @property - def humidity(self): + def humidity(self) -> int | float | None: """humidity current reading. It makes sure a reading is available Raises RuntimeError exception for checksum failure and for insufficient @@ -276,7 +277,7 @@ class DHT11(DHTBase): :param ~board.Pin pin: digital pin used for communication """ - def __init__(self, pin, use_pulseio=_USE_PULSEIO): + def __init__(self, pin: Pin, use_pulseio: bool = _USE_PULSEIO): super().__init__(True, pin, 18000, use_pulseio) @@ -286,5 +287,5 @@ class DHT22(DHTBase): :param ~board.Pin pin: digital pin used for communication """ - def __init__(self, pin, use_pulseio=_USE_PULSEIO): + def __init__(self, pin: Pin, use_pulseio: bool = _USE_PULSEIO): super().__init__(False, pin, 1000, use_pulseio) From f88349e02ef537ad63c53e304b6b9d21c333deb0 Mon Sep 17 00:00:00 2001 From: Tyler Crumpton Date: Sun, 31 Oct 2021 17:11:26 -0500 Subject: [PATCH 2/5] Fix Union types --- adafruit_dht.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/adafruit_dht.py b/adafruit_dht.py index 8a34d93..b0ffecf 100644 --- a/adafruit_dht.py +++ b/adafruit_dht.py @@ -30,7 +30,6 @@ import time from os import uname from digitalio import DigitalInOut, Pull, Direction -from microcontroller import Pin _USE_PULSEIO = False try: @@ -40,6 +39,12 @@ except (ImportError, NotImplementedError): pass # This is OK, we'll try to bitbang it! +try: + # Used only for typing + from typing import Union + from microcontroller import Pin +except ImportError: + pass __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_DHT.git" @@ -61,8 +66,8 @@ def __init__(self, dht11: bool, pin: Pin, trig_wait: int, use_pulseio: bool): self._pin = pin self._trig_wait = trig_wait self._last_called: float = 0 - self._humidity: int | float | None = None - self._temperature: int | float | None = None + self._humidity: Union[int, float, None] = None + self._temperature: Union[int, float, None] = None self._use_pulseio = use_pulseio if "Linux" not in uname() and not self._use_pulseio: raise Exception("Bitbanging is not supported when using CircuitPython.") @@ -198,8 +203,8 @@ def measure(self) -> None: ): self._last_called = time.monotonic() - new_temperature: int | float = 0 - new_humidity: int | float = 0 + new_temperature: Union[int, float] = 0 + new_humidity: Union[int, float] = 0 if self._use_pulseio: pulses = self._get_pulses_pulseio() @@ -251,7 +256,7 @@ def measure(self) -> None: self._humidity = new_humidity @property - def temperature(self) -> int | float | None: + def temperature(self) -> Union[int, float, None]: """temperature current reading. It makes sure a reading is available Raises RuntimeError exception for checksum failure and for insufficient @@ -261,7 +266,7 @@ def temperature(self) -> int | float | None: return self._temperature @property - def humidity(self) -> int | float | None: + def humidity(self) -> Union[int, float, None]: """humidity current reading. It makes sure a reading is available Raises RuntimeError exception for checksum failure and for insufficient From a6e68f3ffb8d53f6c86ad7a6ccf1ee66a9154b81 Mon Sep 17 00:00:00 2001 From: Tyler Crumpton Date: Sun, 31 Oct 2021 17:19:17 -0500 Subject: [PATCH 3/5] Remove variable types --- adafruit_dht.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/adafruit_dht.py b/adafruit_dht.py index b0ffecf..44a4aab 100644 --- a/adafruit_dht.py +++ b/adafruit_dht.py @@ -65,9 +65,9 @@ def __init__(self, dht11: bool, pin: Pin, trig_wait: int, use_pulseio: bool): self._dht11 = dht11 self._pin = pin self._trig_wait = trig_wait - self._last_called: float = 0 - self._humidity: Union[int, float, None] = None - self._temperature: Union[int, float, None] = None + self._last_called = 0 + self._humidity = None + self._temperature = None self._use_pulseio = use_pulseio if "Linux" not in uname() and not self._use_pulseio: raise Exception("Bitbanging is not supported when using CircuitPython.") @@ -203,8 +203,8 @@ def measure(self) -> None: ): self._last_called = time.monotonic() - new_temperature: Union[int, float] = 0 - new_humidity: Union[int, float] = 0 + new_temperature = 0 + new_humidity = 0 if self._use_pulseio: pulses = self._get_pulses_pulseio() From ebe3bbc3a7b9fbcfd9046823138789260bda192c Mon Sep 17 00:00:00 2001 From: Tyler Crumpton Date: Sun, 31 Oct 2021 17:44:01 -0500 Subject: [PATCH 4/5] Fix unhappy array typing --- adafruit_dht.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_dht.py b/adafruit_dht.py index 44a4aab..73d53bc 100644 --- a/adafruit_dht.py +++ b/adafruit_dht.py @@ -112,7 +112,7 @@ def _pulses_to_binary(self, pulses: array.array[int], start: int, stop: int) -> return binary - def _get_pulses_pulseio(self) -> array.array[int]: + def _get_pulses_pulseio(self) -> array.array: """_get_pulses implements the communication protocol for DHT11 and DHT22 type devices. It sends a start signal of a specific length and listens and measures the From 0b71c903a065d28b4c9f2301e1089b9463e12dcd Mon Sep 17 00:00:00 2001 From: Tyler Crumpton Date: Sun, 31 Oct 2021 17:50:16 -0500 Subject: [PATCH 5/5] Fix other array type hints --- adafruit_dht.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_dht.py b/adafruit_dht.py index 73d53bc..bdef594 100644 --- a/adafruit_dht.py +++ b/adafruit_dht.py @@ -83,7 +83,7 @@ def exit(self) -> None: print("De-initializing self.pulse_in") self.pulse_in.deinit() - def _pulses_to_binary(self, pulses: array.array[int], start: int, stop: int) -> int: + def _pulses_to_binary(self, pulses: array.array, start: int, stop: int) -> int: """Takes pulses, a list of transition times, and converts them to a 1's or 0's. The pulses array contains the transition times. pulses starts with a low transition time followed by a high transistion time. @@ -140,7 +140,7 @@ def _get_pulses_pulseio(self) -> array.array: pulses.append(self.pulse_in.popleft()) return pulses - def _get_pulses_bitbang(self) -> array.array[int]: + def _get_pulses_bitbang(self) -> array.array: """_get_pulses implements the communication protcol for DHT11 and DHT22 type devices. It sends a start signal of a specific length and listens and measures the