@@ -214,7 +214,7 @@ async def _ensure_connected(self):
214214 self ._reset_disconnect_timer ()
215215 return
216216 _LOGGER .debug ("%s: Connecting; RSSI: %s" , self .name , self .rssi )
217- client = await establish_connection (
217+ client : BleakClientWithServiceCache = await establish_connection (
218218 BleakClientWithServiceCache ,
219219 self ._device ,
220220 self .name ,
@@ -223,19 +223,33 @@ async def _ensure_connected(self):
223223 ble_device_callback = lambda : self ._device ,
224224 )
225225 _LOGGER .debug ("%s: Connected; RSSI: %s" , self .name , self .rssi )
226- resolved = self ._resolve_characteristics (client .services )
227- if not resolved :
228- # Try to handle services failing to load
229- resolved = self ._resolve_characteristics (await client .get_services ())
226+
227+ try :
228+ self ._resolve_characteristics (client .services )
229+ except CharacteristicMissingError as ex :
230+ _LOGGER .debug (
231+ "%s: characteristic missing, clearing cache: %s; RSSI: %s" ,
232+ self .name ,
233+ ex ,
234+ self .rssi ,
235+ exc_info = True ,
236+ )
237+ await client .clear_cache ()
238+ await self ._execute_forced_disconnect ()
239+ raise
240+
230241 self ._client = client
231242 self ._reset_disconnect_timer ()
232243 await self ._start_notify ()
233244
234- def _resolve_characteristics (self , services : BleakGATTServiceCollection ) -> bool :
245+ def _resolve_characteristics (self , services : BleakGATTServiceCollection ) -> None :
235246 """Resolve characteristics."""
236247 self ._read_char = services .get_characteristic (READ_CHAR_UUID )
248+ if not self ._read_char :
249+ raise CharacteristicMissingError (READ_CHAR_UUID )
237250 self ._write_char = services .get_characteristic (WRITE_CHAR_UUID )
238- return bool (self ._read_char and self ._write_char )
251+ if not self ._write_char :
252+ raise CharacteristicMissingError (WRITE_CHAR_UUID )
239253
240254 def _reset_disconnect_timer (self ):
241255 """Reset disconnect timer."""
@@ -307,17 +321,6 @@ async def _send_command_locked(self, key: str, command: bytes) -> bytes:
307321 await self ._ensure_connected ()
308322 try :
309323 return await self ._execute_command_locked (key , command )
310- except CharacteristicMissingError as ex :
311- _LOGGER .debug (
312- "%s: characteristic missing, clearing cache: %s; RSSI: %s" ,
313- self .name ,
314- ex ,
315- self .rssi ,
316- exc_info = True ,
317- )
318- await self ._client .clear_cache ()
319- await self ._execute_forced_disconnect ()
320- raise
321324 except BleakDBusError as ex :
322325 # Disconnect so we can reset state and try again
323326 await asyncio .sleep (0.25 )
@@ -353,10 +356,8 @@ async def _start_notify(self) -> None:
353356 async def _execute_command_locked (self , key : str , command : bytes ) -> bytes :
354357 """Execute command and read response."""
355358 assert self ._client is not None
356- if not self ._read_char :
357- raise CharacteristicMissingError (READ_CHAR_UUID )
358- if not self ._write_char :
359- raise CharacteristicMissingError (WRITE_CHAR_UUID )
359+ assert self ._read_char is not None
360+ assert self ._write_char is not None
360361 self ._notify_future = asyncio .Future ()
361362 client = self ._client
362363
0 commit comments