Skip to content

Commit 44639a0

Browse files
committed
Clean up lint
1 parent 928160d commit 44639a0

File tree

5 files changed

+82
-35
lines changed

5 files changed

+82
-35
lines changed

adafruit_ble/characteristics/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def __get__(self, service, cls=None):
141141

142142
def __set__(self, service, value):
143143
self._ensure_bound(service, value)
144-
if value == None:
144+
if value is None:
145145
value = b""
146146
bleio_characteristic = service.bleio_characteristics[self.field_name]
147147
bleio_characteristic.value = value

adafruit_ble/services/apple.py

Lines changed: 76 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ class UnknownApple1Service(Service):
4646
uuid = VendorUUID("9fa480e0-4967-4542-9390-d343dc5d04ae")
4747

4848
class _NotificationAttribute:
49-
def __init__(self, id, *, max_length=False):
50-
self._id = id
49+
def __init__(self, attribute_id, *, max_length=False):
50+
self._id = attribute_id
5151
self._max_length = max_length
5252

5353
def __get__(self, notification, cls):
@@ -72,18 +72,56 @@ def __get__(self, notification, cls):
7272
return value
7373

7474
class Notification:
75+
"""One notification that appears in the iOS notification center."""
76+
# pylint: disable=too-many-instance-attributes
77+
7578
app_id = _NotificationAttribute(0)
79+
"""String id of the app that generated the notification. It is not the name of the app. For
80+
example, Slack is "com.tinyspeck.chatlyio" and Twitter is "com.atebits.Tweetie2"."""
81+
7682
title = _NotificationAttribute(1, max_length=True)
83+
"""Title of the notification. Varies per app."""
84+
7785
subtitle = _NotificationAttribute(2, max_length=True)
86+
"""Subtitle of the notification. Varies per app."""
87+
7888
message = _NotificationAttribute(3, max_length=True)
89+
"""Message body of the notification. Varies per app."""
90+
7991
message_size = _NotificationAttribute(4)
92+
"""Total length of the message string."""
93+
8094
_raw_date = _NotificationAttribute(5)
8195
positive_action_label = _NotificationAttribute(6)
96+
"""Human readable label of the positive action."""
97+
8298
negative_action_label = _NotificationAttribute(7)
99+
"""Human readable label of the negative action."""
100+
101+
def __init__(self, notification_id, event_flags, category_id, category_count, *, control_point,
102+
data_source):
103+
self.id = notification_id # pylint: disable=invalid-name
104+
"""Integer id of the notification."""
83105

84-
def __init__(self, id, event_flags, category_id, category_count, *, control_point, data_source):
85-
self.id = id
86106
self.removed = False
107+
"""True when the notification has been cleared on the iOS device."""
108+
109+
110+
self.silent = False
111+
self.important = False
112+
self.preexisting = False
113+
"""True if the notification existed before we connected to the iOS device."""
114+
115+
self.positive_action = False
116+
"""True if the notification has a positive action to respond with. For example, this could
117+
be answering a phone call."""
118+
119+
self.negative_action = False
120+
"""True if the notification has a negative action to respond with. For example, this could
121+
be declining a phone call."""
122+
123+
self.category_count = 0
124+
"""Number of other notifications with the same category."""
87125

88126
self.update(event_flags, category_id, category_count)
89127

