@@ -82,6 +82,7 @@ class Tion:
8282 def __init__ (self , mac : str | BLEDevice ):
8383 self ._mac = mac
8484 self ._btle : BleakClient = BleakClient (mac )
85+ self ._next_btle_device : BleakClient | None = None
8586 self ._delegation = TionDelegation ()
8687 self ._fan_speed = 0
8788 self ._model : str = self .__class__ .__name__
@@ -291,7 +292,7 @@ def connection_status(self):
291292 @retry (retries = 1 , delay = 2 )
292293 async def _try_connect (self ) -> bool :
293294 """Tries to connect with retries"""
294-
295+ self . set_new_btle_device ()
295296 return await self ._btle .connect (timeout = 5.0 )
296297
297298 @final
@@ -315,6 +316,8 @@ async def _disconnect(self):
315316 _LOGGER .debug (f"Disconnecting. { self .connection_status = } ." )
316317 if self .connection_status != "disc" :
317318 await self ._btle .disconnect ()
319+ self .set_new_btle_device ()
320+
318321 _LOGGER .debug (f"_disconnect done. { self .connection_status = } " )
319322
320323 @final
@@ -570,4 +573,12 @@ def update_btle_device(self, new_device: str | BLEDevice):
570573 if new_device is None :
571574 _LOGGER .info (f"Skipping update due to { new_device = } !" )
572575 return
573- self ._btle = BleakClient (new_device )
576+ self ._next_btle_device = new_device
577+
578+ @final
579+ def set_new_btle_device (self ):
580+ if self ._next_btle_device is not None :
581+ async with self ._semaphore :
582+ _LOGGER .debug ("Updating _btle instance" )
583+ self ._btle = BleakClient (self ._next_btle_device )
584+ self ._next_btle_device = None
0 commit comments