Skip to content

Commit

Permalink
allow unpair without being connected
Browse files Browse the repository at this point in the history
  • Loading branch information
jochenjagers committed Sep 16, 2022
1 parent d343cdd commit be7cd01
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to `Semantic Versioning <https://semver.org/spec/v2.0.0
Changed
-------
* Relax ``async-timeout`` version to support different installations. Merged #1009.
* ``unpair`` function of ``BleakClient`` in WinRT backend can be called without being connected to remove stored device information

`0.17.0`_ (2022-09-12)
======================
Expand Down
57 changes: 41 additions & 16 deletions bleak/backends/winrt/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@
# protocol_error: typing.Optional[int]


def _address_to_int(address: str) -> int:
"""Converts the Bluetooth device address string to its representing integer
Args:
address (str): Bluetooth device address to convert
Returns:
int: integer representation of the given Bluetooth device address
"""
_address_separators = [":", "-"]
for char in _address_separators:
address = address.replace(char, "")

return int(address, base=16)


def _ensure_success(result: Any, attr: Optional[str], fail_msg: str) -> Any:
"""
Ensures that *status* is ``GattCommunicationStatus.SUCCESS``, otherwise
Expand Down Expand Up @@ -453,26 +469,35 @@ async def unpair(self) -> bool:
"""

# New local device information object created since the object from the requester isn't updated
device_information = await DeviceInformation.create_from_id_async(
self._requester.device_information.id
)
if device_information.pairing.is_paired:
if self._requester:
device = self._requester
should_close = False
else:
device = await BluetoothLEDevice.from_bluetooth_address_async(
_address_to_int(self.address)
)
should_close = True

try:
# New local device information object created since the object from the requester isn't updated
device_information = await DeviceInformation.create_from_id_async(
device.device_information.id
)
unpairing_result = await device_information.pairing.unpair_async()

if unpairing_result.status not in (
DeviceUnpairingResultStatus.UNPAIRED,
DeviceUnpairingResultStatus.ALREADY_UNPAIRED,
):
raise BleakError(
f"Could not unpair with device: {unpairing_result.status}"
)
finally:
if should_close:
# close the device object if this was created locally
device.close()

else:
logger.info("Unpaired with device.")
return True
if unpairing_result.status not in (
DeviceUnpairingResultStatus.UNPAIRED,
DeviceUnpairingResultStatus.ALREADY_UNPAIRED,
):
raise BleakError(f"Could not unpair with device: {unpairing_result.status}")

return not device_information.pairing.is_paired
logger.info("Unpaired with device.")
return True

# GATT services methods

Expand Down

0 comments on commit be7cd01

Please sign in to comment.