tesla-fleet-api 0.2.2 → 0.4.0

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 (192) hide show
  1. package/dist/_virtual/_rolldown/runtime.cjs +22 -0
  2. package/dist/_virtual/_rolldown/runtime.mjs +12 -0
  3. package/dist/charging.cjs +53 -0
  4. package/dist/charging.d.cts +31 -0
  5. package/dist/charging.d.mts +30 -0
  6. package/dist/charging.mjs +53 -0
  7. package/dist/commands.cjs +802 -0
  8. package/dist/commands.d.cts +350 -0
  9. package/dist/commands.d.mts +349 -0
  10. package/dist/commands.mjs +800 -0
  11. package/dist/const.cjs +300 -0
  12. package/dist/const.d.cts +287 -0
  13. package/dist/const.d.mts +287 -0
  14. package/dist/const.mjs +263 -0
  15. package/dist/energy.cjs +137 -0
  16. package/dist/energy.d.cts +95 -0
  17. package/{src/energy.ts → dist/energy.d.mts} +17 -140
  18. package/dist/energy.mjs +137 -0
  19. package/dist/energyspecific.cjs +99 -0
  20. package/dist/energyspecific.d.cts +78 -0
  21. package/dist/energyspecific.d.mts +77 -0
  22. package/dist/energyspecific.mjs +99 -0
  23. package/dist/exceptions.cjs +1487 -0
  24. package/dist/exceptions.d.cts +1142 -0
  25. package/dist/exceptions.d.mts +1142 -0
  26. package/dist/exceptions.mjs +1324 -0
  27. package/dist/index.cjs +226 -0
  28. package/dist/index.d.cts +14 -0
  29. package/dist/index.d.mts +14 -0
  30. package/dist/index.mjs +14 -0
  31. package/dist/partner.cjs +32 -0
  32. package/dist/partner.d.cts +24 -0
  33. package/dist/partner.d.mts +23 -0
  34. package/dist/partner.mjs +32 -0
  35. package/dist/router.cjs +0 -0
  36. package/dist/router.d.cts +1 -0
  37. package/dist/router.d.mts +1 -0
  38. package/dist/router.mjs +1 -0
  39. package/dist/signing/crypto.cjs +31 -0
  40. package/dist/signing/crypto.mjs +27 -0
  41. package/dist/signing/session.cjs +112 -0
  42. package/dist/signing/session.d.cts +69 -0
  43. package/dist/signing/session.d.mts +69 -0
  44. package/dist/signing/session.mjs +110 -0
  45. package/dist/tariff.cjs +292 -0
  46. package/dist/{tariff.d.ts → tariff.d.cts} +17 -17
  47. package/dist/tariff.d.mts +29 -0
  48. package/dist/tariff.mjs +291 -0
  49. package/dist/teslafleetapi.cjs +125 -0
  50. package/dist/teslafleetapi.d.cts +64 -0
  51. package/dist/teslafleetapi.d.mts +64 -0
  52. package/dist/teslafleetapi.mjs +125 -0
  53. package/dist/teslemetry.cjs +36 -0
  54. package/dist/teslemetry.d.cts +23 -0
  55. package/dist/teslemetry.d.mts +23 -0
  56. package/dist/teslemetry.mjs +36 -0
  57. package/dist/tessie.cjs +34 -0
  58. package/dist/tessie.d.cts +23 -0
  59. package/dist/tessie.d.mts +23 -0
  60. package/dist/tessie.mjs +34 -0
  61. package/dist/types/commands.d.cts +28 -0
  62. package/dist/types/commands.d.mts +28 -0
  63. package/dist/types/live_status.d.cts +30 -0
  64. package/dist/types/live_status.d.mts +30 -0
  65. package/dist/types/products.d.cts +58 -0
  66. package/dist/types/products.d.mts +58 -0
  67. package/dist/types/responses.d.cts +9 -0
  68. package/dist/types/responses.d.mts +9 -0
  69. package/dist/types/site_info.d.cts +154 -0
  70. package/dist/types/site_info.d.mts +154 -0
  71. package/dist/types/vehicle.d.cts +27 -0
  72. package/dist/types/vehicle.d.mts +27 -0
  73. package/dist/types/vehicle_data.d.cts +278 -0
  74. package/dist/types/vehicle_data.d.mts +278 -0
  75. package/dist/types.cjs +0 -0
  76. package/dist/types.d.cts +9 -0
  77. package/dist/types.d.mts +9 -0
  78. package/dist/types.mjs +1 -0
  79. package/dist/user.cjs +28 -0
  80. package/dist/user.d.cts +17 -0
  81. package/dist/user.d.mts +16 -0
  82. package/dist/user.mjs +28 -0
  83. package/dist/vehicle.cjs +804 -0
  84. package/dist/vehicle.d.cts +550 -0
  85. package/dist/vehicle.d.mts +549 -0
  86. package/dist/vehicle.mjs +804 -0
  87. package/dist/vehiclesigned.cjs +20 -0
  88. package/dist/vehiclesigned.d.cts +14 -0
  89. package/dist/{vehiclesigned.d.ts → vehiclesigned.d.mts} +3 -3
  90. package/dist/vehiclesigned.mjs +20 -0
  91. package/dist/vehiclespecific.cjs +604 -0
  92. package/dist/vehiclespecific.d.cts +433 -0
  93. package/dist/vehiclespecific.d.mts +432 -0
  94. package/dist/vehiclespecific.mjs +604 -0
  95. package/package.json +108 -35
  96. package/dist/charging.d.ts +0 -30
  97. package/dist/charging.js +0 -51
  98. package/dist/charging.js.map +0 -1
  99. package/dist/commands.d.ts +0 -352
  100. package/dist/commands.js +0 -875
  101. package/dist/commands.js.map +0 -1
  102. package/dist/energy.d.ts +0 -94
  103. package/dist/energy.js +0 -125
  104. package/dist/energy.js.map +0 -1
  105. package/dist/energyspecific.d.ts +0 -77
  106. package/dist/energyspecific.js +0 -97
  107. package/dist/energyspecific.js.map +0 -1
  108. package/dist/helpers.d.ts +0 -2
  109. package/dist/helpers.js +0 -10
  110. package/dist/helpers.js.map +0 -1
  111. package/dist/index.d.ts +0 -14
  112. package/dist/index.js +0 -14
  113. package/dist/index.js.map +0 -1
  114. package/dist/partner.d.ts +0 -23
  115. package/dist/partner.js +0 -31
  116. package/dist/partner.js.map +0 -1
  117. package/dist/signing/crypto.d.ts +0 -10
  118. package/dist/signing/crypto.js +0 -26
  119. package/dist/signing/crypto.js.map +0 -1
  120. package/dist/signing/errors.d.ts +0 -29
  121. package/dist/signing/errors.js +0 -40
  122. package/dist/signing/errors.js.map +0 -1
  123. package/dist/signing/session.d.ts +0 -69
  124. package/dist/signing/session.js +0 -102
  125. package/dist/signing/session.js.map +0 -1
  126. package/dist/tariff.js +0 -302
  127. package/dist/tariff.js.map +0 -1
  128. package/dist/teslafleetapi.d.ts +0 -58
  129. package/dist/teslafleetapi.js +0 -120
  130. package/dist/teslafleetapi.js.map +0 -1
  131. package/dist/teslemetry.d.ts +0 -29
  132. package/dist/teslemetry.js +0 -39
  133. package/dist/teslemetry.js.map +0 -1
  134. package/dist/tessie.d.ts +0 -23
  135. package/dist/tessie.js +0 -28
  136. package/dist/tessie.js.map +0 -1
  137. package/dist/types/commands.d.ts +0 -44
  138. package/dist/types/commands.js +0 -11
  139. package/dist/types/commands.js.map +0 -1
  140. package/dist/types/live_status.d.ts +0 -26
  141. package/dist/types/live_status.js +0 -2
  142. package/dist/types/live_status.js.map +0 -1
  143. package/dist/types/products.d.ts +0 -54
  144. package/dist/types/products.js +0 -2
  145. package/dist/types/products.js.map +0 -1
  146. package/dist/types/responses.d.ts +0 -5
  147. package/dist/types/responses.js +0 -2
  148. package/dist/types/responses.js.map +0 -1
  149. package/dist/types/site_info.d.ts +0 -150
  150. package/dist/types/site_info.js +0 -2
  151. package/dist/types/site_info.js.map +0 -1
  152. package/dist/types/vehicle.d.ts +0 -22
  153. package/dist/types/vehicle.js +0 -2
  154. package/dist/types/vehicle.js.map +0 -1
  155. package/dist/types/vehicle_data.d.ts +0 -274
  156. package/dist/types/vehicle_data.js +0 -2
  157. package/dist/types/vehicle_data.js.map +0 -1
  158. package/dist/user.d.ts +0 -16
  159. package/dist/user.js +0 -26
  160. package/dist/user.js.map +0 -1
  161. package/dist/vehicle.d.ts +0 -548
  162. package/dist/vehicle.js +0 -781
  163. package/dist/vehicle.js.map +0 -1
  164. package/dist/vehiclesigned.js +0 -19
  165. package/dist/vehiclesigned.js.map +0 -1
  166. package/dist/vehiclespecific.d.ts +0 -432
  167. package/dist/vehiclespecific.js +0 -601
  168. package/dist/vehiclespecific.js.map +0 -1
  169. package/src/charging.ts +0 -66
  170. package/src/commands.ts +0 -1134
  171. package/src/energyspecific.ts +0 -116
  172. package/src/helpers.ts +0 -11
  173. package/src/index.ts +0 -20
  174. package/src/partner.ts +0 -37
  175. package/src/signing/crypto.ts +0 -29
  176. package/src/signing/errors.ts +0 -50
  177. package/src/signing/session.ts +0 -132
  178. package/src/tariff.ts +0 -375
  179. package/src/teslafleetapi.ts +0 -182
  180. package/src/teslemetry.ts +0 -54
  181. package/src/tessie.ts +0 -42
  182. package/src/types/commands.ts +0 -62
  183. package/src/types/live_status.ts +0 -27
  184. package/src/types/products.ts +0 -61
  185. package/src/types/responses.ts +0 -1
  186. package/src/types/site_info.ts +0 -159
  187. package/src/types/vehicle.ts +0 -23
  188. package/src/types/vehicle_data.ts +0 -286
  189. package/src/user.ts +0 -35
  190. package/src/vehicle.ts +0 -895
  191. package/src/vehiclesigned.ts +0 -20
  192. package/src/vehiclespecific.ts +0 -818
