From b2038a855cc3159f9ecb0f9e3b4c17d84057f81d Mon Sep 17 00:00:00 2001 From: Brett Date: Sun, 21 Jul 2024 11:29:27 +1000 Subject: [PATCH] Remove console log --- src/vehicle.ts | 51 ++++++++++++++++++++------------------------------ 1 file changed, 20 insertions(+), 31 deletions(-) diff --git a/src/vehicle.ts b/src/vehicle.ts index ceac45a..fb77871 100644 --- a/src/vehicle.ts +++ b/src/vehicle.ts @@ -73,54 +73,43 @@ export class VehicleAccessory { // Get data and schedule refresh - this.refresh(); + this.refresh(true); setInterval(() => this.refresh(), REFRESH_INTERVAL); } - 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); - }) + 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); + }); + }) .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(); } - 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 this.platform.TeslaFleetApi.wake(this.accessory.context.vin).then(awake => { + if (awake) { + this.accessory.context.state = "online"; return Promise.resolve(); } - interval = interval + 2000; - } - return Promise.reject("Vehicle didn't wake up"); + return Promise.reject("Vehicle did not wake up"); + }); } }