From a1ad57bf91d613ff243b83d281938ba57dad00b9 Mon Sep 17 00:00:00 2001 From: Brett Date: Sun, 21 Jul 2024 11:33:34 +1000 Subject: [PATCH] Revert "Remove console log" This reverts commit b2038a855cc3159f9ecb0f9e3b4c17d84057f81d. --- src/vehicle.ts | 51 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/src/vehicle.ts b/src/vehicle.ts index fb77871..ceac45a 100644 --- a/src/vehicle.ts +++ b/src/vehicle.ts @@ -73,43 +73,54 @@ export class VehicleAccessory { // Get data and schedule refresh - this.refresh(true); + this.refresh(); setInterval(() => this.refresh(), REFRESH_INTERVAL); } - async refresh(first = false): Promise { - return this.platform.TeslaFleetApi.status(this.accessory.context.vin).then((status) => { - this.accessory.context.state = status; - if (status === "asleep" && !first) { - this.emitter.emit("offline"); - return; - } - return this.platform.TeslaFleetApi.state(this.accessory.context.vin) - .then((data) => { - data.state = status; - this.emitter.emit("vehicle_data", data); - }); - }) + async refresh(): Promise { + this.vehicle + .vehicle_data([ + "charge_state", + "climate_state", + "drive_state", + "location_data", + "vehicle_state", + ]) + .then((data) => { + this.accessory.context.state = data.state; + this.emitter.emit("vehicle_data", data); + }) .catch(({ status, data }) => { + if (status === 408) { + this.platform.log.debug(`${this.accessory.displayName} is offline`); + this.accessory.context.state = "offline"; + this.emitter.emit("offline"); + return; + } if (data?.error) { this.platform.log.warn(`${this.accessory.displayName} return status ${status}: ${data.error}`); return; } this.platform.log.error(`${this.accessory.displayName} return status ${status}: ${data}`); }); - } async wakeUpAndWait(): Promise { if (this.accessory.context.state === "online") { return Promise.resolve(); } - return this.platform.TeslaFleetApi.wake(this.accessory.context.vin).then(awake => { - if (awake) { - this.accessory.context.state = "online"; + await this.vehicle.wake_up(); + + let interval = 2000; + for (let x = 0; x < 5; x++) { + await new Promise((resolve) => setTimeout(resolve, interval)); + const { state } = await this.vehicle.vehicle(); + this.accessory.context.state = state; + if (state === "online") { return Promise.resolve(); } - return Promise.reject("Vehicle did not wake up"); - }); + interval = interval + 2000; + } + return Promise.reject("Vehicle didn't wake up"); } }