tesla-fleet-api 0.9.10__py3-none-any.whl → 1.0.0__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. tesla_fleet_api/__init__.py +7 -22
  2. tesla_fleet_api/const.py +1 -225
  3. tesla_fleet_api/exceptions.py +117 -0
  4. tesla_fleet_api/tesla/__init__.py +11 -0
  5. tesla_fleet_api/tesla/bluetooth.py +33 -0
  6. tesla_fleet_api/{charging.py → tesla/charging.py} +1 -1
  7. tesla_fleet_api/{energy.py → tesla/energysite.py} +41 -33
  8. tesla_fleet_api/{teslafleetapi.py → tesla/fleet.py} +8 -53
  9. tesla_fleet_api/{teslafleetoauth.py → tesla/oauth.py} +3 -4
  10. tesla_fleet_api/{partner.py → tesla/partner.py} +1 -1
  11. tesla_fleet_api/tesla/tesla.py +52 -0
  12. tesla_fleet_api/{user.py → tesla/user.py} +1 -1
  13. tesla_fleet_api/teslemetry/__init__.py +5 -0
  14. tesla_fleet_api/{teslemetry.py → teslemetry/teslemetry.py} +16 -25
  15. tesla_fleet_api/teslemetry/vehicle.py +73 -0
  16. tesla_fleet_api/tessie/__init__.py +5 -0
  17. tesla_fleet_api/{tessie.py → tessie/tessie.py} +17 -9
  18. tesla_fleet_api/tessie/vehicle.py +41 -0
  19. {tesla_fleet_api-0.9.10.dist-info → tesla_fleet_api-1.0.0.dist-info}/METADATA +3 -2
  20. tesla_fleet_api-1.0.0.dist-info/RECORD +24 -0
  21. tesla_fleet_api/energyspecific.py +0 -125
  22. tesla_fleet_api/pb2/__init__.py +0 -0
  23. tesla_fleet_api/pb2/__init__.pyi +0 -9
  24. tesla_fleet_api/pb2/car_server_pb2.py +0 -175
  25. tesla_fleet_api/pb2/car_server_pb2.pyi +0 -904
  26. tesla_fleet_api/pb2/common_pb2.py +0 -33
  27. tesla_fleet_api/pb2/common_pb2.pyi +0 -130
  28. tesla_fleet_api/pb2/errors_pb2.py +0 -17
  29. tesla_fleet_api/pb2/errors_pb2.pyi +0 -32
  30. tesla_fleet_api/pb2/keys_pb2.py +0 -15
  31. tesla_fleet_api/pb2/keys_pb2.pyi +0 -21
  32. tesla_fleet_api/pb2/managed_charging_pb2.py +0 -15
  33. tesla_fleet_api/pb2/managed_charging_pb2.pyi +0 -17
  34. tesla_fleet_api/pb2/signatures_pb2.py +0 -35
  35. tesla_fleet_api/pb2/signatures_pb2.pyi +0 -152
  36. tesla_fleet_api/pb2/universal_message_pb2.py +0 -30
  37. tesla_fleet_api/pb2/universal_message_pb2.pyi +0 -148
  38. tesla_fleet_api/pb2/vcsec_pb2.py +0 -79
  39. tesla_fleet_api/pb2/vcsec_pb2.pyi +0 -482
  40. tesla_fleet_api/pb2/vehicle_pb2.py +0 -125
  41. tesla_fleet_api/pb2/vehicle_pb2.pyi +0 -1183
  42. tesla_fleet_api/teslafleetopensource.py +0 -61
  43. tesla_fleet_api/vehicle.py +0 -880
  44. tesla_fleet_api/vehiclesigned.py +0 -1129
  45. tesla_fleet_api/vehiclespecific.py +0 -509
  46. tesla_fleet_api-0.9.10.dist-info/RECORD +0 -42
  47. {tesla_fleet_api-0.9.10.dist-info → tesla_fleet_api-1.0.0.dist-info}/LICENSE +0 -0
  48. {tesla_fleet_api-0.9.10.dist-info → tesla_fleet_api-1.0.0.dist-info}/WHEEL +0 -0
  49. {tesla_fleet_api-0.9.10.dist-info → tesla_fleet_api-1.0.0.dist-info}/top_level.txt +0 -0
