1010_LOGGER = logging .getLogger (__name__ )
1111
1212
13- def retry (retries : int = 3 ):
13+ def retry (retries : int = 3 , delay : int = 0 ):
1414 def decor (f : Callable ):
1515 def wrapper (* args , ** kwargs ):
1616 for i in range (retries ):
@@ -23,6 +23,8 @@ def wrapper(*args, **kwargs):
2323 next_message = "Will try again" if i < retries else "Will not try again"
2424 _LOGGER .warning ("Got exception: %s. %s" , str (_e ), next_message )
2525 pass
26+ if delay > 0 :
27+ time .sleep (delay )
2628 else :
2729 _LOGGER .critical ("Retry limit (%d) exceeded for %s(%s, %s)" , retries , f .__name__ , args , kwargs )
2830
@@ -344,30 +346,25 @@ def connection_status(self):
344346
345347 return connection_status
346348
349+ @retry (retries = 1 , delay = 2 )
350+ def _try_connect (self ) -> None :
351+ """Tries to connect with retries"""
352+
353+ self ._btle .connect (self .mac , btle .ADDR_TYPE_RANDOM )
354+
347355 def _connect (self , need_notifications : bool = True ):
348356 _LOGGER .debug ("Connecting" )
349357 if self .connection_status == "disc" :
350- try :
351- self ._btle .connect (self .mac , btle .ADDR_TYPE_RANDOM )
352- for tc in self ._btle .getCharacteristics ():
353- if tc .uuid == self .uuid_notify :
354- self .notify = tc
355- if tc .uuid == self .uuid_write :
356- self .write = tc
357- if need_notifications :
358- self ._enable_notifications ()
359- else :
360- _LOGGER .debug ("Notifications was not requested" )
361- self .__failed_connects = 0
362- except btle .BTLEDisconnectError as e :
363- _LOGGER .warning ("Got BTLEDisconnectError:%s" , str (e ))
364- if self .__failed_connects < 1 :
365- self .__failed_connects += 1
366- _LOGGER .debug ("Will try again." )
367- time .sleep (2 )
368- self ._connect (need_notifications )
369- else :
370- raise e
358+ self ._try_connect ()
359+ for tc in self ._btle .getCharacteristics ():
360+ if tc .uuid == self .uuid_notify :
361+ self .notify = tc
362+ if tc .uuid == self .uuid_write :
363+ self .write = tc
364+ if need_notifications :
365+ self ._enable_notifications ()
366+ else :
367+ _LOGGER .debug ("Notifications was not requested" )
371368
372369 def _disconnect (self ):
373370 if self .connection_status != "disc" :
0 commit comments