Skip to content

Commit

Permalink
fix: fix wake up decorator to avoid recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
alandtse committed Jan 24, 2020
1 parent 0892b91 commit e3c7c17
Showing 1 changed file with 52 additions and 47 deletions.
99 changes: 52 additions & 47 deletions teslajsonpy/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ def valid_result(result):
sleep_delay = 2
inst = args[0]
car_id = args[1]
is_wake_command = len(args) >= 3 and args[2] == "wake_up"
result = None
if inst.car_online.get(inst._id_to_vin(car_id)):
try:
Expand All @@ -214,74 +215,78 @@ def valid_result(result):
pass
if valid_result(result):
return result
_LOGGER.debug(
"wake_up needed for %s -> %s \n"
"Info: args:%s, kwargs:%s, "
"car_id:%s, car_online:%s",
func.__name__, # pylint: disable=no-member
result,
args,
kwargs,
car_id,
inst.car_online,
)
inst.car_online[inst._id_to_vin(car_id)] = False
while (
"wake_if_asleep" in kwargs
and kwargs["wake_if_asleep"]
and
# Check online state
(
car_id is None
or (
not inst._id_to_vin(car_id)
or not inst.car_online.get(inst._id_to_vin(car_id))
)
)
):
_LOGGER.debug("Attempting to wake up")
result = await inst._wake_up(car_id)
if not is_wake_command:
_LOGGER.debug(
"%s(%s): Wake Attempt(%s): %s",
func.__name__, # pylint: disable=no-member,
car_id,
retries,
"wake_up needed for %s -> %s \n"
"Info: args:%s, kwargs:%s, "
"VIN:%s, car_online:%s",
func.__name__, # pylint: disable=no-member
result,
args,
kwargs,
inst._id_to_vin(car_id)[-5:],
inst.car_online,
)
if not result:
if retries < 5:
await asyncio.sleep(sleep_delay ** (retries + 2))
retries += 1
continue
inst.car_online[inst._id_to_vin(car_id)] = False
raise RetryLimitError
break
inst.car_online[inst._id_to_vin(car_id)] = False
while (
"wake_if_asleep" in kwargs
and kwargs["wake_if_asleep"]
and
# Check online state
(
car_id is None
or (
not inst._id_to_vin(car_id)
or not inst.car_online.get(inst._id_to_vin(car_id))
)
)
):
_LOGGER.debug("Attempting to wake up")
result = await inst._wake_up(car_id)
_LOGGER.debug(
"%s(%s): Wake Attempt(%s): %s",
func.__name__, # pylint: disable=no-member,
inst._id_to_vin(car_id)[-5:],
retries,
result,
)
if not result:
if retries < 5:
await asyncio.sleep(sleep_delay ** (retries + 2))
retries += 1
continue
inst.car_online[inst._id_to_vin(car_id)] = False
raise RetryLimitError
break
# try function five more times
retries = 0
result = None
_LOGGER.debug(
"Vehicle is awake, trying function %s",
"Retrying %s(%s %s)",
func.__name__, # pylint: disable=no-member,
args,
kwargs,
)
while True and func.__name__ != "wake_up": # pylint: disable=no-member,
while not valid_result(result):
await asyncio.sleep(sleep_delay ** (retries + 1))
try:
result = await func(*args, **kwargs)
_LOGGER.debug(
"%s(%s): Retry Attempt(%s): %s",
"%s(%s %s):\n Retry Attempt(%s): %s",
func.__name__, # pylint: disable=no-member,
car_id,
args,
kwargs,
retries,
"Success" if valid_result(result) else result,
)
except TeslaException:
pass
finally:
retries += 1
if valid_result(result):
inst.car_online[inst._id_to_vin(car_id)] = True
return result
if retries >= 5:
raise RetryLimitError
await asyncio.sleep(sleep_delay ** (retries + 1))
inst.car_online[inst._id_to_vin(car_id)] = True
return result

return wrapped

Expand Down

0 comments on commit e3c7c17

Please sign in to comment.