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,29 +105,34 @@ 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()
async with BleakClient( try:
dev.address, async with BleakClient(
adapter=interface or self._interface, dev.address,
timeout=timeout or self._connect_timeout, adapter=interface or self._interface,
disconnected_callback=self._disconnected_callback, timeout=timeout or self._connect_timeout,
) as client: disconnected_callback=self._disconnected_callback,
entity = self._cache.get(client.address) ) as client:
if not client: entity = self._cache.get(client.address)
if entity: if not client:
entity.connected = False if entity:
self.notify(BluetoothConnectionFailedEvent, entity) entity.connected = False
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(
client=client, client=client,
device=dev, device=dev,
loop=asyncio.get_event_loop(), loop=asyncio.get_event_loop(),
thread=threading.current_thread(), thread=threading.current_thread(),
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:
data = await conn.client.read_gatt_char(service_uuid) try:
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:
await conn.client.write_gatt_char(service_uuid, data) try:
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,