@@ -96,6 +96,7 @@ def __init__(self, mac: str):
9696 self ._heating : bool = False
9797 self ._filter_remain : float = 0.0
9898 self ._error_code : int = 0
99+ self .__failed_connects : int = 0
99100
100101 if self .mac == "dummy" :
101102 _LOGGER .warning ("Dummy mode detected!" )
@@ -295,6 +296,7 @@ def connection_status(self):
295296 return connection_status
296297
297298 def _connect (self , need_notifications : bool = True ):
299+ _LOGGER .debug ("Connecting" )
298300 if self .connection_status == "disc" :
299301 try :
300302 self ._btle .connect (self .mac , btle .ADDR_TYPE_RANDOM )
@@ -307,10 +309,16 @@ def _connect(self, need_notifications: bool = True):
307309 self ._enable_notifications ()
308310 else :
309311 _LOGGER .debug ("Notifications was not requested" )
312+ self .__failed_connects = 0
310313 except btle .BTLEDisconnectError as e :
311- _LOGGER .warning ("Got %s exception" , str (e ))
312- time .sleep (2 )
313- raise e
314+ _LOGGER .warning ("Got BTLEDisconnectError:%s" , str (e ))
315+ if self .__failed_connects < 1 :
316+ self .__failed_connects += 1
317+ _LOGGER .debug ("Will try again." )
318+ time .sleep (2 )
319+ self ._connect (need_notifications )
320+ else :
321+ raise e
314322
315323 def _disconnect (self ):
316324 if self .connection_status != "disc" :
0 commit comments