Skip to content

Commit

Permalink
Merge pull request #245 from hbldh/release/v0.7.1
Browse files Browse the repository at this point in the history
Release/v0.7.1
  • Loading branch information
hbldh authored Jul 2, 2020
2 parents 5e8f8de + 85be832 commit 3c948ef
Show file tree
Hide file tree
Showing 36 changed files with 854 additions and 614 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ jobs:
os: [ubuntu-latest, windows-latest, macos-latest]
python-version: [3.5, 3.6, 3.7, 3.8]
exclude:
- os: windows-latest
python-version: 3.8
- os: macos-latest
python-version: 3.5
steps:
Expand Down
19 changes: 18 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@ All notable changes to this project will be documented in this file.
The format is based on `Keep a Changelog <https://keepachangelog.com/en/1.0.0/>`_,
and this project adheres to `Semantic Versioning <https://semver.org/spec/v2.0.0.html>`_.

`0.7.1`_ (2020-07-02)
---------------------

Changed
~~~~~~~

* Improved, more explantory error on BlueZ backend when ``BleakClient`` cannot find the desired device when trying to connect. (#238)
* Better-than-nothing documentation about scanning filters added (#230).
* Ran black on code which was forgotten in 0.7.0. Large diffs due to that.
* Re-adding Python 3.8 CI "tests" on Windows again.

Fixed
~~~~~

* Fix when characteristic updates value faster than asyncio schedule (#240 & #241)


`0.7.0`_ (2020-06-30)
---------------------
Expand Down Expand Up @@ -231,7 +247,8 @@ Fixed
* Bleak created.


.. _Unreleased: https://github.com/hbldh/bleak/compare/v0.7.0...develop
.. _Unreleased: https://github.com/hbldh/bleak/compare/v0.7.1...develop
.. _0.7.1: https://github.com/hbldh/bleak/compare/v0.7.1...v0.7.0
.. _0.7.0: https://github.com/hbldh/bleak/compare/v0.7.0...v0.6.4
.. _0.6.4: https://github.com/hbldh/bleak/compare/v0.6.3...v0.6.4
.. _0.6.3: https://github.com/hbldh/bleak/compare/v0.6.2...v0.6.3
Expand Down
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
include AUTHORS.rst
include CHANGELOG.rst
include CONTRIBUTING.rst
include HISTORY.rst
include LICENSE
include README.rst

Expand Down
2 changes: 1 addition & 1 deletion bleak/__version__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# -*- coding: utf-8 -*-

__version__ = "0.7.0"
__version__ = "0.7.1"
6 changes: 4 additions & 2 deletions bleak/backends/bluezdbus/characteristic.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
# "authorize"
}

_handle_regex = re.compile('/char([0-9a-fA-F]*)')
_handle_regex = re.compile("/char([0-9a-fA-F]*)")


class BleakGATTCharacteristicBlueZDBus(BleakGATTCharacteristic):
Expand Down Expand Up @@ -92,7 +92,9 @@ def get_descriptor(
if isinstance(specifier, int):
return next(filter(lambda x: x.handle == specifier, self.descriptors))
else:
return next(filter(lambda x: x.uuid == str(specifier), self.descriptors))
return next(
filter(lambda x: x.uuid == str(specifier), self.descriptors)
)
except StopIteration:
return None

Expand Down
55 changes: 44 additions & 11 deletions bleak/backends/bluezdbus/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ async def connect(self, **kwargs) -> bool:
# is done in the `get_device_object_path` method. Try to get it from
# BlueZ instead.
# Otherwise, use the old fallback and hope for the best.
bluez_devices = list(filter(lambda d: d.address.lower() == self.address.lower(), discovered))
bluez_devices = list(
filter(lambda d: d.address.lower() == self.address.lower(), discovered)
)
if bluez_devices:
self._device_path = bluez_devices[0].details["path"]
else:
Expand Down Expand Up @@ -151,7 +153,15 @@ def _services_resolved_callback(message):
).asFuture(self.loop)
except RemoteError as e:
await self._cleanup_all()
raise BleakError(str(e))
if 'Method "Connect" with signature "" on interface' in str(e):
raise BleakError(
"Device with address {0} could not be found. "
"Try increasing `timeout` value or moving the device closer.".format(
self.address
)
)
else:
raise BleakError(str(e))

if await self.is_connected():
logger.debug("Connection successful.")
Expand Down Expand Up @@ -346,15 +356,24 @@ async def get_services(self) -> BleakGATTServiceCollection:
)
)
self.services.add_descriptor(
BleakGATTDescriptorBlueZDBus(desc, object_path, _characteristic[0].uuid, int(_characteristic[0].handle))
BleakGATTDescriptorBlueZDBus(
desc,
object_path,
_characteristic[0].uuid,
int(_characteristic[0].handle),
)
)

self._services_resolved = True
return self.services

