@@ -216,6 +216,28 @@ def _check_device(self, device_path: str) -> None:
216
216
if device_path not in self ._properties :
217
217
raise BleakError (f"device '{ device_path .split ('/' )[- 1 ]} ' not found" )
218
218
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
+
219
241
async def async_init (self ):
220
242
"""
221
243
Connects to the D-Bus message bus and begins monitoring signals.
@@ -711,9 +733,7 @@ def get_device_name(self, device_path: str) -> str:
711
733
Raises:
712
734
BleakError: if the device is not present in BlueZ
713
735
"""
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" )
717
737
718
738
def is_connected (self , device_path : str ) -> bool :
719
739
"""
@@ -820,12 +840,11 @@ async def _wait_condition(
820
840
Raises:
821
841
BleakError: if the device is not present in BlueZ
822
842
"""
823
- self ._check_device (device_path )
843
+ value = self ._get_device_property (
844
+ device_path , defs .DEVICE_INTERFACE , property_name
845
+ )
824
846
825
- if (
826
- self ._properties [device_path ][defs .DEVICE_INTERFACE ][property_name ]
827
- == property_value
828
- ):
847
+ if value == property_value :
829
848
return
830
849
831
850
event = asyncio .Event ()
@@ -917,6 +936,9 @@ def _parse_msg(self, message: Message):
917
936
except KeyError :
918
937
pass
919
938
939
+ if obj_path in self ._properties and not self ._properties [obj_path ]:
940
+ del self ._properties [obj_path ]
941
+
920
942
if interface == defs .ADAPTER_INTERFACE :
921
943
try :
922
944
self ._adapters .remove (obj_path )
0 commit comments