From 8421d50cff9398240fb57dc4387864e9dd0b683f Mon Sep 17 00:00:00 2001 From: Brett Date: Thu, 4 Jul 2024 16:12:26 +1000 Subject: [PATCH] No longer required context --- src/vehicle-services/chargeswitch.ts | 20 +++++--------------- src/vehicle-services/windows.ts | 10 +++++++--- src/vehicle.ts | 8 ++++---- 3 files changed, 16 insertions(+), 22 deletions(-) diff --git a/src/vehicle-services/chargeswitch.ts b/src/vehicle-services/chargeswitch.ts index 037990f..53e70e2 100644 --- a/src/vehicle-services/chargeswitch.ts +++ b/src/vehicle-services/chargeswitch.ts @@ -1,4 +1,3 @@ -import { Characteristic, CharacteristicValue } from "homebridge"; import { VehicleAccessory } from "../vehicle.js"; import { BaseService } from "./base.js"; @@ -8,23 +7,14 @@ export class ChargeSwitchService extends BaseService { const on = this.service .getCharacteristic(this.parent.platform.Characteristic.On) - //.onGet(this.getOn.bind(this)) - .onSet((value) => this.setOn(value, on)); + .onSet(async (value) => { + this.parent.wakeUpAndWait() + .then(() => value ? this.parent.vehicle.charge_start() : this.parent.vehicle.charge_stop()) + .then(() => on.updateValue(value)); + }); this.parent.emitter.on("vehicle_data", (data) => { on.updateValue(data.charge_state?.user_charge_enable_request ?? data.charge_state?.charge_enable_request); }); } - - getOn(): boolean { - return !!(this.parent.accessory.context?.charge_state?.user_charge_enable_request - ?? this.parent.accessory.context?.charge_state?.charge_enable_request); - } - - async setOn(value: CharacteristicValue, characteristic: Characteristic): Promise { - await this.parent.wakeUpAndWait().then(() => - value ? this.parent.vehicle.charge_start().then(() => characteristic.updateValue(value)) - : this.parent.vehicle.charge_stop().then(() => characteristic.updateValue(value)) - ); - } } diff --git a/src/vehicle-services/windows.ts b/src/vehicle-services/windows.ts index fd69038..5136cc8 100644 --- a/src/vehicle-services/windows.ts +++ b/src/vehicle-services/windows.ts @@ -5,6 +5,9 @@ import { VehicleAccessory } from "../vehicle.js"; import { BaseService } from "./base.js"; export class WindowService extends BaseService { + latitude: number = 0; + longitude: number = 0; + constructor(parent: VehicleAccessory) { super(parent, parent.platform.Service.Window, "Vent Windows", "vent_windows"); @@ -20,6 +23,9 @@ export class WindowService extends BaseService { .onSet((value) => this.setPosition(value, targetPosition)); this.parent.emitter.on("vehicle_data", (data) => { + this.latitude = data.drive_state.latitude; + this.longitude = data.drive_state.longitude; + const position = data.vehicle_state.fd_window * 25 + data.vehicle_state.fp_window * 25 + data.vehicle_state.rd_window * 25 + @@ -32,11 +38,9 @@ export class WindowService extends BaseService { async setPosition(value: CharacteristicValue, characteristic: Characteristic): Promise { value = Math.round(value as number / 100) * 100; this.log.debug("Setting windows to", value); - const { latitude, longitude } = - this.parent.accessory.context?.drive_state ?? {}; await this.parent.wakeUpAndWait() - .then(() => this.parent.vehicle.window_control(value === 100 ? "vent" : "close", latitude, longitude)) + .then(() => this.parent.vehicle.window_control(value === 100 ? "vent" : "close", this.latitude, this.longitude)) .then(() => characteristic.updateValue(value)); } } diff --git a/src/vehicle.ts b/src/vehicle.ts index b725b56..f33c4a7 100644 --- a/src/vehicle.ts +++ b/src/vehicle.ts @@ -31,12 +31,12 @@ import { WindowService } from "./vehicle-services/windows.js"; export type VehicleContext = { vin: string; state: string; - charge_state: ChargeState; + /*charge_state: ChargeState; climate_state: ClimateState; drive_state: DriveState; gui_settings: GUISettings; vehicle_config: VehicleConfig; - vehicle_state: VehicleState; + vehicle_state: VehicleState;*/ }; export interface VehicleDataEvent { @@ -100,10 +100,10 @@ export class VehicleAccessory { ]) .then((data) => { this.accessory.context.state = data.state; - this.accessory.context.charge_state = data.charge_state; + /*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.accessory.context.vehicle_state = data.vehicle_state;*/ this.emitter.emit("vehicle_data", data); }) .catch(({ status, data }) => {