Skip to content

Commit 4998b27

Browse files
authored
Merge pull request #14 from zachariahpifer/type-annotations
Add Type Annotations
2 parents f51a46e + 824b56e commit 4998b27

File tree

1 file changed

+36
-29
lines changed

1 file changed

+36
-29
lines changed

adafruit_ble_apple_media.py

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
https://developer.apple.com/library/archive/documentation/CoreBluetooth/Reference/AppleMediaService_Reference/Introduction/Introduction.html#//apple_ref/doc/uid/TP40014716-CH2-SW1
1313
1414
"""
15+
try:
16+
from typing import Union, Type
17+
18+
AppleMediaServiceType = Union["AppleMediaService", Type["AppleMediaService"]]
19+
except ImportError:
20+
pass
21+
1522
import struct
1623
import time
1724

@@ -36,7 +43,7 @@ class _RemoteCommand(ComplexCharacteristic):
3643

3744
uuid = VendorUUID("9B3C81D8-57B1-4A8A-B8DF-0E56F7CA51C2")
3845

39-
def __init__(self):
46+
def __init__(self) -> None:
4047
super().__init__(
4148
properties=Characteristic.WRITE_NO_RESPONSE | Characteristic.NOTIFY,
4249
read_perm=Attribute.OPEN,
@@ -45,7 +52,7 @@ def __init__(self):
4552
fixed_length=False,
4653
)
4754

48-
def bind(self, service):
55+
def bind(self, service: Service) -> _bleio.PacketBuffer:
4956
"""Binds the characteristic to the given Service."""
5057
bound_characteristic = super().bind(service)
5158
return _bleio.PacketBuffer(bound_characteristic, buffer_size=1)
@@ -56,7 +63,7 @@ class _EntityUpdate(ComplexCharacteristic):
5663

5764
uuid = VendorUUID("2F7CABCE-808D-411F-9A0C-BB92BA96C102")
5865

59-
def __init__(self):
66+
def __init__(self) -> None:
6067
super().__init__(
6168
properties=Characteristic.WRITE | Characteristic.NOTIFY,
6269
read_perm=Attribute.OPEN,
@@ -65,7 +72,7 @@ def __init__(self):
6572
fixed_length=False,
6673
)
6774

68-
def bind(self, service):
75+
def bind(self, service: Service) -> _bleio.PacketBuffer:
6976
"""Binds the characteristic to the given Service."""
7077
bound_characteristic = super().bind(service)
7178
return _bleio.PacketBuffer(bound_characteristic, buffer_size=8)
@@ -76,7 +83,7 @@ class _EntityAttribute(Characteristic): # pylint: disable=too-few-public-method
7683

7784
uuid = VendorUUID("C6B2F38C-23AB-46D8-A6AB-A3A870BBD5D7")
7885

79-
def __init__(self):
86+
def __init__(self) -> None:
8087
super().__init__(
8188
properties=Characteristic.WRITE | Characteristic.READ,
8289
read_perm=Attribute.OPEN,
@@ -86,11 +93,11 @@ def __init__(self):
8693

8794

8895
class _MediaAttribute:
89-
def __init__(self, entity_id, attribute_id):
96+
def __init__(self, entity_id: int, attribute_id: int) -> None:
9097
self.key = (entity_id, attribute_id)
9198

9299
@staticmethod
93-
def _update(obj):
100+
def _update(obj: AppleMediaServiceType) -> None:
94101
if not obj._buffer:
95102
obj._buffer = bytearray(128)
96103
length_read = obj._entity_update.readinto(obj._buffer)
@@ -107,7 +114,7 @@ def _update(obj):
107114
value = str(obj._buffer[3:length_read], "utf-8")
108115
obj._attribute_cache[(entity_id, attribute_id)] = value
109116

110-
def __get__(self, obj, cls):
117+
def __get__(self, obj: AppleMediaServiceType, cls) -> str:
111118
self._update(obj)
112119
if self.key not in obj._attribute_cache:
113120
siblings = [self.key[1]]
@@ -123,25 +130,25 @@ def __get__(self, obj, cls):
123130

124131

125132
class _MediaAttributePlaybackState:
126-
def __init__(self, playback_value):
133+
def __init__(self, playback_value: int):
127134
self._playback_value = playback_value
128135

129-
def __get__(self, obj, cls):
136+
def __get__(self, obj: AppleMediaServiceType, cls) -> bool:
130137
info = obj._playback_info
131138
if info:
132139
return int(info.split(",")[0]) == self._playback_value
133140
return False
134141

135142

136143
class _MediaAttributePlaybackInfo:
137-
def __init__(self, position):
144+
def __init__(self, position: int) -> None:
138145
self._position = position
139146

140-
def __get__(self, obj, cls):
147+
def __get__(self, obj: AppleMediaServiceType, cls) -> float:
141148
info = obj._playback_info
142149
if info:
143150
return float(info.split(",")[self._position])
144-
return 0
151+
return 0.0
145152

146153

147154
class UnsupportedCommand(Exception):
@@ -200,7 +207,7 @@ class AppleMediaService(Service):
200207
duration = _MediaAttribute(2, 3)
201208
"""Current track's duration as a string."""
202209

