From 6a97023aa628553a5b78bbdc21bed43df9ddbfee Mon Sep 17 00:00:00 2001 From: Rich Saupe Date: Mon, 2 May 2022 10:38:45 -0600 Subject: [PATCH 1/2] refactor: adds type annotations reported by mypy and updated pylint version in .pre-commit-config.yaml --- .pre-commit-config.yaml | 2 +- adafruit_ble_ibbq.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 7467c1d..dfb0b41 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -18,7 +18,7 @@ repos: - id: end-of-file-fixer - id: trailing-whitespace - repo: https://github.com/pycqa/pylint - rev: v2.11.1 + rev: v2.12.2 hooks: - id: pylint name: pylint (library code) diff --git a/adafruit_ble_ibbq.py b/adafruit_ble_ibbq.py index e19ecf8..bcb61e9 100644 --- a/adafruit_ble_ibbq.py +++ b/adafruit_ble_ibbq.py @@ -41,7 +41,7 @@ class _SettingsResult(ComplexCharacteristic): def __init__(self): super().__init__(properties=Characteristic.NOTIFY) - def bind(self, service): + def bind(self, service: Service) -> _bleio.PacketBuffer: """Bind to an IBBQService.""" bound_characteristic = super().bind(service) bound_characteristic.set_cccd(notify=True) @@ -57,7 +57,7 @@ class _RealtimeData(ComplexCharacteristic): def __init__(self): super().__init__(properties=Characteristic.NOTIFY) - def bind(self, service): + def bind(self, service: Service) -> _bleio.PacketBuffer: """Bind to an IBBQService.""" bound_characteristic = super().bind(service) bound_characteristic.set_cccd(notify=True) @@ -74,7 +74,7 @@ class IBBQService(Service): _UNITS_CELSIUS_MSG = b"\x02\x00\x00\x00\x00\x00" _REQUEST_BATTERY_LEVEL_MSG = b"\x08\x24\x00\x00\x00\x00" - def __init__(self, service=None): + def __init__(self, service: Service = None) -> None: super().__init__(service=service) # Defer creating buffers until needed, since MTU is not known yet. self._settings_result_buf = None From 8e68e6c31ee43edd4cf2d7d0bd7b8eb9684a7fb5 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Mon, 16 May 2022 10:56:25 -0500 Subject: [PATCH 2/2] more specific service type. Adding remaining types --- adafruit_ble_ibbq.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/adafruit_ble_ibbq.py b/adafruit_ble_ibbq.py index bcb61e9..83e1e9c 100644 --- a/adafruit_ble_ibbq.py +++ b/adafruit_ble_ibbq.py @@ -20,6 +20,10 @@ InkBird and EasyBBQ (from PyleUSA) are brands that use the iBBQ protocol in their products. """ +try: + from typing import Optional, Tuple +except ImportError: + pass import struct @@ -38,10 +42,10 @@ class _SettingsResult(ComplexCharacteristic): uuid = StandardUUID(0xFFF1) - def __init__(self): + def __init__(self) -> None: super().__init__(properties=Characteristic.NOTIFY) - def bind(self, service: Service) -> _bleio.PacketBuffer: + def bind(self, service: "IBBQService") -> _bleio.PacketBuffer: """Bind to an IBBQService.""" bound_characteristic = super().bind(service) bound_characteristic.set_cccd(notify=True) @@ -54,10 +58,10 @@ class _RealtimeData(ComplexCharacteristic): uuid = StandardUUID(0xFFF4) - def __init__(self): + def __init__(self) -> None: super().__init__(properties=Characteristic.NOTIFY) - def bind(self, service: Service) -> _bleio.PacketBuffer: + def bind(self, service: "IBBQService") -> _bleio.PacketBuffer: """Bind to an IBBQService.""" bound_characteristic = super().bind(service) bound_characteristic.set_cccd(notify=True) @@ -74,7 +78,7 @@ class IBBQService(Service): _UNITS_CELSIUS_MSG = b"\x02\x00\x00\x00\x00\x00" _REQUEST_BATTERY_LEVEL_MSG = b"\x08\x24\x00\x00\x00\x00" - def __init__(self, service: Service = None) -> None: + def __init__(self, service: Optional["IBBQService"] = None) -> None: super().__init__(service=service) # Defer creating buffers until needed, since MTU is not known yet. self._settings_result_buf = None @@ -106,19 +110,19 @@ def __init__(self, service: Service = None) -> None: ) """Send control messages here.""" - def init(self): + def init(self) -> None: """Perform initial "pairing", which is not regular BLE pairing.""" self.account_and_verify = self._CREDENTIALS_MSG self.settings_data = self._REALTIME_DATA_ENABLE_MSG - def display_fahrenheit(self): + def display_fahrenheit(self) -> None: """Display temperatures on device in degrees Fahrenheit. Note: This does not change the units returned by `temperatures`. """ self.settings_data = self._UNITS_FAHRENHEIT_MSG - def display_celsius(self): + def display_celsius(self) -> None: """Display temperatures on device in degrees Celsius. Note: This does not change the units returned by `temperatures`. @@ -126,7 +130,7 @@ def display_celsius(self): self.settings_data = self._UNITS_CELSIUS_MSG @property - def temperatures(self): + def temperatures(self) -> Optional[Tuple]: """Return a tuple of temperatures for all the possible temperature probes on the device. Temperatures are in degrees Celsius. Unconnected probes return 0.0. """ @@ -145,7 +149,7 @@ def temperatures(self): return None @property - def battery_level(self): + def battery_level(self) -> Optional[Tuple[float, float]]: """Get current battery level in volts as ``(current_voltage, max_voltage)``. Results are approximate and may differ from the actual battery voltage by 0.1v or so.