@eiannone/tesla-api 1.12.0 → 1.14.1

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.
@@ -0,0 +1,17 @@
1
+ {
2
+ // Usare IntelliSense per informazioni sui possibili attributi.
3
+ // Al passaggio del mouse vengono visualizzate le descrizioni degli attributi esistenti.
4
+ // Per altre informazioni, visitare: https://go.microsoft.com/fwlink/?linkid=830387
5
+ "version": "0.2.0",
6
+ "configurations": [
7
+ {
8
+ "type": "pwa-node",
9
+ "request": "launch",
10
+ "name": "Get-id",
11
+ "skipFiles": [
12
+ "<node_internals>/**"
13
+ ],
14
+ "program": "${workspaceFolder}\\examples\\get-id.js"
15
+ }
16
+ ]
17
+ }
package/TeslaApi.js CHANGED
@@ -21,8 +21,8 @@ class ApiError extends Error {
21
21
  }
22
22
 
23
23
  class TeslaApi {
24
- constructor(access_token = null, vehicle_id = null, refresh_token = null) {
25
- this.vid = vehicle_id;
24
+ constructor(access_token = null, id = null, refresh_token = null) {
25
+ this.vid = id;
26
26
  this.token = access_token;
27
27
  this.refresh_token = refresh_token;
28
28
  this.timeout = 10000;
@@ -47,7 +47,7 @@ class TeslaApi {
47
47
  }
48
48
  }
49
49
 
50
- async #apiCall(path, method = 'GET', params = undefined) {
50
+ async #apiCall(path = "", method = 'GET', params = undefined) {
51
51
  return new Promise((resolve, reject) => {
52
52
  const postData = (typeof params != 'undefined')? JSON.stringify(params) : '';
53
53
  let headers = { 'user-agent': "TeslaEma", 'Authorization': "Bearer " + this.token };
@@ -102,30 +102,30 @@ class TeslaApi {
102
102
  }
103
103
 
104
104
  async getVehicles() {
105
- return this.#apiCall("");
105
+ return this.#apiCall();
106
106
  }
107
107
 
108
- async getVehicle(vehicle_id = null) {
109
- return this.#apiCall((vehicle_id == null)? this.vid : vehicle_id);
108
+ async getVehicle(id = null) {
109
+ return this.#apiCall((id == null)? this.vid : id);
110
110
  }
111
111
 
112
- async getVehicleData(vehicle_id = null) {
113
- const vid = (vehicle_id == null)? this.vid : vehicle_id;
112
+ async getVehicleData(id = null) {
113
+ const vid = (id == null)? this.vid : id;
114
114
  return this.#apiCall(vid + "/vehicle_data");
115
115
  }
116
116
 
117
- async getChargeState(vehicle_id = null) {
118
- const vid = (vehicle_id == null)? this.vid : vehicle_id;
117
+ async getChargeState(id = null) {
118
+ const vid = (id == null)? this.vid : id;
119
119
  return this.#apiCall(vid + "/data_request/charge_state");
120
120
  }
121
121
 
122
- async wakeUp(vehicle_id = null) {
123
- const vid = (vehicle_id == null)? this.vid : vehicle_id;
122
+ async wakeUp(id = null) {
123
+ const vid = (id == null)? this.vid : id;
124
124
  return this.#apiCall(vid + "/wake_up", "POST");
125
125
  }
126
126
 
127
- async command(command, params = undefined, vehicle_id = null) {
128
- const vid = (vehicle_id == null)? this.vid : vehicle_id;
127
+ async command(command, params = undefined, id = null) {
128
+ const vid = (id == null)? this.vid : id;
129
129
  return this.#apiCall(vid + "/command/" + command, "POST", params);
130
130
  }
131
131
 
@@ -242,6 +242,19 @@ class TeslaApi {
242
242
  throw error;
243
243
  }
244
244
  }
245
+
246
+ async getId(vehicle_id) {
247
+ return this.#apiCall().then(vehicles => {
248
+ for (let v = 0; v < vehicles.length; v++) {
249
+ if (!vehicles[v].hasOwnProperty('vehicle_id') || !vehicles[v].hasOwnProperty('id_s')) continue;
250
+ if (vehicles[v].vehicle_id == vehicle_id) {
251
+ this.vid = vehicles[v].id_s;
252
+ return this.vid;
253
+ }
254
+ }
255
+ throw new ApiError("Vehicle not found");
256
+ });
257
+ }
245
258
  }
246
259
 
247
260
  export { ApiError, TeslaApi }
package/TeslaStream.js CHANGED
@@ -58,7 +58,7 @@ export default class TeslaStream extends EventEmitter {
58
58
  }
59
59
  if (this.ws.readyState == WebSocket.CONNECTING) {
60
60
  this.ws.removeAllListeners('open');
61
- this.ws.on('open', _ => { disconnect(reconnect, unsubscribe); });
61
+ this.ws.on('open', _ => this.disconnect(reconnect, unsubscribe));
62
62
  return;
63
63
  }
64
64
  this.state = CLOSING;
@@ -0,0 +1,4 @@
1
+ import {TeslaApi, ApiError} from '../TeslaApi.js';
2
+
3
+ const api = new TeslaApi('eu-abcde');
4
+ api.getId('1234').then(console.log);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eiannone/tesla-api",
3
- "version": "1.12.0",
3
+ "version": "1.14.1",
4
4
  "description": "Nodejs Tesla API",
5
5
  "type": "module",
6
6
  "main": "index.js",