@@ -0,0 +1,802 @@
1
+ const require_runtime = require("./_virtual/_rolldown/runtime.cjs");
2
+ const require_const = require("./const.cjs");
3
+ const require_vehiclespecific = require("./vehiclespecific.cjs");
4
+ const require_session = require("./signing/session.cjs");
5
+ const require_crypto = require("./signing/crypto.cjs");
6
+ const require_exceptions = require("./exceptions.cjs");
7
+ let crypto = require("crypto");
8
+ crypto = require_runtime.__toESM(crypto, 1);
9
+ let _teslemetry_tesla_protocol_command_universal_message = require("@teslemetry/tesla-protocol/command/universal_message");
10
+ let _teslemetry_tesla_protocol_command_signatures = require("@teslemetry/tesla-protocol/command/signatures");
11
+ let _teslemetry_tesla_protocol_command_vcsec = require("@teslemetry/tesla-protocol/command/vcsec");
12
+ let _teslemetry_tesla_protocol_command_errors = require("@teslemetry/tesla-protocol/command/errors");
13
+ let _teslemetry_tesla_protocol_command_car_server = require("@teslemetry/tesla-protocol/command/car_server");
14
+ let _teslemetry_tesla_protocol_command_common = require("@teslemetry/tesla-protocol/command/common");
15
+ let _teslemetry_tesla_protocol_command_vehicle = require("@teslemetry/tesla-protocol/command/vehicle");
16
+ // src/commands.ts
17
+ const VOID = {};
18
+ /** Maximum HMAC-personalized (re)send attempts for one logical command, matching the reference implementation. */
19
+ const MAX_ATTEMPTS = 4;
20
+ const TrunkFields = {
21
+ front: "frontTrunk",
22
+ rear: "rearTrunk"
23
+ };
24
+ /** Node's REST `Seats` values mapped onto the seat-heater oneof field they select. */
25
+ const SeatHeaterFields = {
26
+ [require_const.Seats.FRONT_LEFT]: "carSeatFrontLeft",
27
+ [require_const.Seats.FRONT_RIGHT]: "carSeatFrontRight",
28
+ [require_const.Seats.REAR_LEFT]: "carSeatRearLeft",
29
+ [require_const.Seats.REAR_LEFT_BACK]: "carSeatRearLeftBack",
30
+ [require_const.Seats.REAR_CENTER]: "carSeatRearCenter",
31
+ [require_const.Seats.REAR_RIGHT]: "carSeatRearRight",
32
+ [require_const.Seats.REAR_RIGHT_BACK]: "carSeatRearRightBack",
33
+ [require_const.Seats.THIRD_LEFT]: "carSeatThirdRowLeft",
34
+ [require_const.Seats.THIRD_RIGHT]: "carSeatThirdRowRight"
35
+ };
36
+ const SeatHeaterLevels = [
37
+ "seatHeaterOff",
38
+ "seatHeaterLow",
39
+ "seatHeaterMed",
40
+ "seatHeaterHigh"
41
+ ];
42
+ /** Seat cooling and auto-climate only cover the front seats on the wire protocol. */
43
+ const SeatCoolerPositions = {
44
+ [require_const.Seats.FRONT_LEFT]: _teslemetry_tesla_protocol_command_car_server.HvacSeatCoolerActions_HvacSeatCoolerPositionE.HvacSeatCoolerPosition_FrontLeft,
45
+ [require_const.Seats.FRONT_RIGHT]: _teslemetry_tesla_protocol_command_car_server.HvacSeatCoolerActions_HvacSeatCoolerPositionE.HvacSeatCoolerPosition_FrontRight
46
+ };
47
+ const SeatCoolerLevels = [
48
+ _teslemetry_tesla_protocol_command_car_server.HvacSeatCoolerActions_HvacSeatCoolerLevelE.HvacSeatCoolerLevel_Off,
49
+ _teslemetry_tesla_protocol_command_car_server.HvacSeatCoolerActions_HvacSeatCoolerLevelE.HvacSeatCoolerLevel_Low,
50
+ _teslemetry_tesla_protocol_command_car_server.HvacSeatCoolerActions_HvacSeatCoolerLevelE.HvacSeatCoolerLevel_Med,
51
+ _teslemetry_tesla_protocol_command_car_server.HvacSeatCoolerActions_HvacSeatCoolerLevelE.HvacSeatCoolerLevel_High
52
+ ];
53
+ /** Keyed by the 1-indexed `AutoSeat` REST value, which equals the proto enum value. */
54
+ const AutoSeatPositions = {
55
+ [require_const.AutoSeat.FRONT_LEFT]: _teslemetry_tesla_protocol_command_car_server.AutoSeatClimateAction_AutoSeatPositionE.AutoSeatPosition_FrontLeft,
56
+ [require_const.AutoSeat.FRONT_RIGHT]: _teslemetry_tesla_protocol_command_car_server.AutoSeatClimateAction_AutoSeatPositionE.AutoSeatPosition_FrontRight
57
+ };
58
+ const StwHeatLevels = [
59
+ _teslemetry_tesla_protocol_command_common.StwHeatLevel.StwHeatLevel_Off,
60
+ _teslemetry_tesla_protocol_command_common.StwHeatLevel.StwHeatLevel_Low,
61
+ _teslemetry_tesla_protocol_command_common.StwHeatLevel.StwHeatLevel_High
62
+ ];
63
+ const ClimateKeeperActions = {
64
+ [require_const.ClimateModes.Off]: _teslemetry_tesla_protocol_command_car_server.HvacClimateKeeperAction_ClimateKeeperActionE.ClimateKeeperAction_Off,
65
+ [require_const.ClimateModes.Keep]: _teslemetry_tesla_protocol_command_car_server.HvacClimateKeeperAction_ClimateKeeperActionE.ClimateKeeperAction_On,
66
+ [require_const.ClimateModes.Dog]: _teslemetry_tesla_protocol_command_car_server.HvacClimateKeeperAction_ClimateKeeperActionE.ClimateKeeperAction_Dog,
67
+ [require_const.ClimateModes.Camp]: _teslemetry_tesla_protocol_command_car_server.HvacClimateKeeperAction_ClimateKeeperActionE.ClimateKeeperAction_Camp
68
+ };
69
+ /** `set_cop_temp` accepts either the enum index or the Celsius/Fahrenheit value it represents. */
70
+ const CopActivationTemps = {
71
+ 0: _teslemetry_tesla_protocol_command_vehicle.ClimateState_CopActivationTemp.CopActivationTempLow,
72
+ 30: _teslemetry_tesla_protocol_command_vehicle.ClimateState_CopActivationTemp.CopActivationTempLow,
73
+ 90: _teslemetry_tesla_protocol_command_vehicle.ClimateState_CopActivationTemp.CopActivationTempLow,
74
+ 1: _teslemetry_tesla_protocol_command_vehicle.ClimateState_CopActivationTemp.CopActivationTempMedium,
75
+ 35: _teslemetry_tesla_protocol_command_vehicle.ClimateState_CopActivationTemp.CopActivationTempMedium,
76
+ 95: _teslemetry_tesla_protocol_command_vehicle.ClimateState_CopActivationTemp.CopActivationTempMedium,
77
+ 2: _teslemetry_tesla_protocol_command_vehicle.ClimateState_CopActivationTemp.CopActivationTempHigh,
78
+ 40: _teslemetry_tesla_protocol_command_vehicle.ClimateState_CopActivationTemp.CopActivationTempHigh,
79
+ 100: _teslemetry_tesla_protocol_command_vehicle.ClimateState_CopActivationTemp.CopActivationTempHigh
80
+ };
81
+ function sleep(ms) {
82
+ return new Promise((resolve) => setTimeout(resolve, ms));
83
+ }
84
+ function ok(reason = "") {
85
+ return { response: {
86
+ result: true,
87
+ reason
88
+ } };
89
+ }
90
+ function fail(reason) {
91
+ return { response: {
92
+ result: false,
93
+ reason
94
+ } };
95
+ }
96
+ /**
97
+ * Transport-agnostic signed-command builder for the Tesla Vehicle Command
98
+ * Protocol: mirrors `python-tesla-fleet-api`'s `Commands` ABC. Every method
99
+ * here builds a `RoutableMessage`, signs it (HMAC-personalized), and
100
+ * dispatches it through the single abstract seam `_send`. A concrete
101
+ * subclass fills that seam for one transport - the cloud `/signed_command`
102
+ * HTTP endpoint (`VehicleSigned`) today, a BLE GATT link in a future story.
103
+ *
104
+ * AES-GCM personalized signing (the BLE-only auth mode) is intentionally out
105
+ * of scope here; see AGENTS.md.
106
+ */
107
+ var Commands = class extends require_vehiclespecific.default {
108
+ #privateKey;
109
+ #publicKey;
110
+ /** Delay between WAIT-retries; overridable so tests don't have to wait for real timers. */
111
+ retryDelayMs = 2e3;
112
+ fromDestination;
113
+ sessions;
114
+ constructor(parent, vin, privateKey, publicKey) {
115
+ super(parent, vin);
116
+ if (vin.length !== 17) throw new Error(`Signed commands require a 17-character VIN, got ${vin.length} characters.`);
117
+ const resolvedPrivateKey = privateKey ?? parent.private_key;
118
+ if (!resolvedPrivateKey) throw new Error("No private key provided for signed commands.");
119
+ this.#privateKey = resolvedPrivateKey;
120
+ this.#publicKey = publicKey ?? require_crypto.publicKeyFor(this.#privateKey);
121
+ this.fromDestination = crypto.default.randomBytes(16);
122
+ const deriveKey = (vehiclePublicKey) => require_crypto.deriveSharedKey(this.#privateKey, vehiclePublicKey);
123
+ this.sessions = {
124
+ [_teslemetry_tesla_protocol_command_universal_message.Domain.DOMAIN_VEHICLE_SECURITY]: new require_session.Session(_teslemetry_tesla_protocol_command_universal_message.Domain.DOMAIN_VEHICLE_SECURITY, deriveKey),
125
+ [_teslemetry_tesla_protocol_command_universal_message.Domain.DOMAIN_INFOTAINMENT]: new require_session.Session(_teslemetry_tesla_protocol_command_universal_message.Domain.DOMAIN_INFOTAINMENT, deriveKey)
126
+ };
127
+ }
128
+ sessionFor(domain) {
129
+ const session = this.sessions[domain];
130
+ if (!session) throw new Error(`No signed-command session configured for domain ${domain}.`);
131
+ return session;
132
+ }
133
+ /**
134
+ * Absorb session info piggy-backed on any signed-command reply and raise
135
+ * on a whitelist rejection or a terminal message fault. Every concrete
136
+ * `_send` must call this on the reply - passing the `uuid` of the request
137
+ * it just sent - before returning it.
138
+ *
139
+ * A `SessionInfo` is untrusted wire data until its `session_info_tag` is
140
+ * verified: the tag is an HMAC over the exact bytes received, keyed by a
141
+ * key derived from the *candidate* vehicle public key carried inside that
142
+ * same SessionInfo, and bound to this request's `uuid` as a challenge (so
143
+ * a captured older reply can't be replayed against a newer request). Only
144
+ * once that tag checks out do we act on anything the message claims -
145
+ * including its own whitelist-status field - and even then `Session.commit`
146
+ * still refuses a clock time that regresses within the same epoch.
147
+ */
148
+ validateAndUpdateSession(resp, requestUuid) {
149
+ if (resp.sessionInfo && resp.sessionInfo.length > 0) {
150
+ const domain = resp.fromDestination?.domain;
151
+ const session = domain !== void 0 ? this.sessions[domain] : void 0;
152
+ if (!session) throw new require_exceptions.SessionInfoAuthenticationFault("Session info reply is missing a recognized source domain.");
153
+ if (resp.requestUuid && resp.requestUuid.length > 0 && !requestUuid.equals(Buffer.from(resp.requestUuid))) throw new require_exceptions.SessionInfoAuthenticationFault("Session info reply does not match an outstanding request; discarded.");
154
+ const info = _teslemetry_tesla_protocol_command_signatures.SessionInfo.decode(resp.sessionInfo);
155
+ const keys = session.keysFor(Buffer.from(info.publicKey));
156
+ const tag = resp.signatureData?.sessionInfoTag?.tag;
157
+ if (!tag || tag.length === 0) throw new require_exceptions.SessionInfoAuthenticationFault("Session info reply is missing its authentication tag; discarded.");
158
+ const metadata = Buffer.concat([
159
+ Buffer.from([
160
+ _teslemetry_tesla_protocol_command_signatures.Tag.TAG_SIGNATURE_TYPE,
161
+ 1,
162
+ _teslemetry_tesla_protocol_command_signatures.SignatureType.SIGNATURE_TYPE_HMAC
163
+ ]),
164
+ Buffer.from([_teslemetry_tesla_protocol_command_signatures.Tag.TAG_PERSONALIZATION, 17]),
165
+ Buffer.from(this.vin, "ascii"),
166
+ Buffer.from([_teslemetry_tesla_protocol_command_signatures.Tag.TAG_CHALLENGE, requestUuid.length]),
167
+ requestUuid,
168
+ Buffer.from([_teslemetry_tesla_protocol_command_signatures.Tag.TAG_END])
169
+ ]);
170
+ const expectedTag = crypto.default.createHmac("sha256", keys.sessionInfoKey).update(Buffer.concat([metadata, Buffer.from(resp.sessionInfo)])).digest();
171
+ const receivedTag = Buffer.from(tag);
172
+ if (expectedTag.length !== receivedTag.length || !crypto.default.timingSafeEqual(expectedTag, receivedTag)) throw new require_exceptions.SessionInfoAuthenticationFault("Session info reply failed authentication (invalid tag); discarded.");
173
+ if (info.status === _teslemetry_tesla_protocol_command_signatures.SessionInfoStatus.SESSION_INFO_STATUS_KEY_NOT_ON_WHITELIST) throw new require_exceptions.NotOnWhitelistFault();
174
+ if (!session.commit(info, keys)) throw new require_exceptions.SessionInfoAuthenticationFault("Session info reply is stale (clock regressed within the same epoch); discarded.");
175
+ }
176
+ const fault = resp.signedMessageStatus?.signedMessageFault;
177
+ if (fault !== void 0 && fault !== _teslemetry_tesla_protocol_command_universal_message.MessageFaultE.MESSAGEFAULT_ERROR_NONE) throw new (require_exceptions.MESSAGE_FAULTS[fault] ?? require_exceptions.TeslaFleetMessageFault)({ fault });
178
+ }
179
+ async handshake(domain) {
180
+ const msg = _teslemetry_tesla_protocol_command_universal_message.RoutableMessage.create({
181
+ toDestination: { domain },
182
+ fromDestination: { routingAddress: this.fromDestination },
183
+ sessionInfoRequest: { publicKey: this.#publicKey },
184
+ uuid: crypto.default.randomBytes(16)
185
+ });
186
+ await this._send(msg, "session_info");
187
+ }
188
+ buildHmacMessage(session, command) {
189
+ const personalized = session.hmacPersonalized();
190
+ if (!session.hmacKey) throw new Error("Session HMAC key is missing.");
191
+ const metadata = Buffer.concat([
192
+ Buffer.from([
193
+ _teslemetry_tesla_protocol_command_signatures.Tag.TAG_SIGNATURE_TYPE,
194
+ 1,
195
+ _teslemetry_tesla_protocol_command_signatures.SignatureType.SIGNATURE_TYPE_HMAC_PERSONALIZED
196
+ ]),
197
+ Buffer.from([
198
+ _teslemetry_tesla_protocol_command_signatures.Tag.TAG_DOMAIN,
199
+ 1,
200
+ session.domain
201
+ ]),
202
+ Buffer.from([_teslemetry_tesla_protocol_command_signatures.Tag.TAG_PERSONALIZATION, 17]),
203
+ Buffer.from(this.vin, "ascii"),
204
+ Buffer.from([_teslemetry_tesla_protocol_command_signatures.Tag.TAG_EPOCH, personalized.epoch.length]),
205
+ Buffer.from(personalized.epoch),
206
+ Buffer.from([_teslemetry_tesla_protocol_command_signatures.Tag.TAG_EXPIRES_AT, 4]),
207
+ require_crypto.uint32BE(personalized.expiresAt),
208
+ Buffer.from([_teslemetry_tesla_protocol_command_signatures.Tag.TAG_COUNTER, 4]),
209
+ require_crypto.uint32BE(personalized.counter),
210
+ Buffer.from([_teslemetry_tesla_protocol_command_signatures.Tag.TAG_END])
211
+ ]);
212
+ const tag = crypto.default.createHmac("sha256", session.hmacKey).update(Buffer.concat([metadata, command])).digest();
213
+ return _teslemetry_tesla_protocol_command_universal_message.RoutableMessage.create({
214
+ toDestination: { domain: session.domain },
215
+ fromDestination: { routingAddress: this.fromDestination },
216
+ protobufMessageAsBytes: command,
217
+ uuid: crypto.default.randomBytes(16),
218
+ signatureData: {
219
+ signerIdentity: _teslemetry_tesla_protocol_command_signatures.KeyIdentity.create({ publicKey: this.#publicKey }),
220
+ HMACPersonalizedData: {
221
+ ...personalized,
222
+ tag
223
+ }
224
+ }
225
+ });
226
+ }
227
+ /**
228
+ * Build, sign, dispatch (with WAIT/epoch-fault retry), and interpret one
229
+ * domain command.
230
+ *
231
+ * VCSEC requires messages to arrive in strict counter order and warns
232
+ * clients to avoid simultaneous requests to it at all (unlike
233
+ * Infotainment, which tolerates a sliding window) - so for
234
+ * `DOMAIN_VEHICLE_SECURITY` the *entire* dispatch (handshake, build,
235
+ * send, and any retry) runs under the domain's lock, guaranteeing at
236
+ * most one VCSEC message is ever in flight. Infotainment keeps the
237
+ * narrower build-only lock, serializing only counter assignment.
238
+ */
239
+ async dispatch(domain, command, expectsData) {
240
+ const session = this.sessionFor(domain);
241
+ const serializeWholeDispatch = domain === _teslemetry_tesla_protocol_command_universal_message.Domain.DOMAIN_VEHICLE_SECURITY;
242
+ const run = async () => {
243
+ if (!session.ready) await this.handshake(domain);
244
+ for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt++) {
245
+ const msg = serializeWholeDispatch ? this.buildHmacMessage(session, command) : await session.withLock(() => this.buildHmacMessage(session, command));
246
+ let resp;
247
+ try {
248
+ resp = await this._send(msg, "protobuf_message_as_bytes", expectsData);
249
+ } catch (error) {
250
+ if ((error instanceof require_exceptions.TeslaFleetMessageFaultIncorrectEpoch || error instanceof require_exceptions.TeslaFleetMessageFaultInvalidTokenOrCounter) && attempt < 3) continue;
251
+ throw error;
252
+ }
253
+ if (resp.signedMessageStatus?.operationStatus === _teslemetry_tesla_protocol_command_universal_message.OperationStatusE.OPERATIONSTATUS_WAIT) {
254
+ if (attempt >= 3) return fail("Too many retries");
255
+ await sleep(this.retryDelayMs);
256
+ continue;
257
+ }
258
+ const result = this.interpretResponse(resp);
259
+ if (result === "wait") {
260
+ if (attempt >= 3) return fail("Too many retries");
261
+ await sleep(this.retryDelayMs);
262
+ continue;
263
+ }
264
+ return result;
265
+ }
266
+ return fail("Too many retries");
267
+ };
268
+ return serializeWholeDispatch ? session.withLock(run) : run();
269
+ }
270
+ interpretResponse(resp) {
271
+ if (!resp.protobufMessageAsBytes || resp.protobufMessageAsBytes.length === 0) return ok();
272
+ const domain = resp.fromDestination?.domain;
273
+ if (domain === _teslemetry_tesla_protocol_command_universal_message.Domain.DOMAIN_VEHICLE_SECURITY) return this.interpretVcsecResponse(resp.protobufMessageAsBytes);
274
+ if (domain === _teslemetry_tesla_protocol_command_universal_message.Domain.DOMAIN_INFOTAINMENT) return this.interpretInfotainmentResponse(resp.protobufMessageAsBytes);
275
+ return ok();
276
+ }
277
+ interpretVcsecResponse(bytes) {
278
+ const vcsec = _teslemetry_tesla_protocol_command_vcsec.FromVCSECMessage.decode(bytes);
279
+ if (vcsec.nominalError) return fail((0, _teslemetry_tesla_protocol_command_errors.genericErrorEToJSON)(vcsec.nominalError.genericError));
280
+ if (vcsec.vehicleStatus) return ok();
281
+ if (vcsec.commandStatus) {
282
+ if (vcsec.commandStatus.operationStatus === _teslemetry_tesla_protocol_command_vcsec.OperationStatusE.OPERATIONSTATUS_OK) return ok();
283
+ if (vcsec.commandStatus.operationStatus === _teslemetry_tesla_protocol_command_vcsec.OperationStatusE.OPERATIONSTATUS_WAIT) return "wait";
284
+ if (vcsec.commandStatus.operationStatus === _teslemetry_tesla_protocol_command_vcsec.OperationStatusE.OPERATIONSTATUS_ERROR) {
285
+ const information = vcsec.commandStatus.signedMessageStatus?.signedMessageInformation;
286
+ if (information) throw new (require_exceptions.SIGNED_MESSAGE_INFORMATION_FAULTS[information] ?? require_exceptions.SignedMessageInformationFault)({ information: (0, _teslemetry_tesla_protocol_command_vcsec.signedMessageInformationEToJSON)(information) });
287
+ }
288
+ }
289
+ return ok();
290
+ }
291
+ interpretInfotainmentResponse(bytes) {
292
+ const response = _teslemetry_tesla_protocol_command_car_server.Response.decode(bytes);
293
+ if (response.ping) return ok(String(response.ping.localTimestamp ?? ""));
294
+ if (response.actionStatus) return { response: {
295
+ result: response.actionStatus.result === _teslemetry_tesla_protocol_command_car_server.OperationStatusE.OPERATIONSTATUS_OK,
296
+ reason: response.actionStatus.resultReason?.plainText ?? ""
297
+ } };
298
+ return ok();
299
+ }
300
+ async sendVehicleSecurity(command) {
301
+ return this.dispatch(_teslemetry_tesla_protocol_command_universal_message.Domain.DOMAIN_VEHICLE_SECURITY, Buffer.from(_teslemetry_tesla_protocol_command_vcsec.UnsignedMessage.encode(command).finish()), false);
302
+ }
303
+ async sendInfotainment(action) {
304
+ return this.dispatch(_teslemetry_tesla_protocol_command_universal_message.Domain.DOMAIN_INFOTAINMENT, Buffer.from(_teslemetry_tesla_protocol_command_car_server.Action.encode(action).finish()), true);
305
+ }
306
+ vehicleAction(action) {
307
+ return this.sendInfotainment(_teslemetry_tesla_protocol_command_car_server.Action.create({ vehicleAction: action }));
308
+ }
309
+ resolveSeat(seat_position) {
310
+ return typeof seat_position === "string" ? require_const.Seats[seat_position] : seat_position;
311
+ }
312
+ /**
313
+ * Controls the front (which_trunk: "front") or rear (which_trunk: "rear") trunk.
314
+ * @param which_trunk "front" or "rear"
315
+ */
316
+ async actuate_truck(which_trunk) {
317
+ const field = TrunkFields[which_trunk];
318
+ if (!field) throw new Error(`Invalid trunk: ${which_trunk}`);
319
+ const closureMoveRequest = {};
320
+ closureMoveRequest[field] = _teslemetry_tesla_protocol_command_vcsec.ClosureMoveTypeE.CLOSURE_MOVE_TYPE_MOVE;
321
+ return this.sendVehicleSecurity(_teslemetry_tesla_protocol_command_vcsec.UnsignedMessage.create({ closureMoveRequest: _teslemetry_tesla_protocol_command_vcsec.ClosureMoveRequest.create(closureMoveRequest) }));
322
+ }
323
+ /**
324
+ * Adjusts vehicle media playback volume.
325
+ * @param volume A floating point number from 0.0 to 11.0.
326
+ */
327
+ async adjust_volume(volume) {
328
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ mediaUpdateVolume: _teslemetry_tesla_protocol_command_car_server.MediaUpdateVolume.create({ volumeAbsoluteFloat: volume }) }));
329
+ }
330
+ /** Starts climate preconditioning. */
331
+ async auto_conditioning_start() {
332
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ hvacAutoAction: _teslemetry_tesla_protocol_command_car_server.HvacAutoAction.create({ powerOn: true }) }));
333
+ }
334
+ /** Stops climate preconditioning. */
335
+ async auto_conditioning_stop() {
336
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ hvacAutoAction: _teslemetry_tesla_protocol_command_car_server.HvacAutoAction.create({ powerOn: false }) }));
337
+ }
338
+ /** Cancels the countdown to install the vehicle software update. */
339
+ async cancel_software_update() {
340
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ vehicleControlCancelSoftwareUpdateAction: _teslemetry_tesla_protocol_command_car_server.VehicleControlCancelSoftwareUpdateAction.create({}) }));
341
+ }
342
+ /** Charges in max range mode -- we recommend limiting the use of this mode to long trips. */
343
+ async charge_max_range() {
344
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ chargingStartStopAction: _teslemetry_tesla_protocol_command_car_server.ChargingStartStopAction.create({ startMaxRange: VOID }) }));
345
+ }
346
+ /** Closes the charge port door. */
347
+ async charge_port_door_close() {
348
+ return this.sendVehicleSecurity(_teslemetry_tesla_protocol_command_vcsec.UnsignedMessage.create({ closureMoveRequest: _teslemetry_tesla_protocol_command_vcsec.ClosureMoveRequest.create({ chargePort: _teslemetry_tesla_protocol_command_vcsec.ClosureMoveTypeE.CLOSURE_MOVE_TYPE_CLOSE }) }));
349
+ }
350
+ /** Opens the charge port door. */
351
+ async charge_port_door_open() {
352
+ return this.sendVehicleSecurity(_teslemetry_tesla_protocol_command_vcsec.UnsignedMessage.create({ closureMoveRequest: _teslemetry_tesla_protocol_command_vcsec.ClosureMoveRequest.create({ chargePort: _teslemetry_tesla_protocol_command_vcsec.ClosureMoveTypeE.CLOSURE_MOVE_TYPE_OPEN }) }));
353
+ }
354
+ /** Charges in Standard mode. */
355
+ async charge_standard() {
356
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ chargingStartStopAction: _teslemetry_tesla_protocol_command_car_server.ChargingStartStopAction.create({ startStandard: VOID }) }));
357
+ }
358
+ /** Starts charging the vehicle. */
359
+ async charge_start() {
360
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ chargingStartStopAction: _teslemetry_tesla_protocol_command_car_server.ChargingStartStopAction.create({ start: VOID }) }));
361
+ }
362
+ /** Stops charging the vehicle. */
363
+ async charge_stop() {
364
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ chargingStartStopAction: _teslemetry_tesla_protocol_command_car_server.ChargingStartStopAction.create({ stop: VOID }) }));
365
+ }
366
+ /**
367
+ * 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.
368
+ */
369
+ async clear_pin_to_drive_admin() {
370
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ vehicleControlResetPinToDriveAdminAction: _teslemetry_tesla_protocol_command_car_server.VehicleControlResetPinToDriveAdminAction.create({}) }));
371
+ }
372
+ /** Locks the vehicle. */
373
+ async door_lock() {
374
+ return this.sendVehicleSecurity(_teslemetry_tesla_protocol_command_vcsec.UnsignedMessage.create({ RKEAction: _teslemetry_tesla_protocol_command_vcsec.RKEActionE.RKE_ACTION_LOCK }));
375
+ }
376
+ /** Unlocks the vehicle. */
377
+ async door_unlock() {
378
+ return this.sendVehicleSecurity(_teslemetry_tesla_protocol_command_vcsec.UnsignedMessage.create({ RKEAction: _teslemetry_tesla_protocol_command_vcsec.RKEActionE.RKE_ACTION_UNLOCK }));
379
+ }
380
+ /** Erases user's data from the user interface. Requires the vehicle to be in park. */
381
+ async erase_user_data() {
382
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ eraseUserDataAction: _teslemetry_tesla_protocol_command_car_server.EraseUserDataAction.create({}) }));
383
+ }
384
+ /** Briefly flashes the vehicle headlights. Requires the vehicle to be in park. */
385
+ async flash_lights() {
386
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ vehicleControlFlashLightsAction: _teslemetry_tesla_protocol_command_car_server.VehicleControlFlashLightsAction.create({}) }));
387
+ }
388
+ /**
389
+ * Restricts certain vehicle UI functionality from guest users
390
+ * @param enable `true` or `false`
391
+ */
392
+ async guest_mode(enable) {
393
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ guestModeAction: _teslemetry_tesla_protocol_command_vehicle.VehicleState_GuestMode.create({ GuestModeActive: enable }) }));
394
+ }
395
+ /** Honks the vehicle horn. Requires the vehicle to be in park. */
396
+ async honk_horn() {
397
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ vehicleControlHonkHornAction: _teslemetry_tesla_protocol_command_car_server.VehicleControlHonkHornAction.create({}) }));
398
+ }
399
+ /** Advances media player to next favorite track. */
400
+ async media_next_fav() {
401
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ mediaNextFavorite: {} }));
402
+ }
403
+ /** Advances media player to next track. */
404
+ async media_next_track() {
405
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ mediaNextTrack: {} }));
406
+ }
407
+ /** Advances media player to previous favorite track. */
408
+ async media_prev_fav() {
409
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ mediaPreviousFavorite: {} }));
410
+ }
411
+ /** Advances media player to previous track. */
412
+ async media_prev_track() {
413
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ mediaPreviousTrack: {} }));
414
+ }
415
+ /** Toggles current play/pause state. */
416
+ async media_toggle_playback() {
417
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ mediaPlayAction: {} }));
418
+ }
419
+ /** Turns the volume down by one. */
420
+ async media_volume_down() {
421
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ mediaUpdateVolume: _teslemetry_tesla_protocol_command_car_server.MediaUpdateVolume.create({ volumeDelta: -1 }) }));
422
+ }
423
+ /**
424
+ * Start navigation to given coordinates.
425
+ * @param lat Latitude
426
+ * @param lon Longitude
427
+ * @param order Trip order: `0` replaces the current trip (default), `1` prepends it as the next stop, `2` appends it as the last stop
428
+ */
429
+ async navigation_gps_request(lat, lon, order = 0) {
430
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ navigationGpsRequest: _teslemetry_tesla_protocol_command_car_server.NavigationGpsRequest.create({
431
+ lat,
432
+ lon,
433
+ order
434
+ }) }));
435
+ }
436
+ /**
437
+ * Sends a location to the in-vehicle navigation system.
438
+ *
439
+ * `locale` and `type` are REST-only fields with no equivalent in the
440
+ * signed-command protocol (which only carries a destination string and a
441
+ * multi-stop order), so they are accepted for cross-transport signature
442
+ * parity but not sent.
443
+ * @param value Destination text
444
+ */
445
+ async navigation_request(value, _locale, _type) {
446
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ navigationRequest: _teslemetry_tesla_protocol_command_car_server.NavigationRequest.create({ destination: value }) }));
447
+ }
448
+ /**
449
+ * Start navigation to a supercharger.
450
+ * @param id Supercharger ID
451
+ * @param order Trip order: `0` replaces the current trip (default), `1` prepends it as the next stop, `2` appends it as the last stop
452
+ */
453
+ async navigation_sc_request(id, order) {
454
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ navigationSuperchargerRequest: _teslemetry_tesla_protocol_command_car_server.NavigationSuperchargerRequest.create({
455
+ id,
456
+ remoteNavTripOrder: order ?? 0
457
+ }) }));
458
+ }
459
+ /**
460
+ * Sets automatic seat heating and cooling. Only the front seats support this action.
461
+ * @param auto_seat_position `AutoSeat` position (1-indexed: front left is 1, front right is 2)
462
+ * @param auto_climate_on `true` or `false`
463
+ */
464
+ async remote_auto_seat_climate_request(auto_seat_position, auto_climate_on) {
465
+ const position = AutoSeatPositions[typeof auto_seat_position === "string" ? require_const.AutoSeat[auto_seat_position] : auto_seat_position];
466
+ if (position === void 0) throw new Error(`Invalid seat position for auto seat climate: ${auto_seat_position}`);
467
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ autoSeatClimateAction: _teslemetry_tesla_protocol_command_car_server.AutoSeatClimateAction.create({ carseat: [_teslemetry_tesla_protocol_command_car_server.AutoSeatClimateAction_CarSeat.create({
468
+ on: auto_climate_on,
469
+ seatPosition: position
470
+ })] }) }));
471
+ }
472
+ /**
473
+ * Sets automatic steering wheel heating on/off.
474
+ * @param on `true` or `false`
475
+ */
476
+ async remote_auto_steering_wheel_heat_climate_request(on) {
477
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ autoStwHeatAction: _teslemetry_tesla_protocol_command_car_server.AutoStwHeatAction.create({ on }) }));
478
+ }
479
+ /**
480
+ * Plays a sound through the vehicle external speaker.
481
+ * @param sound Fart is `0`, Locate ping is `2000`
482
+ */
483
+ async remote_boombox(sound = 0) {
484
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ boomboxAction: _teslemetry_tesla_protocol_command_car_server.BoomboxAction.create({ sound }) }));
485
+ }
486
+ /**
487
+ * Sets seat cooling. Only the front seats support this action.
488
+ * @param seat_position Seat position
489
+ * @param seat_cooler_level Cooling level
490
+ */
491
+ async remote_seat_cooler_request(seat_position, seat_cooler_level) {
492
+ const position = SeatCoolerPositions[this.resolveSeat(seat_position)];
493
+ if (position === void 0) throw new Error(`Invalid seat position for seat cooling: ${seat_position}`);
494
+ const level = SeatCoolerLevels[seat_cooler_level];
495
+ if (level === void 0) throw new Error(`Invalid seat cooler level: ${seat_cooler_level}`);
496
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ hvacSeatCoolerActions: _teslemetry_tesla_protocol_command_car_server.HvacSeatCoolerActions.create({ hvacSeatCoolerAction: [_teslemetry_tesla_protocol_command_car_server.HvacSeatCoolerActions_HvacSeatCoolerAction.create({
497
+ seatCoolerLevel: level,
498
+ seatPosition: position
499
+ })] }) }));
500
+ }
501
+ /**
502
+ * Sets seat heating.
503
+ * @param seat_position Seat position
504
+ * @param seat_heater_level Heating level
505
+ */
506
+ async remote_seat_heater_request(seat_position, seat_heater_level) {
507
+ const field = SeatHeaterFields[this.resolveSeat(seat_position)];
508
+ if (!field) throw new Error(`Invalid seat position: ${seat_position}`);
509
+ const levelField = SeatHeaterLevels[seat_heater_level];
510
+ if (!levelField) throw new Error(`Invalid seat heater level: ${seat_heater_level}`);
511
+ const heaterAction = {};
512
+ heaterAction[field] = VOID;
513
+ heaterAction[levelField] = VOID;
514
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ hvacSeatHeaterActions: _teslemetry_tesla_protocol_command_car_server.HvacSeatHeaterActions.create({ hvacSeatHeaterAction: [_teslemetry_tesla_protocol_command_car_server.HvacSeatHeaterActions_HvacSeatHeaterAction.create(heaterAction)] }) }));
515
+ }
516
+ /** Starts the vehicle remotely. Requires keyless driving to be enabled. */
517
+ async remote_start_drive() {
518
+ return this.sendVehicleSecurity(_teslemetry_tesla_protocol_command_vcsec.UnsignedMessage.create({ RKEAction: _teslemetry_tesla_protocol_command_vcsec.RKEActionE.RKE_ACTION_REMOTE_DRIVE }));
519
+ }
520
+ /**
521
+ * Sets steering wheel heat level.
522
+ * @param level Heating level (0=Off, 1=Low, 2=High)
523
+ */
524
+ async remote_steering_wheel_heat_level_request(level) {
525
+ const stwLevel = StwHeatLevels[level];
526
+ if (stwLevel === void 0) throw new Error(`Invalid steering wheel heat level: ${level}`);
527
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ stwHeatLevelAction: _teslemetry_tesla_protocol_command_car_server.StwHeatLevelAction.create({ stwHeatLevel: stwLevel }) }));
528
+ }
529
+ /**
530
+ * Sets steering wheel heating on/off. For vehicles that do not support auto steering wheel heat.
531
+ * @param on `true` or `false`
532
+ */
533
+ async remote_steering_wheel_heater_request(on) {
534
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ hvacSteeringWheelHeaterAction: _teslemetry_tesla_protocol_command_car_server.HvacSteeringWheelHeaterAction.create({ powerOn: on }) }));
535
+ }
536
+ /** Removes PIN to Drive. Requires the car to be in Pin to Drive mode and not in Valet mode. */
537
+ async reset_pin_to_drive_pin() {
538
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ vehicleControlResetPinToDriveAction: _teslemetry_tesla_protocol_command_car_server.VehicleControlResetPinToDriveAction.create({}) }));
539
+ }
540
+ /** Removes PIN for Valet Mode. */
541
+ async reset_valet_pin() {
542
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ vehicleControlResetValetPinAction: _teslemetry_tesla_protocol_command_car_server.VehicleControlResetValetPinAction.create({}) }));
543
+ }
544
+ /**
545
+ * Schedules a vehicle software update (over the air "OTA") to be installed in the future.
546
+ * @param offset_sec Offset in seconds
547
+ */
548
+ async schedule_software_update(offset_sec) {
549
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ vehicleControlScheduleSoftwareUpdateAction: _teslemetry_tesla_protocol_command_car_server.VehicleControlScheduleSoftwareUpdateAction.create({ offsetSec: offset_sec }) }));
550
+ }
551
+ /**
552
+ * Turns Bioweapon Defense Mode on and off.
553
+ * @param on `true` or `false`
554
+ * @param manual_override `true` or `false`
555
+ */
556
+ async set_bioweapon_mode(on, manual_override) {
557
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ hvacBioweaponModeAction: _teslemetry_tesla_protocol_command_car_server.HvacBioweaponModeAction.create({
558
+ on,
559
+ manualOverride: manual_override
560
+ }) }));
561
+ }
562
+ /**
563
+ * Sets the vehicle overheat protection.
564
+ * @param on `true` or `false`
565
+ * @param fan_only `true` or `false`
566
+ */
567
+ async set_cabin_overheat_protection(on, fan_only) {
568
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ setCabinOverheatProtectionAction: _teslemetry_tesla_protocol_command_car_server.SetCabinOverheatProtectionAction.create({
569
+ on,
570
+ fanOnly: fan_only
571
+ }) }));
572
+ }
573
+ /**
574
+ * Sets the vehicle's charge limit to a custom percentage.
575
+ * @param percent Charge limit percentage typically between 50-100
576
+ */
577
+ async set_charge_limit(percent) {
578
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ chargingSetLimitAction: _teslemetry_tesla_protocol_command_car_server.ChargingSetLimitAction.create({ percent }) }));
579
+ }
580
+ /**
581
+ * Sets the charging amps.
582
+ * @param charging_amps Current limit in Amps typically between 5-32
583
+ */
584
+ async set_charging_amps(charging_amps) {
585
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ setChargingAmpsAction: _teslemetry_tesla_protocol_command_car_server.SetChargingAmpsAction.create({ chargingAmps: charging_amps }) }));
586
+ }
587
+ /**
588
+ * Enables climate keeper mode.
589
+ * @param climate_keeper_mode `0` is Off, `1` is Keep, `2` is Dog, `3` is Camp
590
+ */
591
+ async set_climate_keeper_mode(climate_keeper_mode) {
592
+ const mode = typeof climate_keeper_mode === "string" ? require_const.ClimateModes[climate_keeper_mode] : climate_keeper_mode;
593
+ const action = ClimateKeeperActions[mode];
594
+ if (action === void 0) throw new Error(`Invalid climate keeper mode: ${climate_keeper_mode}`);
595
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ hvacClimateKeeperAction: {
596
+ ClimateKeeperAction: action,
597
+ manualOverride: false,
598
+ manualOverrideMode: []
599
+ } }));
600
+ }
601
+ /**
602
+ * Adjusts the Cabin Overheat Protection temperature (COP).
603
+ * @param cop_temp Integer representing Low (0/30C/90F), Medium (1/35C/95F), or High (2/40C/100F)
604
+ */
605
+ async set_cop_temp(cop_temp) {
606
+ const copActivationTemp = CopActivationTemps[cop_temp];
607
+ if (copActivationTemp === void 0) throw new Error(`Invalid cop_temp: ${cop_temp}`);
608
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ setCopTempAction: _teslemetry_tesla_protocol_command_car_server.SetCopTempAction.create({ copActivationTemp }) }));
609
+ }
610
+ /**
611
+ * Sets a four-digit passcode for PIN to Drive.
612
+ * @param on `true` or `false`
613
+ * @param password Four-digit passcode
614
+ */
615
+ async set_pin_to_drive(on, password) {
616
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ vehicleControlSetPinToDriveAction: _teslemetry_tesla_protocol_command_car_server.VehicleControlSetPinToDriveAction.create({
617
+ on,
618
+ password: String(password)
619
+ }) }));
620
+ }
621
+ /**
622
+ * Sets an override for preconditioning — it should default to empty if no override is used.
623
+ * @param on `true` or `false`
624
+ * @param manual_override `true` or `false`
625
+ */
626
+ async set_preconditioning_max(on, manual_override) {
627
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ hvacSetPreconditioningMaxAction: _teslemetry_tesla_protocol_command_car_server.HvacSetPreconditioningMaxAction.create({
628
+ on,
629
+ manualOverride: manual_override
630
+ }) }));
631
+ }
632
+ /**
633
+ * Sets a time at which charging should be completed.
634
+ * @param enable `true` or `false`
635
+ * @param time Minutes after midnight
636
+ */
637
+ async set_scheduled_charging(enable, time) {
638
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ scheduledChargingAction: _teslemetry_tesla_protocol_command_car_server.ScheduledChargingAction.create({
639
+ enabled: enable,
640
+ chargingTime: time
641
+ }) }));
642
+ }
643
+ /**
644
+ * Sets a time at which departure should be completed.
645
+ *
646
+ * `preconditioning_enabled` and `off_peak_charging_enabled` are accepted
647
+ * for cross-transport signature parity but have no field in the signed
648
+ * command protobuf, so they are not sent.
649
+ * @param enable `true` or `false`
650
+ * @param preconditioning_enabled not representable in a signed command
651
+ * @param preconditioning_weekdays_only `true` or `false`
652
+ * @param departure_time Minutes after midnight
653
+ * @param off_peak_charging_enabled not representable in a signed command
654
+ * @param off_peak_charging_weekdays_only `true` or `false`
655
+ * @param end_off_peak_time Minutes after midnight
656
+ */
657
+ async set_scheduled_departure(enable = true, _preconditioning_enabled = false, preconditioning_weekdays_only = false, departure_time = 0, _off_peak_charging_enabled = false, off_peak_charging_weekdays_only = false, end_off_peak_time = 0) {
658
+ const preconditioningTimes = preconditioning_weekdays_only ? _teslemetry_tesla_protocol_command_common.PreconditioningTimes.create({ weekdays: VOID }) : _teslemetry_tesla_protocol_command_common.PreconditioningTimes.create({ allWeek: VOID });
659
+ const offPeakChargingTimes = off_peak_charging_weekdays_only ? _teslemetry_tesla_protocol_command_common.OffPeakChargingTimes.create({ weekdays: VOID }) : _teslemetry_tesla_protocol_command_common.OffPeakChargingTimes.create({ allWeek: VOID });
660
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ scheduledDepartureAction: _teslemetry_tesla_protocol_command_car_server.ScheduledDepartureAction.create({
661
+ enabled: enable,
662
+ departureTime: departure_time,
663
+ preconditioningTimes,
664
+ offPeakChargingTimes,
665
+ offPeakHoursEndTime: end_off_peak_time
666
+ }) }));
667
+ }
668
+ /**
669
+ * Enables and disables Sentry Mode.
670
+ * @param on `true` or `false`
671
+ */
672
+ async set_sentry_mode(on) {
673
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ vehicleControlSetSentryModeAction: _teslemetry_tesla_protocol_command_car_server.VehicleControlSetSentryModeAction.create({ on }) }));
674
+ }
675
+ /**
676
+ * Sets the driver and/or passenger-side cabin temperature (and other zones if sync is enabled).
677
+ * @param driver_temp Driver temperature
678
+ * @param passenger_temp Passenger temperature
679
+ */
680
+ async set_temps(driver_temp, passenger_temp) {
681
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ hvacTemperatureAdjustmentAction: _teslemetry_tesla_protocol_command_car_server.HvacTemperatureAdjustmentAction.create({
682
+ driverTempCelsius: driver_temp,
683
+ passengerTempCelsius: passenger_temp
684
+ }) }));
685
+ }
686
+ /**
687
+ * Turns on Valet Mode and sets a four-digit passcode.
688
+ * @param on `true` or `false`
689
+ * @param password Four-digit passcode
690
+ */
691
+ async set_valet_mode(on, password) {
692
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ vehicleControlSetValetModeAction: _teslemetry_tesla_protocol_command_car_server.VehicleControlSetValetModeAction.create({
693
+ on,
694
+ password: String(password)
695
+ }) }));
696
+ }
697
+ /**
698
+ * Changes the name of a vehicle.
699
+ * @param vehicle_name New name
700
+ */
701
+ async set_vehicle_name(vehicle_name) {
702
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ setVehicleNameAction: _teslemetry_tesla_protocol_command_car_server.SetVehicleNameAction.create({ vehicleName: vehicle_name }) }));
703
+ }
704
+ /**
705
+ * Activates Speed Limit Mode with a four-digit PIN.
706
+ * @param pin Four-digit PIN
707
+ */
708
+ async speed_limit_activate(pin) {
709
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ drivingSpeedLimitAction: _teslemetry_tesla_protocol_command_car_server.DrivingSpeedLimitAction.create({
710
+ activate: true,
711
+ pin: String(pin)
712
+ }) }));
713
+ }
714
+ /**
715
+ * Deactivates Speed Limit Mode and resets the associated PIN.
716
+ * @param pin Four-digit PIN
717
+ */
718
+ async speed_limit_clear_pin(pin) {
719
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ drivingClearSpeedLimitPinAction: _teslemetry_tesla_protocol_command_car_server.DrivingClearSpeedLimitPinAction.create({ pin: String(pin) }) }));
720
+ }
721
+ /** Deactivates Speed Limit Mode and resets the associated PIN, for fleet managers or owners. */
722
+ async speed_limit_clear_pin_admin() {
723
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ drivingClearSpeedLimitPinAdminAction: _teslemetry_tesla_protocol_command_car_server.DrivingClearSpeedLimitPinAdminAction.create({}) }));
724
+ }
725
+ /**
726
+ * Deactivates Speed Limit Mode.
727
+ * @param pin Four-digit PIN
728
+ */
729
+ async speed_limit_deactivate(pin) {
730
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ drivingSpeedLimitAction: _teslemetry_tesla_protocol_command_car_server.DrivingSpeedLimitAction.create({
731
+ activate: false,
732
+ pin: String(pin)
733
+ }) }));
734
+ }
735
+ /**
736
+ * Sets the maximum speed allowed when Speed Limit Mode is active.
737
+ * @param limit_mph Maximum speed in MPH
738
+ */
739
+ async speed_limit_set_limit(limit_mph) {
740
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ drivingSetSpeedLimitAction: _teslemetry_tesla_protocol_command_car_server.DrivingSetSpeedLimitAction.create({ limitMph: limit_mph }) }));
741
+ }
742
+ /**
743
+ * Controls the panoramic sunroof on the Model S.
744
+ *
745
+ * "stop" has no corresponding field in the signed-command protobuf (only
746
+ * vent/close/open), so it is rejected rather than guessed at.
747
+ * @param state "close" or "vent"
748
+ */
749
+ async sun_roof_control(state) {
750
+ let action;
751
+ if (state === "vent") action = _teslemetry_tesla_protocol_command_car_server.VehicleControlSunroofOpenCloseAction.create({ vent: VOID });
752
+ else if (state === "close") action = _teslemetry_tesla_protocol_command_car_server.VehicleControlSunroofOpenCloseAction.create({ close: VOID });
753
+ else throw new Error("sun_roof_control: \"stop\" is not representable in a signed command.");
754
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ vehicleControlSunroofOpenCloseAction: action }));
755
+ }
756
+ /**
757
+ * Records a drive note. Overrides the inherited plaintext command so it is
758
+ * signed and dispatched over the vehicle-command protocol rather than
759
+ * silently falling through to the unsigned REST endpoint on vehicles that
760
+ * require signing.
761
+ * @param note Drive note
762
+ */
763
+ async take_drivenote(note) {
764
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ takeDrivenoteAction: _teslemetry_tesla_protocol_command_car_server.TakeDrivenoteAction.create({ note }) }));
765
+ }
766
+ /**
767
+ * Turns on HomeLink (used to open and close garage doors).
768
+ * @param lat Latitude
769
+ * @param lon Longitude
770
+ * @param token
771
+ */
772
+ async trigger_homelink(lat, lon, token = "") {
773
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ vehicleControlTriggerHomelinkAction: _teslemetry_tesla_protocol_command_car_server.VehicleControlTriggerHomelinkAction.create({
774
+ location: _teslemetry_tesla_protocol_command_common.LatLong.create({
775
+ latitude: lat,
776
+ longitude: lon
777
+ }),
778
+ token
779
+ }) }));
780
+ }
781
+ /**
782
+ * Sends upcoming calendar entries to the vehicle. Overrides the inherited
783
+ * plaintext command so it is signed and dispatched over the vehicle-command
784
+ * protocol rather than silently falling through to the unsigned REST
785
+ * endpoint on vehicles that require signing.
786
+ * @param calendar_data Calendar data
787
+ */
788
+ async upcoming_calendar_entries(calendar_data) {
789
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ uiSetUpcomingCalendarEntries: _teslemetry_tesla_protocol_command_car_server.UiSetUpcomingCalendarEntries.create({ calendarData: calendar_data }) }));
790
+ }
791
+ /**
792
+ * Control the windows of a parked vehicle. Supported commands: vent and close.
793
+ * @param command "vent" or "close"
794
+ * @param lat Latitude
795
+ * @param lon Longitude
796
+ */
797
+ async window_control(command, _lat, _lon) {
798
+ const action = command === "vent" ? _teslemetry_tesla_protocol_command_car_server.VehicleControlWindowAction.create({ vent: VOID }) : _teslemetry_tesla_protocol_command_car_server.VehicleControlWindowAction.create({ close: VOID });
799
+ return this.vehicleAction(_teslemetry_tesla_protocol_command_car_server.VehicleAction.create({ vehicleControlWindowAction: action }));
800
+ }
801
+ };
802
+ exports.default = Commands;