@@ -93,8 +131,11 @@ def __init__(self, id, event_flags, category_id, category_count, *, control_poin
93131
self.data_source = data_source
94132

95133
def update(self, event_flags, category_id, category_count):
134+
"""Update the notification and clear the attribute cache."""
96135
self.category_id = category_id
97136

137+
self.category_count = category_count
138+
98139
self.silent = (event_flags & (1 << 0)) != 0
99140
self.important = (event_flags & (1 << 1)) != 0
100141
self.preexisting = (event_flags & (1 << 2)) != 0
@@ -103,17 +144,8 @@ def update(self, event_flags, category_id, category_count):
103144

104145
self._attribute_cache = {}
105146

106-
@property
107-
def app(self):
108-
self.control_point.write(struct.pack("<BIB", 0, self.id, 0))
109-
while self.data_source.in_waiting == 0:
110-
pass
111-
print(self.data_source.in_waiting)
112-
print(self.data_source.read())
113-
return ""
114-
115-
116147
def __str__(self):
148+
# pylint: disable=too-many-branches
117149
flags = []
118150
category = None
119151
if self.category_id == 0:
@@ -151,48 +183,62 @@ def __str__(self):
151183
flags.append("positive_action")
152184
if self.negative_action:
153185
flags.append("negative_action")
154-
return category + " " + " ".join(flags) + " " + self.app_id + " " + str(self.title) + " " + str(self.subtitle) + " " + str(self.message) + " "# + self.date
186+
return (category + " " +
187+
" ".join(flags) + " " +
188+
self.app_id + " " +
189+
str(self.title) + " " +
190+
str(self.subtitle) + " " +
191+
str(self.message))
155192

156193
class AppleNotificationService(Service):
157194
"""Notification service."""
158195
uuid = VendorUUID("7905F431-B5CE-4E99-A40F-4B1E122D00D0")
159196

160197
control_point = StreamIn(uuid=VendorUUID("69D1D8F3-45E1-49A8-9821-9BBDFDAAD9D9"))
161-
data_source = StreamOut(uuid=VendorUUID("22EAC6E9-24D6-4BB5-BE44-B36ACE7C7BFB"), buffer_size=1024)
162-
notification_source = StreamOut(uuid=VendorUUID("9FBF120D-6301-42D9-8C58-25E699A21DBD"), buffer_size=8*100)
198+
data_source = StreamOut(uuid=VendorUUID("22EAC6E9-24D6-4BB5-BE44-B36ACE7C7BFB"),
199+
buffer_size=1024)
200+
notification_source = StreamOut(uuid=VendorUUID("9FBF120D-6301-42D9-8C58-25E699A21DBD"),
201+
buffer_size=8*100)
163202

164203
def __init__(self, service=None):
165204
super().__init__(service=service)
166205
self._active_notifications = {}
167206

168207
def _update(self):
169-
while self.notification_source.in_waiting > 7:
170-
buffer = self.notification_source.read(8)
171-
event_id, event_flags, category_id, category_count, id = struct.unpack("<BBBBI", buffer)
208+
# Pylint is incorrectly inferring the type of self.notification_source so disable no-member.
209+
while self.notification_source.in_waiting > 7: # pylint: disable=no-member
210+
buffer = self.notification_source.read(8) # pylint: disable=no-member
211+
event_id, event_flags, category_id, category_count, nid = struct.unpack("<BBBBI",
212+
buffer)
172213
if event_id == 0:
173-
self._active_notifications[id] = Notification(id, event_flags, category_id,
174-
category_count, control_point=self.control_point, data_source=self.data_source)
175-
yield self._active_notifications[id]
214+
self._active_notifications[nid] = Notification(nid, event_flags, category_id,
215+
category_count,
216+
control_point=self.control_point,
217+
data_source=self.data_source)
218+
yield self._active_notifications[nid]
176219
elif event_id == 1:
177-
self._active_notifications[id].update(event_flags, category_id, category_count)
220+
self._active_notifications[nid].update(event_flags, category_id, category_count)
178221
yield None
179222
elif event_id == 2:
180-
self._active_notifications[id].removed = True
181-
del self._active_notifications[id]
223+
self._active_notifications[nid].removed = True
224+
del self._active_notifications[nid]
182225
yield None
183-
#print(event_id, event_flags, category_id, category_count)
184-
185226

186227
def wait_for_new_notifications(self, timeout=None):
228+
"""Waits for new notifications and yields them. Returns on timeout, update, disconnect or
229+
clear."""
187230
start_time = time.monotonic()
188231
while timeout is None or timeout > time.monotonic() - start_time:
189-
new_notification = next(self._update())
232+
try:
233+
new_notification = next(self._update())
234+
except StopIteration:
235+
return
190236
if new_notification:
191237
yield new_notification
192-
return
193238

194239
@property
195240
def active_notifications(self):
241+
"""A dictionary of active notifications keyed by id."""
196242
for _ in self._update():
197243
pass
198244
return self._active_notifications

adafruit_ble/services/standard/hid.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ class HIDService(Service):
192192

193193
def __init__(self, hid_descriptor=None, service=None):
194194
super().__init__(report_map=hid_descriptor)
195+
if service:
196+
# TODO: Add support for connecting to a remote hid server.
197+
pass
195198
self._init_devices()
196199

197200
def _init_devices(self):

examples/ble_apple_notifications.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33
pairing, prints existing notifications and then prints any new ones as they arrive.
44
"""
55

6-
# TODO: Add support for accessing individual fields.
7-
6+
import time
87
import adafruit_ble
98
from adafruit_ble.advertising.standard import SolicitServicesAdvertisement
109
from adafruit_ble.services.apple import AppleNotificationService
11-
import time
1210

1311
radio = adafruit_ble.BLERadio()
1412
a = SolicitServicesAdvertisement()

examples/ble_current_time_service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
pairing and then prints the time every second.
44
"""
55

6+
import time
67
import adafruit_ble
78
from adafruit_ble.advertising.standard import SolicitServicesAdvertisement
89
from adafruit_ble.services.standard import CurrentTimeService
9-
import time
1010

1111
radio = adafruit_ble.BLERadio()
1212
a = SolicitServicesAdvertisement()

0 commit comments

Comments
 (0)