Skip to content

Commit

Permalink
fix(controller): add lock for get_vehicles in update
Browse files Browse the repository at this point in the history
  • Loading branch information
alandtse committed Dec 26, 2019
1 parent 7585646 commit 4eb2855
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions teslajsonpy/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def __init__(
self._last_wake_up_time = {} # succesful wake_ups by car
self._last_attempted_update_time = 0 # all attempts by controller
self.__lock = {}
self.__controller_lock = None
self.__wakeup_conds = {}
self.car_online = {}

Expand All @@ -73,6 +74,7 @@ async def connect(self, test_login=False) -> Tuple[Text, Text]:
if test_login:
return (self.__connection.refresh_token, self.__connection.access_token)
self._last_attempted_update_time = time.time()
self.__controller_lock = asyncio.Lock()

for car in cars:
self.__lock[car["id"]] = asyncio.Lock()
Expand Down Expand Up @@ -457,14 +459,14 @@ async def update(self, car_id=None, wake_if_asleep=False, force=False):
"""
cur_time = time.time()
# async with self.__lock:
# Update the online cars using get_vehicles()
last_update = self._last_attempted_update_time
if force or cur_time - last_update > self.update_interval:
cars = await self.get_vehicles()
for car in cars:
self.car_online[car["id"]] = car["state"] == "online"
self._last_attempted_update_time = cur_time
async with self.__controller_lock:
# Update the online cars using get_vehicles()
last_update = self._last_attempted_update_time
if force or cur_time - last_update > self.update_interval:
cars = await self.get_vehicles()
for car in cars:
self.car_online[car["id"]] = car["state"] == "online"
self._last_attempted_update_time = cur_time
# Only update online vehicles that haven't been updated recently
# The throttling is per car's last succesful update
# Note: This separate check is because there may be individual cars
Expand Down

0 comments on commit 4eb2855

Please sign in to comment.