11import abc
22import asyncio
3+ import inspect
34import logging
45import time
56from typing import Callable , List , final
@@ -17,20 +18,22 @@ class MaxTriesExceededError(Exception):
1718
1819def retry (retries : int = 2 , delay : int = 0 ):
1920 def decor (f : Callable ):
20- def wrapper (* args , ** kwargs ):
21+ async def wrapper (* args , ** kwargs ):
2122 last_info_exception = None
2223 last_warning_exception = None
2324 for i in range (retries + 1 ):
2425 try :
2526 _LOGGER .debug ("Trying %d/%d: %s(args=%s,kwargs=%s)" , i , retries , f .__name__ , args , kwargs )
27+ if inspect .iscoroutinefunction (f ):
28+ return await f (* args , ** kwargs )
2629 return f (* args , ** kwargs )
2730 except exc .BleakError as _e :
2831 next_message = "Will try again" if i < retries else "Will not try again"
2932 _LOGGER .warning ("Got exception: %s. %s" , str (_e ), next_message )
3033 last_warning_exception = _e
3134 pass
3235 if delay > 0 :
33- time .sleep (delay )
36+ await asyncio .sleep (delay )
3437 else :
3538 _LOGGER .critical ("Retry limit (%d) exceeded for %s(%s, %s)" , retries , f .__name__ , args , kwargs )
3639 if _LOGGER .level > logging .INFO and last_info_exception is not None :
0 commit comments