12
12
https://developer.apple.com/library/archive/documentation/CoreBluetooth/Reference/AppleMediaService_Reference/Introduction/Introduction.html#//apple_ref/doc/uid/TP40014716-CH2-SW1
13
13
14
14
"""
15
+ try :
16
+ from typing import Union , Type
17
+
18
+ AppleMediaServiceType = Union ["AppleMediaService" , Type ["AppleMediaService" ]]
19
+ except ImportError :
20
+ pass
21
+
15
22
import struct
16
23
import time
17
24
@@ -36,7 +43,7 @@ class _RemoteCommand(ComplexCharacteristic):
36
43
37
44
uuid = VendorUUID ("9B3C81D8-57B1-4A8A-B8DF-0E56F7CA51C2" )
38
45
39
- def __init__ (self ):
46
+ def __init__ (self ) -> None :
40
47
super ().__init__ (
41
48
properties = Characteristic .WRITE_NO_RESPONSE | Characteristic .NOTIFY ,
42
49
read_perm = Attribute .OPEN ,
@@ -45,7 +52,7 @@ def __init__(self):
45
52
fixed_length = False ,
46
53
)
47
54
48
- def bind (self , service ) :
55
+ def bind (self , service : Service ) -> _bleio . PacketBuffer :
49
56
"""Binds the characteristic to the given Service."""
50
57
bound_characteristic = super ().bind (service )
51
58
return _bleio .PacketBuffer (bound_characteristic , buffer_size = 1 )
@@ -56,7 +63,7 @@ class _EntityUpdate(ComplexCharacteristic):
56
63
57
64
uuid = VendorUUID ("2F7CABCE-808D-411F-9A0C-BB92BA96C102" )
58
65
59
- def __init__ (self ):
66
+ def __init__ (self ) -> None :
60
67
super ().__init__ (
61
68
properties = Characteristic .WRITE | Characteristic .NOTIFY ,
62
69
read_perm = Attribute .OPEN ,
@@ -65,7 +72,7 @@ def __init__(self):
65
72
fixed_length = False ,
66
73
)
67
74
68
- def bind (self , service ) :
75
+ def bind (self , service : Service ) -> _bleio . PacketBuffer :
69
76
"""Binds the characteristic to the given Service."""
70
77
bound_characteristic = super ().bind (service )
71
78
return _bleio .PacketBuffer (bound_characteristic , buffer_size = 8 )
@@ -76,7 +83,7 @@ class _EntityAttribute(Characteristic): # pylint: disable=too-few-public-method
76
83
77
84
uuid = VendorUUID ("C6B2F38C-23AB-46D8-A6AB-A3A870BBD5D7" )
78
85
79
- def __init__ (self ):
86
+ def __init__ (self ) -> None :
80
87
super ().__init__ (
81
88
properties = Characteristic .WRITE | Characteristic .READ ,
82
89
read_perm = Attribute .OPEN ,
@@ -86,11 +93,11 @@ def __init__(self):
86
93
87
94
88
95
class _MediaAttribute :
89
- def __init__ (self , entity_id , attribute_id ) :
96
+ def __init__ (self , entity_id : int , attribute_id : int ) -> None :
90
97
self .key = (entity_id , attribute_id )
91
98
92
99
@staticmethod
93
- def _update (obj ) :
100
+ def _update (obj : AppleMediaServiceType ) -> None :
94
101
if not obj ._buffer :
95
102
obj ._buffer = bytearray (128 )
96
103
length_read = obj ._entity_update .readinto (obj ._buffer )
@@ -107,7 +114,7 @@ def _update(obj):
107
114
value = str (obj ._buffer [3 :length_read ], "utf-8" )
108
115
obj ._attribute_cache [(entity_id , attribute_id )] = value
109
116
110
- def __get__ (self , obj , cls ):
117
+ def __get__ (self , obj : AppleMediaServiceType , cls ) -> str :
111
118
self ._update (obj )
112
119
if self .key not in obj ._attribute_cache :
113
120
siblings = [self .key [1 ]]
@@ -123,25 +130,25 @@ def __get__(self, obj, cls):
123
130
124
131
125
132
class _MediaAttributePlaybackState :
126
- def __init__ (self , playback_value ):
133
+ def __init__ (self , playback_value : int ):
127
134
self ._playback_value = playback_value
128
135
129
- def __get__ (self , obj , cls ):
136
+ def __get__ (self , obj : AppleMediaServiceType , cls ) -> bool :
130
137
info = obj ._playback_info
131
138
if info :
132
139
return int (info .split ("," )[0 ]) == self ._playback_value
133
140
return False
134
141
135
142
136
143
class _MediaAttributePlaybackInfo :
137
- def __init__ (self , position ) :
144
+ def __init__ (self , position : int ) -> None :
138
145
self ._position = position
139
146
140
- def __get__ (self , obj , cls ):
147
+ def __get__ (self , obj : AppleMediaServiceType , cls ) -> float :
141
148
info = obj ._playback_info
142
149
if info :
143
150
return float (info .split ("," )[self ._position ])
144
- return 0
151
+ return 0.0
145
152
146
153
147
154
class UnsupportedCommand (Exception ):
@@ -200,7 +207,7 @@ class AppleMediaService(Service):
200
207
duration = _MediaAttribute (2 , 3 )
201
208
"""Current track's duration as a string."""
202
209
203
- def __init__ (self , ** kwargs ):
210
+ def __init__ (self , ** kwargs ) -> None :
204
211
super ().__init__ (** kwargs )
205
212
self ._buffer = None
206
213
self ._cmd = None
@@ -209,7 +216,7 @@ def __init__(self, **kwargs):
209
216
self ._supported_commands = []
210
217
self ._command_buffer = None
211
218
212
- def _send_command (self , command_id ) :
219
+ def _send_command (self , command_id : bytearray ) -> None :
213
220
if not self ._command_buffer :
214
221
self ._command_buffer = bytearray (13 )
215
222
i = self ._remote_command .readinto ( # pylint: disable=no-member
@@ -226,58 +233,58 @@ def _send_command(self, command_id):
226
233
self ._cmd [0 ] = command_id
227
234
self ._remote_command .write (self ._cmd ) # pylint: disable=no-member
228
235
229
- def play (self ):
236
+ def play (self ) -> None :
230
237
"""Plays the current track. Does nothing if already playing."""
231
238
self ._send_command (0 )
232
239
233
- def pause (self ):
240
+ def pause (self ) -> None :
234
241
"""Pauses the current track. Does nothing if already paused."""
235
242
self ._send_command (1 )
236
243
237
- def toggle_play_pause (self ):
244
+ def toggle_play_pause (self ) -> None :
238
245
"""Plays the current track if it is paused. Otherwise it pauses the track."""
239
246
self ._send_command (2 )
240
247
241
- def next_track (self ):
248
+ def next_track (self ) -> None :
242
249
"""Stops playing the current track and plays the next one."""
243
250
self ._send_command (3 )
244
251
245
- def previous_track (self ):
252
+ def previous_track (self ) -> None :
246
253
"""Stops playing the current track and plays the previous track."""
247
254
self ._send_command (4 )
248
255
249
- def volume_up (self ):
256
+ def volume_up (self ) -> None :
250
257
"""Increases the playback volume."""
251
258
self ._send_command (5 )
252
259
253
- def volume_down (self ):
260
+ def volume_down (self ) -> None :
254
261
"""Decreases the playback volume."""
255
262
self ._send_command (6 )
256
263
257
- def advance_repeat_mode (self ):
264
+ def advance_repeat_mode (self ) -> None :
258
265
"""Advances the repeat mode. Modes are: Off, One and All"""
259
266
self ._send_command (7 )
260
267
261
- def advance_shuffle_mode (self ):
268
+ def advance_shuffle_mode (self ) -> None :
262
269
"""Advances the shuffle mode. Modes are: Off, One and All"""
263
270
self ._send_command (8 )
264
271
265
- def skip_forward (self ):
272
+ def skip_forward (self ) -> None :
266
273
"""Skips forwards in the current track"""
267
274
self ._send_command (9 )
268
275
269
- def skip_backward (self ):
276
+ def skip_backward (self ) -> None :
270
277
"""Skips backwards in the current track"""
271
278
self ._send_command (10 )
272
279
273
- def like_track (self ):
280
+ def like_track (self ) -> None :
274
281
"""Likes the current track"""
275
282
self ._send_command (11 )
276
283
277
- def dislike_track (self ):
284
+ def dislike_track (self ) -> None :
278
285
"""Dislikes the current track"""
279
286
self ._send_command (12 )
280
287
281
- def bookmark_track (self ):
288
+ def bookmark_track (self ) -> None :
282
289
"""Bookmarks the current track"""
283
290
self ._send_command (13 )
0 commit comments