Skip to content

Commit c90a3a9

Browse files
authored
Merge pull request #17 from HundredVisionsGuy/type-annotations
Type annotations
2 parents 9dd45a6 + 94e62b6 commit c90a3a9

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

adafruit_ble_apple_notification_center.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,16 @@
1717
* Adafruit's BLE library: https://github.com/adafruit/Adafruit_CircuitPython_BLE
1818
"""
1919

20+
from __future__ import annotations
21+
2022
import struct
2123
import time
2224

25+
try:
26+
from typing import Generator, Union, Dict, Optional, Any
27+
except ImportError:
28+
pass
29+
2330
from adafruit_ble.services import Service
2431
from adafruit_ble.uuid import VendorUUID
2532
from adafruit_ble.characteristics.stream import StreamIn, StreamOut
@@ -29,11 +36,11 @@
2936

3037

3138
class _NotificationAttribute:
32-
def __init__(self, attribute_id, *, max_length=False):
39+
def __init__(self, attribute_id: int, *, max_length: bool = False) -> None:
3340
self._id = attribute_id
3441
self._max_length = max_length
3542

36-
def __get__(self, notification, cls):
43+
def __get__(self, notification: Notification, cls: Any) -> str:
3744
if self._id in notification._attribute_cache:
3845
return notification._attribute_cache[self._id]
3946

@@ -104,14 +111,14 @@ class Notification:
104111

105112
def __init__(
106113
self,
107-
notification_id,
108-
event_flags,
109-
category_id,
110-
category_count,
114+
notification_id: int,
115+
event_flags: int,
116+
category_id: int,
117+
category_count: int,
111118
*,
112-
control_point,
113-
data_source
114-
):
119+
control_point: StreamIn,
120+
data_source: StreamOut,
121+
) -> None:
115122
self.id = notification_id # pylint: disable=invalid-name
116123
"""Integer id of the notification."""
117124

@@ -136,28 +143,28 @@ def __init__(
136143

137144
self.update(event_flags, category_id, category_count)
138145

139-
self._attribute_cache = {}
146+
self._attribute_cache: Dict[int, str] = {}
140147

141148
self.control_point = control_point
142149
self.data_source = data_source
143150

144-
def send_positive_action(self):
151+
def send_positive_action(self) -> None:
145152
"""Sends positive action on this notification. For example, to accept an IncomingCall."""
146153
cmd = 2 # ANCS_CMD_PERFORM_NOTIFICATION_ACTION,
147154
uid = self.id
148155
action_id = 0 # ANCS_ACTION_POSITIVE
149156
buffer = struct.pack("<BIB", cmd, uid, action_id)
150157
self.control_point.write(buffer)
151158

152-
def send_negative_action(self):
159+
def send_negative_action(self) -> None:
153160
"""Sends negative action on this notification. For example, to decline an IncomingCall."""
154161
cmd = 2 # ANCS_CMD_PERFORM_NOTIFICATION_ACTION,
155162
uid = self.id
156163
action_id = 1 # ANCS_ACTION_NEGATIVE
157164
buffer = struct.pack("<BIB", cmd, uid, action_id)
158165
self.control_point.write(buffer)
159166

160-
def update(self, event_flags, category_id, category_count):
167+
def update(self, event_flags: int, category_id: int, category_count: int) -> None:
161168
"""Update the notification and clear the attribute cache."""
162169
self.category_id = category_id
163170

@@ -171,7 +178,7 @@ def update(self, event_flags, category_id, category_count):
171178

172179
self._attribute_cache = {}
173180

174-
def __str__(self):
181+
def __str__(self) -> str:
175182
# pylint: disable=too-many-branches
176183
flags = []
177184
category = None
@@ -223,11 +230,11 @@ class AppleNotificationCenterService(Service):
223230
uuid=VendorUUID("9FBF120D-6301-42D9-8C58-25E699A21DBD"), buffer_size=8 * 100
224231
)
225232

226-
def __init__(self, service=None):
233+
def __init__(self, service: Service = None) -> None:
227234
super().__init__(service=service)
228-
self._active_notifications = {}
235+
self._active_notifications: Dict[tuple, Notification] = {}
229236

230-
def _update(self):
237+
def _update(self) -> Generator[Union[Notification, None], None, None]:
231238
# Pylint is incorrectly inferring the type of self.notification_source so disable no-member.
232239
while self.notification_source.in_waiting > 7: # pylint: disable=no-member
233240
buffer = self.notification_source.read(8) # pylint: disable=no-member
@@ -254,7 +261,9 @@ def _update(self):
254261
del self._active_notifications[nid]
255262
yield None
256263

257-
def wait_for_new_notifications(self, timeout=None):
264+
def wait_for_new_notifications(
265+
self, timeout: Optional[float] = None
266+
) -> Generator[Union[Notification, None], None, None]:
258267
"""Waits for new notifications and yields them. Returns on timeout, update, disconnect or
259268
clear."""
260269
start_time = time.monotonic()
@@ -267,7 +276,7 @@ def wait_for_new_notifications(self, timeout=None):
267276
yield new_notification
268277

269278
@property
270-
def active_notifications(self):
279+
def active_notifications(self) -> dict:
271280
"""A dictionary of active notifications keyed by id."""
272281
for _ in self._update():
273282
pass

0 commit comments

Comments
 (0)