Skip to content

Commit

Permalink
Merge pull request #95 from alandtse/#ha37684
Browse files Browse the repository at this point in the history
fix: change controller_lock to be a function lock
  • Loading branch information
alandtse committed Jul 12, 2020
2 parents dc993dc + 23e7de5 commit 1059202
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions teslajsonpy/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,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 # controls access to self.car_online
self.__update_lock = None # controls access to update function
self.__wakeup_conds = {}
self.car_online = {}
self.car_state = {}
Expand Down Expand Up @@ -266,7 +266,7 @@ async def connect(

cars = await self.get_vehicles()
self._last_attempted_update_time = time.time()
self.__controller_lock = asyncio.Lock()
self.__update_lock = asyncio.Lock()

for car in cars:
vin = car["vin"]
Expand Down Expand Up @@ -507,20 +507,19 @@ async def _wake_up(self, car_id):
car_id = self._update_id(car_id)
async with self.__wakeup_conds[car_vin]:
cur_time = int(time.time())
async with self.__controller_lock:
if not self.car_online[car_vin] or (
cur_time - self._last_wake_up_time[car_vin] > self.update_interval
):
result = await self.post(
car_id, "wake_up", wake_if_asleep=False
) # avoid wrapper loop
self.car_online[car_vin] = result["response"]["state"] == "online"
self.car_state[car_vin] = result["response"]
self._last_wake_up_time[car_vin] = cur_time
_LOGGER.debug(
"Wakeup %s: %s", car_vin[-5:], self.car_state[car_vin]["state"]
)
return self.car_online[car_vin]
if not self.car_online[car_vin] or (
cur_time - self._last_wake_up_time[car_vin] > self.update_interval
):
result = await self.post(
car_id, "wake_up", wake_if_asleep=False
) # avoid wrapper loop
self.car_online[car_vin] = result["response"]["state"] == "online"
self.car_state[car_vin] = result["response"]
self._last_wake_up_time[car_vin] = cur_time
_LOGGER.debug(
"Wakeup %s: %s", car_vin[-5:], self.car_state[car_vin]["state"]
)
return self.car_online[car_vin]

async def update(self, car_id=None, wake_if_asleep=False, force=False):
# pylint: disable=too-many-locals,too-many-statements
Expand Down Expand Up @@ -652,8 +651,8 @@ async def _get_and_process_data(vin: Text) -> None:
)
)

cur_time = time.time()
async with self.__controller_lock:
async with self.__update_lock:
cur_time = time.time()
# Update the online cars using get_vehicles()
last_update = self._last_attempted_update_time
if force or cur_time - last_update > ONLINE_INTERVAL:
Expand Down

0 comments on commit 1059202

Please sign in to comment.