203-
def __init__(self, **kwargs):
210+
def __init__(self, **kwargs) -> None:
204211
super().__init__(**kwargs)
205212
self._buffer = None
206213
self._cmd = None
@@ -209,7 +216,7 @@ def __init__(self, **kwargs):
209216
self._supported_commands = []
210217
self._command_buffer = None
211218

212-
def _send_command(self, command_id):
219+
def _send_command(self, command_id: bytearray) -> None:
213220
if not self._command_buffer:
214221
self._command_buffer = bytearray(13)
215222
i = self._remote_command.readinto( # pylint: disable=no-member
@@ -226,58 +233,58 @@ def _send_command(self, command_id):
226233
self._cmd[0] = command_id
227234
self._remote_command.write(self._cmd) # pylint: disable=no-member
228235

229-
def play(self):
236+
def play(self) -> None:
230237
"""Plays the current track. Does nothing if already playing."""
231238
self._send_command(0)
232239

233-
def pause(self):
240+
def pause(self) -> None:
234241
"""Pauses the current track. Does nothing if already paused."""
235242
self._send_command(1)
236243

237-
def toggle_play_pause(self):
244+
def toggle_play_pause(self) -> None:
238245
"""Plays the current track if it is paused. Otherwise it pauses the track."""
239246
self._send_command(2)
240247

241-
def next_track(self):
248+
def next_track(self) -> None:
242249
"""Stops playing the current track and plays the next one."""
243250
self._send_command(3)
244251

245-
def previous_track(self):
252+
def previous_track(self) -> None:
246253
"""Stops playing the current track and plays the previous track."""
247254
self._send_command(4)
248255

249-
def volume_up(self):
256+
def volume_up(self) -> None:
250257
"""Increases the playback volume."""
251258
self._send_command(5)
252259

253-
def volume_down(self):
260+
def volume_down(self) -> None:
254261
"""Decreases the playback volume."""
255262
self._send_command(6)
256263

257-
def advance_repeat_mode(self):
264+
def advance_repeat_mode(self) -> None:
258265
"""Advances the repeat mode. Modes are: Off, One and All"""
259266
self._send_command(7)
260267

261-
def advance_shuffle_mode(self):
268+
def advance_shuffle_mode(self) -> None:
262269
"""Advances the shuffle mode. Modes are: Off, One and All"""
263270
self._send_command(8)
264271

265-
def skip_forward(self):
272+
def skip_forward(self) -> None:
266273
"""Skips forwards in the current track"""
267274
self._send_command(9)
268275

269-
def skip_backward(self):
276+
def skip_backward(self) -> None:
270277
"""Skips backwards in the current track"""
271278
self._send_command(10)
272279

273-
def like_track(self):
280+
def like_track(self) -> None:
274281
"""Likes the current track"""
275282
self._send_command(11)
276283

277-
def dislike_track(self):
284+
def dislike_track(self) -> None:
278285
"""Dislikes the current track"""
279286
self._send_command(12)
280287

281-
def bookmark_track(self):
288+
def bookmark_track(self) -> None:
282289
"""Bookmarks the current track"""
283290
self._send_command(13)

0 commit comments

Comments
 (0)