tesla-fleet-api 0.1.3 → 0.2.2

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.
Files changed (49) hide show
  1. package/dist/commands.d.ts +352 -0
  2. package/dist/commands.js +875 -0
  3. package/dist/commands.js.map +1 -0
  4. package/dist/energy.js +6 -2
  5. package/dist/energy.js.map +1 -1
  6. package/dist/index.d.ts +7 -1
  7. package/dist/index.js +6 -1
  8. package/dist/index.js.map +1 -1
  9. package/dist/signing/crypto.d.ts +10 -0
  10. package/dist/signing/crypto.js +26 -0
  11. package/dist/signing/crypto.js.map +1 -0
  12. package/dist/signing/errors.d.ts +29 -0
  13. package/dist/signing/errors.js +40 -0
  14. package/dist/signing/errors.js.map +1 -0
  15. package/dist/signing/session.d.ts +69 -0
  16. package/dist/signing/session.js +102 -0
  17. package/dist/signing/session.js.map +1 -0
  18. package/dist/tariff.d.ts +29 -0
  19. package/dist/tariff.js +302 -0
  20. package/dist/tariff.js.map +1 -0
  21. package/dist/teslafleetapi.d.ts +7 -0
  22. package/dist/teslafleetapi.js +29 -13
  23. package/dist/teslafleetapi.js.map +1 -1
  24. package/dist/teslemetry.d.ts +4 -0
  25. package/dist/teslemetry.js +15 -1
  26. package/dist/teslemetry.js.map +1 -1
  27. package/dist/types/site_info.d.ts +37 -8
  28. package/dist/vehicle.d.ts +12 -0
  29. package/dist/vehicle.js +12 -0
  30. package/dist/vehicle.js.map +1 -1
  31. package/dist/vehiclesigned.d.ts +13 -0
  32. package/dist/vehiclesigned.js +19 -0
  33. package/dist/vehiclesigned.js.map +1 -0
  34. package/dist/vehiclespecific.d.ts +3 -15
  35. package/dist/vehiclespecific.js.map +1 -1
  36. package/package.json +16 -6
  37. package/src/commands.ts +1134 -0
  38. package/src/energy.ts +197 -135
  39. package/src/index.ts +12 -1
  40. package/src/signing/crypto.ts +29 -0
  41. package/src/signing/errors.ts +50 -0
  42. package/src/signing/session.ts +132 -0
  43. package/src/tariff.ts +375 -0
  44. package/src/teslafleetapi.ts +160 -118
  45. package/src/teslemetry.ts +39 -24
  46. package/src/types/site_info.ts +38 -8
  47. package/src/vehicle.ts +15 -0
  48. package/src/vehiclesigned.ts +20 -0
  49. package/src/vehiclespecific.ts +811 -721
