-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
150 lines (122 loc) · 6.74 KB
/
main.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
# This is an example of how to use the library tesla_api
import os
import config
import lib.tesla_api.tesla_api_2024 as tesla_api
import lib.logger as logger
import json
def register_developer_account():
# check, if .dev_registered file exists
try:
with open(".dev_registered", "r") as f:
if f.read() == "registered":
print("The developer account is already registered")
print("delete the file '.dev_registered' to re-register the developer account")
return
except FileNotFoundError:
pass
print("Registering a developer account")
print("Please go to the following URL and register a developer account and your app (Python program):")
print("https://developer.tesla.com/")
input("Press [Enter] to continue, when registration is complete or <Ctrl-C> to cancel...")
if config.tesla_client_id == "aaaaaaaaaaaa-bbbb-cccc-ddddddddddd":
print("Please enter the client_id and client_secret and the rest of the info into the config.py file")
logger.error("Please enter the client_id and client_secret and the rest of the info into the config.py file and restart")
exit(1)
# generate the keys
# check, if os command openssl is available
if os.name == 'nt':
print("Sorry, this script might not work on Windows")
return
if os.system("openssl version") != 0: # check, if openssl is installed with return code
print("Sorry, this script requires openssl to be installed")
return
# check, if the keys already exist
if os.path.exists("lib/tesla_api/TeslaKeys/privatekey.pem"):
print("The keys already exist in the directory 'lib/tesla_api/TeslaKeys'")
else:
print("Generating your keys in the directory 'lib/tesla_api/TeslaKeys'...")
os.system("openssl ecparam -name prime256v1 -genkey -noout -out lib/tesla_api/TeslaKeys/privatekey.pem")
os.system("openssl ec -in lib/tesla_api/TeslaKeys/privatekey.pem -pubout -out lib/tesla_api/TeslaKeys/com.tesla.3p.public-key.pem")
print("done")
# push the public key to your domain
print("Please push the public key 'lib/tesla_api/TeslaKeys/com.tesla.3p.public-key.pem' to your domain at the following URL:")
print(f"https://{config.tesla_redirect_domain}/.well-known/appspecific/com.tesla.3p.public-key.pem")
print(f"optional: put the php script 'tesla_api/WWW-example/index.php' to your {config.tesla_redirect_uri} directory to ease the code retrieval for later customer registration")
input("Press Enter to continue, when the public key is pushed...")
# get partner token
print("Getting the partner token...")
partner_token = tesla_api.tesla_get_partner_auth_token(config.tesla_client_id, config.tesla_client_secret, config.tesla_audience)
if partner_token is None:
print("Getting the partner token failed")
return
# print("Partner token:", partner_token)
print(f"Registering the partner account and domain {config.tesla_redirect_domain}")
_r = tesla_api.tesla_register_partner_account(partner_token, config.tesla_audience) # fixme do this for every audience where you expect to register customers
# already printed from above fn. print("Registration result:", _r)
if _r is None:
print("Something went wrong during registration, check logging")
return
if _r.get("client_id") == config.tesla_client_id:
print("Registration was successful!")
else:
re = input("Was the registration successful? (y/n/d) (d=don't know) ")
if re == "d":
# print a good result
print("A good result looks like this (field order may vary):")
print("{'client_id': 'aaaaaaaaaaaa-bbbb-cccc-dddddddddddd', 'name': 'Your app name', 'description': 'your application description', 'domain': 'your.domain', 'ca': None, 'created_at': '2024-02-02T02:02:02.002Z', 'updated_at': '2024-02-02T02:02:02.002Z', 'enterprise_tier': 'free', 'account_id': 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'issuer': None, 'public_key': 'aaaaaaaaaaaaaaaa ...")
# ask again
re = input("Was the registration successful? (y/n) ")
if re != "y":
print("Please check the configuration and try again")
exit(1)
# save the state in a signal file
with open(".dev_registered", "w") as f:
f.write("registered")
pubkey_from_reg = _r.get("public_key")
# wait for key press
input("Press Enter to continue...")
# bonus step
print("Checking the public key with the Tesla server...")
_r = tesla_api.tesla_partner_check_public_key(partner_token, config.tesla_audience)
if _r.get("public_key") == pubkey_from_reg:
print("The public key is correctly registered with the Tesla server!")
else:
print("The public key is not correctly registered with the Tesla server!")
print("registered:", pubkey_from_reg)
print("server:", _r.get("public_key"))
exit(1)
# wait for key press
input("Press Enter to continue...")
if __name__ == '__main__':
print("\nWelcome to the guided tour of the tesla_api library\n\n")
# register the developer account - it checks if the account is marked as already registered
register_developer_account()
myTesla = tesla_api.TeslaAPI()
# check, if token myTesla.token_file is available
if not os.path.exists(myTesla.token_file):
print("The token file does not yet exist - we register the customer account now...")
tesla_api.tesla_register_customer(myTesla)
# get the vehicle info
vl = myTesla.get_vehicles_list()
print("Vehicle list from server:")
print(json.dumps(vl,indent=4))
vin = vl[0]['vin']
# wake up the car to be able to get the vehicle data
myTesla.cmd_wakeup()
# get the vehicle data
vd = myTesla.get_vehicle_data()
print("The following vehicle data is delivered as a structure from server:")
print(json.dumps(vd,indent=4))
# before sending commands, we have to register the key with the car (once)
print("Install the app key to the car using the Tesla app")
tesla_api.tesla_register_customer_key() # this will install the key to the car using Tesla's server and the Tesla app
# to send commands from this app, we have to build the Go client "tesla-control" first
# check, if the Go client is available
if not os.path.exists("lib/tesla_api/tesla-control/tesla-control"):
print("The Go client 'tesla-control' is not available")
print("Please build it first following the instructions in the go building.txt file in the directory 'lib/tesla_api/tesla-control'")
# stop here
exit()
# send a command to the car
myTesla.tesla_command("honk")
# see the source code of the tesla_api library for more commands