Wrap BleakError exceptions into AssertionError.

This commit is contained in:
Fabio Manganiello 2023-03-23 17:40:30 +01:00
parent d6805a8b18
commit 6267943786
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -13,6 +13,7 @@ from typing import (
from bleak import BleakClient, BleakScanner from bleak import BleakClient, BleakScanner
from bleak.backends.device import BLEDevice from bleak.backends.device import BLEDevice
from bleak.exc import BleakError
from typing_extensions import override from typing_extensions import override
from platypush.context import get_or_create_event_loop from platypush.context import get_or_create_event_loop
@ -23,11 +24,11 @@ from platypush.message.event.bluetooth import (
BluetoothDeviceLostEvent, BluetoothDeviceLostEvent,
) )
from .._manager import BaseBluetoothManager
from .._types import RawServiceClass
from ._cache import DeviceCache from ._cache import DeviceCache
from ._connection import BluetoothConnection from ._connection import BluetoothConnection
from ._event_handler import EventHandler from ._event_handler import EventHandler
from .._manager import BaseBluetoothManager
from .._types import RawServiceClass
class BLEManager(BaseBluetoothManager): class BLEManager(BaseBluetoothManager):
@ -104,6 +105,7 @@ class BLEManager(BaseBluetoothManager):
async with self._connection_locks.get(dev.address, asyncio.Lock()) as lock: async with self._connection_locks.get(dev.address, asyncio.Lock()) as lock:
self._connection_locks[dev.address] = lock or asyncio.Lock() self._connection_locks[dev.address] = lock or asyncio.Lock()
try:
async with BleakClient( async with BleakClient(
dev.address, dev.address,
adapter=interface or self._interface, adapter=interface or self._interface,
@ -116,7 +118,9 @@ class BLEManager(BaseBluetoothManager):
entity.connected = False entity.connected = False
self.notify(BluetoothConnectionFailedEvent, entity) self.notify(BluetoothConnectionFailedEvent, entity)
raise AssertionError(f'Could not connect to the device {device}') raise AssertionError(
f'Could not connect to the device {device}'
)
# Yield the BluetoothConnection object # Yield the BluetoothConnection object
self._connections[dev.address] = BluetoothConnection( self._connections[dev.address] = BluetoothConnection(
@ -127,6 +131,8 @@ class BLEManager(BaseBluetoothManager):
close_event=close_event, close_event=close_event,
) )
yield self._connections[dev.address] yield self._connections[dev.address]
except BleakError as e:
raise AssertionError(f'Could not connect to the device {device}') from e
async def _read( async def _read(
self, self,
@ -140,7 +146,12 @@ class BLEManager(BaseBluetoothManager):
a service UUID. a service UUID.
""" """
async with self._connect(device, interface, connect_timeout) as conn: async with self._connect(device, interface, connect_timeout) as conn:
try:
data = await conn.client.read_gatt_char(service_uuid) data = await conn.client.read_gatt_char(service_uuid)
except BleakError as e:
raise AssertionError(
f'Could not read from the device {device}: {e}'
) from e
return data return data
@ -157,7 +168,12 @@ class BLEManager(BaseBluetoothManager):
service UUID. service UUID.
""" """
async with self._connect(device, interface, connect_timeout) as conn: async with self._connect(device, interface, connect_timeout) as conn:
try:
await conn.client.write_gatt_char(service_uuid, data) await conn.client.write_gatt_char(service_uuid, data)
except BleakError as e:
raise AssertionError(
f'Could not write to the device {device}: {e}'
) from e
async def _scan( async def _scan(
self, self,