From f3121bce240d1f10c00a3362caf7e18ad1c99c2c Mon Sep 17 00:00:00 2001 From: Brett Date: Thu, 4 Jul 2024 15:39:39 +1000 Subject: [PATCH] Fix tessie refresh --- src/vehicle.ts | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/src/vehicle.ts b/src/vehicle.ts index da96dd3..b1f0edb 100644 --- a/src/vehicle.ts +++ b/src/vehicle.ts @@ -73,32 +73,39 @@ export class VehicleAccessory { // Get data and schedule refresh - this.refresh(); + this.refresh(true); setInterval(() => this.refresh(), REFRESH_INTERVAL); } - async refresh(): Promise { - this.platform.TeslaFleetApi.state(this.accessory.context.vin) - .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.accessory.context.charge_state = data.charge_state; + this.accessory.context.climate_state = data.climate_state; + this.accessory.context.drive_state = data.drive_state; + this.accessory.context.vehicle_state = data.vehicle_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 { + console.log(this.accessory.context.state); if (this.accessory.context.state === "online") { return Promise.resolve(); }