Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated logging to lazy version and replaced format by f-string for BleakClientWinRT #1000

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Changed
argument to avoid services being resolved again.
* The BlueZ D-Bus backend now uses ``dbus-fast`` instead of ``dbus-next`` which significantly improves performance.
* The BlueZ D-Bus backend will not avoid trying to connect to devices that are already connected. Fixes #992.
* Updated logging to lazy version and replaced format by f-string for BleakClientWinRT

Fixed
-----
Expand Down
57 changes: 17 additions & 40 deletions bleak/backends/winrt/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,6 @@
for u in ("00001812-0000-1000-8000-00805f9b34fb",) # Human Interface Device Service
)

_pairing_statuses = {
getattr(DevicePairingResultStatus, v): v
for v in dir(DevicePairingResultStatus)
if "_" not in v and isinstance(getattr(DevicePairingResultStatus, v), int)
}


_unpairing_statuses = {
getattr(DeviceUnpairingResultStatus, v): v
for v in dir(DeviceUnpairingResultStatus)
if "_" not in v and isinstance(getattr(DeviceUnpairingResultStatus, v), int)
}

# TODO: we can use this when minimum Python is 3.8
# class _Result(typing.Protocol):
# status: GattCommunicationStatus
Expand Down Expand Up @@ -191,7 +178,7 @@ def __init__(
self._max_pdu_size_changed_token: Optional[EventRegistrationToken] = None

def __str__(self):
return "BleakClientWinRT ({0})".format(self.address)
return f"{type(self).__name__} ({self.address})"

# Connectivity methods

Expand All @@ -215,11 +202,9 @@ async def connect(self, **kwargs) -> bool:
if device:
self._device_info = device.details.adv.bluetooth_address
else:
raise BleakError(
"Device with address {0} was not found.".format(self.address)
)
raise BleakError(f"Device with address {self.address} was not found.")

logger.debug("Connecting to BLE device @ {0}".format(self.address))
logger.debug("Connecting to BLE device @ %s", self.address)

args = [
self._device_info,
Expand Down Expand Up @@ -448,17 +433,11 @@ def handler(sender, args):
DevicePairingResultStatus.PAIRED,
DevicePairingResultStatus.ALREADY_PAIRED,
):
raise BleakError(
"Could not pair with device: {0}: {1}".format(
pairing_result.status,
_pairing_statuses.get(pairing_result.status),
)
)
raise BleakError(f"Could not pair with device: {pairing_result.status}")
else:
logger.info(
"Paired to device with protection level {0}.".format(
pairing_result.protection_level_used
)
"Paired to device with protection level %d.",
pairing_result.protection_level_used,
)
return True
else:
Expand Down Expand Up @@ -486,11 +465,9 @@ async def unpair(self) -> bool:
DeviceUnpairingResultStatus.ALREADY_UNPAIRED,
):
raise BleakError(
"Could not unpair with device: {0}: {1}".format(
unpairing_result.status,
_unpairing_statuses.get(unpairing_result.status),
)
f"Could not unpair with device: {unpairing_result.status}"
)

else:
logger.info("Unpaired with device.")
return True
Expand Down Expand Up @@ -609,7 +586,7 @@ async def read_gatt_char(
else:
characteristic = char_specifier
if not characteristic:
raise BleakError("Characteristic {0} was not found!".format(char_specifier))
raise BleakError(f"Characteristic {char_specifier} was not found!")

value = bytearray(
_ensure_success(
Expand All @@ -623,7 +600,7 @@ async def read_gatt_char(
)
)

logger.debug(f"Read Characteristic {characteristic.handle:04X} : {value}")
logger.debug("Read Characteristic %04X : %s", characteristic.handle, value)

return value

Expand All @@ -648,7 +625,7 @@ async def read_gatt_descriptor(self, handle: int, **kwargs) -> bytearray:

descriptor = self.services.get_descriptor(handle)
if not descriptor:
raise BleakError("Descriptor with handle {0} was not found!".format(handle))
raise BleakError(f"Descriptor with handle {handle} was not found!")

value = bytearray(
_ensure_success(
Expand All @@ -662,7 +639,7 @@ async def read_gatt_descriptor(self, handle: int, **kwargs) -> bytearray:
)
)

logger.debug(f"Read Descriptor {handle:04X} : {value}")
logger.debug("Read Descriptor %04X : %s", handle, value)

return value

Expand Down Expand Up @@ -690,7 +667,7 @@ async def write_gatt_char(
else:
characteristic = char_specifier
if not characteristic:
raise BleakError("Characteristic {} was not found!".format(char_specifier))
raise BleakError(f"Characteristic {char_specifier} was not found!")

response = (
GattWriteOption.WRITE_WITH_RESPONSE
Expand Down Expand Up @@ -722,7 +699,7 @@ async def write_gatt_descriptor(

descriptor = self.services.get_descriptor(handle)
if not descriptor:
raise BleakError("Descriptor with handle {0} was not found!".format(handle))
raise BleakError(f"Descriptor with handle {handle} was not found!")

buf = Buffer(len(data))
buf.length = buf.capacity
Expand All @@ -734,7 +711,7 @@ async def write_gatt_descriptor(
f"Could not write value {data} to descriptor {handle:04X}",
)

logger.debug(f"Write Descriptor {handle:04X} : {data}")
logger.debug("Write Descriptor %04X : %s", handle, data)

async def start_notify(
self,
Expand Down Expand Up @@ -780,7 +757,7 @@ def bleak_callback(s, d):
else:
characteristic = char_specifier
if not characteristic:
raise BleakError("Characteristic {0} not found!".format(char_specifier))
raise BleakError(f"Characteristic {char_specifier} not found!")

if self._notification_callbacks.get(characteristic.handle):
await self.stop_notify(characteristic)
Expand Down Expand Up @@ -846,7 +823,7 @@ async def stop_notify(
else:
characteristic = char_specifier
if not characteristic:
raise BleakError("Characteristic {} not found!".format(char_specifier))
raise BleakError(f"Characteristic {char_specifier} not found!")

_ensure_success(
await characteristic.obj.write_client_characteristic_configuration_descriptor_async(
Expand Down