Wrap bluetooth.connect
in a per-device locked section.
This commit is contained in:
parent
9112239ac3
commit
73bf2446bd
1 changed files with 14 additions and 10 deletions
|
@ -1,5 +1,5 @@
|
|||
import base64
|
||||
from asyncio import Event, ensure_future
|
||||
from asyncio import Event, Lock, ensure_future
|
||||
from contextlib import asynccontextmanager
|
||||
from threading import RLock, Timer
|
||||
from time import time
|
||||
|
@ -127,6 +127,7 @@ class BluetoothBlePlugin(AsyncRunnablePlugin, EntityManager):
|
|||
self._scan_enabled = Event()
|
||||
self._scan_controller_timer: Optional[Timer] = None
|
||||
self._connections: Dict[str, BleakClient] = {}
|
||||
self._connection_locks: Dict[str, Lock] = {}
|
||||
self._devices: Dict[str, BLEDevice] = {}
|
||||
self._device_last_updated_at: Dict[str, float] = {}
|
||||
self._device_name_by_addr = device_names or {}
|
||||
|
@ -255,7 +256,6 @@ class BluetoothBlePlugin(AsyncRunnablePlugin, EntityManager):
|
|||
or old_props.get('TxPower') != new_props.get('TxPower')
|
||||
):
|
||||
event_types.append(BluetoothDeviceSignalUpdateEvent)
|
||||
|
||||
else:
|
||||
event_types.append(BluetoothDeviceFoundEvent)
|
||||
|
||||
|
@ -278,6 +278,10 @@ class BluetoothBlePlugin(AsyncRunnablePlugin, EntityManager):
|
|||
timeout: Optional[float] = None,
|
||||
) -> AsyncGenerator[BleakClient, None]:
|
||||
dev = await self._get_device(device)
|
||||
|
||||
async with self._connection_locks.get(dev.address, Lock()) as lock:
|
||||
self._connection_locks[dev.address] = lock or Lock()
|
||||
|
||||
async with BleakClient(
|
||||
dev.address,
|
||||
adapter=interface or self._interface,
|
||||
|
@ -285,7 +289,7 @@ class BluetoothBlePlugin(AsyncRunnablePlugin, EntityManager):
|
|||
) as client:
|
||||
self._connections[dev.address] = client
|
||||
yield client
|
||||
self._connections.pop(dev.address)
|
||||
self._connections.pop(dev.address, None)
|
||||
|
||||
async def _read(
|
||||
self,
|
||||
|
|
Loading…
Reference in a new issue