diff --git a/adafruit_dht.py b/adafruit_dht.py index d11881b..bdef594 100644 --- a/adafruit_dht.py +++ b/adafruit_dht.py @@ -39,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" @@ -49,7 +55,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 @@ -68,16 +74,16 @@ def __init__(self, dht11, pin, trig_wait, use_pulseio): # 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, 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 +112,7 @@ def _pulses_to_binary(self, pulses, start, stop): return binary - def _get_pulses_pulseio(self): + 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 @@ -134,7 +140,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: """_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 +185,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. @@ -250,7 +256,7 @@ def measure(self): self._humidity = new_humidity @property - def temperature(self): + 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 @@ -260,7 +266,7 @@ def temperature(self): return self._temperature @property - def humidity(self): + 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 @@ -276,7 +282,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 +292,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)