Skip to content

Commit c9bc45a

Browse files
committed
Improve error handling when a device is not present in BlueZ
1 parent f1b8425 commit c9bc45a

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

bleak/backends/bluezdbus/manager.py

+30-8
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,28 @@ def _check_device(self, device_path: str) -> None:
216216
if device_path not in self._properties:
217217
raise BleakError(f"device '{device_path.split('/')[-1]}' not found")
218218

219+
def _get_device_property(
220+
self, device_path: str, interface: str, property_name: str
221+
) -> Any:
222+
self._check_device(device_path)
223+
device_properties = self._properties[device_path]
224+
225+
try:
226+
interface_properties = device_properties[interface]
227+
except KeyError:
228+
raise BleakError(
229+
f"Interface {interface} not found for device '{device_path.split('/')[-1]}'"
230+
)
231+
232+
try:
233+
value = interface_properties[property_name]
234+
except KeyError:
235+
raise BleakError(
236+
f"Property '{property_name}' not found for '{device_path.split('/')[-1]} and {interface}'"
237+
)
238+
239+
return value
240+
219241
async def async_init(self):
220242
"""
221243
Connects to the D-Bus message bus and begins monitoring signals.
@@ -711,9 +733,7 @@ def get_device_name(self, device_path: str) -> str:
711733
Raises:
712734
BleakError: if the device is not present in BlueZ
713735
"""
714-
self._check_device(device_path)
715-
716-
return self._properties[device_path][defs.DEVICE_INTERFACE]["Name"]
736+
return self._get_device_property(device_path, defs.DEVICE_INTERFACE, "Name")
717737

718738
def is_connected(self, device_path: str) -> bool:
719739
"""
@@ -820,12 +840,11 @@ async def _wait_condition(
820840
Raises:
821841
BleakError: if the device is not present in BlueZ
822842
"""
823-
self._check_device(device_path)
843+
value = self._get_device_property(
844+
device_path, defs.DEVICE_INTERFACE, property_name
845+
)
824846

825-
if (
826-
self._properties[device_path][defs.DEVICE_INTERFACE][property_name]
827-
== property_value
828-
):
847+
if value == property_value:
829848
return
830849

831850
event = asyncio.Event()
@@ -917,6 +936,9 @@ def _parse_msg(self, message: Message):
917936
except KeyError:
918937
pass
919938

939+
if obj_path in self._properties and not self._properties[obj_path]:
940+
del self._properties[obj_path]
941+
920942
if interface == defs.ADAPTER_INTERFACE:
921943
try:
922944
self._adapters.remove(obj_path)

0 commit comments

Comments
 (0)