forked from platypush/platypush
Fallback logic that uses DBus to disconnect from a BT device.
This logic will be used if the connection wasn't opened by the current process and therefore a call to DBus is required to terminate it.
This commit is contained in:
parent
913ef6f8cd
commit
2f49ddf33a
4 changed files with 37 additions and 1 deletions
|
@ -227,6 +227,35 @@ class BLEManager(BaseBluetoothManager):
|
|||
'Error while disconnecting from %s: %s', conn.device.address, e
|
||||
)
|
||||
|
||||
def _dbus_disconnect(self, device: BLEDevice):
|
||||
"""
|
||||
Disconnect from a device using the DBus API (only available on Linux).
|
||||
This may be required if there is an active connection to the device that is not owned by the
|
||||
"""
|
||||
entity = self._cache.get(device.address)
|
||||
assert entity, f'Unknown device: "{device.address}"'
|
||||
path = device.details.get('path')
|
||||
assert path, f'The device "{device.address}" has no reported system path'
|
||||
assert path.startswith('/org/bluez/'), (
|
||||
f'The device "{device.address}" is not a BlueZ device. Programmatic '
|
||||
'system disconnection is only supported on Linux'
|
||||
)
|
||||
|
||||
try:
|
||||
import pydbus
|
||||
|
||||
bus = pydbus.SystemBus()
|
||||
dbus_device = bus.get('org.bluez', path)
|
||||
dbus_device.Disconnect()
|
||||
except Exception as e:
|
||||
raise AssertionError(
|
||||
f'Could not disconnect from {device.address}: {e}'
|
||||
) from e
|
||||
|
||||
if entity.connected:
|
||||
entity.connected = False
|
||||
self.notify(BluetoothDeviceDisconnectedEvent, entity)
|
||||
|
||||
@override
|
||||
def connect(
|
||||
self,
|
||||
|
@ -279,7 +308,11 @@ class BLEManager(BaseBluetoothManager):
|
|||
|
||||
# Check if there are any active connections
|
||||
connection = self._connections.get(dev.address, None)
|
||||
assert connection, f'No active connections to the device {device} were found'
|
||||
if not connection:
|
||||
# If there are no active connections in this process, try to
|
||||
# disconnect through the DBus API
|
||||
self._dbus_disconnect(dev)
|
||||
return
|
||||
|
||||
# Set the close event and wait for any connection thread to terminate
|
||||
if connection.close_event:
|
||||
|
|
|
@ -57,6 +57,7 @@ class BluetoothPlugin(RunnablePlugin, EnumSwitchEntityManager):
|
|||
* **bleak** (``pip install bleak``)
|
||||
* **bluetooth-numbers** (``pip install bluetooth-numbers``)
|
||||
* **TheengsDecoder** (``pip install TheengsDecoder``)
|
||||
* **pydbus** (``pip install pydbus``)
|
||||
* **pybluez** (``pip install git+https://github.com/pybluez/pybluez``)
|
||||
* **PyOBEX** (``pip install git+https://github.com/BlackLight/PyOBEX``)
|
||||
|
||||
|
|
|
@ -17,6 +17,7 @@ manifest:
|
|||
- bleak
|
||||
- bluetooth-numbers
|
||||
- TheengsDecoder
|
||||
- pydbus
|
||||
- git+https://github.com/pybluez/pybluez
|
||||
- git+https://github.com/BlackLight/PyOBEX
|
||||
package: platypush.plugins.bluetooth
|
||||
|
|
1
setup.py
1
setup.py
|
@ -178,6 +178,7 @@ setup(
|
|||
'bleak',
|
||||
'bluetooth-numbers',
|
||||
'TheengsDecoder',
|
||||
'pydbus',
|
||||
'pybluez @ https://github.com/pybluez/pybluez/tarball/master',
|
||||
'PyOBEX @ https://github.com/BlackLight/PyOBEX/tarball/master',
|
||||
],
|
||||
|
|
Loading…
Reference in a new issue