Skip to content

Commit

Permalink
Fix bugs where car would not wake for command (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
alandtse committed Mar 5, 2019
1 parent 43df0db commit 6768552
Show file tree
Hide file tree
Showing 11 changed files with 275 additions and 56 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',
],
)
4 changes: 2 additions & 2 deletions teslajsonpy/BatterySensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def __init__(self, data, controller):
self.update()

def update(self):
self._controller.update(self._id)
self._controller.update(self._id, wake_if_asleep=False)
data = self._controller.get_charging_params(self._id)
if data:
self.__battery_level = data['battery_level']
Expand Down Expand Up @@ -46,7 +46,7 @@ def __init__(self, data, controller):
self.update()

def update(self):
self._controller.update(self._id)
self._controller.update(self._id, wake_if_asleep=False)
data = self._controller.get_charging_params(self._id)
if data:
self.__battery_range = data['battery_range']
Expand Down
4 changes: 2 additions & 2 deletions teslajsonpy/BinarySensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def __init__(self, data, controller):
self.update()

def update(self):
self._controller.update(self._id)
self._controller.update(self._id, wake_if_asleep=False)
data = self._controller.get_drive_params(self._id)
if data:
if not data['shift_state'] or data['shift_state'] == 'P':
Expand Down Expand Up @@ -45,7 +45,7 @@ def __init__(self, data, controller):
self.bin_type = 0x2

def update(self):
self._controller.update(self._id)
self._controller.update(self._id, wake_if_asleep=False)
data = self._controller.get_charging_params(self._id)
if data:
if data['charging_state'] in ["Disconnected", "Stopped", "NoPower"]:
Expand Down
16 changes: 10 additions & 6 deletions teslajsonpy/Charger.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def __init__(self, data, controller):
self.update()

def update(self):
self._controller.update(self._id)
self._controller.update(self._id, wake_if_asleep=False)
data = self._controller.get_charging_params(self._id)
if data and (time.time() - self.__manual_update_time > 60):
if data['charging_state'] != "Charging":
Expand All @@ -25,14 +25,16 @@ def update(self):

def start_charge(self):
if not self.__charger_state:
data = self._controller.command(self._id, 'charge_start')
data = self._controller.command(self._id, 'charge_start',
wake_if_asleep=True)
if data and data['response']['result']:
self.__charger_state = True
self.__manual_update_time = time.time()

def stop_charge(self):
if self.__charger_state:
data = self._controller.command(self._id, 'charge_stop')
data = self._controller.command(self._id, 'charge_stop',
wake_if_asleep=True)
if data and data['response']['result']:
self.__charger_state = False
self.__manual_update_time = time.time()
Expand All @@ -58,21 +60,23 @@ def __init__(self, data, controller):
self.update()

def update(self):
self._controller.update(self._id)
self._controller.update(self._id, wake_if_asleep=False)
data = self._controller.get_charging_params(self._id)
if data and (time.time() - self.__manual_update_time > 60):
self.__maxrange_state = data['charge_to_max_range']

def set_max(self):
if not self.__maxrange_state:
data = self._controller.command(self._id, 'charge_max_range')
data = self._controller.command(self._id, 'charge_max_range',
wake_if_asleep=True)
if data['response']['result']:
self.__maxrange_state = True
self.__manual_update_time = time.time()

def set_standard(self):
if self.__maxrange_state:
data = self._controller.command(self._id, 'charge_standard')
data = self._controller.command(self._id, 'charge_standard',
wake_if_asleep=True)
if data and data['response']['result']:
self.__maxrange_state = False
self.__manual_update_time = time.time()
Expand Down
17 changes: 12 additions & 5 deletions teslajsonpy/Climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def get_fan_status(self):
return self.__fan_status

def update(self):
self._controller.update(self._id)
self._controller.update(self._id, wake_if_asleep=False)

data = self._controller.get_climate_params(self._id)
if data:
Expand All @@ -56,20 +56,27 @@ def update(self):
def set_temperature(self, temp):
temp = round(temp, 1)
self.__manual_update_time = time.time()
data = self._controller.command(self._id, 'set_temps', {"driver_temp": temp, "passenger_temp": temp})
data = self._controller.command(self._id, 'set_temps',
{"driver_temp": temp,
"passenger_temp": temp},
wake_if_asleep=True)
if data['response']['result']:
self.__driver_temp_setting = temp
self.__passenger_temp_setting = temp

