Skip to content

Commit

Permalink
Fix for issues like: "tesla lock took longer than the scheduled updat…
Browse files Browse the repository at this point in the history
…e interval 0:00:30" in HASS.
  • Loading branch information
zabuldon committed Sep 14, 2017
1 parent 071d290 commit e777840
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions teslajsonpy/controller.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import time
import datetime
from threading import RLock

from teslajsonpy.connection import Connection
from teslajsonpy.BatterySensor import Battery
from teslajsonpy.Lock import Lock
from teslajsonpy.Climate import Climate, TempSensor
from teslajsonpy.BinarySensor import ParkingSensor, ChargerConnectionSensor
from teslajsonpy.Charger import ChargerSwitch
from teslajsonpy.GPS import GPS


Expand All @@ -22,15 +24,16 @@ def __init__(self, email, password, update_interval):
self.__lock = RLock()

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

for car in cars:
self.__last_update_time[car['id']] = 0
self.wake_up(car['id'])
self.update(car['id'])
self.__vehicles.append(Climate(car, self))
self.__vehicles.append(Battery(car, self))
self.__vehicles.append(TempSensor(car, self))
self.__vehicles.append(Lock(car, self))
self.__vehicles.append(ChargerConnectionSensor(car, self))
self.__vehicles.append(ChargerSwitch(car, self))
self.__vehicles.append(ParkingSensor(car, self))
self.__vehicles.append(GPS(car, self))

Expand All @@ -56,10 +59,11 @@ def update(self, car_id):
if time.time() - self.__last_update_time[car_id] > self.update_interval:
self.__lock.acquire()
self.wake_up(car_id)
self.__climate[car_id] = self.data_request(car_id, 'climate_state')
self.__charging[car_id] = self.data_request(car_id, 'charge_state')
self.__state[car_id] = self.data_request(car_id, 'vehicle_state')
self.__driving[car_id] = self.data_request(car_id, 'drive_state')
data = self.get(car_id, 'data')['response']
self.__climate[car_id] = data['climate_state']
self.__charging[car_id] = data['charge_state']
self.__state[car_id] = data['vehicle_state']
self.__driving[car_id] = data['drive_state']
self.__last_update_time[car_id] = time.time()
self.__lock.release()

Expand Down

0 comments on commit e777840

Please sign in to comment.