@@ -1,1129 +0,0 @@
1
- from __future__ import annotations
2
- import base64
3
- from random import randbytes
4
- from typing import Any, TYPE_CHECKING
5
- import time
6
- import struct
7
- import hmac
8
- import hashlib
9
- from cryptography.hazmat.primitives.asymmetric import ec
10
- from cryptography.hazmat.primitives.serialization import PublicFormat, Encoding
11
- from asyncio import Lock, sleep
12
-
13
- from tesla_fleet_api.pb2.errors_pb2 import GenericError_E
14
-
15
- from .exceptions import MESSAGE_FAULTS, SIGNED_MESSAGE_INFORMATION_FAULTS, TeslaFleetMessageFaultIncorrectEpoch, TeslaFleetMessageFaultInvalidTokenOrCounter
16
-
17
- from .const import (
18
- LOGGER,
19
- Trunk,
20
- ClimateKeeperMode,
21
- CabinOverheatProtectionTemp,
22
- SunRoofCommand,
23
- WindowCommand,
24
- )
25
- from .vehiclespecific import VehicleSpecific
26
-
27
- from .pb2.universal_message_pb2 import (
28
- OPERATIONSTATUS_WAIT,
29
- OPERATIONSTATUS_ERROR,
30
- DOMAIN_VEHICLE_SECURITY,
31
- DOMAIN_INFOTAINMENT,
32
- Domain,
33
- RoutableMessage,
34
- )
35
- from .pb2.car_server_pb2 import (
36
- Response,
37
- Action,
38
- MediaPlayAction,
39
- VehicleAction,
40
- VehicleControlFlashLightsAction,
41
- ChargingStartStopAction,
42
- ChargingSetLimitAction,
43
- EraseUserDataAction,
44
- DrivingClearSpeedLimitPinAction,
45
- DrivingSetSpeedLimitAction,
46
- DrivingSpeedLimitAction,
47
- HvacAutoAction,
48
- HvacSeatHeaterActions,
49
- HvacSeatCoolerActions,
50
- HvacSetPreconditioningMaxAction,
51
- HvacSteeringWheelHeaterAction,
52
- HvacTemperatureAdjustmentAction,
53
- GetNearbyChargingSites,
54
- # NearbyChargingSites,
55
- # Superchargers,
56
- VehicleControlCancelSoftwareUpdateAction,
57
- VehicleControlHonkHornAction,
58
- VehicleControlResetValetPinAction,
59
- VehicleControlScheduleSoftwareUpdateAction,
60
- VehicleControlSetSentryModeAction,
61
- VehicleControlSetValetModeAction,
62
- VehicleControlSunroofOpenCloseAction,
63
- VehicleControlTriggerHomelinkAction,
64
- VehicleControlWindowAction,
65
- HvacBioweaponModeAction,
66
- AutoSeatClimateAction,
67
- # Ping,
68
- ScheduledChargingAction,
69
- ScheduledDepartureAction,
70
- HvacClimateKeeperAction,
71
- SetChargingAmpsAction,
72
- SetCabinOverheatProtectionAction,
73
- SetVehicleNameAction,
74
- SetCopTempAction,
75
- VehicleControlSetPinToDriveAction,
76
- VehicleControlResetPinToDriveAction,
77
- MediaNextTrack,
78
- MediaNextFavorite,
79
- MediaUpdateVolume,
80
- MediaPreviousTrack,
81
- MediaPreviousFavorite,
82
- )
83
- from .pb2.vehicle_pb2 import VehicleState
84
- from .pb2.vcsec_pb2 import (
85
- # SignedMessage_information_E,
86
- OPERATIONSTATUS_OK,
87
- FromVCSECMessage,
88
- UnsignedMessage,
89
- RKEAction_E,
90
- ClosureMoveRequest,
91
- ClosureMoveType_E,
92
- )
93
- from .pb2.signatures_pb2 import (
94
- SIGNATURE_TYPE_HMAC_PERSONALIZED,
95
- TAG_DOMAIN,
96
- TAG_SIGNATURE_TYPE,
97
- SessionInfo,
98
- HMAC_Personalized_Signature_Data,
99
- TAG_PERSONALIZATION,
100
- TAG_EPOCH,
101
- TAG_EXPIRES_AT,
102
- TAG_COUNTER,
103
- TAG_END,
104
- )
105
- from .pb2.common_pb2 import (
106
- Void,
107
- PreconditioningTimes,
108
- OffPeakChargingTimes,
109
- # ChargeSchedule,
110
- # PreconditionSchedule,
111
- )
112
-
113
- if TYPE_CHECKING:
114
- from .vehicle import Vehicle
115
-
116
-
117
- class Session:
118
- """A connect to a domain"""
119
-
120
- key: bytes
121
- counter: int
122
- epoch: bytes
123
- delta: int
124
- hmac: bytes
125
- publicKey: bytes
126
- lock: Lock
127
-
128
- def __init__(self):
129
- self.lock = Lock()
130
- self.counter = 0
131
-
132
- def update(self, sessionInfo: SessionInfo, privateKey: ec.EllipticCurvePrivateKey):
133
- """Update the session with new information"""
134
- self.counter = sessionInfo.counter
135
- self.epoch = sessionInfo.epoch
136
- self.delta = int(time.time()) - sessionInfo.clock_time
137
- if (self.publicKey != sessionInfo.publicKey):
138
- self.publicKey = sessionInfo.publicKey
139
- self.key = hashlib.sha1(
140
- privateKey.exchange(
141
- ec.ECDH(),
142
- ec.EllipticCurvePublicKey.from_encoded_point(
143
- ec.SECP256R1(), self.publicKey
144
- ),
145
- ),
146
- ).digest()[:16]
147
- self.hmac = hmac.new(
148
- self.key, "authenticated command".encode(), hashlib.sha256
149
- ).digest()
150
-
151
- def get(self) -> HMAC_Personalized_Signature_Data:
152
- """Sign a command and return session metadata"""
153
- self.counter = self.counter+1
154
- return HMAC_Personalized_Signature_Data(
155
- epoch=self.epoch,
156
- counter=self.counter,
157
- expires_at=int(time.time()) - self.delta + 10,
158
- )
159
-
160
-
161
- class VehicleSigned(VehicleSpecific):
162
- """Class describing the Tesla Fleet API vehicle endpoints and commands for a specific vehicle with command signing."""
163
-
164
- private_key: ec.EllipticCurvePrivateKey
165
- _public_key: bytes
166
- _from_destination: bytes
167
- _sessions: dict[int, Session]
168
-
169
- def __init__(
170
- self, parent: Vehicle, vin: str, key: ec.EllipticCurvePrivateKey | None = None
171
- ):
172
- super().__init__(parent, vin)
173
- if key:
174
- self.private_key = key
175
- elif parent._parent.private_key:
176
- self.private_key = parent._parent.private_key
177
- else:
178
- raise ValueError("No private key.")
179
-
180
- self._public_key = self.private_key.public_key().public_bytes(
181
- encoding=Encoding.X962, format=PublicFormat.UncompressedPoint
182
- )
183
- self._from_destination = randbytes(16)
184
- self._sessions = {}
185
-
186
- async def _send(self, msg: RoutableMessage) -> RoutableMessage:
187
- """Serialize a message and send to the signed command endpoint."""
188
-
189
- async with self._sessions[msg.to_destination.domain].lock:
190
- resp = await self.signed_command(
191
- base64.b64encode(msg.SerializeToString()).decode()
192
- )
193
-
194
- resp_msg = RoutableMessage.FromString(base64.b64decode(resp["response"]))
195
-
196
- # Check UUID?
197
- # Check RoutingAdress?
198
-
199
- if resp_msg.session_info:
200
- self._sessions[resp_msg.from_destination.domain].update(
201
- SessionInfo.FromString(resp_msg.session_info), self.private_key
202
- )
203
-
204
- if resp_msg.signedMessageStatus.signed_message_fault:
205
- raise MESSAGE_FAULTS[resp_msg.signedMessageStatus.signed_message_fault]
206
-
207
- return resp_msg
208
-
209
- async def _handshake(self, domain: Domain) -> Session:
210
- """Perform a handshake with the vehicle."""
211
- if session := self._sessions.get(domain):
212
- return session
213
- self._sessions[domain] = Session()
214
-
215
- LOGGER.debug(f"Handshake with domain {Domain.Name(domain)}")
216
- msg = RoutableMessage()
217
- msg.to_destination.domain = domain
218
- msg.from_destination.routing_address = self._from_destination
219
- msg.session_info_request.public_key = self._public_key
220
- msg.uuid = randbytes(16)
221
-
222
- # Send handshake message
223
- await self._send(msg)
224
-
225
- return self._sessions[domain]
226
-
227
- async def _sendVehicleSecurity(self, command: UnsignedMessage) -> dict[str, Any]:
228
- """Sign and send a message to Infotainment computer."""
229
- return await self._sign(DOMAIN_VEHICLE_SECURITY, command.SerializeToString())
230
-
231
- async def _sendInfotainment(self, command: Action) -> dict[str, Any]:
232
- """Sign and send a message to Infotainment computer."""
233
- return await self._sign(DOMAIN_INFOTAINMENT, command.SerializeToString())
234
-
235
- async def _sign(
236
- self, domain: Domain, command: bytes, attempt: int = 1
237
- ) -> dict[str, Any]:
238
- """Send a signed message to the vehicle."""
239
- LOGGER.debug(f"Sending to domain {Domain.Name(domain)}")
240
-
241
- session = await self._handshake(domain)
242
- hmac_personalized = session.get()
243
-
244
- msg = RoutableMessage()
245
- msg.to_destination.domain = domain
246
- msg.from_destination.routing_address = self._from_destination
247
- msg.protobuf_message_as_bytes = command
248
- msg.uuid = randbytes(16)
249
-
250
- metadata = bytes([
251
- TAG_SIGNATURE_TYPE,
252
- 1,
253
- SIGNATURE_TYPE_HMAC_PERSONALIZED,
254
- TAG_DOMAIN,
255
- 1,
256
- domain,
257
- TAG_PERSONALIZATION,
258
- 17,
259
- *self.vin.encode(),
260
- TAG_EPOCH,
261
- len(hmac_personalized.epoch),
262
- *hmac_personalized.epoch,
263
- TAG_EXPIRES_AT,
264
- 4,
265
- *struct.pack(">I", hmac_personalized.expires_at),
266
- TAG_COUNTER,
267
- 4,
268
- *struct.pack(">I", hmac_personalized.counter),
269
- TAG_END,
270
- ])
271
-
272
- hmac_personalized.tag = hmac.new(
273
- session.hmac, metadata + command, hashlib.sha256
274
- ).digest()
275
-
276
- # I think this whole section could be improved
277
- msg.signature_data.HMAC_Personalized_data.CopyFrom(hmac_personalized)
278
- msg.signature_data.signer_identity.public_key = self._public_key
279
-
280
- try:
281
- resp = await self._send(msg)
282
- except (
283
- TeslaFleetMessageFaultIncorrectEpoch,
284
- TeslaFleetMessageFaultInvalidTokenOrCounter,
285
- ) as e:
286
- attempt += 1
287
- if attempt > 3:
288
- # We tried 3 times, give up, raise the error
289
- raise e
290
- return await self._sign(domain, command, attempt)
291
-
292
- if resp.signedMessageStatus.operation_status == OPERATIONSTATUS_WAIT:
293
- attempt += 1
294
- if attempt > 3:
295
- # We tried 3 times, give up, raise the error
296
- return {"response": {"result": False, "reason": "Too many retries"}}
297
- async with session.lock:
298
- await sleep(2)
299
- return await self._sign(domain, command, attempt)
300
-
301
- if resp.HasField("protobuf_message_as_bytes"):
302
- if(resp.from_destination.domain == DOMAIN_VEHICLE_SECURITY):
303
- vcsec = FromVCSECMessage.FromString(resp.protobuf_message_as_bytes)
304
- LOGGER.debug("VCSEC Response: %s", vcsec)
305
- if vcsec.HasField("nominalError"):
306
- LOGGER.error("Command failed with reason: %s", vcsec.nominalError.genericError)
307
- return {
308
- "response": {
309
- "result": False,
310
- "reason": GenericError_E.Name(vcsec.nominalError.genericError)
311
- }
312
- }
313
- elif vcsec.commandStatus.operationStatus == OPERATIONSTATUS_OK:
314
- return {"response": {"result": True, "reason": ""}}
315
- elif vcsec.commandStatus.operationStatus == OPERATIONSTATUS_WAIT:
316
- attempt += 1
317
- if attempt > 3:
318
- # We tried 3 times, give up, raise the error
319
- return {"response": {"result": False, "reason": "Too many retries"}}
320
- async with session.lock:
321
- await sleep(2)
322
- return await self._sign(domain, command, attempt)
323
- elif vcsec.commandStatus.operationStatus == OPERATIONSTATUS_ERROR:
324
- if(resp.HasField("signedMessageStatus")):
325
- raise SIGNED_MESSAGE_INFORMATION_FAULTS[vcsec.commandStatus.signedMessageStatus.signedMessageInformation]
326
-
327
- elif(resp.from_destination.domain == DOMAIN_INFOTAINMENT):
328
- response = Response.FromString(resp.protobuf_message_as_bytes)
329
- LOGGER.debug("Infotainment Response: %s", response)
330
- if (response.HasField("ping")):
331
- print(response.ping)
332
- return {
333
- "response": {
334
- "result": True,
335
- "reason": response.ping.local_timestamp
336
- }
337
- }
338
- if response.HasField("actionStatus"):
339
- return {
340
- "response": {
341
- "result": response.actionStatus.result == OPERATIONSTATUS_OK,
342
- "reason": response.actionStatus.result_reason.plain_text or ""
343
- }
344
- }
345
-
346
- return {"response": {"result": True, "reason": ""}}
347
-
348
- async def actuate_trunk(self, which_trunk: Trunk | str) -> dict[str, Any]:
349
- """Controls the front or rear trunk."""
350
- if which_trunk == Trunk.FRONT:
351
- return await self._sendVehicleSecurity(
352
- UnsignedMessage(
353
- closureMoveRequest=ClosureMoveRequest(
354
- frontTrunk=ClosureMoveType_E.CLOSURE_MOVE_TYPE_MOVE
355
- )
356
- )
357
- )
358
- if which_trunk == Trunk.REAR:
359
- return await self._sendVehicleSecurity(
360
- UnsignedMessage(
361
- closureMoveRequest=ClosureMoveRequest(
362
- rearTrunk=ClosureMoveType_E.CLOSURE_MOVE_TYPE_MOVE
363
- )
364
- )
365
- )
366
-
367
- async def adjust_volume(self, volume: float) -> dict[str, Any]:
368
- """Adjusts vehicle media playback volume."""
369
- return await self._sendInfotainment(
370
- Action(
371
- vehicleAction=VehicleAction(
372
- mediaUpdateVolume=MediaUpdateVolume(volume_absolute_float=volume)
373
- )
374
- )
375
- )
376
-
377
- async def auto_conditioning_start(self) -> dict[str, Any]:
378
- """Starts climate preconditioning."""
379
- return await self._sendInfotainment(
380
- Action(
381
- vehicleAction=VehicleAction(
382
- hvacAutoAction=HvacAutoAction(power_on=True)
383
- )
384
- )
385
- )
386
-
387
- async def auto_conditioning_stop(self) -> dict[str, Any]:
388
- """Stops climate preconditioning."""
389
- return await self._sendInfotainment(
390
- Action(
391
- vehicleAction=VehicleAction(
392
- hvacAutoAction=HvacAutoAction(power_on=False)
393
- )
394
- )
395
- )
396
-
397
- async def cancel_software_update(self) -> dict[str, Any]:
398
- """Cancels the countdown to install the vehicle software update."""
399
- return await self._sendInfotainment(
400
- Action(
401
- vehicleAction=VehicleAction(
402
- vehicleControlCancelSoftwareUpdateAction=VehicleControlCancelSoftwareUpdateAction()
403
- )
404
- )
405
- )
406
-
407
- async def charge_max_range(self) -> dict[str, Any]:
408
- """Charges in max range mode -- we recommend limiting the use of this mode to long trips."""
409
- return await self._sendInfotainment(
410
- Action(
411
- vehicleAction=VehicleAction(
412
- chargingStartStopAction=ChargingStartStopAction(
413
- start_max_range=Void()
414
- )
415
- )
416
- )
417
- )
418
-
419
- async def charge_port_door_close(self) -> dict[str, Any]:
420
- """Closes the charge port door."""
421
- return await self._sendVehicleSecurity(
422
- UnsignedMessage(
423
- closureMoveRequest=ClosureMoveRequest(
424
- chargePort=ClosureMoveType_E.CLOSURE_MOVE_TYPE_CLOSE
425
- )
426
- )
427
- )
428
-
429
- async def charge_port_door_open(self) -> dict[str, Any]:
430
- """Opens the charge port door."""
431
- return await self._sendVehicleSecurity(
432
- UnsignedMessage(
433
- closureMoveRequest=ClosureMoveRequest(
434
- chargePort=ClosureMoveType_E.CLOSURE_MOVE_TYPE_OPEN
435
- )
436
- )
437
- )
438
-
439
- async def charge_standard(self) -> dict[str, Any]:
440
- """Charges in Standard mode."""
441
- return await self._sendInfotainment(
442
- Action(
443
- vehicleAction=VehicleAction(
444
- chargingStartStopAction=ChargingStartStopAction(
445
- start_standard=Void()
446
- )
447
- )
448
- )
449
- )
450
-
451
- async def charge_start(self) -> dict[str, Any]:
452
- """Starts charging the vehicle."""
453
- return await self._sendInfotainment(
454
- Action(
455
- vehicleAction=VehicleAction(
456
- chargingStartStopAction=ChargingStartStopAction(start=Void())
457
- )
458
- )
459
- )
460
-
461
- async def charge_stop(self) -> dict[str, Any]:
462
- """Stops charging the vehicle."""
463
- return await self._sendInfotainment(
464
- Action(
465
- vehicleAction=VehicleAction(
466
- chargingStartStopAction=ChargingStartStopAction(stop=Void())
467
- )
468
- )
469
- )
470
-
471
- async def clear_pin_to_drive_admin(self, pin: str):
472
- """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."""
473
- return await self._sendInfotainment(
474
- Action(
475
- vehicleAction=VehicleAction(
476
- drivingClearSpeedLimitPinAction=DrivingClearSpeedLimitPinAction(
477
- pin=pin
478
- )
479
- )
480
- )
481
- )
482
-
483
- async def door_lock(self) -> dict[str, Any]:
484
- """Locks the vehicle."""
485
- return await self._sendVehicleSecurity(
486
- UnsignedMessage(RKEAction=RKEAction_E.RKE_ACTION_LOCK)
487
- )
488
-
489
- async def door_unlock(self) -> dict[str, Any]:
490
- """Unlocks the vehicle."""
491
- return await self._sendVehicleSecurity(
492
- UnsignedMessage(RKEAction=RKEAction_E.RKE_ACTION_UNLOCK)
493
- )
494
-
495
- async def erase_user_data(self) -> dict[str, Any]:
496
- """Erases user's data from the user interface. Requires the vehicle to be in park."""
497
- return await self._sendInfotainment(
498
- Action(
499
- vehicleAction=VehicleAction(eraseUserDataAction=EraseUserDataAction())
500
- )
501
- )
502
-
503
- async def flash_lights(self) -> dict[str, Any]:
504
- """Briefly flashes the vehicle headlights. Requires the vehicle to be in park."""
505
- return await self._sendInfotainment(
506
- Action(
507
- vehicleAction=VehicleAction(
508
- vehicleControlFlashLightsAction=VehicleControlFlashLightsAction()
509
- )
510
- )
511
- )
512
-
513
- async def guest_mode(self, enable: bool) -> dict[str, Any]:
514
- """Restricts certain vehicle UI functionality from guest users"""
515
- return await self._sendInfotainment(
516
- Action(
517
- vehicleAction=VehicleAction(
518
- guestModeAction=VehicleState.GuestMode(GuestModeActive=enable)
519
- )
520
- )
521
- )
522
-
523
- async def honk_horn(self) -> dict[str, Any]:
524
- """Honks the vehicle horn. Requires the vehicle to be in park."""
525
- return await self._sendInfotainment(
526
- Action(
527
- vehicleAction=VehicleAction(
528
- vehicleControlHonkHornAction=VehicleControlHonkHornAction()
529
- )
530
- )
531
- )
532
-
533
- async def media_next_fav(self) -> dict[str, Any]:
534
- """Advances media player to next favorite track."""
535
- return await self._sendInfotainment(
536
- Action(vehicleAction=VehicleAction(mediaNextFavorite=MediaNextFavorite()))
537
- )
538
-
539
- async def media_next_track(self) -> dict[str, Any]:
540
- """Advances media player to next track."""
541
- return await self._sendInfotainment(
542
- Action(vehicleAction=VehicleAction(mediaNextTrack=MediaNextTrack()))
543
- )
544
-
545
- async def media_prev_fav(self) -> dict[str, Any]:
546
- """Advances media player to previous favorite track."""
547
- return await self._sendInfotainment(
548
- Action(
549
- vehicleAction=VehicleAction(
550
- mediaPreviousFavorite=MediaPreviousFavorite()
551
- )
552
- )
553
- )
554
-
555
- async def media_prev_track(self) -> dict[str, Any]:
556
- """Advances media player to previous track."""
557
- return await self._sendInfotainment(
558
- Action(vehicleAction=VehicleAction(mediaPreviousTrack=MediaPreviousTrack()))
559
- )
560
-
561
- async def media_toggle_playback(self) -> dict[str, Any]:
562
- """Toggles current play/pause state."""
563
- return await self._sendInfotainment(
564
- Action(vehicleAction=VehicleAction(mediaPlayAction=MediaPlayAction()))
565
- )
566
-
567
- async def media_volume_down(self) -> dict[str, Any]:
568
- """Turns the volume down by one."""
569
- return await self._sendInfotainment(
570
- Action(
571
- vehicleAction=VehicleAction(
572
- mediaUpdateVolume=MediaUpdateVolume(volume_delta=-1)
573
- )
574
- )
575
- )
576
-
577
- # This one is new
578
- async def media_volume_up(self) -> dict[str, Any]:
579
- """Turns the volume up by one."""
580
- return await self._sendInfotainment(
581
- Action(
582
- vehicleAction=VehicleAction(
583
- mediaUpdateVolume=MediaUpdateVolume(volume_delta=1)
584
- )
585
- )
586
- )
587
-
588
- # navigation_gps_request doesnt require signing
589
- # navigation_request doesnt require signing
590
- # navigation_sc_request doesnt require signing
591
-
592
- async def remote_auto_seat_climate_request(
593
- self, auto_seat_position: int, auto_climate_on: bool
594
- ) -> dict[str, Any]:
595
- """Sets automatic seat heating and cooling."""
596
- # AutoSeatPosition_FrontLeft = 1;
597
- # AutoSeatPosition_FrontRight = 2;
598
- return await self._sendInfotainment(
599
- Action(
600
- vehicleAction=VehicleAction(
601
- autoSeatClimateAction=AutoSeatClimateAction(
602
- carseat=[
603
- AutoSeatClimateAction.CarSeat(
604
- on=auto_climate_on, seat_position=auto_seat_position
605
- )
606
- ]
607
- )
608
- )
609
- )
610
- )
611
-
612
- # remote_auto_steering_wheel_heat_climate_request has no protobuf
613
-
614
- # remote_boombox not implemented
615
-
616
- async def remote_seat_cooler_request(
617
- self, seat_position: int, seat_cooler_level: int
618
- ) -> dict[str, Any]:
619
- """Sets seat cooling."""
620
- # HvacSeatCoolerLevel_Unknown = 0;
621
- # HvacSeatCoolerLevel_Off = 1;
622
- # HvacSeatCoolerLevel_Low = 2;
623
- # HvacSeatCoolerLevel_Med = 3;
624
- # HvacSeatCoolerLevel_High = 4;
625
- # HvacSeatCoolerPosition_Unknown = 0;
626
- # HvacSeatCoolerPosition_FrontLeft = 1;
627
- # HvacSeatCoolerPosition_FrontRight = 2;
628
- return await self._sendInfotainment(
629
- Action(
630
- vehicleAction=VehicleAction(
631
- hvacSeatCoolerActions=HvacSeatCoolerActions(
632
- hvacSeatCoolerAction=[
633
- HvacSeatCoolerActions.HvacSeatCoolerAction(
634
- seat_cooler_level=seat_cooler_level + 1,
635
- seat_position=seat_position,
636
- )
637
- ]
638
- )
639
- )
640
- )
641
- )
642
-
643
- async def remote_seat_heater_request(
644
- self, seat_position: int, seat_heater_level: int
645
- ) -> dict[str, Any]:
646
- """Sets seat heating."""
647
- # HvacSeatCoolerLevel_Unknown = 0;
648
- # HvacSeatCoolerLevel_Off = 1;
649
- # HvacSeatCoolerLevel_Low = 2;
650
- # HvacSeatCoolerLevel_Med = 3;
651
- # HvacSeatCoolerLevel_High = 4;
652
- # Void CAR_SEAT_UNKNOWN = 6;
653
- # Void CAR_SEAT_FRONT_LEFT = 7;
654
- # Void CAR_SEAT_FRONT_RIGHT = 8;
655
- # Void CAR_SEAT_REAR_LEFT = 9;
656
- # Void CAR_SEAT_REAR_LEFT_BACK = 10;
657
- # Void CAR_SEAT_REAR_CENTER = 11;
658
- # Void CAR_SEAT_REAR_RIGHT = 12;
659
- # Void CAR_SEAT_REAR_RIGHT_BACK = 13;
660
- # Void CAR_SEAT_THIRD_ROW_LEFT = 14;
661
- # Void CAR_SEAT_THIRD_ROW_RIGHT = 15;
662
-
663
- heater_action_dict = {}
664
- match seat_position:
665
- case 0:
666
- heater_action_dict["CAR_SEAT_FRONT_LEFT"] = Void()
667
- case 1:
668
- heater_action_dict["CAR_SEAT_FRONT_RIGHT"] = Void()
669
- case 2:
670
- heater_action_dict["CAR_SEAT_REAR_LEFT"] = Void()
671
- case 3:
672
- heater_action_dict["CAR_SEAT_REAR_LEFT_BACK"] = Void()
673
- case 4:
674
- heater_action_dict["CAR_SEAT_REAR_CENTER"] = Void()
675
- case 5:
676
- heater_action_dict["CAR_SEAT_REAR_RIGHT"] = Void()
677
- case 6:
678
- heater_action_dict["CAR_SEAT_REAR_RIGHT_BACK"] = Void()
679
- case 7:
680
- heater_action_dict["CAR_SEAT_THIRD_ROW_LEFT"] = Void()
681
- case 8:
682
- heater_action_dict["CAR_SEAT_THIRD_ROW_RIGHT"] = Void()
683
- case _:
684
- raise ValueError(f"Invalid seat position: {seat_position}")
685
- match seat_heater_level:
686
- case 0:
687
- heater_action_dict["SEAT_HEATER_OFF"] = Void()
688
- case 1:
689
- heater_action_dict["SEAT_HEATER_LOW"] = Void()
690
- case 2:
691
- heater_action_dict["SEAT_HEATER_MEDIUM"] = Void()
692
- case 3:
693
- heater_action_dict["SEAT_HEATER_HIGH"] = Void()
694
- case _:
695
- raise ValueError(f"Invalid seat heater level: {seat_heater_level}")
696
-
697
- heater_action = HvacSeatHeaterActions.HvacSeatHeaterAction(**heater_action_dict)
698
- return await self._sendInfotainment(
699
- Action(
700
- vehicleAction=VehicleAction(
701
- hvacSeatHeaterActions=HvacSeatHeaterActions(
702
- hvacSeatHeaterAction=[heater_action]
703
- )
704
- )
705
- )
706
- )
707
-
708
- async def remote_start_drive(self) -> dict[str, Any]:
709
- """Starts the vehicle remotely. Requires keyless driving to be enabled."""
710
- return await self._sendVehicleSecurity(
711
- UnsignedMessage(RKEAction=RKEAction_E.RKE_ACTION_REMOTE_DRIVE)
712
- )
713
-
714
- async def remote_steering_wheel_heat_level_request(
715
- self, level: int
716
- ) -> dict[str, Any]:
717
- """Sets steering wheel heat level."""
718
- raise NotImplementedError()
719
-
720
- async def remote_steering_wheel_heater_request(self, on: bool) -> dict[str, Any]:
721
- """Sets steering wheel heating on/off. For vehicles that do not support auto steering wheel heat."""
722
- return await self._sendInfotainment(
723
- Action(
724
- vehicleAction=VehicleAction(
725
- hvacSteeringWheelHeaterAction=HvacSteeringWheelHeaterAction(
726
- power_on=on
727
- )
728
- )
729
- )
730
- )
731
-
732
- async def reset_pin_to_drive_pin(self) -> dict[str, Any]:
733
- """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."""
734
- return await self._sendInfotainment(
735
- Action(
736
- vehicleAction=VehicleAction(
737
- vehicleControlResetPinToDriveAction=VehicleControlResetPinToDriveAction()
738
- )
739
- )
740
- )
741
-
742
- async def reset_valet_pin(self) -> dict[str, Any]:
743
- """Removes PIN for Valet Mode."""
744
- return await self._sendInfotainment(
745
- Action(
746
- vehicleAction=VehicleAction(
747
- vehicleControlResetValetPinAction=VehicleControlResetValetPinAction()
748
- )
749
- )
750
- )
751
-
752
- async def schedule_software_update(self, offset_sec: int) -> dict[str, Any]:
753
- """Schedules a vehicle software update (over the air "OTA") to be installed in the future."""
754
- return await self._sendInfotainment(
755
- Action(
756
- vehicleAction=VehicleAction(
757
- vehicleControlScheduleSoftwareUpdateAction=VehicleControlScheduleSoftwareUpdateAction(
758
- offset_sec=offset_sec
759
- )
760
- )
761
- )
762
- )
763
-
764
- async def set_bioweapon_mode(
765
- self, on: bool, manual_override: bool
766
- ) -> dict[str, Any]:
767
- """Turns Bioweapon Defense Mode on and off."""
768
- return await self._sendInfotainment(
769
- Action(
770
- vehicleAction=VehicleAction(
771
- hvacBioweaponModeAction=HvacBioweaponModeAction(
772
- on=on, manual_override=manual_override
773
- )
774
- )
775
- )
776
- )
777
-
778
- async def set_cabin_overheat_protection(
779
- self, on: bool, fan_only: bool
780
- ) -> dict[str, Any]:
781
- """Sets the vehicle overheat protection."""
782
- return await self._sendInfotainment(
783
- Action(
784
- vehicleAction=VehicleAction(
785
- setCabinOverheatProtectionAction=SetCabinOverheatProtectionAction(
786
- on=on, fan_only=fan_only
787
- )
788
- )
789
- )
790
- )
791
-
792
- async def set_charge_limit(self, percent: int) -> dict[str, Any]:
793
- """Sets the vehicle charge limit."""
794
- return await self._sendInfotainment(
795
- Action(
796
- vehicleAction=VehicleAction(
797
- chargingSetLimitAction=ChargingSetLimitAction(percent=percent)
798
- )
799
- )
800
- )
801
-
802
- async def set_charging_amps(self, charging_amps: int) -> dict[str, Any]:
803
- """Sets the vehicle charging amps."""
804
- return await self._sendInfotainment(
805
- Action(
806
- vehicleAction=VehicleAction(
807
- setChargingAmpsAction=SetChargingAmpsAction(
808
- charging_amps=charging_amps
809
- )
810
- )
811
- )
812
- )
813
-
814
- async def set_climate_keeper_mode(
815
- self, climate_keeper_mode: ClimateKeeperMode | int
816
- ) -> dict[str, Any]:
817
- """Enables climate keeper mode."""
818
- if isinstance(climate_keeper_mode, ClimateKeeperMode):
819
- climate_keeper_mode = climate_keeper_mode.value
820
- return await self._sendInfotainment(
821
- Action(
822
- vehicleAction=VehicleAction(
823
- hvacClimateKeeperAction=HvacClimateKeeperAction(
824
- ClimateKeeperAction=climate_keeper_mode,
825
- # manual_override
826
- )
827
- )
828
- )
829
- )
830
-
831
- async def set_cop_temp(
832
- self, cop_temp: CabinOverheatProtectionTemp | int
833
- ) -> dict[str, Any]:
834
- """Adjusts the Cabin Overheat Protection temperature (COP)."""
835
- if isinstance(cop_temp, CabinOverheatProtectionTemp):
836
- cop_temp = cop_temp.value
837
- return await self._sendInfotainment(
838
- Action(
839
- vehicleAction=VehicleAction(
840
- setCopTempAction=SetCopTempAction(copActivationTemp=cop_temp + 1)
841
- )
842
- )
843
- )
844
-
845
- async def set_pin_to_drive(self, on: bool, password: str | int) -> dict[str, Any]:
846
- """Sets a four-digit passcode for PIN to Drive. This PIN must then be entered before the vehicle can be driven."""
847
- return await self._sendInfotainment(
848
- Action(
849
- vehicleAction=VehicleAction(
850
- vehicleControlSetPinToDriveAction=VehicleControlSetPinToDriveAction(
851
- on=on, password=str(password)
852
- )
853
- )
854
- )
855
- )
856
-
857
- async def set_preconditioning_max(
858
- self, on: bool, manual_override: bool
859
- ) -> dict[str, Any]:
860
- """Sets an override for preconditioning — it should default to empty if no override is used."""
861
- return await self._sendInfotainment(
862
- Action(
863
- vehicleAction=VehicleAction(
864
- hvacSetPreconditioningMaxAction=HvacSetPreconditioningMaxAction(
865
- on=on,
866
- manual_override=manual_override,
867
- # manual_override_mode
868
- )
869
- )
870
- )
871
- )
872
-
873
- async def set_scheduled_charging(self, enable: bool, time: int) -> dict[str, Any]:
874
- """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)."""
875
- return await self._sendInfotainment(
876
- Action(
877
- vehicleAction=VehicleAction(
878
- scheduledChargingAction=ScheduledChargingAction(
879
- enable=enable, charging_time=time
880
- )
881
- )
882
- )
883
- )
884
-
885
- async def set_scheduled_departure(
886
- self,
887
- enable: bool = True,
888
- preconditioning_enabled: bool = False,
889
- preconditioning_weekdays_only: bool = False,
890
- departure_time: int = 0,
891
- off_peak_charging_enabled: bool = False,
892
- off_peak_charging_weekdays_only: bool = False,
893
- end_off_peak_time: int = 0,
894
- ) -> dict[str, Any]:
895
- """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)."""
896
-
897
- if preconditioning_weekdays_only:
898
- preconditioning_times = PreconditioningTimes(weekdays=Void)
899
- else:
900
- preconditioning_times = PreconditioningTimes(all_week=Void)
901
-
902
- if off_peak_charging_weekdays_only:
903
- off_peak_charging_times = OffPeakChargingTimes(weekdays=Void)
904
- else:
905
- off_peak_charging_times = OffPeakChargingTimes(all_week=Void)
906
-
907
- return await self._sendInfotainment(
908
- Action(
909
- vehicleAction=VehicleAction(
910
- scheduledDepartureAction=ScheduledDepartureAction(
911
- enabled=enable,
912
- departure_time=departure_time,
913
- preconditioning_times=preconditioning_times,
914
- off_peak_charging_times=off_peak_charging_times,
915
- off_peak_hours_end_time=end_off_peak_time,
916
- )
917
- )
918
- )
919
- )
920
-
921
- async def set_sentry_mode(self, on: bool) -> dict[str, Any]:
922
- """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."""
923
- return await self._sendInfotainment(
924
- Action(
925
- vehicleAction=VehicleAction(
926
- vehicleControlSetSentryModeAction=VehicleControlSetSentryModeAction(
927
- on=on
928
- )
929
- )
930
- )
931
- )
932
-
933
- async def set_temps(
934
- self, driver_temp: float, passenger_temp: float
935
- ) -> dict[str, Any]:
936
- """Sets the driver and/or passenger-side cabin temperature (and other zones if sync is enabled)."""
937
- return await self._sendInfotainment(
938
- Action(
939
- vehicleAction=VehicleAction(
940
- hvacTemperatureAdjustmentAction=HvacTemperatureAdjustmentAction(
941
- driver_temp_celsius=driver_temp,
942
- passenger_temp_celsius=passenger_temp,
943
- )
944
- )
945
- )
946
- )
947
-
948
- async def set_valet_mode(self, on: bool, password: str | int) -> dict[str, Any]:
949
- """Turns on Valet Mode and sets a four-digit passcode that must then be entered to disable Valet Mode."""
950
- return await self._sendInfotainment(
951
- Action(
952
- vehicleAction=VehicleAction(
953
- vehicleControlSetValetModeAction=VehicleControlSetValetModeAction(
954
- on=on, password=str(password)
955
- )
956
- )
957
- )
958
- )
959
-
960
- async def set_vehicle_name(self, vehicle_name: str) -> dict[str, Any]:
961
- """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."""
962
- return await self._sendInfotainment(
963
- Action(
964
- vehicleAction=VehicleAction(
965
- setVehicleNameAction=SetVehicleNameAction(vehicle_name=vehicle_name)
966
- )
967
- )
968
- )
969
-
970
- async def speed_limit_activate(self, pin: str | int) -> dict[str, Any]:
971
- """Activates Speed Limit Mode with a four-digit PIN."""
972
- return await self._sendInfotainment(
973
- Action(
974
- vehicleAction=VehicleAction(
975
- drivingSpeedLimitAction=DrivingSpeedLimitAction(
976
- activate=True, pin=str(pin)
977
- )
978
- )
979
- )
980
- )
981
-
982
- async def speed_limit_clear_pin(self, pin: str | int) -> dict[str, Any]:
983
- """Deactivates Speed Limit Mode and resets the associated PIN."""
984
- return await self._sendInfotainment(
985
- Action(
986
- vehicleAction=VehicleAction(
987
- drivingClearSpeedLimitPinAction=DrivingClearSpeedLimitPinAction(
988
- pin=str(pin)
989
- )
990
- )
991
- )
992
- )
993
-
994
- # speed_limit_clear_pin_admin doesnt require signing
995
-
996
- async def speed_limit_deactivate(self, pin: str | int) -> dict[str, Any]:
997
- """Deactivates Speed Limit Mode."""
998
- return await self._sendInfotainment(
999
- Action(
1000
- vehicleAction=VehicleAction(
1001
- drivingSpeedLimitAction=DrivingSpeedLimitAction(
1002
- activate=False, pin=str(pin)
1003
- )
1004
- )
1005
- )
1006
- )
1007
-
1008
- async def speed_limit_set_limit(self, limit_mph: int) -> dict[str, Any]:
1009
- """Sets the maximum speed allowed when Speed Limit Mode is active."""
1010
- return await self._sendInfotainment(
1011
- Action(
1012
- vehicleAction=VehicleAction(
1013
- drivingSetSpeedLimitAction=DrivingSetSpeedLimitAction(
1014
- limit_mph=limit_mph
1015
- )
1016
- )
1017
- )
1018
- )
1019
-
1020
- async def sun_roof_control(self, state: str | SunRoofCommand) -> dict[str, Any]:
1021
- """Controls the panoramic sunroof on the Model S."""
1022
- if isinstance(state, SunRoofCommand):
1023
- state = state.value
1024
- action = VehicleControlSunroofOpenCloseAction()
1025
- match state:
1026
- case "vent":
1027
- action = VehicleControlSunroofOpenCloseAction(vent=Void())
1028
- case "open":
1029
- action = VehicleControlSunroofOpenCloseAction(open=Void())
1030
- case "close":
1031
- action = VehicleControlSunroofOpenCloseAction(close=Void())
1032
-
1033
- return await self._sendInfotainment(
1034
- Action(
1035
- vehicleAction=VehicleAction(
1036
- vehicleControlSunroofOpenCloseAction=action
1037
- )
1038
- )
1039
- )
1040
-
1041
- # take_drivenote doesnt require signing
1042
-
1043
- async def trigger_homelink(
1044
- self,
1045
- token: str | None = None,
1046
- lat: float | None = None,
1047
- lon: float | None = None,
1048
- ) -> dict[str, Any]:
1049
- """Turns on HomeLink (used to open and close garage doors)."""
1050
- action = VehicleControlTriggerHomelinkAction()
1051
- if lat is not None and lon is not None:
1052
- action.location.latitude = lat
1053
- action.location.longitude = lon
1054
- if token is not None:
1055
- action.token = token
1056
-
1057
- return await self._sendInfotainment(
1058
- Action(
1059
- vehicleAction=VehicleAction(vehicleControlTriggerHomelinkAction=action)
1060
- )
1061
- )
1062
-
1063
- # upcoming_calendar_entries doesnt require signing
1064
-
1065
- async def window_control(
1066
- self,
1067
- command: str | WindowCommand,
1068
- lat: float | None = None,
1069
- lon: float | None = None,
1070
- ) -> dict[str, Any]:
1071
- """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)."""
1072
- if isinstance(command, WindowCommand):
1073
- command = command.value
1074
-
1075
- if command == "vent":
1076
- action = VehicleControlWindowAction(vent=Void())
1077
- elif command == "close":
1078
- action = VehicleControlWindowAction(close=Void())
1079
-
1080
- return await self._sendInfotainment(
1081
- Action(vehicleAction=VehicleAction(vehicleControlWindowAction=action))
1082
- )
1083
-
1084
- # drivers doesnt require signing
1085
- # drivers_remove doesnt require signing
1086
- # mobile_enabled
1087
-
1088
- async def nearby_charging_sites(
1089
- self,
1090
- count: int | None = None,
1091
- radius: int | None = None,
1092
- detail: bool | None = None,
1093
- ) -> dict[str, Any]:
1094
- """Returns the charging sites near the current location of the vehicle."""
1095
- action = GetNearbyChargingSites()
1096
- if count is not None:
1097
- action.count = count
1098
- if radius is not None:
1099
- action.radius = radius
1100
- if detail is not None:
1101
- action.include_meta_data = detail
1102
-
1103
- return await self._sendInfotainment(
1104
- Action(vehicleAction=VehicleAction(getNearbyChargingSites=action))
1105
- )
1106
-
1107
- # options doesnt require signing
1108
- # recent_alerts doesnt require signing
1109
- # release_notes doesnt require signing
1110
- # service_data doesnt require signing
1111
- # share_invites doesnt require signing
1112
- # share_invites_create doesnt require signing
1113
- # share_invites_redeem doesnt require signing
1114
- # share_invites_revoke doesnt require signing
1115
- # signed command doesnt require signing
1116
- # vehicle doesnt require signing
1117
- # vehicle_data doesnt require signing
1118
- # wake_up doesnt require signing
1119
- # warranty_details doesnt require signing
1120
- # fleet_status doesnt require signing
1121
-
1122
- async def fleet_telemetry_config_create(
1123
- self, config: dict[str, Any]
1124
- ) -> dict[str, Any]:
1125
- """Configures fleet telemetry."""
1126
- raise NotImplementedError
1127
-
1128
- # fleet_telemetry_config_get doesnt require signing
1129
- # fleet_telemetry_config_delete doesnt require signing