Skip to content

Commit

Permalink
Refactoring for HASS
Browse files Browse the repository at this point in the history
  • Loading branch information
zabuldon committed Aug 29, 2017
1 parent 4fe78e9 commit 4ac4d9c
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 93 deletions.
2 changes: 1 addition & 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.5',
version='0.0.6',
packages=['teslajsonpy'],
include_package_data=True,
license='WTFPL',
Expand Down
14 changes: 10 additions & 4 deletions teslajsonpy/BatterySensor.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
from teslajsonpy.vehicle import Vehicle
from teslajsonpy.vehicle import VehicleDevice


class Battery(Vehicle):
class Battery(VehicleDevice):
def __init__(self, data, controller):
Vehicle.__init__(self, data, controller)
VehicleDevice.__init__(self, data, controller)
self.__id = data['id']
self.__vehicle_id = data['vehicle_id']
self.__controller = controller
self.__battery_level = 0
self.__charging_state = None
self.__charge_port_door_open = None

self.name = 'Tesla model {} {}'.format(
str(self.__vin[3]).upper(), self.type)

self.uniq_name = 'Tesla model {} {} {}'.format(
str(self.__vin[3]).upper(), self.__vin, self.type)

self.type = 'battery sensor'
self.measurement = '%'
self.hass_type = 'sensor'
self.na

self.update()

def update(self):
Expand Down
41 changes: 28 additions & 13 deletions teslajsonpy/BinarySensor.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
from teslajsonpy.vehicle import Vehicle
from teslajsonpy.vehicle import VehicleDevice


class ParkingSensor(Vehicle):
class ParkingSensor(VehicleDevice):
def __init__(self, data, controller):
Vehicle.__init__(self, data, controller)
VehicleDevice.__init__(self, data, controller)
self.__id = data['id']
self.__vehicle_id = data['vehicle_id']
self.__vin = data['vin']
self.controller = controller
self.__logger = self.controller.get_logger()
self.__controller = controller
self.__state = False

self.type = 'parking sensor'
self.hass_type = 'binary_sensor'
self.name = 'Tesla model {} {}'.format(
str(self.__vin[3]).upper(), self.type)

self.uniq_name = 'Tesla model {} {} {}'.format(
str(self.__vin[3]).upper(), self.__vin, self.type)

self.update()

def update(self):
self.controller.update(self.__id)
data = self.controller.get_drive_params(self.__id)
self.__controller.update(self.__id)
data = self.__controller.get_drive_params(self.__id)
if not data['shift_state'] or data['shift_state'] == 'P':
self.__state = True
else:
Expand All @@ -29,21 +36,29 @@ def has_battery():
return False


class ChargerConnectionSensor(Vehicle):
class ChargerConnectionSensor(VehicleDevice):
def __init__(self, data, controller):
Vehicle.__init__(self, data, controller)
VehicleDevice.__init__(self, data, controller)
self.__id = data['id']
self.__vehicle_id = data['vehicle_id']
self.__vin = data['vin']
self.controller = controller
self.__logger = self.controller.get_logger()
self.__controller = controller
self.__logger = self.__controller.get_logger()
self.__state = False

self.type = 'charger sensor'
self.hass_type = 'binary_sensor'
self.name = 'Tesla model {} {}'.format(
str(self.__vin[3]).upper(), self.type)

self.uniq_name = 'Tesla model {} {} {}'.format(
str(self.__vin[3]).upper(), self.__vin, self.type)

self.update()

def update(self):
self.controller.update(self.__id)
data = self.controller.get_charging_params(self.__id)
self.__controller.update(self.__id)
data = self.__controller.get_charging_params(self.__id)
if data['charging_state'] in ["Disconnected", "Stopped", "NoPower"]:
self.__state = False
else:
Expand Down
68 changes: 35 additions & 33 deletions teslajsonpy/Climate.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
from teslajsonpy.vehicle import Vehicle
from teslajsonpy.vehicle import VehicleDevice
import time


