Skip to content

Commit edf22ec

Browse files
authored
Merge pull request #76 from tylercrumpton/add-type-hints
Add type hints
2 parents a1e05a0 + 0b71c90 commit edf22ec

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

adafruit_dht.py

+18-12
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@
3939
except (ImportError, NotImplementedError):
4040
pass # This is OK, we'll try to bitbang it!
4141

42+
try:
43+
# Used only for typing
44+
from typing import Union
45+
from microcontroller import Pin
46+
except ImportError:
47+
pass
4248

4349
__version__ = "0.0.0-auto.0"
4450
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_DHT.git"
@@ -49,7 +55,7 @@ class DHTBase:
4955

5056
__hiLevel = 51
5157

52-
def __init__(self, dht11, pin, trig_wait, use_pulseio):
58+
def __init__(self, dht11: bool, pin: Pin, trig_wait: int, use_pulseio: bool):
5359
"""
5460
:param boolean dht11: True if device is DHT11, otherwise DHT22.
5561
:param ~board.Pin pin: digital pin used for communication
@@ -68,16 +74,16 @@ def __init__(self, dht11, pin, trig_wait, use_pulseio):
6874
# We don't use a context because linux-based systems are sluggish
6975
# and we're better off having a running process
7076
if self._use_pulseio:
71-
self.pulse_in = PulseIn(self._pin, 81, True)
77+
self.pulse_in = PulseIn(self._pin, maxlen=81, idle_state=True)
7278
self.pulse_in.pause()
7379

74-
def exit(self):
75-
""" Cleans up the PulseIn process. Must be called explicitly """
80+
def exit(self) -> None:
81+
"""Cleans up the PulseIn process. Must be called explicitly"""
7682
if self._use_pulseio:
7783
print("De-initializing self.pulse_in")
7884
self.pulse_in.deinit()
7985

80-
def _pulses_to_binary(self, pulses, start, stop):
86+
def _pulses_to_binary(self, pulses: array.array, start: int, stop: int) -> int:
8187
"""Takes pulses, a list of transition times, and converts
8288
them to a 1's or 0's. The pulses array contains the transition times.
8389
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):
106112

107113
return binary
108114

109-
def _get_pulses_pulseio(self):
115+
def _get_pulses_pulseio(self) -> array.array:
110116
"""_get_pulses implements the communication protocol for
111117
DHT11 and DHT22 type devices. It sends a start signal
112118
of a specific length and listens and measures the
@@ -134,7 +140,7 @@ def _get_pulses_pulseio(self):
134140
pulses.append(self.pulse_in.popleft())
135141
return pulses
136142

137-
def _get_pulses_bitbang(self):
143+
def _get_pulses_bitbang(self) -> array.array:
138144
"""_get_pulses implements the communication protcol for
139145
DHT11 and DHT22 type devices. It sends a start signal
140146
of a specific length and listens and measures the
@@ -179,7 +185,7 @@ def _get_pulses_bitbang(self):
179185
pulses.append(min(pulses_micro_sec, 65535))
180186
return pulses
181187

182-
def measure(self):
188+
def measure(self) -> None:
183189
"""measure runs the communications to the DHT11/22 type device.
184190
if successful, the class properties temperature and humidity will
185191
return the reading returned from the device.
@@ -250,7 +256,7 @@ def measure(self):
250256
self._humidity = new_humidity
251257

252258
@property
253-
def temperature(self):
259+
def temperature(self) -> Union[int, float, None]:
254260
"""temperature current reading. It makes sure a reading is available
255261
256262
Raises RuntimeError exception for checksum failure and for insufficient
@@ -260,7 +266,7 @@ def temperature(self):
260266
return self._temperature
261267

262268
@property
263-
def humidity(self):
269+
def humidity(self) -> Union[int, float, None]:
264270
"""humidity current reading. It makes sure a reading is available
265271
266272
Raises RuntimeError exception for checksum failure and for insufficient
@@ -276,7 +282,7 @@ class DHT11(DHTBase):
276282
:param ~board.Pin pin: digital pin used for communication
277283
"""
278284

279-
def __init__(self, pin, use_pulseio=_USE_PULSEIO):
285+
def __init__(self, pin: Pin, use_pulseio: bool = _USE_PULSEIO):
280286
super().__init__(True, pin, 18000, use_pulseio)
281287

282288

@@ -286,5 +292,5 @@ class DHT22(DHTBase):
286292
:param ~board.Pin pin: digital pin used for communication
287293
"""
288294

289-
def __init__(self, pin, use_pulseio=_USE_PULSEIO):
295+
def __init__(self, pin: Pin, use_pulseio: bool = _USE_PULSEIO):
290296
super().__init__(False, pin, 1000, use_pulseio)

0 commit comments

Comments
 (0)