tesla-fleet-api 0.0.3__py3-none-any.whl → 0.0.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,1065 +0,0 @@
1
- import aiohttp
2
- from .exceptions import raise_for_status, InvalidRegion, LibraryError
3
- from typing import Any
4
- from enum import StrEnum, IntEnum
5
- from .const import SERVERS
6
-
7
- GET = "GET"
8
- POST = "POST"
9
- DELETE = "DELETE"
10
-
11
-
12
- # Based on https://developer.tesla.com/docs/fleet-api
13
- class TeslaFleetApi:
14
- """Class describing the Tesla Fleet API."""
15
-
16
- server: str
17
- session: aiohttp.ClientSession
18
- headers: dict[str, str]
19
- raise_for_status: bool
20
-
21
- def __init__(
22
- self,
23
- session: aiohttp.ClientSession,
24
- access_token: str,
25
- use_command_protocol: bool = False,
26
- region: str | None = None,
27
- server: str | None = None,
28
- raise_for_status: bool = True,
29
- ):
30
- """Initialize the Tesla Fleet API."""
31
-
32
- self.session = session
33
- self.access_token = access_token
34
- self.use_command_protocol = use_command_protocol
35
-
36
- if region and not server and region not in SERVERS:
37
- raise ValueError(f"Region must be one of {", ".join(SERVERS.keys())}")
38
- self.server = server or SERVERS.get(region)
39
- self.raise_for_status = raise_for_status
40
-
41
- self.user = self.User(self)
42
- self.charging = self.Charging(self)
43
- self.partner = self.Partner(self)
44
- self.vehicle = self.Vehicle(self)
45
-
46
- async def find_server(self) -> None:
47
- """Find the server URL for the Tesla Fleet API."""
48
- for server in SERVERS.values():
49
- self.server = server
50
- try:
51
- await self.user.region()
52
- return
53
- except InvalidRegion:
54
- continue
55
- raise LibraryError("Could not find a valid Tesla API server.")
56
-
57
- async def _request(
58
- self,
59
- method: str,
60
- path: str,
61
- data: dict[str:Any] | None = None,
62
- json: dict[str:Any] | None = None,
63
- params: dict[str:Any] | None = None,
64
- ):
65
- """Send a request to the Tesla Fleet API."""
66
-
67
- if not self.server:
68
- raise ValueError("Server was not set at init. Call find_server() first.")
69
-
70
- if data:
71
- data = {k: v for k, v in data.items() if v is not None}
72
- if json:
73
- json = {k: v for k, v in json.items() if v is not None}
74
- if params:
75
- params = {k: v for k, v in params.items() if v is not None}
76
-
77
- async with self.session.request(
78
- method,
79
- f"{self.server}/{path}",
80
- headers={
81
- "Authorization": f"Bearer {self.access_token}",
82
- "Content-Type": "application/json",
83
- },
84
- data=data,
85
- json=json,
86
- params=params,
87
- ) as resp:
88
- if self.raise_for_status:
89
- await raise_for_status(resp)
90
- return await resp.json()
91
-
92
- async def status(self):
93
- """This endpoint returns the string "ok" if the API is operating normally. No HTTP headers are required."""
94
- if not self.server:
95
- raise ValueError("Server was not set at init. Call find_server() first.")
96
- async with self.session.get(f"{self.server}/status") as resp:
97
- return await resp.text()
98
-
99
- class Charging:
100
- """Class describing the Tesla Fleet API charging endpoints."""
101
-
102
- def __init__(self, parent):
103
- self._request = parent._request
104
-
105
- async def history(
106
- self,
107
- vin: str | None = None,
108
- startTime: str | None = None,
109
- endTime: str | None = None,
110
- pageNo: int | None = None,
111
- pageSize: int | None = None,
112
- sortBy: str | None = None,
113
- sortOrder: str | None = None,
114
- ) -> dict[str, Any]:
115
- """Returns the paginated charging history."""
116
- return await self._request(
117
- GET,
118
- "api/1/dx/charging/history",
119
- {
120
- vin: vin,
121
- startTime: startTime,
122
- endTime: endTime,
123
- pageNo: pageNo,
124
- pageSize: pageSize,
125
- sortBy: sortBy,
126
- sortOrder: sortOrder,
127
- },
128
- )
129
-
130
- async def sessions(
131
- self,
132
- vin: str | None = None,
133
- date_from: str | None = None,
134
- date_to: str | None = None,
135
- limit: int | None = None,
136
- offset: int | None = None,
137
- ) -> dict[str, Any]:
138
- """Returns the charging session information including pricing and energy data. This endpoint is only available for business accounts that own a fleet of vehicles."""
139
- return await self._request(
140
- GET,
141
- "api/1/dx/charging/sessions",
142
- {
143
- vin: vin,
144
- date_from: date_from,
145
- date_to: date_to,
146
- limit: limit,
147
- offset: offset,
148
- },
149
- )
150
-
151
- class Partner:
152
- """Class describing the Tesla Fleet API partner endpoints"""
153
-
154
- def __init__(self, parent):
155
- self._request = parent._request
156
-
157
- async def public_key(self, domain: str | None = None) -> dict[str, Any]:
158
- """Returns the public key associated with a domain. It can be used to ensure the registration was successful."""
159
- return await self._request(
160
- GET, "api/1/partner_accounts/public_key", data={domain: domain}
161
- )
162
-
163
- async def register(self, domain: str) -> dict[str, Any]:
164
- """Registers an existing account before it can be used for general API access. Each application from developer.tesla.com must complete this step."""
165
- return await self._request(
166
- POST, "api/1/partner_accounts", data={domain: domain}
167
- )
168
-
169
- class User:
170
- """Class describing the Tesla Fleet API user endpoints"""
171
-
172
- def __init__(self, parent):
173
- self._request = parent._request
174
-
175
- async def backup_key(self) -> dict[str, Any]:
176
- """Returns the public key associated with the user."""
177
- return await self._request(GET, "api/1/users/backup_key")
178
-
179
- async def feature_config(self) -> dict[str, Any]:
180
- """Returns any custom feature flag applied to a user."""
181
- return await self._request(GET, "api/1/users/feature_config")
182
-
183
- async def me(self) -> dict[str, Any]:
184
- """Returns a summary of a user's account."""
185
- return await self._request(GET, "api/1/users/me")
186
-
187
- async def orders(self) -> dict[str, Any]:
188
- """Returns the active orders for a user."""
189
- return await self._request(GET, "api/1/users/orders")
190
-
191
- async def region(self) -> dict[str, Any]:
192
- """Returns a user's region and appropriate fleet-api base URL. Accepts no parameters, response is based on the authentication token subject."""
193
- return await self._request(GET, "api/1/users/region")
194
-
195
- class Vehicle:
196
- """Class describing the Tesla Fleet API vehicle endpoints and commands."""
197
-
198
- def __init__(self, parent):
199
- self._request = parent._request
200
- self.use_command_protocol = parent.use_command_protocol
201
-
202
- class Trunk(StrEnum):
203
- """Trunk options"""
204
-
205
- FRONT: "front"
206
- REAR: "rear"
207
-
208
- async def actuate_trunk(
209
- self, vehicle_tag: str | int, which_trunk: Trunk | str
210
- ) -> dict[str, Any]:
211
- """Controls the front or rear trunk."""
212
- if self.use_command_protocol:
213
- raise NotImplementedError("Command Protocol not implemented")
214
- return await self._request(
215
- POST,
216
- f"api/1/vehicles/{vehicle_tag}/command/actuate_trunk",
217
- json={which_trunk: which_trunk},
218
- )
219
-
220
- async def adjust_volume(
221
- self, vehicle_tag: str | int, volume: float
222
- ) -> dict[str, Any]:
223
- """Adjusts vehicle media playback volume."""
224
- if self.use_command_protocol:
225
- raise NotImplementedError("Command Protocol not implemented")
226
- if volume < 0.0 or volume > 11.0:
227
- raise ValueError("Volume must a number from 0.0 to 11.0")
228
- return await self._request(
229
- POST,
230
- f"api/1/vehicles/{vehicle_tag}/command/adjust_volume",
231
- json={volume: volume},
232
- )
233
-
234
- async def auto_conditioning_start(
235
- self, vehicle_tag: str | int
236
- ) -> dict[str, Any]:
237
- """Starts climate preconditioning."""
238
- if self.use_command_protocol:
239
- raise NotImplementedError("Command Protocol not implemented")
240
- return await self._request(
241
- POST, f"api/1/vehicles/{vehicle_tag}/command/auto_conditioning_start"
242
- )
243
-
244
- async def auto_conditioning_stop(
245
- self, vehicle_tag: str | int
246
- ) -> dict[str, Any]:
247
- """Stops climate preconditioning."""
248
- if self.use_command_protocol:
249
- raise NotImplementedError("Command Protocol not implemented")
250
- return await self._request(
251
- POST, f"api/1/vehicles/{vehicle_tag}/command/auto_conditioning_stop"
252
- )
253
-
254
- async def cancel_software_update(
255
- self, vehicle_tag: str | int
256
- ) -> dict[str, Any]:
257
- """Cancels the countdown to install the vehicle software update."""
258
- if self.use_command_protocol:
259
- raise NotImplementedError("Command Protocol not implemented")
260
- return await self._request(
261
- POST, f"api/1/vehicles/{vehicle_tag}/command/cancel_software_update"
262
- )
263
-
264
- async def charge_max_range(self, vehicle_tag: str | int) -> dict[str, Any]:
265
- """Charges in max range mode -- we recommend limiting the use of this mode to long trips."""
266
- if self.use_command_protocol:
267
- raise NotImplementedError("Command Protocol not implemented")
268
- return await self._request(
269
- POST, f"api/1/vehicles/{vehicle_tag}/command/charge_max_range"
270
- )
271
-
272
- async def charge_port_door_close(
273
- self, vehicle_tag: str | int
274
- ) -> dict[str, Any]:
275
- """Closes the charge port door."""
276
- if self.use_command_protocol:
277
- raise NotImplementedError("Command Protocol not implemented")
278
- return await self._request(
279
- POST, f"api/1/vehicles/{vehicle_tag}/command/charge_port_door_close"
280
- )
281
-
282
- async def charge_port_door_open(self, vehicle_tag: str | int) -> dict[str, Any]:
283
- """Opens the charge port door."""
284
- if self.use_command_protocol:
285
- raise NotImplementedError("Command Protocol not implemented")
286
- return await self._request(
287
- POST, f"api/1/vehicles/{vehicle_tag}/command/charge_port_door_open"
288
- )
289
-
290
- async def charge_standard(self, vehicle_tag: str | int) -> dict[str, Any]:
291
- """Charges in Standard mode."""
292
- if self.use_command_protocol:
293
- raise NotImplementedError("Command Protocol not implemented")
294
- return await self._request(
295
- POST, f"api/1/vehicles/{vehicle_tag}/command/charge_standard"
296
- )
297
-
298
- async def charge_start(self, vehicle_tag: str | int) -> dict[str, Any]:
299
- """Starts charging the vehicle."""
300
- if self.use_command_protocol:
301
- raise NotImplementedError("Command Protocol not implemented")
302
- return await self._request(
303
- POST, f"api/1/vehicles/{vehicle_tag}/command/charge_start"
304
- )
305
-
306
- async def charge_stop(self, vehicle_tag: str | int) -> dict[str, Any]:
307
- """Stops charging the vehicle."""
308
- if self.use_command_protocol:
309
- raise NotImplementedError("Command Protocol not implemented")
310
- return await self._request(
311
- POST, f"api/1/vehicles/{vehicle_tag}/command/charge_stop"
312
- )
313
-
314
- async def clear_pin_to_drive_admin(self, vehicle_tag: str | int):
315
- """Deactivates PIN to Drive and resets the associated PIN for vehicles running firmware versions 2023.44+. This command is only accessible to fleet managers or owners."""
316
- return await self._request(
317
- POST, f"api/1/vehicles/{vehicle_tag}/command/clear_pin_to_drive_admin"
318
- )
319
-
320
- async def door_lock(self, vehicle_tag: str | int) -> dict[str, Any]:
321
- """Locks the vehicle."""
322
- if self.use_command_protocol:
323
- raise NotImplementedError("Command Protocol not implemented")
324
- return await self._request(
325
- POST, f"api/1/vehicles/{vehicle_tag}/command/door_lock"
326
- )
327
-
328
- async def door_unlock(self, vehicle_tag: str | int) -> dict[str, Any]:
329
- """Unlocks the vehicle."""
330
- if self.use_command_protocol:
331
- raise NotImplementedError("Command Protocol not implemented")
332
- return await self._request(
333
- POST, f"api/1/vehicles/{vehicle_tag}/command/door_unlock"
334
- )
335
-
336
- async def erase_user_data(self, vehicle_tag: str | int) -> dict[str, Any]:
337
- """Erases user's data from the user interface. Requires the vehicle to be in park."""
338
- return await self._request(
339
- POST, f"api/1/vehicles/{vehicle_tag}/command/erase_user_data"
340
- )
341
-
342
- async def flash_lights(self, vehicle_tag: str | int) -> dict[str, Any]:
343
- """Briefly flashes the vehicle headlights. Requires the vehicle to be in park."""
344
- if self.use_command_protocol:
345
- raise NotImplementedError("Command Protocol not implemented")
346
- return await self._request(
347
- POST, f"api/1/vehicles/{vehicle_tag}/command/flash_lights"
348
- )
349
-
350
- async def guest_mode(
351
- self, vehicle_tag: str | int, enable: bool
352
- ) -> dict[str, Any]:
353
- """Restricts certain vehicle UI functionality from guest users"""
354
- if self.use_command_protocol:
355
- raise NotImplementedError("Command Protocol not implemented")
356
- return await self._request(
357
- POST,
358
- f"api/1/vehicles/{vehicle_tag}/command/guest_mode",
359
- json={enable: enable},
360
- )
361
-
362
- async def honk_horn(self, vehicle_tag: str | int) -> dict[str, Any]:
363
- """Honks the vehicle horn. Requires the vehicle to be in park."""
364
- if self.use_command_protocol:
365
- raise NotImplementedError("Command Protocol not implemented")
366
- return await self._request(
367
- POST, f"api/1/vehicles/{vehicle_tag}/command/honk_horn"
368
- )
369
-
370
- async def media_next_fav(self, vehicle_tag: str | int) -> dict[str, Any]:
371
- """Advances media player to next favorite track."""
372
- return await self._request(
373
- POST, f"api/1/vehicles/{vehicle_tag}/command/media_next_fav"
374
- )
375
-
376
- async def media_next_track(self, vehicle_tag: str | int) -> dict[str, Any]:
377
- """Advances media player to next track."""
378
- return await self._request(
379
- POST, f"api/1/vehicles/{vehicle_tag}/command/media_next_track"
380
- )
381
-
382
- async def media_prev_fav(self, vehicle_tag: str | int) -> dict[str, Any]:
383
- """Advances media player to previous favorite track."""
384
- return await self._request(
385
- POST, f"api/1/vehicles/{vehicle_tag}/command/media_prev_fav"
386
- )
387
-
388
- async def media_prev_track(self, vehicle_tag: str | int) -> dict[str, Any]:
389
- """Advances media player to previous track."""
390
- return await self._request(
391
- POST, f"api/1/vehicles/{vehicle_tag}/command/media_prev_track"
392
- )
393
-
394
- async def media_toggle_playback(self, vehicle_tag: str | int) -> dict[str, Any]:
395
- """Toggles current play/pause state."""
396
- return await self._request(
397
- POST, f"api/1/vehicles/{vehicle_tag}/command/media_toggle_playback"
398
- )
399
-
400
- async def media_volume_down(self, vehicle_tag: str | int) -> dict[str, Any]:
401
- """Turns the volume down by one."""
402
- return await self._request(
403
- POST, f"api/1/vehicles/{vehicle_tag}/command/media_volume_down"
404
- )
405
-
406
- async def navigation_gps_request(
407
- self, vehicle_tag: str | int, lat: float, lon: float, order: int
408
- ) -> dict[str, Any]:
409
- """Start navigation to given coordinates. Order can be used to specify order of multiple stops."""
410
- if self.use_command_protocol:
411
- raise NotImplementedError("Command Protocol not implemented")
412
- return await self._request(
413
- POST,
414
- f"api/1/vehicles/{vehicle_tag}/command/navigation_gps_request",
415
- json={lat: lat, lon: lon, order: order},
416
- )
417
-
418
- async def navigation_request(
419
- self, vehicle_tag: str | int, type: str, locale: str, timestamp_ms: str
420
- ) -> dict[str, Any]:
421
- """Sends a location to the in-vehicle navigation system."""
422
- return await self._request(
423
- POST,
424
- f"api/1/vehicles/{vehicle_tag}/command/navigation_request",
425
- json={type: type, locale: locale, timestamp_ms: timestamp_ms},
426
- )
427
-
428
- async def navigation_sc_request(
429
- self, vehicle_tag: str | int, id: int, order: int
430
- ) -> dict[str, Any]:
431
- """Sends a location to the in-vehicle navigation system."""
432
- return await self._request(
433
- POST,
434
- f"api/1/vehicles/{vehicle_tag}/command/navigation_sc_request",
435
- json={type: type, id: id, order: order},
436
- )
437
-
438
- async def remote_auto_seat_climate_request(
439
- self, vehicle_tag: str | int, auto_seat_position: int, auto_climate_on: bool
440
- ) -> dict[str, Any]:
441
- """Sets automatic seat heating and cooling."""
442
- if self.use_command_protocol:
443
- raise NotImplementedError("Command Protocol not implemented")
444
- return await self._request(
445
- POST,
446
- f"api/1/vehicles/{vehicle_tag}/command/remote_auto_seat_climate_request",
447
- json={
448
- auto_seat_position: auto_seat_position,
449
- auto_climate_on: auto_climate_on,
450
- },
451
- )
452
-
453
- async def remote_auto_steering_wheel_heat_climate_request(
454
- self, vehicle_tag: str | int, on: bool
455
- ) -> dict[str, Any]:
456
- """Sets automatic steering wheel heating on/off."""
457
- return await self._request(
458
- POST,
459
- f"api/1/vehicles/{vehicle_tag}/command/remote_auto_steering_wheel_heat_climate_request",
460
- json={on: on},
461
- )
462
-
463
- async def remote_boombox(
464
- self, vehicle_tag: str | int, sound: int
465
- ) -> dict[str, Any]:
466
- """Plays a sound through the vehicle external speaker."""
467
- return await self._request(
468
- POST,
469
- f"api/1/vehicles/{vehicle_tag}/command/remote_boombox",
470
- json={sound: sound},
471
- )
472
-
473
- async def remote_seat_cooler_request(
474
- self, vehicle_tag: str | int, seat_position: int, seat_cooler_level: int
475
- ) -> dict[str, Any]:
476
- """Sets seat cooling."""
477
- if self.use_command_protocol:
478
- raise NotImplementedError("Command Protocol not implemented")
479
- return await self._request(
480
- POST,
481
- f"api/1/vehicles/{vehicle_tag}/command/remote_seat_cooler_request",
482
- json={
483
- seat_position: seat_position,
484
- seat_cooler_level: seat_cooler_level,
485
- },
486
- )
487
-
488
- async def remote_seat_heater_request(
489
- self, vehicle_tag: str | int
490
- ) -> dict[str, Any]:
491
- """Sets seat heating."""
492
- if self.use_command_protocol:
493
- raise NotImplementedError("Command Protocol not implemented")
494
- return await self._request(
495
- POST,
496
- f"api/1/vehicles/{vehicle_tag}/command/remote_seat_heater_request",
497
- )
498
-
499
- async def remote_start_drive(self, vehicle_tag: str | int) -> dict[str, Any]:
500
- """Starts the vehicle remotely. Requires keyless driving to be enabled."""
501
- if self.use_command_protocol:
502
- raise NotImplementedError("Command Protocol not implemented")
503
- return await self._request(
504
- POST, f"api/1/vehicles/{vehicle_tag}/command/remote_start_drive"
505
- )
506
-
507
- async def remote_steering_wheel_heat_level_request(
508
- self, vehicle_tag: str | int, level: int
509
- ) -> dict[str, Any]:
510
- """Sets steering wheel heat level."""
511
- if self.use_command_protocol:
512
- raise NotImplementedError("Command Protocol not implemented")
513
- return await self._request(
514
- POST,
515
- f"api/1/vehicles/{vehicle_tag}/command/remote_steering_wheel_heat_level_request",
516
- json={level: level},
517
- )
518
-
519
- async def remote_steering_wheel_heater_request(
520
- self, vehicle_tag: str | int, on: bool
521
- ) -> dict[str, Any]:
522
- """Sets steering wheel heating on/off. For vehicles that do not support auto steering wheel heat."""
523
- if self.use_command_protocol:
524
- raise NotImplementedError("Command Protocol not implemented")
525
- return await self._request(
526
- POST,
527
- f"api/1/vehicles/{vehicle_tag}/command/remote_steering_wheel_heater_request",
528
- json={on: on},
529
- )
530
-
531
- async def reset_pin_to_drive_pin(
532
- self, vehicle_tag: str | int
533
- ) -> dict[str, Any]:
534
- """Removes PIN to Drive. Requires the car to be in Pin to Drive mode and not in Valet mode. Note that this only works if PIN to Drive is not active. This command also requires the Tesla Vehicle Command Protocol - for more information, please see refer to the documentation here."""
535
- if self.use_command_protocol:
536
- raise NotImplementedError("Command Protocol not implemented")
537
- return await self._request(
538
- POST, f"api/1/vehicles/{vehicle_tag}/command/reset_pin_to_drive_pin"
539
- )
540
-
541
- async def reset_valet_pin(self, vehicle_tag: str | int) -> dict[str, Any]:
542
- """Removes PIN for Valet Mode."""
543
- if self.use_command_protocol:
544
- raise NotImplementedError("Command Protocol not implemented")
545
- return await self._request(
546
- POST, f"api/1/vehicles/{vehicle_tag}/command/reset_valet_pin"
547
- )
548
-
549
- async def schedule_software_update(
550
- self, vehicle_tag: str | int, offset_sec: int
551
- ) -> dict[str, Any]:
552
- """Schedules a vehicle software update (over the air "OTA") to be installed in the future."""
553
- if self.use_command_protocol:
554
- raise NotImplementedError("Command Protocol not implemented")
555
- return await self._request(
556
- POST,
557
- f"api/1/vehicles/{vehicle_tag}/command/schedule_software_update",
558
- json={offset_sec: offset_sec},
559
- )
560
-
561
- async def set_bioweapon_mode(
562
- self, vehicle_tag: str | int, on: bool, manual_override: bool
563
- ) -> dict[str, Any]:
564
- """Turns Bioweapon Defense Mode on and off."""
565
- if self.use_command_protocol:
566
- raise NotImplementedError("Command Protocol not implemented")
567
- return await self._request(
568
- POST,
569
- f"api/1/vehicles/{vehicle_tag}/command/set_bioweapon_mode",
570
- json={on: on, manual_override: manual_override},
571
- )
572
-
573
- async def set_cabin_overheat_protection(
574
- self, vehicle_tag: str | int, on: bool, fan_only: bool
575
- ) -> dict[str, Any]:
576
- """Sets the vehicle overheat protection."""
577
- if self.use_command_protocol:
578
- raise NotImplementedError("Command Protocol not implemented")
579
- return await self._request(
580
- POST,
581
- f"api/1/vehicles/{vehicle_tag}/command/set_cabin_overheat_protection",
582
- json={on: on, fan_only: fan_only},
583
- )
584
-
585
- async def set_charge_limit(
586
- self, vehicle_tag: str | int, percent: int
587
- ) -> dict[str, Any]:
588
- """Sets the vehicle charge limit."""
589
- if self.use_command_protocol:
590
- raise NotImplementedError("Command Protocol not implemented")
591
- return await self._request(
592
- POST,
593
- f"api/1/vehicles/{vehicle_tag}/command/set_charge_limit",
594
- json={percent: percent},
595
- )
596
-
597
- async def set_charging_amps(
598
- self, vehicle_tag: str | int, charging_amps: int
599
- ) -> dict[str, Any]:
600
- """Sets the vehicle charging amps."""
601
- if self.use_command_protocol:
602
- raise NotImplementedError("Command Protocol not implemented")
603
- return await self._request(
604
- POST,
605
- f"api/1/vehicles/{vehicle_tag}/command/set_charging_amps",
606
- json={charging_amps: charging_amps},
607
- )
608
-
609
- class ClimateKeeperMode(IntEnum):
610
- """Climate Keeper Mode options"""
611
-
612
- OFF = 0
613
- KEEP_MODE = 1
614
- DOG_MODE = 2
615
- CAMP_MODE = 3
616
-
617
- async def set_climate_keeper_mode(
618
- self, vehicle_tag: str | int, climate_keeper_mode: ClimateKeeperMode | int
619
- ) -> dict[str, Any]:
620
- """Enables climate keeper mode."""
621
- if self.use_command_protocol:
622
- raise NotImplementedError("Command Protocol not implemented")
623
- return await self._request(
624
- POST,
625
- f"api/1/vehicles/{vehicle_tag}/command/set_climate_keeper_mode",
626
- json={climate_keeper_mode: climate_keeper_mode},
627
- )
628
-
629
- class CopTemp(IntEnum):
630
- """COP Temp options"""
631
-
632
- LOW = 0 # 30C 90F
633
- MEDIUM = 1 # 35C 95F
634
- HIGH = 2 # 40C 100F
635
-
636
- async def set_cop_temp(
637
- self, vehicle_tag: str | int, cop_temp: CopTemp | int
638
- ) -> dict[str, Any]:
639
- """Adjusts the Cabin Overheat Protection temperature (COP)."""
640
- if self.use_command_protocol:
641
- raise NotImplementedError("Command Protocol not implemented")
642
- return await self._request(
643
- POST,
644
- f"api/1/vehicles/{vehicle_tag}/command/set_cop_temp",
645
- json={cop_temp: cop_temp},
646
- )
647
-
648
- async def set_pin_to_drive(
649
- self, vehicle_tag: str | int, on: bool, password: str | int
650
- ) -> dict[str, Any]:
651
- """Sets a four-digit passcode for PIN to Drive. This PIN must then be entered before the vehicle can be driven."""
652
- if self.use_command_protocol:
653
- raise NotImplementedError("Command Protocol not implemented")
654
- return await self._request(
655
- POST,
656
- f"api/1/vehicles/{vehicle_tag}/command/set_pin_to_drive",
657
- json={on: on, password: str(password)},
658
- )
659
-
660
- async def set_preconditioning_max(
661
- self, vehicle_tag: str | int, on: bool, manual_override: bool
662
- ) -> dict[str, Any]:
663
- """Sets an override for preconditioning — it should default to empty if no override is used."""
664
- if self.use_command_protocol:
665
- raise NotImplementedError("Command Protocol not implemented")
666
- return await self._request(
667
- POST,
668
- f"api/1/vehicles/{vehicle_tag}/command/set_preconditioning_max",
669
- json={on: on, manual_override: manual_override},
670
- )
671
-
672
- async def set_scheduled_charging(
673
- self, vehicle_tag: str | int, enable: bool, time: int
674
- ) -> dict[str, Any]:
675
- """Sets a time at which charging should be completed. The time parameter is minutes after midnight (e.g: time=120 schedules charging for 2:00am vehicle local time)."""
676
- if self.use_command_protocol:
677
- raise NotImplementedError("Command Protocol not implemented")
678
- return await self._request(
679
- POST,
680
- f"api/1/vehicles/{vehicle_tag}/command/set_scheduled_charging",
681
- json={enable: enable, time: time},
682
- )
683
-
684
- async def set_scheduled_departure(
685
- self, vehicle_tag: str | int, enable: bool, time: int
686
- ) -> dict[str, Any]:
687
- """Sets a time at which departure should be completed. The time parameter is minutes after midnight (e.g: time=120 schedules departure for 2:00am vehicle local time)."""
688
- if self.use_command_protocol:
689
- raise NotImplementedError("Command Protocol not implemented")
690
- return await self._request(
691
- POST,
692
- f"api/1/vehicles/{vehicle_tag}/command/set_scheduled_departure",
693
- json={enable: enable, time: time},
694
- )
695
-
696
- async def set_sentry_mode(
697
- self, vehicle_tag: str | int, on: bool
698
- ) -> dict[str, Any]:
699
- """Enables and disables Sentry Mode. Sentry Mode allows customers to watch the vehicle cameras live from the mobile app, as well as record sentry events."""
700
- if self.use_command_protocol:
701
- raise NotImplementedError("Command Protocol not implemented")
702
- return await self._request(
703
- POST,
704
- f"api/1/vehicles/{vehicle_tag}/command/set_sentry_mode",
705
- json={on: on},
706
- )
707
-
708
- async def set_temps(
709
- self, vehicle_tag: str | int, driver_temp: int, passenger_temp: int
710
- ) -> dict[str, Any]:
711
- """Sets the driver and/or passenger-side cabin temperature (and other zones if sync is enabled)."""
712
- if self.use_command_protocol:
713
- raise NotImplementedError("Command Protocol not implemented")
714
- return await self._request(
715
- POST,
716
- f"api/1/vehicles/{vehicle_tag}/command/set_temps",
717
- json={driver_temp: driver_temp, passenger_temp: passenger_temp},
718
- )
719
-
720
- async def set_valet_mode(
721
- self, vehicle_tag: str | int, on: bool, password: str | int
722
- ) -> dict[str, Any]:
723
- """Turns on Valet Mode and sets a four-digit passcode that must then be entered to disable Valet Mode."""
724
- if self.use_command_protocol:
725
- raise NotImplementedError("Command Protocol not implemented")
726
- return await self._request(
727
- POST,
728
- f"api/1/vehicles/{vehicle_tag}/command/set_valet_mode",
729
- json={on: on, password: str(password)},
730
- )
731
-
732
- async def set_vehicle_name(
733
- self, vehicle_tag: str | int, vehicle_name: str
734
- ) -> dict[str, Any]:
735
- """Changes the name of a vehicle. This command also requires the Tesla Vehicle Command Protocol - for more information, please see refer to the documentation here."""
736
- if self.use_command_protocol:
737
- raise NotImplementedError("Command Protocol not implemented")
738
- return await self._request(
739
- POST,
740
- f"api/1/vehicles/{vehicle_tag}/command/set_vehicle_name",
741
- json={vehicle_name: vehicle_name},
742
- )
743
-
744
- async def speed_limit_activate(
745
- self, vehicle_tag: str | int, pin: str | int
746
- ) -> dict[str, Any]:
747
- """Activates Speed Limit Mode with a four-digit PIN."""
748
- if self.use_command_protocol:
749
- raise NotImplementedError("Command Protocol not implemented")
750
- return await self._request(
751
- POST,
752
- f"api/1/vehicles/{vehicle_tag}/command/speed_limit_activate",
753
- json={pin: str(pin)},
754
- )
755
-
756
- async def speed_limit_clear_pin(
757
- self, vehicle_tag: str | int, pin: str | int
758
- ) -> dict[str, Any]:
759
- """Deactivates Speed Limit Mode and resets the associated PIN."""
760
- if self.use_command_protocol:
761
- raise NotImplementedError("Command Protocol not implemented")
762
- return await self._request(
763
- POST,
764
- f"api/1/vehicles/{vehicle_tag}/command/speed_limit_clear_pin",
765
- json={pin: str(pin)},
766
- )
767
-
768
- async def speed_limit_clear_pin_admin(
769
- self, vehicle_tag: str | int
770
- ) -> dict[str, Any]:
771
- """Deactivates Speed Limit Mode and resets the associated PIN for vehicles running firmware versions 2023.38+. This command is only accessible to fleet managers or owners."""
772
- return await self._request(
773
- POST,
774
- f"api/1/vehicles/{vehicle_tag}/command/speed_limit_clear_pin_admin",
775
- )
776
-
777
- async def speed_limit_deactivate(
778
- self, vehicle_tag: str | int, pin: str | int
779
- ) -> dict[str, Any]:
780
- """Deactivates Speed Limit Mode."""
781
- if self.use_command_protocol:
782
- raise NotImplementedError("Command Protocol not implemented")
783
- return await self._request(
784
- POST,
785
- f"api/1/vehicles/{vehicle_tag}/command/speed_limit_deactivate",
786
- json={pin: str(pin)},
787
- )
788
-
789
- async def speed_limit_set_limit(
790
- self, vehicle_tag: str | int, limit_mph: int
791
- ) -> dict[str, Any]:
792
- """Sets the maximum speed allowed when Speed Limit Mode is active."""
793
- if self.use_command_protocol:
794
- raise NotImplementedError("Command Protocol not implemented")
795
- return await self._request(
796
- POST,
797
- f"api/1/vehicles/{vehicle_tag}/command/speed_limit_set_limit",
798
- json={limit_mph: limit_mph},
799
- )
800
-
801
- class SunRoof(StrEnum):
802
- """Sunroof options"""
803
-
804
- STOP = "stop"
805
- CLOSE = "close"
806
- VENT = "vent"
807
-
808
- async def sun_roof_control(
809
- self, vehicle_tag: str | int, state: str | SunRoof
810
- ) -> dict[str, Any]:
811
- """Controls the panoramic sunroof on the Model S."""
812
- return await self._request(
813
- POST,
814
- f"api/1/vehicles/{vehicle_tag}/command/sun_roof_control",
815
- {state: state},
816
- )
817
-
818
- async def take_drivenote(
819
- self, vehicle_tag: str | int, note: str
820
- ) -> dict[str, Any]:
821
- """Records a drive note. The note parameter is truncated to 80 characters in length."""
822
- return await self._request(
823
- POST,
824
- f"api/1/vehicles/{vehicle_tag}/command/take_drivenote",
825
- {note: note},
826
- )
827
-
828
- async def trigger_homelink(
829
- self, vehicle_tag: str | int, lat: float, lon: float, token: str
830
- ) -> dict[str, Any]:
831
- """Turns on HomeLink (used to open and close garage doors)."""
832
- if self.use_command_protocol:
833
- raise NotImplementedError("Command Protocol not implemented")
834
- return await self._request(
835
- POST,
836
- f"api/1/vehicles/{vehicle_tag}/command/trigger_homelink",
837
- {lat: lat, lon: lon, token: token},
838
- )
839
-
840
- async def upcoming_calendar_entries(
841
- self, vehicle_tag: str | int, calendar_data: str
842
- ) -> dict[str, Any]:
843
- """Upcoming calendar entries stored on the vehicle."""
844
- return await self._request(
845
- POST,
846
- f"api/1/vehicles/{vehicle_tag}/command/upcoming_calendar_entries",
847
- {calendar_data: calendar_data},
848
- )
849
-
850
- class WindowControl(StrEnum):
851
- """Window Control options"""
852
-
853
- VENT = "vent"
854
- CLOSE = "close"
855
-
856
- async def window_control(
857
- self,
858
- vehicle_tag: str | int,
859
- lat: float,
860
- lon: float,
861
- command: str | WindowControl,
862
- ) -> dict[str, Any]:
863
- """Control the windows of a parked vehicle. Supported commands: vent and close. When closing, specify lat and lon of user to ensure they are within range of vehicle (unless this is an M3 platform vehicle)."""
864
- return await self._request(
865
- POST,
866
- f"api/1/vehicles/{vehicle_tag}/command/window_control",
867
- {lat: lat, lon: lon, command: command},
868
- )
869
-
870
- async def drivers(self, vehicle_tag: str | int) -> dict[str, Any]:
871
- """Returns all allowed drivers for a vehicle. This endpoint is only available for the vehicle owner."""
872
- return await self._request(GET, f"api/1/vehicles/{vehicle_tag}/drivers")
873
-
874
- async def drivers_remove(
875
- self, vehicle_tag: str | int, share_user_id: str | int | None = None
876
- ) -> dict[str, Any]:
877
- """Removes driver access from a vehicle. Share users can only remove their own access. Owners can remove share access or their own."""
878
- return await self._request(
879
- DELETE,
880
- f"api/1/vehicles/{vehicle_tag}/drivers",
881
- {share_user_id: share_user_id},
882
- )
883
-
884
- async def list(
885
- self, page: int | None = None, per_page: int | None = None
886
- ) -> dict[str, Any]:
887
- """Returns vehicles belonging to the account."""
888
- return await self._request(
889
- GET, "api/1/vehicles", {page: page, per_page: per_page}
890
- )
891
-
892
- async def mobile_enabled(self, vehicle_tag: str | int) -> dict[str, Any]:
893
- """Returns whether or not mobile access is enabled for the vehicle."""
894
- return await self._request(
895
- GET, f"api/1/vehicles/{vehicle_tag}/mobile_enabled"
896
- )
897
-
898
- async def nearby_charging_sites(
899
- self,
900
- vehicle_tag: str | int,
901
- count: int | None = None,
902
- radius: int | None = None,
903
- detail: bool | None = None,
904
- ) -> dict[str, Any]:
905
- """Returns the charging sites near the current location of the vehicle."""
906
- return await self._request(
907
- GET,
908
- f"api/1/vehicles/{vehicle_tag}/nearby_charging_sites",
909
- {count: count, radius: radius, detail: detail},
910
- )
911
-
912
- async def options(self, vin: str) -> dict[str, Any]:
913
- """Returns vehicle option details."""
914
- return await self._request(GET, "api/1/dx/vehicles/options", {vin: vin})
915
-
916
- async def recent_alerts(self, vehicle_tag: str | int) -> dict[str, Any]:
917
- """List of recent alerts"""
918
- return await self._request(
919
- GET, f"api/1/vehicles/{vehicle_tag}/recent_alerts"
920
- )
921
-
922
- async def release_notes(
923
- self,
924
- vehicle_tag: str | int,
925
- staged: bool | None = None,
926
- language: int | None = None,
927
- ) -> dict[str, Any]:
928
- """Returns firmware release notes."""
929
- return await self._request(
930
- GET,
931
- f"api/1/vehicles/{vehicle_tag}/release_notes",
932
- {staged: staged, language: language},
933
- )
934
-
935
- async def service_data(self, vehicle_tag: str | int) -> dict[str, Any]:
936
- """Returns service data."""
937
- return await self._request(
938
- GET, f"api/1/vehicles/{vehicle_tag}/service_data"
939
- )
940
-
941
- async def share_invites(self, vehicle_tag: str | int) -> dict[str, Any]:
942
- """Returns the share invites for a vehicle."""
943
- return await self._request(GET, f"api/1/vehicles/{vehicle_tag}/invitations")
944
-
945
- async def share_invites_create(self, vehicle_tag: str | int) -> dict[str, Any]:
946
- """Creates a share invite for a vehicle."""
947
- return await self._request(
948
- POST, f"api/1/vehicles/{vehicle_tag}/invitations"
949
- )
950
-
951
- async def share_invites_redeem(self, code: str) -> dict[str, Any]:
952
- """Redeems a share invite."""
953
- return await self._request(POST, "api/1/invitations/redeem", {code: code})
954
-
955
- async def share_invites_revoke(
956
- self, vehicle_tag: str | int, id: str
957
- ) -> dict[str, Any]:
958
- """Revokes a share invite."""
959
- return await self._request(
960
- POST, f"api/1/vehicles/{vehicle_tag}/invitations/{id}/revoke"
961
- )
962
-
963
- async def signed_command(
964
- self, vehicle_tag: str | int, routable_message: str
965
- ) -> dict[str, Any]:
966
- """Signed Commands is a generic endpoint replacing legacy commands."""
967
- return await self._request(
968
- POST,
969
- f"api/1/vehicles/{vehicle_tag}/signed_command",
970
- {routable_message: routable_message},
971
- )
972
-
973
- async def subscriptions(
974
- self, device_token: str, device_type: str
975
- ) -> dict[str, Any]:
976
- """Returns the list of vehicles for which this mobile device currently subscribes to push notifications."""
977
- return await self._request(
978
- GET,
979
- "api/1/subscriptions",
980
- query={device_token: device_token, device_type: device_type},
981
- )
982
-
983
- async def subscriptions_set(
984
- self, device_token: str, device_type: str
985
- ) -> dict[str, Any]:
986
- """Allows a mobile device to specify which vehicles to receive push notifications from."""
987
- return await self._request(
988
- POST,
989
- "api/1/subscriptions",
990
- query={device_token: device_token, device_type: device_type},
991
- )
992
-
993
- async def vehicle(self, vehicle_tag: str | int) -> dict[str, Any]:
994
- """Returns information about a vehicle."""
995
- return await self._request(GET, f"api/1/vehicles/{vehicle_tag}")
996
-
997
- class Endpoints(StrEnum):
998
- """Endpoints options"""
999
-
1000
- CHARGE_STATE = "charge_state"
1001
- CLIMATE_STATE = "climate_state"
1002
- CLOSURES_STATE = "closures_state"
1003
- DRIVE_STATE = "drive_state"
1004
- GUI_SETTINGS = "gui_settings"
1005
- LOCATION_DATA = "location_data"
1006
- VEHICLE_CONFIG = "vehicle_config"
1007
- VEHICLE_STATE = "vehicle_state"
1008
- VEHICLE_DATA_COMBO = "vehicle_data_combo"
1009
-
1010
- async def vehicle_data(
1011
- self,
1012
- vehicle_tag: str | int,
1013
- endpoints: Endpoints | str | None = None,
1014
- ) -> dict[str, Any]:
1015
- """Makes a live call to the vehicle. This may return cached data if the vehicle is offline. For vehicles running firmware versions 2023.38+, location_data is required to fetch vehicle location. This will result in a location sharing icon to show on the vehicle UI."""
1016
- return await self._request(
1017
- GET,
1018
- f"api/1/vehicles/{vehicle_tag}/vehicle_data",
1019
- {endpoints: endpoints},
1020
- )
1021
-
1022
- class DeviceType(StrEnum):
1023
- """Device Type options"""
1024
-
1025
- ANDROID = "android"
1026
- IOS_DEVELOPMENT = "ios-development"
1027
- IOS_ENTERPRISE = "ios-enterprise"
1028
- IOS_BETA = "ios-beta"
1029
- IOS_PRODUCTION = "ios-production"
1030
-
1031
- async def vehicle_subscriptions(
1032
- self, device_token: str, device_type: DeviceType | str
1033
- ) -> dict[str, Any]:
1034
- """Returns the list of vehicles for which this mobile device currently subscribes to push notifications."""
1035
- return await self._request(
1036
- GET,
1037
- "api/1/vehicle_subscriptions",
1038
- {device_token: device_token, device_type: device_type},
1039
- )
1040
-
1041
- async def vehicle_subscriptions_set(
1042
- self, device_token: str, device_type: DeviceType | str
1043
- ) -> dict[str, Any]:
1044
- """Allows a mobile device to specify which vehicles to receive push notifications from."""
1045
- return await self._request(
1046
- POST,
1047
- "api/1/vehicle_subscriptions",
1048
- params={device_token: device_token, device_type: device_type},
1049
- )
1050
-
1051
- async def wake_up(self, vehicle_tag: str | int) -> dict[str, Any]:
1052
- """Wakes the vehicle from sleep, which is a state to minimize idle energy consumption."""
1053
- return await self._request(POST, f"api/1/vehicles/{vehicle_tag}/wake_up")
1054
-
1055
- async def warranty_details(self, vin: str | None) -> dict[str, Any]:
1056
- """Returns warranty details."""
1057
- return await self._request(GET, "api/1/dx/warranty/details", {vin: vin})
1058
-
1059
- async def fleet_telemetry_config(
1060
- self, config: dict[str, Any]
1061
- ) -> dict[str, Any]:
1062
- """Configures fleet telemetry."""
1063
- return await self._request(
1064
- POST, "api/1/vehicles/fleet_telemetry_config", json=config
1065
- )