Skip to content

Commit

Permalink
fix: avoid recursion in wake_up wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
alandtse committed Jan 25, 2020
1 parent 2926b34 commit b2e74d1
Showing 1 changed file with 42 additions and 43 deletions.
85 changes: 42 additions & 43 deletions teslajsonpy/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,56 +214,55 @@ def valid_result(result):
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)):
if inst.car_online.get(inst._id_to_vin(car_id)) or is_wake_command:
try:
result = await func(*args, **kwargs)
except TeslaException:
pass
if valid_result(result):
if valid_result(result) or is_wake_command:
return result
if not is_wake_command:
_LOGGER.debug(
"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,
)
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(
"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,
"%s(%s): Wake Attempt(%s): %s",
func.__name__, # pylint: disable=no-member,
inst._id_to_vin(car_id)[-5:],
inst.car_online,
retries,
result,
)
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
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("Reached retry limit; aborting")
break
# try function five more times
retries = 0
result = None
Expand All @@ -290,7 +289,7 @@ def valid_result(result):
finally:
retries += 1
if retries >= 5:
raise RetryLimitError
raise RetryLimitError("Reached retry limit; aborting")
inst.car_online[inst._id_to_vin(car_id)] = True
return result

Expand Down

0 comments on commit b2e74d1

Please sign in to comment.