Skip to content

Commit

Permalink
Fix potential infinite recursion (#31)
Browse files Browse the repository at this point in the history
* Fix potential infinite recursion on wake_up with wrapper
  • Loading branch information
alandtse authored Mar 3, 2019
1 parent 7dbe8fa commit 2c45f2d
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions teslajsonpy/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from teslajsonpy.GPS import GPS, Odometer
from teslajsonpy.Exceptions import TeslaException
from functools import wraps
from .Exceptions import RetryLimitError
import logging
from .Exceptions import RetryLimitError
_LOGGER = logging.getLogger(__name__)


Expand Down Expand Up @@ -127,15 +127,15 @@ def valid_result(result):
inst._car_online)
inst._car_online[vehicle_id] = False
while ('wake_if_asleep' in kwargs and kwargs['wake_if_asleep']
and not valid_result(result) and
and
# Check online state
(vehicle_id is None or
(vehicle_id is not None and
vehicle_id in inst._car_online and
not inst._car_online[vehicle_id]))):
result = inst._wake_up(vehicle_id, *args, **kwargs)
_LOGGER.debug("Wake Attempt(%s): %s" % (retries, result))
if not valid_result(result):
if not result:
if retries < 5:
time.sleep(sleep_delay**(retries+2))
retries += 1
Expand Down Expand Up @@ -175,10 +175,14 @@ def _wake_up(self, vehicle_id, *args, **kwargs):
cur_time = int(time.time())
if (not self._car_online[vehicle_id] or
(cur_time - self._last_wake_up_time[vehicle_id] > 300)):
result = self.post(vehicle_id, 'wake_up')['response']['state']
_LOGGER.debug("Wakeup %s: %s" % (vehicle_id, result))
self._car_online[vehicle_id] = result == 'online'
result = self.post(vehicle_id,
'wake_up',
wake_if_asleep=False) # avoid wrapper loop
self._car_online[vehicle_id] = (result['response']['state'] ==
'online')
self._last_wake_up_time[vehicle_id] = cur_time
_LOGGER.debug("Wakeup %s: %s", vehicle_id,
result['response']['state'])
return self._car_online[vehicle_id]

def update(self, car_id=None, wake_if_asleep=False, force=False):
Expand Down

0 comments on commit 2c45f2d

Please sign in to comment.