-
Notifications
You must be signed in to change notification settings - Fork 9
/
teslacontrol.py
160 lines (133 loc) · 3.7 KB
/
teslacontrol.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
from flask import Flask
import os
import teslajson
from geopy.distance import vincenty
import json
TESLA_EMAIL = ''
TESLA_PASSWORD = ''
VEHICLE_VIN = ''
HOME_LOCATION = (, )
def establish_connection(token=None):
c = teslajson.Connection(email=TESLA_EMAIL, password=TESLA_PASSWORD, access_token=token)
return c
def get_climate(c, car):
climate = None
for v in c.vehicles:
if v["vin"] == car:
d = v.data_request("climate_state")
climate = d
return climate
def get_lockstatus(c, car):
lockstatus = None
for v in c.vehicles:
if v["vin"] == car:
d = v.data_request("vehicle_state")
lockstatus = d
return lockstatus
def get_isclimateon(c, car):
return get_climate(c, car)['is_climate_on']
def get_iscarlocked(c,car):
return get_lockstatus(c, car)['locked']
def get_location(c, car):
location = None
for v in c.vehicles:
if v["vin"] == car:
d = v.data_request("drive_state")
location = (d["latitude"], d["longitude"])
return location
def get_distancefromhome(c, car):
current_location = get_location(c, car)
return vincenty(current_location, HOME_LOCATION).meters
def get_isvehiclehome(c, car):
if get_distancefromhome(c, car) <= 100:
return True
else:
return False
def start_hvac(c, car):
for v in c.vehicles:
if v["vin"] == car:
result = v.command('auto_conditioning_start')['response']['result']
return result
def stop_hvac(c, car):
for v in c.vehicles:
if v["vin"] == car:
result = v.command('auto_conditioning_stop')['response']['result']
return result
def door_lock(c, car):
for v in c.vehicles:
if v["vin"] == car:
result = v.command('door_lock')['response']['result']
return result
def door_unlock(c, car):
for v in c.vehicles:
if v["vin"] == car:
result = v.command('door_unlock')['response']['result']
return result
app = Flask(__name__)
@app.route('/')
def index():
return 'Hello, World!'
@app.route('/api/distancefromhome')
def distancefromhome():
c = establish_connection()
data = {}
data['distancefromhome'] = str(get_distancefromhome(c, VEHICLE_VIN))
return json.dumps(data)
@app.route('/api/isvehiclehome')
def isvehiclehome():
c = establish_connection()
data = {}
data['isvehiclehome'] = str(get_isvehiclehome(c, VEHICLE_VIN))
return json.dumps(data)
@app.route('/api/getclimate')
def getclimate():
c = establish_connection()
return str(get_climate(c, VEHICLE_VIN))
@app.route('/api/isclimateon')
def isclimateon():
c = establish_connection()
data = {}
data['isclimateon'] = str(get_isclimateon(c, VEHICLE_VIN))
return json.dumps(data)
@app.route('/api/starthvac')
def starthvac():
c = establish_connection()
data = {}
data['result'] = str(start_hvac(c, VEHICLE_VIN))
return json.dumps(data)
@app.route('/api/stophvac')
def stophvac():
c = establish_connection()
data = {}
data['result'] = str(stop_hvac(c, VEHICLE_VIN))
return json.dumps(data)
@app.route('/api/getlockstatus')
def getlockstatus():
c = establish_connection()
return str(get_lockstatus(c, VEHICLE_VIN))
@app.route('/api/iscarlocked')
def iscarlocked():
c = establish_connection()
data = {}
data['iscarlocked'] = str(get_iscarlocked(c, VEHICLE_VIN))
return json.dumps(data)
@app.route('/api/doorlock')
def doorlock():
c = establish_connection()
data = {}
data['result'] = str(door_lock(c, VEHICLE_VIN))
return json.dumps(data)
@app.route('/api/doorunlock')
def doorunlock():
c = establish_connection()
data = {}
data['result'] = str(door_unlock(c, VEHICLE_VIN))
return json.dumps(data)
@app.route('/api/refresh')
def refresh():
c = establish_connection()
data = {}
data['iscarlocked'] = str(get_iscarlocked(c, VEHICLE_VIN))
data['isclimateon'] = str(get_isclimateon(c, VEHICLE_VIN))
data['isvehiclehome'] = str(get_isvehiclehome(c, VEHICLE_VIN))
return json.dumps(data)