diff --git a/src/platform.ts b/src/platform.ts index dd9d061..3517a59 100644 --- a/src/platform.ts +++ b/src/platform.ts @@ -57,7 +57,7 @@ export class TeslaFleetApiPlatform implements DynamicPlatformPlugin { .then(async ({ vehicles, energy_sites }) => { vehicles.forEach(async (product) => { this.TeslaFleetApi.vehicle!; - const uuid = this.api.hap.uuid.generate(product.vin); + const uuid = this.api.hap.uuid.generate(`${PLATFORM_NAME}:${product.vin}`); const cachedAccessory = this.accessories.find( (accessory) => accessory.UUID === uuid ); diff --git a/src/vehicle-services/chargeport.ts b/src/vehicle-services/chargeport.ts index 40f4660..fb9c639 100644 --- a/src/vehicle-services/chargeport.ts +++ b/src/vehicle-services/chargeport.ts @@ -8,12 +8,23 @@ export class ChargePortService extends BaseService { const currentState = this.service .getCharacteristic(this.parent.platform.Characteristic.LockCurrentState); - //.onGet(this.getState.bind(this)); const targetState = this.service .getCharacteristic(this.parent.platform.Characteristic.LockTargetState) - //.onGet(this.getState.bind(this)) - .onSet((value) => this.setState(value, currentState)); // Set current instead + .onSet(async (value) => { + targetState.updateValue(value); + await this.parent.wakeUpAndWait().then(() => + value === 1 ? + this.vehicle.charge_port_door_close() + .then(() => + currentState.updateValue(1) + ) : + this.vehicle.charge_port_door_open() + .then(() => + currentState.updateValue(0) + ) + ); + }); this.parent.emitter.on("vehicle_data", (data) => { const state = (data.charge_state.charge_port_latch === "Engaged") ? 1 : 0; @@ -21,18 +32,4 @@ export class ChargePortService extends BaseService { targetState.updateValue(state); }); } - - async setState(value: CharacteristicValue, characteristic: Characteristic): Promise { - await this.parent.wakeUpAndWait().then(() => - value === 1 ? - this.vehicle.charge_port_door_close() - .then(() => - characteristic.updateValue(this.parent.platform.Characteristic.LockTargetState.SECURED) - ) : - this.vehicle.charge_port_door_open() - .then(() => - characteristic.updateValue(this.parent.platform.Characteristic.LockTargetState.UNSECURED) - ) - ); - } } diff --git a/src/vehicle-services/lock.ts b/src/vehicle-services/lock.ts index 0a66176..dca224a 100644 --- a/src/vehicle-services/lock.ts +++ b/src/vehicle-services/lock.ts @@ -8,29 +8,24 @@ export class LockService extends BaseService { const currentState = this.service .getCharacteristic(this.parent.platform.Characteristic.LockCurrentState); - //.onGet(this.getState.bind(this)); const targetState = this.service .getCharacteristic(this.parent.platform.Characteristic.LockTargetState) - //.onGet(this.getState.bind(this)) - .onSet((value) => this.setState(value, targetState)); + .onSet(async (value) => { + targetState.updateValue(value); + await this.parent.wakeUpAndWait().then(() => + value ? + this.parent.vehicle.door_lock() + .then(() => currentState.updateValue(1)) : + this.parent.vehicle.door_unlock() + .then(() => currentState.updateValue(0)) + ); + }); this.parent.emitter.on("vehicle_data", (data) => { - currentState.updateValue(data.vehicle_state.locked ? - this.platform.Characteristic.LockTargetState.SECURED : - this.platform.Characteristic.LockTargetState.UNSECURED); + const state = data.vehicle_state.locked ? 1 : 0; + currentState.updateValue(state); + targetState.updateValue(state); }); } - - async setState(value: CharacteristicValue, characteristic: Characteristic): Promise { - const open = value === this.parent.platform.Characteristic.LockTargetState.UNSECURED; - - await this.parent.wakeUpAndWait().then(() => - open ? - this.parent.vehicle.door_lock() - .then(() => characteristic.updateValue(this.parent.platform.Characteristic.LockTargetState.SECURED)) : - this.parent.vehicle.door_unlock() - .then(() => characteristic.updateValue(this.parent.platform.Characteristic.LockTargetState.UNSECURED)) - ); - } -} +} \ No newline at end of file