class Climate(Vehicle):
class Climate(VehicleDevice):
def __init__(self, data, controller):
Vehicle.__init__(self, data, controller)
VehicleDevice.__init__(self, data, controller)
self.__id = data['id']
self.__vehicle_id = data['vehicle_id']
self.__vin = data['vin']
self.__state = data['state']
self.__remote_start_enabled = data['remote_start_enabled']
self.__in_service = data['in_service']
self.controller = controller
self.__logger = self.controller.get_logger()

self.__controller = controller
self.__is_auto_conditioning_on = False
self.__inside_temp = 0
self.__outside_temp = 0
Expand All @@ -22,7 +20,16 @@ def __init__(self, data, controller):
self.__is_climate_on = False
self.__fan_status = 0
self.__manual_update_time = 0
self.type = 'climate'

self.name = 'Tesla model {} {}'.format(
str(self.__vin[3]).upper(), self.type)

self.uniq_name = 'Tesla model {} {} {}'.format(
str(self.__vin[3]).upper(), self.__vin, self.type)

self.type = 'HVAC system'
self.hass_type = 'climate'

self.update()

def is_hvac_enabled(self):
Expand All @@ -38,12 +45,9 @@ def get_fan_status(self):
return self.__fan_status

def update(self):
self.__logger.debug("Updating climate params started. Vehicle ID: %s Sensor type is: %s"
% (self.__id, self.type))
self.controller.update(self.__id)
self.__controller.update(self.__id)

data = self.controller.get_climate_params(self.__id)
self.__logger.debug(data)
data = self.__controller.get_climate_params(self.__id)
if time.time() - self.__manual_update_time > 60:
self.__is_auto_conditioning_on = data['is_auto_conditioning_on']
self.__is_climate_on = data['is_climate_on']
Expand All @@ -54,51 +58,54 @@ def update(self):
self.__inside_temp = data['inside_temp'] if data['inside_temp'] else self.__inside_temp
self.__outside_temp = data['outside_temp'] if data['outside_temp'] else self.__outside_temp
self.__fan_status = data['fan_status']
self.__logger.debug("Updating climate params finished. Vehicle ID: %s Sensor type is: %s"
% (self.__id, self.type))

def set_temperature(self, temp):
temp = round(temp, 1)
self.__manual_update_time = time.time()
self.__logger.debug("Updating goal temperature. Temperature is %s" % temp)
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})
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')
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')
if data['response']['result']:
self.__is_auto_conditioning_on = False
self.__is_climate_on = False
self.update()

def set_fan_status(self, status):
return self

@staticmethod
def has_battery():
return False


class TempSensor(Vehicle):
class TempSensor(VehicleDevice):
def __init__(self, data, controller):
Vehicle.__init__(self, data, controller)
VehicleDevice.__init__(self, data, controller)
self.__id = data['id']
self.__vehicle_id = data['vehicle_id']
self.__vin = data['vin']
self.controller = controller
self.__controller = controller
self.__inside_temp = 0
self.__outside_temp = 0
self.type = 'temp sensor'
self.__logger = self.controller.get_logger()

self.name = 'Tesla model {} {}'.format(
str(self.__vin[3]).upper(), self.type)

self.uniq_name = 'Tesla model {} {} {}'.format(
str(self.__vin[3]).upper(), self.__vin, self.type)

self.type = 'temperature sensor'
self.measurement = 'C'
self.hass_type = 'sensor'

self.update()

def get_inside_temp(self):
Expand All @@ -108,15 +115,10 @@ def get_outside_temp(self):
return self.__outside_temp

def update(self):
self.__logger.debug("Updating climate params started. Vehicle ID: %s Sensor type is: %s"
% (self.__id, self.type))
self.controller.update(self.__id)
data = self.controller.get_climate_params(self.__id)
self.__logger.debug(data)
self.__controller.update(self.__id)
data = self.__controller.get_climate_params(self.__id)
self.__inside_temp = data['inside_temp'] if data['inside_temp'] else self.__inside_temp
self.__outside_temp = data['outside_temp'] if data['outside_temp'] else self.__outside_temp
self.__logger.debug("Updating climate params finished. Vehicle ID: %s Sensor type is: %s"
% (self.__id, self.type))