@@ -0,0 +1,352 @@
1
+ import { RoutableMessage } from "@teslemetry/tesla-protocol/command/universal_message";
2
+ import { VehicleStatus } from "@teslemetry/tesla-protocol/command/vcsec";
3
+ import { ClimateMode, CommandResponse, Level, Seat, Trunk } from "./types/commands.js";
4
+ import Vehicle from "./vehicle.js";
5
+ import VehicleSpecific from "./vehiclespecific.js";
6
+ /**
7
+ * Transport-agnostic signed-command builder for the Tesla Vehicle Command
8
+ * Protocol: mirrors `python-tesla-fleet-api`'s `Commands` ABC. Every method
9
+ * here builds a `RoutableMessage`, signs it (HMAC-personalized), and
10
+ * dispatches it through the single abstract seam `_send`. A concrete
11
+ * subclass fills that seam for one transport - the cloud `/signed_command`
12
+ * HTTP endpoint (`VehicleSigned`) today, a BLE GATT link in a future story.
13
+ *
14
+ * AES-GCM personalized signing (the BLE-only auth mode) is intentionally out
15
+ * of scope here; see AGENTS.md.
16
+ */
17
+ export default abstract class Commands extends VehicleSpecific {
18
+ #private;
19
+ /** Delay between WAIT-retries; overridable so tests don't have to wait for real timers. */
20
+ protected retryDelayMs: number;
21
+ private readonly fromDestination;
22
+ private readonly sessions;
23
+ constructor(parent: Vehicle, vin: string, privateKey?: Buffer, publicKey?: Buffer);
24
+ /**
25
+ * Transmit a signed (or session-handshake) `RoutableMessage` to the
26
+ * vehicle and return its reply. Implementations must call
27
+ * `validateAndUpdateSession(resp, msg.uuid)` on the reply before
28
+ * returning it.
29
+ */
30
+ protected abstract _send(msg: RoutableMessage, requires: string, expectsData?: boolean, confirmBroadcast?: (status: VehicleStatus) => boolean): Promise<RoutableMessage>;
31
+ private sessionFor;
32
+ /**
33
+ * Absorb session info piggy-backed on any signed-command reply and raise
34
+ * on a whitelist rejection or a terminal message fault. Every concrete
35
+ * `_send` must call this on the reply - passing the `uuid` of the request
36
+ * it just sent - before returning it.
37
+ *
38
+ * A `SessionInfo` is untrusted wire data until its `session_info_tag` is
39
+ * verified: the tag is an HMAC over the exact bytes received, keyed by a
40
+ * key derived from the *candidate* vehicle public key carried inside that
41
+ * same SessionInfo, and bound to this request's `uuid` as a challenge (so
42
+ * a captured older reply can't be replayed against a newer request). Only
43
+ * once that tag checks out do we act on anything the message claims -
44
+ * including its own whitelist-status field - and even then `Session.commit`
45
+ * still refuses a clock time that regresses within the same epoch.
46
+ */
47
+ protected validateAndUpdateSession(resp: RoutableMessage, requestUuid: Buffer): void;
48
+ private handshake;
49
+ private buildHmacMessage;
50
+ /**
51
+ * Build, sign, dispatch (with WAIT/epoch-fault retry), and interpret one
52
+ * domain command.
53
+ *
54
+ * VCSEC requires messages to arrive in strict counter order and warns
55
+ * clients to avoid simultaneous requests to it at all (unlike
56
+ * Infotainment, which tolerates a sliding window) - so for
57
+ * `DOMAIN_VEHICLE_SECURITY` the *entire* dispatch (handshake, build,
58
+ * send, and any retry) runs under the domain's lock, guaranteeing at
59
+ * most one VCSEC message is ever in flight. Infotainment keeps the
60
+ * narrower build-only lock, serializing only counter assignment.
61
+ */
62
+ private dispatch;
63
+ private interpretResponse;
64
+ private interpretVcsecResponse;
65
+ private interpretInfotainmentResponse;
66
+ private sendVehicleSecurity;
67
+ private sendInfotainment;
68
+ private vehicleAction;
69
+ private resolveSeat;
70
+ /**
71
+ * Controls the front (which_trunk: "front") or rear (which_trunk: "rear") trunk.
72
+ * @param which_trunk "front" or "rear"
73
+ */
74
+ actuate_truck(which_trunk: Trunk): Promise<CommandResponse>;
75
+ /**
76
+ * Adjusts vehicle media playback volume.
77
+ * @param volume A floating point number from 0.0 to 11.0.
78
+ */
79
+ adjust_volume(volume: number): Promise<CommandResponse>;
80
+ /** Starts climate preconditioning. */
81
+ auto_conditioning_start(): Promise<CommandResponse>;
82
+ /** Stops climate preconditioning. */
83
+ auto_conditioning_stop(): Promise<CommandResponse>;
84
+ /** Cancels the countdown to install the vehicle software update. */
85
+ cancel_software_update(): Promise<CommandResponse>;
86
+ /** Charges in max range mode -- we recommend limiting the use of this mode to long trips. */
87
+ charge_max_range(): Promise<CommandResponse>;
88
+ /** Closes the charge port door. */
89
+ charge_port_door_close(): Promise<CommandResponse>;
90
+ /** Opens the charge port door. */
91
+ charge_port_door_open(): Promise<CommandResponse>;
92
+ /** Charges in Standard mode. */
93
+ charge_standard(): Promise<CommandResponse>;
94
+ /** Starts charging the vehicle. */
95
+ charge_start(): Promise<CommandResponse>;
96
+ /** Stops charging the vehicle. */
97
+ charge_stop(): Promise<CommandResponse>;
98
+ /**
99
+ * 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.
100
+ */
101
+ clear_pin_to_drive_admin(): Promise<CommandResponse>;
102
+ /** Locks the vehicle. */
103
+ door_lock(): Promise<CommandResponse>;
104
+ /** Unlocks the vehicle. */
105
+ door_unlock(): Promise<CommandResponse>;
106
+ /** Erases user's data from the user interface. Requires the vehicle to be in park. */
107
+ erase_user_data(): Promise<CommandResponse>;
108
+ /** Briefly flashes the vehicle headlights. Requires the vehicle to be in park. */
109
+ flash_lights(): Promise<CommandResponse>;
110
+ /**
111
+ * Restricts certain vehicle UI functionality from guest users
112
+ * @param enable `true` or `false`
113
+ */
114
+ guest_mode(enable: boolean): Promise<CommandResponse>;
115
+ /** Honks the vehicle horn. Requires the vehicle to be in park. */
116
+ honk_horn(): Promise<CommandResponse>;
117
+ /** Advances media player to next favorite track. */
118
+ media_next_fav(): Promise<CommandResponse>;
119
+ /** Advances media player to next track. */
120
+ media_next_track(): Promise<CommandResponse>;
121
+ /** Advances media player to previous favorite track. */
122
+ media_prev_fav(): Promise<CommandResponse>;
123
+ /** Advances media player to previous track. */
124
+ media_prev_track(): Promise<CommandResponse>;
125
+ /** Toggles current play/pause state. */
126
+ media_toggle_playback(): Promise<CommandResponse>;
127
+ /** Turns the volume down by one. */
128
+ media_volume_down(): Promise<CommandResponse>;
129
+ /**
130
+ * Start navigation to given coordinates. Order can be used to specify order of multiple stops.
131
+ * @param lat Latitude
132
+ * @param lon Longitude
133
+ * @param order Order of multiple stops
134
+ */
135
+ navigation_gps_request(lat: number, lon: number, order?: number): Promise<CommandResponse>;
136
+ /**
137
+ * Sends a location to the in-vehicle navigation system.
138
+ *
139
+ * `locale` and `type` are REST-only fields with no equivalent in the
140
+ * signed-command protocol (which only carries a destination string and a
141
+ * multi-stop order), so they are accepted for cross-transport signature
142
+ * parity but not sent.
143
+ * @param value Destination text
144
+ */
145
+ navigation_request(value: string, _locale?: string, _type?: string): Promise<CommandResponse>;
146
+ /**
147
+ * Start navigation to a supercharger.
148
+ *
149
+ * The signed-command protobuf has a single `order` field carrying the
150
+ * supercharger identifier; a caller-supplied multi-stop `order` has no
151
+ * representable slot and is rejected rather than silently dropped.
152
+ * @param id Supercharger ID
153
+ * @param order Order of multiple stops - not representable in a signed command
154
+ */
155
+ navigation_sc_request(id: number, order?: number): Promise<CommandResponse>;
156
+ /**
157
+ * Sets automatic seat heating and cooling. Only the front seats support this action.
158
+ * @param auto_seat_position Seat position
159
+ * @param auto_climate_on `true` or `false`
160
+ */
161
+ remote_auto_seat_climate_request(auto_seat_position: Seat | number, auto_climate_on: boolean): Promise<CommandResponse>;
162
+ /**
163
+ * Sets automatic steering wheel heating on/off.
164
+ * @param on `true` or `false`
165
+ */
166
+ remote_auto_steering_wheel_heat_climate_request(on: boolean): Promise<CommandResponse>;
167
+ /**
168
+ * Plays a sound through the vehicle external speaker.
169
+ * @param sound Fart is `0`, Locate ping is `2000`
170
+ */
171
+ remote_boombox(sound?: number): Promise<CommandResponse>;
172
+ /**
173
+ * Sets seat cooling. Only the front seats support this action.
174
+ * @param seat_position Seat position
175
+ * @param seat_cooler_level Cooling level
176
+ */
177
+ remote_seat_cooler_request(seat_position: Seat | number, seat_cooler_level: Level): Promise<CommandResponse>;
178
+ /**
179
+ * Sets seat heating.
180
+ * @param seat_position Seat position
181
+ * @param seat_heater_level Heating level
182
+ */
183
+ remote_seat_heater_request(seat_position: Seat | number, seat_heater_level: Level): Promise<CommandResponse>;
184
+ /** Starts the vehicle remotely. Requires keyless driving to be enabled. */
185
+ remote_start_drive(): Promise<CommandResponse>;
186
+ /**
187
+ * Sets steering wheel heat level.
188
+ * @param level Heating level (0=Off, 1=Low, 2=High)
189
+ */
190
+ remote_steering_wheel_heat_level_request(level: Level): Promise<CommandResponse>;
191
+ /**
192
+ * Sets steering wheel heating on/off. For vehicles that do not support auto steering wheel heat.
193
+ * @param on `true` or `false`
194
+ */
195
+ remote_steering_wheel_heater_request(on: boolean): Promise<CommandResponse>;
196
+ /** Removes PIN to Drive. Requires the car to be in Pin to Drive mode and not in Valet mode. */
197
+ reset_pin_to_drive_pin(): Promise<CommandResponse>;
198
+ /** Removes PIN for Valet Mode. */
199
+ reset_valet_pin(): Promise<CommandResponse>;
200
+ /**
201
+ * Schedules a vehicle software update (over the air "OTA") to be installed in the future.
202
+ * @param offset_sec Offset in seconds
203
+ */
204
+ schedule_software_update(offset_sec: number): Promise<CommandResponse>;
205
+ /**
206
+ * Turns Bioweapon Defense Mode on and off.
207
+ * @param on `true` or `false`
208
+ * @param manual_override `true` or `false`
209
+ */
210
+ set_bioweapon_mode(on: boolean, manual_override: boolean): Promise<CommandResponse>;
211
+ /**
212
+ * Sets the vehicle overheat protection.
213
+ * @param on `true` or `false`
214
+ * @param fan_only `true` or `false`
215
+ */
216
+ set_cabin_overheat_protection(on: boolean, fan_only: boolean): Promise<CommandResponse>;
217
+ /**
218
+ * Sets the vehicle's charge limit to a custom percentage.
219
+ * @param percent Charge limit percentage typically between 50-100
220
+ */
221
+ set_charge_limit(percent: number): Promise<CommandResponse>;
222
+ /**
223
+ * Sets the charging amps.
224
+ * @param charging_amps Current limit in Amps typically between 5-32
225
+ */
226
+ set_charging_amps(charging_amps: number): Promise<CommandResponse>;
227
+ /**
228
+ * Enables climate keeper mode.
229
+ * @param climate_keeper_mode `0` is Off, `1` is Keep, `2` is Dog, `3` is Camp
230
+ */
231
+ set_climate_keeper_mode(climate_keeper_mode: number | ClimateMode): Promise<CommandResponse>;
232
+ /**
233
+ * Adjusts the Cabin Overheat Protection temperature (COP).
234
+ * @param cop_temp Integer representing Low (0/30C/90F), Medium (1/35C/95F), or High (2/40C/100F)
235
+ */
236
+ set_cop_temp(cop_temp: 0 | 1 | 2 | 30 | 35 | 40 | 90 | 95 | 100): Promise<CommandResponse>;
237
+ /**
238
+ * Sets a four-digit passcode for PIN to Drive.
239
+ * @param on `true` or `false`
240
+ * @param password Four-digit passcode
241
+ */
242
+ set_pin_to_drive(on: boolean, password: string | number): Promise<CommandResponse>;
243
+ /**
244
+ * Sets an override for preconditioning — it should default to empty if no override is used.
245
+ * @param on `true` or `false`
246
+ * @param manual_override `true` or `false`
247
+ */
248
+ set_preconditioning_max(on: boolean, manual_override: boolean): Promise<CommandResponse>;
249
+ /**
250
+ * Sets a time at which charging should be completed.
251
+ * @param enable `true` or `false`
252
+ * @param time Minutes after midnight
253
+ */
254
+ set_scheduled_charging(enable: boolean, time: number): Promise<CommandResponse>;
255
+ /**
256
+ * Sets a time at which departure should be completed.
257
+ *
258
+ * `preconditioning_enabled` and `off_peak_charging_enabled` are accepted
259
+ * for cross-transport signature parity but have no field in the signed
260
+ * command protobuf, so they are not sent.
261
+ * @param enable `true` or `false`
262
+ * @param preconditioning_enabled not representable in a signed command
263
+ * @param preconditioning_weekdays_only `true` or `false`
264
+ * @param departure_time Minutes after midnight
265
+ * @param off_peak_charging_enabled not representable in a signed command
266
+ * @param off_peak_charging_weekdays_only `true` or `false`
267
+ * @param end_off_peak_time Minutes after midnight
268
+ */
269
+ set_scheduled_departure(enable?: boolean, _preconditioning_enabled?: boolean, preconditioning_weekdays_only?: boolean, departure_time?: number, _off_peak_charging_enabled?: boolean, off_peak_charging_weekdays_only?: boolean, end_off_peak_time?: number): Promise<CommandResponse>;
270
+ /**
271
+ * Enables and disables Sentry Mode.
272
+ * @param on `true` or `false`
273
+ */
274
+ set_sentry_mode(on: boolean): Promise<CommandResponse>;
275
+ /**
276
+ * Sets the driver and/or passenger-side cabin temperature (and other zones if sync is enabled).
277
+ * @param driver_temp Driver temperature
278
+ * @param passenger_temp Passenger temperature
279
+ */
280
+ set_temps(driver_temp: number, passenger_temp: number): Promise<CommandResponse>;
281
+ /**
282
+ * Turns on Valet Mode and sets a four-digit passcode.
283
+ * @param on `true` or `false`
284
+ * @param password Four-digit passcode
285
+ */
286
+ set_valet_mode(on: boolean, password: string | number): Promise<CommandResponse>;
287
+ /**
288
+ * Changes the name of a vehicle.
289
+ * @param vehicle_name New name
290
+ */
291
+ set_vehicle_name(vehicle_name: string): Promise<CommandResponse>;
292
+ /**
293
+ * Activates Speed Limit Mode with a four-digit PIN.
294
+ * @param pin Four-digit PIN
295
+ */
296
+ speed_limit_activate(pin: string | number): Promise<CommandResponse>;
297
+ /**
298
+ * Deactivates Speed Limit Mode and resets the associated PIN.
299
+ * @param pin Four-digit PIN
300
+ */
301
+ speed_limit_clear_pin(pin: string | number): Promise<CommandResponse>;
302
+ /** Deactivates Speed Limit Mode and resets the associated PIN, for fleet managers or owners. */
303
+ speed_limit_clear_pin_admin(): Promise<CommandResponse>;
304
+ /**
305
+ * Deactivates Speed Limit Mode.
306
+ * @param pin Four-digit PIN
307
+ */
308
+ speed_limit_deactivate(pin: string | number): Promise<CommandResponse>;
309
+ /**
310
+ * Sets the maximum speed allowed when Speed Limit Mode is active.
311
+ * @param limit_mph Maximum speed in MPH
312
+ */
313
+ speed_limit_set_limit(limit_mph: number): Promise<CommandResponse>;
314
+ /**
315
+ * Controls the panoramic sunroof on the Model S.
316
+ *
317
+ * "stop" has no corresponding field in the signed-command protobuf (only
318
+ * vent/close/open), so it is rejected rather than guessed at.
319
+ * @param state "close" or "vent"
320
+ */
321
+ sun_roof_control(state: "stop" | "close" | "vent"): Promise<CommandResponse>;
322
+ /**
323
+ * Records a drive note. Overrides the inherited plaintext command so it is
324
+ * signed and dispatched over the vehicle-command protocol rather than
325
+ * silently falling through to the unsigned REST endpoint on vehicles that
326
+ * require signing.
327
+ * @param note Drive note
328
+ */
329
+ take_drivenote(note: string): Promise<CommandResponse>;
330
+ /**
331
+ * Turns on HomeLink (used to open and close garage doors).
332
+ * @param lat Latitude
333
+ * @param lon Longitude
334
+ * @param token
335
+ */
336
+ trigger_homelink(lat: number, lon: number, token?: string): Promise<CommandResponse>;
337
+ /**
338
+ * Sends upcoming calendar entries to the vehicle. Overrides the inherited
339
+ * plaintext command so it is signed and dispatched over the vehicle-command
340
+ * protocol rather than silently falling through to the unsigned REST
341
+ * endpoint on vehicles that require signing.
342
+ * @param calendar_data Calendar data
343
+ */
344
+ upcoming_calendar_entries(calendar_data: string): Promise<CommandResponse>;
345
+ /**
346
+ * Control the windows of a parked vehicle. Supported commands: vent and close.
347
+ * @param command "vent" or "close"
348
+ * @param lat Latitude
349
+ * @param lon Longitude
350
+ */
351
+ window_control(command: "vent" | "close", _lat?: number, _lon?: number): Promise<CommandResponse>;
352
+ }