You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Python library for Tesla Fleet API and Tesla Command Protocol, including signed commands and encrypted local Bluetooth (BLE). Also provides interfaces for Teslemetry and Tessie.
1
+
# Tesla Fleet API
3
2
4
-
Based on [Tesla Developer documentation](https://developer.tesla.com/docs/fleet-api)and [Tesla Command Protocol](https://github.com/teslamotors/vehicle-command/blob/main/pkg/protocol/protocol.md)
3
+
Tesla Fleet API is a Python library that provides an interface to interact with Tesla's Fleet API, including signed commands and encrypted local Bluetooth (BLE) communication. It also supports interactions with Teslemetry and Tessie services.
5
4
6
-
**Documentation is currently outdated for V1.0.X**
5
+
## Features
7
6
8
-
## TeslaFleetApi
9
-
This is the base class, however can also be used directly if you have a valid user access_token.
7
+
- Fleet API for vehicles
8
+
- Fleet API for energy sites
9
+
- Fleet API with signed vehicle commands
10
+
- Bluetooth for vehicles
11
+
- Teslemetry integration
12
+
- Tessie integration
10
13
14
+
## Installation
15
+
16
+
You can install the library using pip:
17
+
18
+
```bash
19
+
pip install tesla-fleet-api
11
20
```
21
+
22
+
## Usage
23
+
24
+
### Authentication
25
+
26
+
The `TeslaFleetOAuth` class provides methods that help with authenticating to the Tesla Fleet API. Here's a basic example:
print(f"Please go to {login_url} and authorize access.")
45
+
46
+
# After the user authorizes access, they will be redirected to the redirect_uri with a code
47
+
code =input("Enter the code you received: ")
48
+
49
+
# Exchange the code for a refresh token
50
+
await oauth.get_refresh_token(code)
51
+
print(f"Access token: {oauth.access_token}")
52
+
print(f"Refresh token: {oauth.refresh_token}")
53
+
# Dont forget to store the refresh token so you can use it again later
54
+
55
+
asyncio.run(main())
56
+
```
57
+
58
+
### Fleet API for Vehicles
14
59
60
+
The `TeslaFleetApi` class provides methods to interact with the Fleet API for vehicles. Here's a basic example:
61
+
62
+
```python
63
+
import asyncio
64
+
import aiohttp
15
65
from tesla_fleet_api import TeslaFleetApi
16
66
from tesla_fleet_api.exceptions import TeslaFleetError
17
67
18
-
19
68
asyncdefmain():
20
69
asyncwith aiohttp.ClientSession() as session:
21
70
api = TeslaFleetApi(
@@ -25,64 +74,108 @@ async def main():
25
74
)
26
75
27
76
try:
28
-
data = await api.vehicle.list()
77
+
data =await api.vehicles.list()
29
78
print(data)
30
79
except TeslaFleetError as e:
31
80
print(e)
32
81
33
82
asyncio.run(main())
34
83
```
35
84
36
-
## TeslaFleetOAuth
37
-
This extends TeslaFleetApi to support OAuth, and requires a client_id, and either a refresh_token or initial authentication code.
85
+
For more detailed examples, see [Fleet API for Vehicles](docs/fleet_api_vehicles.md).
38
86
39
-
```
40
-
from tesla_fleet_api import TeslaFleetOAuth
87
+
### Fleet API for Energy Sites
88
+
89
+
The `EnergySites` class provides methods to interact with the Fleet API for energy sites. Here's a basic example:
90
+
91
+
```python
92
+
import asyncio
93
+
import aiohttp
94
+
from tesla_fleet_api import TeslaFleetApi
41
95
from tesla_fleet_api.exceptions import TeslaFleetError
42
-
import json
43
96
44
97
asyncdefmain():
45
-
with open("auth.json", "r") as f:
46
-
auth = json.load(f)
47
98
asyncwith aiohttp.ClientSession() as session:
48
-
api = TeslaFleetOAuth(
49
-
session,
50
-
client_id=<client_id>,
51
-
access_token=auth["access_token"],
52
-
refresh_token=auth["refresh_token"],
53
-
expires=auth["expires"],
99
+
api = TeslaFleetApi(
100
+
access_token="<access_token>",
101
+
session=session,
54
102
region="na",
55
103
)
104
+
56
105
try:
57
-
data = await api.vehicle.list()
58
-
print(data)
106
+
energy_sites=await api.energySites.list()
107
+
print(energy_sites)
59
108
except TeslaFleetError as e:
60
109
print(e)
61
110
62
-
with open("auth.json", "w") as f:
63
-
json.dump(
64
-
{
65
-
"access_token": api.access_token,
66
-
"refresh_token": api.refresh_token,
67
-
"expires": api.expires,
68
-
},
69
-
f,
111
+
asyncio.run(main())
112
+
```
113
+
114
+
For more detailed examples, see [Fleet API for Energy Sites](docs/fleet_api_energy_sites.md).
115
+
116
+
### Fleet API with Signed Vehicle Commands
117
+
118
+
The `VehicleSigned` class provides methods to interact with the Fleet API using signed vehicle commands. Here's a basic example:
119
+
120
+
```python
121
+
import asyncio
122
+
import aiohttp
123
+
from tesla_fleet_api import TeslaFleetApi
124
+
from tesla_fleet_api.tesla.vehicle.signed import VehicleSigned
125
+
from tesla_fleet_api.exceptions import TeslaFleetError
126
+
127
+
asyncdefmain():
128
+
asyncwith aiohttp.ClientSession() as session:
129
+
api = TeslaFleetApi(
130
+
access_token="<access_token>",
131
+
session=session,
132
+
region="na",
70
133
)
71
134
135
+
try:
136
+
vehicle = VehicleSigned(api, "<vin>")
137
+
await vehicle.handshake()
138
+
data =await vehicle.wake_up()
139
+
print(data)
140
+
except TeslaFleetError as e:
141
+
print(e)
142
+
72
143
asyncio.run(main())
73
144
```
74
145
75
-
## Teslemetry
76
-
This extends TeslaFleetApi to send requests through Teslemetry, which manages all aspects of Tesla OAuth. This class only requires an access_token from the Teslemetry console.
146
+
For more detailed examples, see [Fleet API with Signed Vehicle Commands](docs/fleet_api_signed_commands.md).
77
147
148
+
### Bluetooth for Vehicles
149
+
150
+
The `TeslaBluetooth` class provides methods to interact with Tesla vehicles using Bluetooth. Here's a basic example:
151
+
152
+
```python
153
+
import asyncio
154
+
from bleak import BleakScanner
155
+
from tesla_fleet_api import TeslaBluetooth
156
+
157
+
asyncdefmain():
158
+
scanner = BleakScanner()
159
+
devices =await scanner.discover()
160
+
for device in devices:
161
+
if TeslaBluetooth().valid_name(device.name):
162
+
print(f"Found Tesla vehicle: {device.name}")
163
+
164
+
asyncio.run(main())
78
165
```
166
+
167
+
For more detailed examples, see [Bluetooth for Vehicles](docs/bluetooth_vehicles.md).
168
+
169
+
### Teslemetry
170
+
171
+
The `Teslemetry` class provides methods to interact with the Teslemetry service. Here's a basic example:
172
+
173
+
```python
79
174
import asyncio
80
175
import aiohttp
81
-
82
176
from tesla_fleet_api import Teslemetry
83
177
from tesla_fleet_api.exceptions import TeslaFleetError
84
178
85
-
86
179
asyncdefmain():
87
180
asyncwith aiohttp.ClientSession() as session:
88
181
api = Teslemetry(
@@ -91,25 +184,26 @@ async def main():
91
184
)
92
185
93
186
try:
94
-
data = await api.vehicle.list()
187
+
data =await api.vehicles.list()
95
188
print(data)
96
189
except TeslaFleetError as e:
97
190
print(e)
98
191
99
192
asyncio.run(main())
100
193
```
101
194
102
-
## Tessie
103
-
This extends TeslaFleetApi to send requests through Tessie, which manages all aspects of Tesla OAuth. This class only requires an access_token from [Tessie](https://dash.tessie.com/settings/api).
195
+
For more detailed examples, see [Teslemetry](docs/teslemetry.md).
104
196
105
-
```
197
+
### Tessie
198
+
199
+
The `Tessie` class provides methods to interact with the Tessie service. Here's a basic example:
200
+
201
+
```python
106
202
import asyncio
107
203
import aiohttp
108
-
109
204
from tesla_fleet_api import Tessie
110
205
from tesla_fleet_api.exceptions import TeslaFleetError
111
206
112
-
113
207
asyncdefmain():
114
208
asyncwith aiohttp.ClientSession() as session:
115
209
api = Tessie(
@@ -118,10 +212,12 @@ async def main():
118
212
)
119
213
120
214
try:
121
-
data = await api.vehicle.list()
215
+
data =await api.vehicles.list()
122
216
print(data)
123
217
except TeslaFleetError as e:
124
218
print(e)
125
219
126
220
asyncio.run(main())
127
221
```
222
+
223
+
For more detailed examples, see [Tessie](docs/tessie.md).
This document provides detailed examples for using Bluetooth for vehicles.
4
+
5
+
## Initialize TeslaBluetooth
6
+
7
+
The `TeslaBluetooth` class provides methods to interact with Tesla vehicles using Bluetooth. Here's a basic example to initialize the `TeslaBluetooth` class and discover nearby Tesla vehicles:
8
+
9
+
```python
10
+
import asyncio
11
+
from bleak import BleakScanner
12
+
from tesla_fleet_api import TeslaBluetooth
13
+
14
+
asyncdefmain():
15
+
scanner = BleakScanner()
16
+
devices =await scanner.discover()
17
+
for device in devices:
18
+
if TeslaBluetooth().valid_name(device.name):
19
+
print(f"Found Tesla vehicle: {device.name}")
20
+
21
+
asyncio.run(main())
22
+
```
23
+
24
+
## Create VehicleBluetooth Instance
25
+
26
+
You can create a `VehicleBluetooth` instance using the `TeslaBluetooth` class. Here's a basic example to create a `VehicleBluetooth` instance and set the private key from a file:
print(f"Woke up VehicleBluetooth instance for VIN: {vehicle.vin}")
76
+
77
+
asyncio.run(main())
78
+
```
79
+
80
+
## Get Vehicle Data
81
+
82
+
You can get data from a `VehicleBluetooth` instance using the `vehicle_data` method. Here's a basic example to get data from a `VehicleBluetooth` instance:
83
+
84
+
```python
85
+
import asyncio
86
+
from tesla_fleet_api import TeslaBluetooth, BluetoothVehicleData
0 commit comments