Skip to content

Commit a2baefe

Browse files
committed
Add type annotations
1 parent ba7146c commit a2baefe

File tree

4 files changed

+33
-17
lines changed

4 files changed

+33
-17
lines changed

adafruit_ble_adafruit/adafruit_service.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@
3939
from adafruit_ble.uuid import VendorUUID
4040
from adafruit_ble.services import Service
4141

42+
try:
43+
from typing import Optional
44+
from _bleio import ScanEntry
45+
except ImportError:
46+
pass
47+
4248

4349
_MANUFACTURING_DATA_ADT = const(0xFF)
4450
_ADAFRUIT_COMPANY_ID = const(0x0822)
@@ -69,7 +75,7 @@ class AdafruitServerAdvertisement(
6975
pid = ManufacturerDataField(_PID_DATA_ID, "<H")
7076
"""The USB PID (product id) for this board."""
7177

72-
def __init__(self, *, entry=None):
78+
def __init__(self, *, entry: Optional[ScanEntry] = None) -> None:
7379
super().__init__(entry=entry)
7480
# Return early if things have been set by an existing ScanEntry.
7581
if entry:
@@ -84,14 +90,14 @@ class AdafruitService(Service):
8490
"""Common superclass for all Adafruit board services."""
8591

8692
@staticmethod
87-
def adafruit_service_uuid(n):
93+
def adafruit_service_uuid(n: int) -> VendorUUID:
8894
"""Generate a VendorUUID which fills in a 16-bit value in the standard
8995
Adafruit Service UUID: ADAFnnnn-C332-42A8-93BD-25E905756CB8.
9096
"""
9197
return VendorUUID("ADAF{:04x}-C332-42A8-93BD-25E905756CB8".format(n))
9298

9399
@classmethod
94-
def measurement_period_charac(cls, msecs=1000):
100+
def measurement_period_charac(cls, msecs: int = 1000) -> Int32Characteristic:
95101
"""Create a measurement_period Characteristic for use by a subclass."""
96102
return Int32Characteristic(
97103
uuid=cls.adafruit_service_uuid(0x0001),
@@ -100,7 +106,7 @@ def measurement_period_charac(cls, msecs=1000):
100106
)
101107

102108
@classmethod
103-
def service_version_charac(cls, version=1):
109+
def service_version_charac(cls, version: int = 1) -> Uint32Characteristic:
104110
"""Create a service_version Characteristic for use by a subclass."""
105111
return Uint32Characteristic(
106112
uuid=cls.adafruit_service_uuid(0x0002),

adafruit_ble_adafruit/addressable_pixel_service.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
from adafruit_ble.characteristics.int import Uint8Characteristic, Uint16Characteristic
2525
from adafruit_ble_adafruit.adafruit_service import AdafruitService
2626

27+
try:
28+
from typing import Optional
29+
except ImportError:
30+
pass
31+
2732
PixelValues = namedtuple(
2833
"PixelValues",
2934
("start", "write_now", "data"),
@@ -57,14 +62,14 @@ class _PixelPacket(ComplexCharacteristic):
5762

5863
uuid = AdafruitService.adafruit_service_uuid(0x903)
5964

60-
def __init__(self):
65+
def __init__(self) -> None:
6166
super().__init__(
6267
properties=Characteristic.WRITE,
6368
read_perm=Attribute.NO_ACCESS,
6469
max_length=self.MAX_LENGTH,
6570
)
6671

67-
def bind(self, service):
72+
def bind(self, service: "AddressablePixelService") -> _bleio.PacketBuffer:
6873
"""Binds the characteristic to the given Service."""
6974
bound_characteristic = super().bind(service)
7075
return _bleio.PacketBuffer(bound_characteristic, buffer_size=1)
@@ -98,12 +103,12 @@ class AddressablePixelService(AdafruitService):
98103
_pixel_packet = _PixelPacket()
99104
"""Pixel-setting data."""
100105

101-
def __init__(self, service=None):
106+
def __init__(self, service: Optional["AddressablePixelService"] = None) -> None:
102107
self._pixel_packet_buf = bytearray(_PixelPacket.MAX_LENGTH)
103108
super().__init__(service=service)
104109

105110
@property
106-
def values(self):
111+
def values(self) -> Optional[PixelValues]:
107112
"""Return a tuple (start, write_now, data) corresponding to the
108113
different parts of ``_pixel_packet``.
109114
"""

adafruit_ble_adafruit/button_service.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class ButtonService(AdafruitService):
3838
measurement_period = AdafruitService.measurement_period_charac(0)
3939
"""Initially 0: send notification only on changes. -1 means stop reading."""
4040

41-
def set_pressed(self, switch, button_a, button_b):
41+
def set_pressed(self, switch: bool, button_a: bool, button_b: bool) -> None:
4242
"""Update the pressed value all at once."""
4343
pressed = 0
4444
if switch:
@@ -51,16 +51,16 @@ def set_pressed(self, switch, button_a, button_b):
5151
self.pressed = pressed
5252

5353
@property
54-
def switch(self):
54+
def switch(self) -> bool:
5555
"""``True`` when the slide switch is set to the left; ``False`` when to the right."""
5656
return bool(self.pressed & 0x1)
5757

5858
@property
59-
def button_a(self):
59+
def button_a(self) -> bool:
6060
"""``True`` when Button A is pressed."""
6161
return bool(self.pressed & 0x2)
6262

6363
@property
64-
def button_b(self):
64+
def button_b(self) -> bool:
6565
"""``True`` when Button B is pressed."""
6666
return bool(self.pressed & 0x4)

adafruit_ble_adafruit/tone_service.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,27 @@
2222
from adafruit_ble.characteristics import Characteristic, ComplexCharacteristic
2323
from adafruit_ble_adafruit.adafruit_service import AdafruitService
2424

25+
try:
26+
from typing import Optional, Tuple
27+
except ImportError:
28+
pass
29+
2530

2631
class _TonePacket(ComplexCharacteristic):
2732
uuid = AdafruitService.adafruit_service_uuid(0xC01)
2833

2934
format = "<HI"
3035
format_size = struct.calcsize(format)
3136

32-
def __init__(self):
37+
def __init__(self) -> None:
3338
super().__init__(
3439
properties=Characteristic.WRITE,
3540
read_perm=Attribute.NO_ACCESS,
3641
max_length=self.format_size,
3742
fixed_length=True,
3843
)
3944

40-
def bind(self, service):
45+
def bind(self, service: "ToneService") -> PacketBuffer:
4146
"""Binds the characteristic to the given Service."""
4247
bound_characteristic = super().bind(service)
4348
return PacketBuffer(bound_characteristic, buffer_size=1)
@@ -54,20 +59,20 @@ class ToneService(AdafruitService):
5459
if duration == 0, play indefinitely.
5560
"""
5661

57-
def __init__(self, service=None):
62+
def __init__(self, service: Optional["ToneService"] = None) -> None:
5863
super().__init__(service=service)
5964
self._tone_packet_buf = bytearray(_TonePacket.format_size)
6065

6166
@property
62-
def tone(self):
67+
def tone(self) -> Optional[Tuple[int, int]]:
6368
"""Return (frequency, duration), or None if no value available"""
6469
buf = self._tone_packet_buf
6570
if self._tone_packet.readinto(buf) == 0:
6671
# No new values available.
6772
return None
6873
return struct.unpack(_TonePacket.format, buf)
6974

70-
def play(self, frequency, duration):
75+
def play(self, frequency: int, duration: float) -> None:
7176
"""
7277
Frequency is in Hz. If frequency == 0, a tone being played is turned off.
7378
Duration is in seconds. If duration == 0, play indefinitely.

0 commit comments

Comments
 (0)