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

[#19] Add type annotations #20

Merged
merged 1 commit into from
Oct 30, 2021
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions adafruit_pm25/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class PM25:

"""

def __init__(self):
def __init__(self) -> None:
# rad, ok make our internal buffer!
self._buffer = bytearray(32)
self.aqi_reading = {
Expand All @@ -68,11 +68,11 @@ def __init__(self):
"particles 100um": None,
}

def _read_into_buffer(self):
def _read_into_buffer(self) -> None:
"""Low level buffer filling function, to be overridden"""
raise NotImplementedError()

def read(self):
def read(self) -> dict:
"""Read any available data from the air quality sensor and
return a dictionary with available particulate/quality data"""
self._read_into_buffer()
Expand Down
15 changes: 12 additions & 3 deletions adafruit_pm25/i2c.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,17 @@

# imports
import time
from digitalio import Direction
from digitalio import Direction, DigitalInOut
from adafruit_bus_device.i2c_device import I2CDevice
from . import PM25

try:
# Used only for typing
import typing # pylint: disable=unused-import
from busio import I2C
except ImportError:
pass


class PM25_I2C(PM25):
"""
Expand Down Expand Up @@ -77,7 +84,9 @@ class PM25_I2C(PM25):

"""

def __init__(self, i2c_bus, reset_pin=None, address=0x12):
def __init__(
self, i2c_bus: I2C, reset_pin: DigitalInOut = None, address: int = 0x12
) -> None:
if reset_pin:
# Reset device
reset_pin.direction = Direction.OUTPUT
Expand All @@ -98,7 +107,7 @@ def __init__(self, i2c_bus, reset_pin=None, address=0x12):
raise RuntimeError("Unable to find PM2.5 device")
super().__init__()

def _read_into_buffer(self):
def _read_into_buffer(self) -> None:
with self.i2c_device as i2c:
try:
i2c.readinto(self._buffer)
Expand Down
13 changes: 10 additions & 3 deletions adafruit_pm25/uart.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,16 @@
"""

import time
from digitalio import Direction
from digitalio import Direction, DigitalInOut
from . import PM25

try:
# Used only for typing
import typing # pylint: disable=unused-import
from busio import UART
except ImportError:
pass


class PM25_UART(PM25):
"""
Expand Down Expand Up @@ -73,7 +80,7 @@ class PM25_UART(PM25):

"""

def __init__(self, uart, reset_pin=None):
def __init__(self, uart: UART, reset_pin: DigitalInOut = None):
if reset_pin:
# Reset device
reset_pin.direction = Direction.OUTPUT
Expand All @@ -86,7 +93,7 @@ def __init__(self, uart, reset_pin=None):
self._uart = uart
super().__init__()

def _read_into_buffer(self):
def _read_into_buffer(self) -> None:
while True:
b = self._uart.read(1)
if not b:
Expand Down