-
Notifications
You must be signed in to change notification settings - Fork 0
/
set-vin-app.py
86 lines (59 loc) · 2.32 KB
/
set-vin-app.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
# encoding: utf-8
import sys
import version
from icons import ICON_ACCOUNT, ICON_REFRESH
from workflow import Workflow
log = None
def refresh_vehicles():
username = wf.stored_data('username')
log.debug("Connecting to Tesla and retrieving list of vehicles")
import teslapy
tesla = teslapy.TeslaPy(username)
tesla.connect(None)
vehicles = tesla.get_vehicles()
if vehicles:
cached_vehicles_list = []
cached_vehicles_dict = {'vehicles' : cached_vehicles_list}
for vehicle in vehicles:
cached_vehicles_list.append(vehicle)
wf.store_data('cached_vehicles', cached_vehicles_dict)
def main(wf):
# type: (Workflow) -> int
username = wf.stored_data('username')
if not username:
wf.add_item(title="Set Tesla Login Credentials",
subtitle="Please wait for credential dialog to appear",
valid=True,
arg="credentials",
icon=ICON_ACCOUNT)
wf.send_feedback()
else:
cached_vehicles_dict = wf.stored_data('cached_vehicles')
if not cached_vehicles_dict:
refresh_vehicles()
cached_vehicles_dict = wf.stored_data('cached_vehicles')
cached_vehicles_list = cached_vehicles_dict['vehicles']
for vehicle in cached_vehicles_list:
name = vehicle['display_name']
vin = vehicle['vin']
wf.add_item(title="%s (VIN %s)" % (name, vin),
subtitle="Set %s as the active vehicle for this workflow" % name,
valid=True,
arg="--vin %s --name %s" % (vin, name))
wf.add_item(title="Refresh list of Tesla Vehicles",
subtitle="Clear and refresh the list of your Tesla Vehicles",
valid=True,
arg="refresh_vehicles",
icon=ICON_REFRESH)
wf.add_item(title="Change Tesla account",
subtitle="Please wait for credential dialog to appear",
valid=True,
arg="credentials",
icon=ICON_ACCOUNT)
wf.send_feedback()
return 0
if __name__ == u"__main__":
wf = Workflow(libraries=['./lib'])
wf.set_last_version(version.version)
log = wf.logger
sys.exit(wf.run(main))