@staticmethod
def has_battery():
Expand Down
32 changes: 18 additions & 14 deletions teslajsonpy/GPS.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,46 @@
from teslajsonpy.vehicle import Vehicle
from teslajsonpy.vehicle import VehicleDevice


class GPS(Vehicle):
class GPS(VehicleDevice):
def __init__(self, data, controller):
Vehicle.__init__(self, data, controller)
VehicleDevice.__init__(self, data, controller)
self.__id = data['id']
self.__vehicle_id = data['vehicle_id']
self.__vin = data['vin']
self.controller = controller
self.type = 'device tracker'
self.__logger = self.controller.get_logger()
self.__controller = controller

self.__longitude = 0
self.__latitude = 0
self.__heading = 0
self.__location = {}

self.last_seen = 0
self.last_updated = 0

self.name = 'Tesla model {} {}'.format(
str(self.__vin[3]).upper(), self.type)

self.uniq_name = 'Tesla model {} {} {}'.format(
str(self.__vin[3]).upper(), self.__vin, self.type)

self.type = 'device tracker'
self.hass_type = 'devices_tracker'

self.update()

def get_location(self):
return self.__location

def update(self):
self.__logger.debug("Updating positioning params started. Vehicle ID: %s Sensor type is: %s"
% (self.__id, self.type))
self.controller.update(self.__id)
data = self.controller.get_drive_params(self.__id)
self.__logger.debug(data)
self.__controller.update(self.__id)
data = self.__controller.get_drive_params(self.__id)
self.__longitude = data['longitude']
self.__latitude = data['latitude']
self.__heading = data['heading']
if data['latitude'] and data['longitude'] and data['heading']:
self.__location = {'longitude': self.__longitude,
'latitude': self.__latitude,
'heading': self.__heading}
self.last_updated
self.__logger.debug("Updating positioning params finished. Vehicle ID: %s Sensor type is: %s"
% (self.__id, self.type))

@staticmethod
def has_battery():
Expand Down
36 changes: 19 additions & 17 deletions teslajsonpy/Lock.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,46 @@
from teslajsonpy.vehicle import Vehicle
from teslajsonpy.vehicle import VehicleDevice
import time


class Lock(Vehicle):
class Lock(VehicleDevice):
def __init__(self, data, controller):
Vehicle.__init__(self, data, controller)
VehicleDevice.__init__(self, data, controller)
self.__id = data['id']
self.__vehicle_id = data['vehicle_id']
self.__vin = data['vin']
self.__state = data['state']
self.controller = controller
self.__logger = self.controller.get_logger()

self.__controller = controller
self.__logger = self.__controller.get_logger()
self.__manual_update_time = 0
self.__lock_state = False
self.type = 'lock'

self.name = 'Tesla model {} {}'.format(
str(self.__vin[3]).upper(), self.type)

self.uniq_name = 'Tesla model {} {} {}'.format(
str(self.__vin[3]).upper(), self.__vin, self.type)

self.type = 'door lock'
self.hass_type = 'lock'

self.update()

def update(self):
self.__logger.debug('Updating lock state started. Vehicle ID: %s' % self.__id)
self.controller.update(self.__id)
data = self.controller.get_state_params(self.__id)
self.__controller.update(self.__id)
data = self.__controller.get_state_params(self.__id)
if time.time() - self.__manual_update_time > 60:
self.__lock_state = data['locked']
self.__logger.debug(data)
self.__logger.debug('Updating lock state finished. Vehicle ID: %s' % self.__id)

def lock(self):
if not self.__lock_state:
data = self.controller.command(self.__id, 'door_lock')
print(data)

data = self.__controller.command(self.__id, 'door_lock')
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')
print(data)
data = self.__controller.command(self.__id, 'door_unlock')
if data['response']['result']:
self.__lock_state = False
self.__manual_update_time = time.time()
Expand Down
1 change: 0 additions & 1 deletion teslajsonpy/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ def __open(self, url, headers={}, data=None, baseurl=""):
except:
pass
opener = build_opener()
#opener.addheaders = [('', self.user_agent)]
resp = opener.open(req)
charset = resp.info().get('charset', 'utf-8')
return json.loads(resp.read().decode(charset))
Loading

0 comments on commit 4ac4d9c

Please sign in to comment.