Skip to content

Commit

Permalink
possible fix for #16
Browse files Browse the repository at this point in the history
  • Loading branch information
zabuldon committed Jan 28, 2019
1 parent 43df0db commit 7b5ddc5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from setuptools import setup
setup(
name='teslajsonpy',
version='0.0.22',
version='0.0.24',
packages=['teslajsonpy'],
include_package_data=True,
python_requires='>=3',
Expand All @@ -17,6 +17,8 @@
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Topic :: Internet',
],
)
3 changes: 3 additions & 0 deletions teslajsonpy/Exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ def __init__(self, code, *args, **kwargs):
self.message = 'SERVICE_MAINTENANCE'
elif self.code > 299:
self.message = "UNKNOWN_ERROR"

class RetryLimitError(TeslaException):
pass
1 change: 1 addition & 0 deletions teslajsonpy/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

class Connection(object):
"""Connection to Tesla Motors API"""

def __init__(self, email, password):
"""Initialize connection object"""
self.user_agent = 'Model S 2.1.79 (SM-G900V; Android REL 4.4.4; en_US'
Expand Down
45 changes: 41 additions & 4 deletions teslajsonpy/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from teslajsonpy.BinarySensor import ParkingSensor, ChargerConnectionSensor
from teslajsonpy.Charger import ChargerSwitch, RangeSwitch
from teslajsonpy.GPS import GPS, Odometer
from functools import wraps
from .Exceptions import RetryLimitError


class Controller:
Expand All @@ -21,11 +23,17 @@ def __init__(self, email, password, update_interval):
self.__driving = {}
self.__gui = {}
self.__last_update_time = {}
self.__last_wake_up_time = {}
self.__lock = RLock()
self.__car_online = {}

cars = self.__connection.get('vehicles')['response']

for car in cars:
self.__last_update_time[car['id']] = 0
self.__last_wake_up_time[car['id']] = 0
self.__update[car['id']] = True
self.__car_online[car['id']] = False
self.update(car['id'])
self.__vehicles.append(Climate(car, self))
self.__vehicles.append(Battery(car, self))
Expand Down Expand Up @@ -55,15 +63,37 @@ def command(self, vehicle_id, name, data={}):
def list_vehicles(self):
return self.__vehicles

def wake_up(self, vehicle_id):
self.post(vehicle_id, 'wake_up')

def wake_up(f):
@wraps(f)
def wrapped(inst, *args, **kwargs):
retries = 0
sleep_delay = 2
while True:
result = inst._wake_up(*args)
if not result:
if retries < 5:

This comment was marked as resolved.

Copy link
@alandtse

alandtse Jan 29, 2019

Collaborator

I may not understand decorators, but where is retries being incremented? This looks like an infinite loop.

This comment was marked as resolved.

Copy link
@alandtse

alandtse Jan 29, 2019

Collaborator

should it be

                    if retries < 5:
                        retries += 1

Edit because I forgot python doesn't have a ++ operator.

time.sleep(sleep_delay)
continue
else:
raise RetryLimitError
else:
break
return f(inst, *args, **kwargs)
return wrapped

def _wake_up(self, vehicle_id):
cur_time = int(time.time())
if not self.__car_online[vehicle_id] or cur_time - self.__last_wake_up_time[vehicle_id] > 300:
self.__car_online[vehicle_id] = self.post(vehicle_id, 'wake_up')['response']['state'] == 'online'
self.__last_wake_up_time[vehicle_id] = cur_time
return self.__car_online[vehicle_id]

@wake_up
def update(self, car_id):
cur_time = time.time()
with self.__lock:
if (self.__update[car_id] and
(cur_time - self.__last_update_time[car_id] > self.update_interval)):
self.wake_up(car_id)
data = self.get(car_id, 'data')
if data and data['response']:
self.__climate[car_id] = data['response']['climate_state']
Expand All @@ -79,26 +109,33 @@ def update(self, car_id):
self.__driving[car_id] = False
self.__gui[car_id] = False

@wake_up
def get_climate_params(self, car_id):
return self.__climate[car_id]

@wake_up
def get_charging_params(self, car_id):
return self.__charging[car_id]

@wake_up
def get_state_params(self, car_id):
return self.__state[car_id]

@wake_up
def get_drive_params(self, car_id):
return self.__driving[car_id]

@wake_up
def get_gui_params(self, car_id):
return self.__gui[car_id]

@wake_up
def get_updates(self, car_id=None):
if car_id is not None:
return self.__update[car_id]
else:
return self.__update

@wake_up
def set_updates(self, car_id, value):
self.__update[car_id] = value

0 comments on commit 7b5ddc5

Please sign in to comment.