# IO methods

async def read_gatt_char(self, char_specifier: Union[BleakGATTCharacteristic, int, str, uuid.UUID], **kwargs) -> bytearray:
async def read_gatt_char(
self,
char_specifier: Union[BleakGATTCharacteristic, int, str, uuid.UUID],
**kwargs
) -> bytearray:
"""Perform read operation on the specified GATT characteristic.
Args:
Expand Down Expand Up @@ -404,7 +423,9 @@ async def read_gatt_char(self, char_specifier: Union[BleakGATTCharacteristic, in
return value

raise BleakError(
"Characteristic with UUID {0} could not be found!".format(char_specifier)
"Characteristic with UUID {0} could not be found!".format(
char_specifier
)
)

value = bytearray(
Expand Down Expand Up @@ -458,7 +479,10 @@ async def read_gatt_descriptor(self, handle: int, **kwargs) -> bytearray:
return value

async def write_gatt_char(
self, char_specifier: Union[BleakGATTCharacteristic, int, str, uuid.UUID], data: bytearray, response: bool = False
self,
char_specifier: Union[BleakGATTCharacteristic, int, str, uuid.UUID],
data: bytearray,
response: bool = False,
) -> None:
"""Perform a write operation on the specified GATT characteristic.
Expand Down Expand Up @@ -491,7 +515,8 @@ async def write_gatt_char(
and "write-without-response" not in characteristic.properties
):
raise BleakError(
"Characteristic %s does not support write operations!" % str(characteristic.uuid)
"Characteristic %s does not support write operations!"
% str(characteristic.uuid)
)
if not response and "write-without-response" not in characteristic.properties:
response = True
Expand Down Expand Up @@ -613,10 +638,14 @@ def callback(sender, data):
):
raise BleakError(
"Notifications on Battery Level Char ({0}) is not "
"possible in BlueZ >= 5.48. Use regular read instead.".format(char_specifier)
"possible in BlueZ >= 5.48. Use regular read instead.".format(
char_specifier
)
)
raise BleakError(
"Characteristic with UUID {0} could not be found!".format(char_specifier)
"Characteristic with UUID {0} could not be found!".format(
char_specifier
)
)
await self._bus.callRemote(
characteristic.path,
Expand All @@ -643,7 +672,9 @@ def callback(sender, data):

self._subscriptions.append(characteristic.handle)

async def stop_notify(self, char_specifier: Union[BleakGATTCharacteristic, int, str, uuid.UUID]) -> None:
async def stop_notify(
self, char_specifier: Union[BleakGATTCharacteristic, int, str, uuid.UUID]
) -> None:
"""Deactivate notification/indication on a specified characteristic.
Args:
Expand Down Expand Up @@ -674,7 +705,9 @@ async def stop_notify(self, char_specifier: Union[BleakGATTCharacteristic, int,

# DBUS introspection method for characteristics.

async def get_all_for_characteristic(self, char_specifier: Union[BleakGATTCharacteristic, int, str, uuid.UUID]) -> dict:
async def get_all_for_characteristic(
self, char_specifier: Union[BleakGATTCharacteristic, int, str, uuid.UUID]
) -> dict:
"""Get all properties for a characteristic.
This method should generally not be needed by end user, since it is a DBus specific method.
Expand Down
8 changes: 7 additions & 1 deletion bleak/backends/bluezdbus/descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
class BleakGATTDescriptorBlueZDBus(BleakGATTDescriptor):
"""GATT Descriptor implementation for BlueZ DBus backend"""

def __init__(self, obj: dict, object_path: str, characteristic_uuid: str, characteristic_handle: int):
def __init__(
self,
obj: dict,
object_path: str,
characteristic_uuid: str,
characteristic_handle: int,
):
super(BleakGATTDescriptorBlueZDBus, self).__init__(obj)
self.__path = object_path
self.__characteristic_uuid = characteristic_uuid
Expand Down
4 changes: 3 additions & 1 deletion bleak/backends/characteristic.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ def descriptors(self) -> List:
raise NotImplementedError()

@abc.abstractmethod
def get_descriptor(self, specifier: Union[int, str, UUID]) -> Union[BleakGATTDescriptor, None]:
def get_descriptor(
self, specifier: Union[int, str, UUID]
) -> Union[BleakGATTDescriptor, None]:
"""Get a descriptor by handle (int) or UUID (str or uuid.UUID)"""
raise NotImplementedError()

Expand Down
20 changes: 16 additions & 4 deletions bleak/backends/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ async def get_services(self) -> BleakGATTServiceCollection:
# I/O methods

@abc.abstractmethod
async def read_gatt_char(self, char_specifier: Union[BleakGATTCharacteristic, int, str, uuid.UUID], **kwargs) -> bytearray:
async def read_gatt_char(
self,
char_specifier: Union[BleakGATTCharacteristic, int, str, uuid.UUID],
**kwargs
) -> bytearray:
"""Perform read operation on the specified GATT characteristic.
Args:
Expand Down Expand Up @@ -152,7 +156,10 @@ async def read_gatt_descriptor(self, handle: int, **kwargs) -> bytearray:

@abc.abstractmethod
async def write_gatt_char(
self, char_specifier: Union[BleakGATTCharacteristic, int, str, uuid.UUID], data: bytearray, response: bool = False
self,
char_specifier: Union[BleakGATTCharacteristic, int, str, uuid.UUID],
data: bytearray,
response: bool = False,
) -> None:
"""Perform a write operation on the specified GATT characteristic.
Expand All @@ -179,7 +186,10 @@ async def write_gatt_descriptor(self, handle: int, data: bytearray) -> None:

@abc.abstractmethod
async def start_notify(
self, char_specifier: Union[BleakGATTCharacteristic, int, str, uuid.UUID], callback: Callable[[str, Any], Any], **kwargs
self,
char_specifier: Union[BleakGATTCharacteristic, int, str, uuid.UUID],
callback: Callable[[str, Any], Any],
**kwargs
) -> None:
"""Activate notifications/indications on a characteristic.
Expand All @@ -202,7 +212,9 @@ def callback(sender, data):
raise NotImplementedError()

@abc.abstractmethod
async def stop_notify(self, char_specifier: Union[BleakGATTCharacteristic, int, str, uuid.UUID]) -> None:
async def stop_notify(
self, char_specifier: Union[BleakGATTCharacteristic, int, str, uuid.UUID]
) -> None:
"""Deactivate notification/indication on a specified characteristic.
Args:
Expand Down
32 changes: 11 additions & 21 deletions bleak/backends/corebluetooth/CentralManagerDelegate.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

CBCentralManagerDelegate = objc.protocolNamed("CBCentralManagerDelegate")

_mac_version = list(map(int, platform.mac_ver()[0].split('.')))
_mac_version = list(map(int, platform.mac_ver()[0].split(".")))
_IS_PRE_10_13 = _mac_version[0] == 10 and _mac_version[1] < 13


Expand Down Expand Up @@ -189,8 +189,7 @@ def did_update_state(self, centralManager):
def centralManagerDidUpdateState_(self, centralManager):
logger.debug("centralManagerDidUpdateState_")
self.event_loop.call_soon_threadsafe(
self.did_update_state,
centralManager,
self.did_update_state, centralManager,
)

@objc.python_method
Expand Down Expand Up @@ -231,8 +230,11 @@ def did_discover_peripheral(
if callback:
callback(peripheral, advertisementData, RSSI)

logger.debug("Discovered device {}: {} @ RSSI: {} (kCBAdvData {})".format(
uuid_string, device.name, RSSI, advertisementData.keys()))
logger.debug(
"Discovered device {}: {} @ RSSI: {} (kCBAdvData {})".format(
uuid_string, device.name, RSSI, advertisementData.keys()
)
)

def centralManager_didDiscoverPeripheral_advertisementData_RSSI_(
self,
Expand All @@ -243,11 +245,7 @@ def centralManager_didDiscoverPeripheral_advertisementData_RSSI_(
):
logger.debug("centralManager_didDiscoverPeripheral_advertisementData_RSSI_")
self.event_loop.call_soon_threadsafe(
self.did_discover_peripheral,
central,
peripheral,
advertisementData,
RSSI,
self.did_discover_peripheral, central, peripheral, advertisementData, RSSI,
)

@objc.python_method
Expand All @@ -264,9 +262,7 @@ def did_connect_peripheral(self, central, peripheral):
def centralManager_didConnectPeripheral_(self, central, peripheral):
logger.debug("centralManager_didConnectPeripheral_")
self.event_loop.call_soon_threadsafe(
self.did_connect_peripheral,
central,
peripheral,
self.did_connect_peripheral, central, peripheral,
)

@objc.python_method
Expand All @@ -285,10 +281,7 @@ def centralManager_didFailToConnectPeripheral_error_(
):
logger.debug("centralManager_didFailToConnectPeripheral_error_")
self.event_loop.call_soon_threadsafe(
self.did_fail_to_connect_peripheral,
centralManager,
peripheral,
error,
self.did_fail_to_connect_peripheral, centralManager, peripheral, error,
)

@objc.python_method
Expand All @@ -306,10 +299,7 @@ def centralManager_didDisconnectPeripheral_error_(
):
logger.debug("centralManager_didDisconnectPeripheral_error_")
self.event_loop.call_soon_threadsafe(
self.did_disconnect_peripheral,
central,
peripheral,
error,
self.did_disconnect_peripheral, central, peripheral, error,
)


Expand Down
Loading

0 comments on commit 3c948ef

Please sign in to comment.