Skip to content

Commit 24a4c6c

Browse files
committed
Add type hints
1 parent a1e05a0 commit 24a4c6c

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

adafruit_dht.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import time
3131
from os import uname
3232
from digitalio import DigitalInOut, Pull, Direction
33+
from microcontroller import Pin
3334

3435
_USE_PULSEIO = False
3536
try:
@@ -49,7 +50,7 @@ class DHTBase:
4950

5051
__hiLevel = 51
5152

52-
def __init__(self, dht11, pin, trig_wait, use_pulseio):
53+
def __init__(self, dht11: bool, pin: Pin, trig_wait: int, use_pulseio: bool):
5354
"""
5455
:param boolean dht11: True if device is DHT11, otherwise DHT22.
5556
:param ~board.Pin pin: digital pin used for communication
@@ -59,25 +60,25 @@ def __init__(self, dht11, pin, trig_wait, use_pulseio):
5960
self._dht11 = dht11
6061
self._pin = pin
6162
self._trig_wait = trig_wait
62-
self._last_called = 0
63-
self._humidity = None
64-
self._temperature = None
63+
self._last_called: float = 0
64+
self._humidity: int | float | None = None
65+
self._temperature: int | float | None = None
6566
self._use_pulseio = use_pulseio
6667
if "Linux" not in uname() and not self._use_pulseio:
6768
raise Exception("Bitbanging is not supported when using CircuitPython.")
6869
# We don't use a context because linux-based systems are sluggish
6970
# and we're better off having a running process
7071
if self._use_pulseio:
71-
self.pulse_in = PulseIn(self._pin, 81, True)
72+
self.pulse_in = PulseIn(self._pin, maxlen=81, idle_state=True)
7273
self.pulse_in.pause()
7374

74-
def exit(self):
75-
""" Cleans up the PulseIn process. Must be called explicitly """
75+
def exit(self) -> None:
76+
"""Cleans up the PulseIn process. Must be called explicitly"""
7677
if self._use_pulseio:
7778
print("De-initializing self.pulse_in")
7879
self.pulse_in.deinit()
7980

80-
def _pulses_to_binary(self, pulses, start, stop):
81+
def _pulses_to_binary(self, pulses: array.array[int], start: int, stop: int) -> int:
8182
"""Takes pulses, a list of transition times, and converts
8283
them to a 1's or 0's. The pulses array contains the transition times.
8384
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):
106107

107108
return binary
108109

109-
def _get_pulses_pulseio(self):
110+
def _get_pulses_pulseio(self) -> array.array[int]:
110111
"""_get_pulses implements the communication protocol for
111112
DHT11 and DHT22 type devices. It sends a start signal
112113
of a specific length and listens and measures the
@@ -134,7 +135,7 @@ def _get_pulses_pulseio(self):
134135
pulses.append(self.pulse_in.popleft())
135136
return pulses
136137

137-
def _get_pulses_bitbang(self):
138+
def _get_pulses_bitbang(self) -> array.array[int]:
138139
"""_get_pulses implements the communication protcol for
139140
DHT11 and DHT22 type devices. It sends a start signal
140141
of a specific length and listens and measures the
@@ -179,7 +180,7 @@ def _get_pulses_bitbang(self):
179180
pulses.append(min(pulses_micro_sec, 65535))
180181
return pulses
181182

182-
def measure(self):
183+
def measure(self) -> None:
183184
"""measure runs the communications to the DHT11/22 type device.
184185
if successful, the class properties temperature and humidity will
185186
return the reading returned from the device.
@@ -197,8 +198,8 @@ def measure(self):
197198
):
198199
self._last_called = time.monotonic()
199200

200-
new_temperature = 0
201-
new_humidity = 0
201+
new_temperature: int | float = 0
202+
new_humidity: int | float = 0
202203

203204
if self._use_pulseio:
204205
pulses = self._get_pulses_pulseio()
@@ -250,7 +251,7 @@ def measure(self):
250251
self._humidity = new_humidity
251252

252253
@property
253-
def temperature(self):
254+
def temperature(self) -> int | float | None:
254255
"""temperature current reading. It makes sure a reading is available
255256
256257
Raises RuntimeError exception for checksum failure and for insufficient
@@ -260,7 +261,7 @@ def temperature(self):
260261
return self._temperature
261262

262263
@property
263-
def humidity(self):
264+
def humidity(self) -> int | float | None:
264265
"""humidity current reading. It makes sure a reading is available
265266
266267
Raises RuntimeError exception for checksum failure and for insufficient
@@ -276,7 +277,7 @@ class DHT11(DHTBase):
276277
:param ~board.Pin pin: digital pin used for communication
277278
"""
278279

279-
def __init__(self, pin, use_pulseio=_USE_PULSEIO):
280+
def __init__(self, pin: Pin, use_pulseio: bool = _USE_PULSEIO):
280281
super().__init__(True, pin, 18000, use_pulseio)
281282

282283

@@ -286,5 +287,5 @@ class DHT22(DHTBase):
286287
:param ~board.Pin pin: digital pin used for communication
287288
"""
288289

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

0 commit comments

Comments
 (0)