Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type hints #76

Merged
merged 5 commits into from
Nov 1, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions adafruit_dht.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)


Expand All @@ -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)