Skip to content

Add typing #22

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

Merged
merged 3 commits into from
Dec 13, 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
24 changes: 19 additions & 5 deletions adafruit_ble_broadcastnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,28 @@
from adafruit_ble.advertising import Advertisement, LazyObjectField
from adafruit_ble.advertising.standard import ManufacturerData, ManufacturerDataField

try:
from typing import Optional
from _bleio import ScanEntry
except ImportError:
pass

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BLE_BroadcastNet.git"

_ble = adafruit_ble.BLERadio() # pylint: disable=invalid-name
_sequence_number = 0 # pylint: disable=invalid-name


def broadcast(measurement, *, broadcast_time=0.1, extended=False):
def broadcast(
measurement: "AdafruitSensorMeasurement",
*,
broadcast_time: float = 0.1,
extended: bool = False
) -> None:
"""Broadcasts the given measurement for the given broadcast time. If extended is False and the
measurement would be too long, it will be split into multiple measurements for transmission.
measurement would be too long, it will be split into multiple measurements for transmission,
each with the given broadcast time.
"""
global _sequence_number # pylint: disable=global-statement,invalid-name
for submeasurement in measurement.split(252 if extended else 31):
Expand Down Expand Up @@ -149,13 +161,15 @@ class AdafruitSensorMeasurement(Advertisement):
"""Battery voltage in millivolts. Saves two bytes over voltage and is more readable in bare
packets."""

def __init__(self, *, entry=None, sequence_number=0):
def __init__(
self, *, entry: Optional[ScanEntry] = None, sequence_number: int = 0
) -> None:
super().__init__(entry=entry)
if entry:
return
self.sequence_number = sequence_number

def __str__(self):
def __str__(self) -> str:
parts = []
for attr in dir(self.__class__):
attribute_instance = getattr(self.__class__, attr)
Expand All @@ -165,7 +179,7 @@ def __str__(self):
parts.append("{}={}".format(attr, str(value)))
return "<{} {} >".format(self.__class__.__name__, " ".join(parts))

def split(self, max_packet_size=31):
def split(self, max_packet_size: int = 31) -> "AdafruitSensorMeasurement":
"""Split the measurement into multiple measurements with the given max_packet_size. Yields
each submeasurement."""
current_size = 8 # baseline for mfg data and sequence number
Expand Down