From 8b46a8cd25a0efa46402af7a367076474522c8e8 Mon Sep 17 00:00:00 2001 From: zachariah pifer Date: Tue, 25 Apr 2023 09:29:17 -0600 Subject: [PATCH 1/3] type annotations --- adafruit_ble_apple_media.py | 65 ++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 29 deletions(-) diff --git a/adafruit_ble_apple_media.py b/adafruit_ble_apple_media.py index e782655..143cb2a 100644 --- a/adafruit_ble_apple_media.py +++ b/adafruit_ble_apple_media.py @@ -12,6 +12,11 @@ https://developer.apple.com/library/archive/documentation/CoreBluetooth/Reference/AppleMediaService_Reference/Introduction/Introduction.html#//apple_ref/doc/uid/TP40014716-CH2-SW1 """ +try: + from typing import Union, Type +except ImportError: + pass + import struct import time @@ -28,6 +33,8 @@ # Disable protected access checks since our private classes are tightly coupled. # pylint: disable=protected-access +AppleMediaServiceType = Union("AppleMediaService", Type["AppleMediaService"]) + class _RemoteCommand(ComplexCharacteristic): """Endpoint for sending commands to a media player. The value read will list all available @@ -36,7 +43,7 @@ class _RemoteCommand(ComplexCharacteristic): uuid = VendorUUID("9B3C81D8-57B1-4A8A-B8DF-0E56F7CA51C2") - def __init__(self): + def __init__(self) -> None: super().__init__( properties=Characteristic.WRITE_NO_RESPONSE | Characteristic.NOTIFY, read_perm=Attribute.OPEN, @@ -45,7 +52,7 @@ def __init__(self): fixed_length=False, ) - def bind(self, service): + def bind(self, service: Service) -> _bleio.PacketBuffer: """Binds the characteristic to the given Service.""" bound_characteristic = super().bind(service) return _bleio.PacketBuffer(bound_characteristic, buffer_size=1) @@ -56,7 +63,7 @@ class _EntityUpdate(ComplexCharacteristic): uuid = VendorUUID("2F7CABCE-808D-411F-9A0C-BB92BA96C102") - def __init__(self): + def __init__(self) -> None: super().__init__( properties=Characteristic.WRITE | Characteristic.NOTIFY, read_perm=Attribute.OPEN, @@ -65,7 +72,7 @@ def __init__(self): fixed_length=False, ) - def bind(self, service): + def bind(self, service: Service) -> _bleio.PacketBuffer: """Binds the characteristic to the given Service.""" bound_characteristic = super().bind(service) return _bleio.PacketBuffer(bound_characteristic, buffer_size=8) @@ -76,7 +83,7 @@ class _EntityAttribute(Characteristic): # pylint: disable=too-few-public-method uuid = VendorUUID("C6B2F38C-23AB-46D8-A6AB-A3A870BBD5D7") - def __init__(self): + def __init__(self) -> None: super().__init__( properties=Characteristic.WRITE | Characteristic.READ, read_perm=Attribute.OPEN, @@ -86,11 +93,11 @@ def __init__(self): class _MediaAttribute: - def __init__(self, entity_id, attribute_id): + def __init__(self, entity_id: int, attribute_id: int) -> None: self.key = (entity_id, attribute_id) @staticmethod - def _update(obj): + def _update(obj: AppleMediaServiceType) -> None: if not obj._buffer: obj._buffer = bytearray(128) length_read = obj._entity_update.readinto(obj._buffer) @@ -107,7 +114,7 @@ def _update(obj): value = str(obj._buffer[3:length_read], "utf-8") obj._attribute_cache[(entity_id, attribute_id)] = value - def __get__(self, obj, cls): + def __get__(self, obj: AppleMediaServiceType, cls) -> str: self._update(obj) if self.key not in obj._attribute_cache: siblings = [self.key[1]] @@ -123,10 +130,10 @@ def __get__(self, obj, cls): class _MediaAttributePlaybackState: - def __init__(self, playback_value): + def __init__(self, playback_value: int): self._playback_value = playback_value - def __get__(self, obj, cls): + def __get__(self, obj: AppleMediaServiceType, cls) -> bool: info = obj._playback_info if info: return int(info.split(",")[0]) == self._playback_value @@ -134,14 +141,14 @@ def __get__(self, obj, cls): class _MediaAttributePlaybackInfo: - def __init__(self, position): + def __init__(self, position: int) -> None: self._position = position - def __get__(self, obj, cls): + def __get__(self, obj: AppleMediaServiceType, cls) -> float: info = obj._playback_info if info: return float(info.split(",")[self._position]) - return 0 + return 0.0 class UnsupportedCommand(Exception): @@ -200,7 +207,7 @@ class AppleMediaService(Service): duration = _MediaAttribute(2, 3) """Current track's duration as a string.""" - def __init__(self, **kwargs): + def __init__(self, **kwargs) -> None: super().__init__(**kwargs) self._buffer = None self._cmd = None @@ -209,7 +216,7 @@ def __init__(self, **kwargs): self._supported_commands = [] self._command_buffer = None - def _send_command(self, command_id): + def _send_command(self, command_id: bytearray) -> None: if not self._command_buffer: self._command_buffer = bytearray(13) i = self._remote_command.readinto( # pylint: disable=no-member @@ -226,58 +233,58 @@ def _send_command(self, command_id): self._cmd[0] = command_id self._remote_command.write(self._cmd) # pylint: disable=no-member - def play(self): + def play(self) -> None: """Plays the current track. Does nothing if already playing.""" self._send_command(0) - def pause(self): + def pause(self) -> None: """Pauses the current track. Does nothing if already paused.""" self._send_command(1) - def toggle_play_pause(self): + def toggle_play_pause(self) -> None: """Plays the current track if it is paused. Otherwise it pauses the track.""" self._send_command(2) - def next_track(self): + def next_track(self) -> None: """Stops playing the current track and plays the next one.""" self._send_command(3) - def previous_track(self): + def previous_track(self) -> None: """Stops playing the current track and plays the previous track.""" self._send_command(4) - def volume_up(self): + def volume_up(self) -> None: """Increases the playback volume.""" self._send_command(5) - def volume_down(self): + def volume_down(self) -> None: """Decreases the playback volume.""" self._send_command(6) - def advance_repeat_mode(self): + def advance_repeat_mode(self) -> None: """Advances the repeat mode. Modes are: Off, One and All""" self._send_command(7) - def advance_shuffle_mode(self): + def advance_shuffle_mode(self) -> None: """Advances the shuffle mode. Modes are: Off, One and All""" self._send_command(8) - def skip_forward(self): + def skip_forward(self) -> None: """Skips forwards in the current track""" self._send_command(9) - def skip_backward(self): + def skip_backward(self) -> None: """Skips backwards in the current track""" self._send_command(10) - def like_track(self): + def like_track(self) -> None: """Likes the current track""" self._send_command(11) - def dislike_track(self): + def dislike_track(self) -> None: """Dislikes the current track""" self._send_command(12) - def bookmark_track(self): + def bookmark_track(self) -> None: """Bookmarks the current track""" self._send_command(13) From 5204c63abdea4558084107254cde2ce72fffca0a Mon Sep 17 00:00:00 2001 From: zachariah pifer Date: Tue, 25 Apr 2023 09:53:21 -0600 Subject: [PATCH 2/3] move AppleMediaServicesTpe declaration - black requires newline after import --- adafruit_ble_apple_media.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_ble_apple_media.py b/adafruit_ble_apple_media.py index 143cb2a..99ae3df 100644 --- a/adafruit_ble_apple_media.py +++ b/adafruit_ble_apple_media.py @@ -14,6 +14,8 @@ """ try: from typing import Union, Type + + AppleMediaServiceType = Union("AppleMediaService", Type["AppleMediaService"]) except ImportError: pass @@ -33,8 +35,6 @@ # Disable protected access checks since our private classes are tightly coupled. # pylint: disable=protected-access -AppleMediaServiceType = Union("AppleMediaService", Type["AppleMediaService"]) - class _RemoteCommand(ComplexCharacteristic): """Endpoint for sending commands to a media player. The value read will list all available From 824b56e5d1e65c3dcdc91330652ebdd8d3e93bdb Mon Sep 17 00:00:00 2001 From: zachariah pifer Date: Tue, 25 Apr 2023 10:06:10 -0600 Subject: [PATCH 3/3] brackets instead of parens --- adafruit_ble_apple_media.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_ble_apple_media.py b/adafruit_ble_apple_media.py index 99ae3df..116256e 100644 --- a/adafruit_ble_apple_media.py +++ b/adafruit_ble_apple_media.py @@ -15,7 +15,7 @@ try: from typing import Union, Type - AppleMediaServiceType = Union("AppleMediaService", Type["AppleMediaService"]) + AppleMediaServiceType = Union["AppleMediaService", Type["AppleMediaService"]] except ImportError: pass