def set_status(self, enabled):
self.__manual_update_time = time.time()
if enabled:
data = self._controller.command(self._id, 'auto_conditioning_start')
data = self._controller.command(self._id,
'auto_conditioning_start',
wake_if_asleep=True)
if data['response']['result']:
self.__is_auto_conditioning_on = True
self.__is_climate_on = True
else:
data = self._controller.command(self._id, 'auto_conditioning_stop')
data = self._controller.command(self._id,
'auto_conditioning_stop',
wake_if_asleep=True)
if data['response']['result']:
self.__is_auto_conditioning_on = False
self.__is_climate_on = False
Expand Down Expand Up @@ -101,7 +108,7 @@ def get_outside_temp(self):
return self.__outside_temp

def update(self):
self._controller.update(self._id)
self._controller.update(self._id, wake_if_asleep=False)
data = self._controller.get_climate_params(self._id)
if data:
self.__inside_temp = data['inside_temp'] if data['inside_temp'] else self.__inside_temp
Expand Down
4 changes: 4 additions & 0 deletions teslajsonpy/Exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ def __init__(self, code, *args, **kwargs):
self.message = 'SERVICE_MAINTENANCE'
elif self.code > 299:
self.message = "UNKNOWN_ERROR"

class RetryLimitError(TeslaException):
def __init__(self, *args, **kwargs):
pass
4 changes: 2 additions & 2 deletions teslajsonpy/GPS.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def get_location(self):
return self.__location

def update(self):
self._controller.update(self._id)
self._controller.update(self._id, wake_if_asleep=False)
data = self._controller.get_drive_params(self._id)
if data:
self.__longitude = data['longitude']
Expand Down Expand Up @@ -54,7 +54,7 @@ def __init__(self, data, controller):
self.__rated = True

def update(self):
self._controller.update(self._id)
self._controller.update(self._id, wake_if_asleep=False)
data = self._controller.get_state_params(self._id)
if data:
self.__odometer = data['odometer']
Expand Down
16 changes: 10 additions & 6 deletions teslajsonpy/Lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,23 @@ def __init__(self, data, controller):
self.update()

def update(self):
self._controller.update(self._id)
self._controller.update(self._id, wake_if_asleep=False)
data = self._controller.get_state_params(self._id)
if data and (time.time() - self.__manual_update_time > 60):
self.__lock_state = data['locked']

def lock(self):
if not self.__lock_state:
data = self._controller.command(self._id, 'door_lock')
data = self._controller.command(self._id, 'door_lock',
wake_if_asleep=True)
if data['response']['result']:
self.__lock_state = True
self.__manual_update_time = time.time()

def unlock(self):
if self.__lock_state:
data = self._controller.command(self._id, 'door_unlock')
data = self._controller.command(self._id, 'door_unlock',
wake_if_asleep=True)
if data['response']['result']:
self.__lock_state = False
self.__manual_update_time = time.time()
Expand Down Expand Up @@ -61,21 +63,23 @@ def __init__(self, data, controller):
self.update()

def update(self):
self._controller.update(self._id)
self._controller.update(self._id, wake_if_asleep=False)
data = self._controller.get_charging_params(self._id)
if data and (time.time() - self.__manual_update_time > 60):
self.__lock_state = not ((data['charge_port_door_open']) and (data['charge_port_door_open']) and (data['charge_port_latch'] != 'Engaged'))

def lock(self):
if not self.__lock_state:
data = self._controller.command(self._id, 'charge_port_door_close')
data = self._controller.command(self._id, 'charge_port_door_close',
wake_if_asleep=True)
if data['response']['result']:
self.__lock_state = True
self.__manual_update_time = time.time()

def unlock(self):
if self.__lock_state:
data = self._controller.command(self._id, 'charge_port_door_open')
data = self._controller.command(self._id, 'charge_port_door_open',
wake_if_asleep=True)
if data['response']['result']:
self.__lock_state = False
self.__manual_update_time = time.time()
Expand Down
7 changes: 7 additions & 0 deletions teslajsonpy/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
from urllib.request import Request, build_opener
from urllib.error import HTTPError
import json
import logging
from teslajsonpy.Exceptions import TeslaException
_LOGGER = logging.getLogger(__name__)


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 Expand Up @@ -50,6 +53,8 @@ def __open(self, url, headers={}, data=None, baseurl=""):
if not baseurl:
baseurl = self.baseurl
req = Request("%s%s" % (baseurl, url), headers=headers)
_LOGGER.debug(url)

try:
req.data = urlencode(data).encode('utf-8')
except TypeError:
Expand All @@ -61,9 +66,11 @@ def __open(self, url, headers={}, data=None, baseurl=""):
charset = resp.info().get('charset', 'utf-8')
data = json.loads(resp.read().decode(charset))
opener.close()
_LOGGER.debug(json.dumps(data))
return data
except HTTPError as e:
if e.code == 408:
_LOGGER.debug("%s", e)
return False
else:
raise TeslaException(e.code)
Loading

0 comments on commit 6768552

Please sign in to comment.