From e03dedd53399b0d801c39f7712a97b508dec6c72 Mon Sep 17 00:00:00 2001 From: Brett Date: Sat, 15 Jun 2024 11:25:27 +1000 Subject: [PATCH] progress --- src/services/chargecurrent.ts | 39 +++++++++++++++++++ src/services/chargelimit.ts | 39 +++++++++++++++++++ src/services/chargeport.ts | 43 +++++++++++---------- src/services/{charge.ts => chargeswitch.ts} | 0 4 files changed, 100 insertions(+), 21 deletions(-) create mode 100644 src/services/chargecurrent.ts create mode 100644 src/services/chargelimit.ts rename src/services/{charge.ts => chargeswitch.ts} (100%) diff --git a/src/services/chargecurrent.ts b/src/services/chargecurrent.ts new file mode 100644 index 0000000..22f53b0 --- /dev/null +++ b/src/services/chargecurrent.ts @@ -0,0 +1,39 @@ +import { CharacteristicValue } from "homebridge"; +import { debounce } from "../utils/debounce.js"; +import { VehicleAccessory } from "../vehicle.js"; +import { BaseService } from "./base.js"; + +export class ChargeCurrentService extends BaseService { + constructor(parent: VehicleAccessory) { + super(parent, parent.platform.Service.Lightbulb, "charge current", "charge_current"); + + const on = this.service + .getCharacteristic(this.parent.platform.Characteristic.On) + .onGet(this.getOn.bind(this)); + + const level = this.service + .getCharacteristic(this.parent.platform.Characteristic.ChargingState) + .onGet(this.getLevel.bind(this)) + .onSet(debounce(this.setLevel.bind(this), 3000)); + + this.parent.emitter.on("vehicle_data", () => { + on.updateValue(this.getOn()); + level.updateValue(this.getLevel()); + }); + } + + getOn(): boolean { + return !!this.parent.accessory.context?.charge_state; + } + + getLevel(): number { + return this.parent.accessory.context?.charge_state?.charge_current_request ?? 16; + } + + setLevel(value: CharacteristicValue): Promise { + const min = 2; + const max = this.parent.accessory.context.charge_state.charge_current_request_max ?? 100; + value = Math.max(min, Math.min(max, value as number)); + return this.parent.vehicle.set_charge_limit(value).then(() => value); + } +} diff --git a/src/services/chargelimit.ts b/src/services/chargelimit.ts new file mode 100644 index 0000000..de49628 --- /dev/null +++ b/src/services/chargelimit.ts @@ -0,0 +1,39 @@ +import { CharacteristicValue } from "homebridge"; +import { debounce } from "../utils/debounce.js"; +import { VehicleAccessory } from "../vehicle.js"; +import { BaseService } from "./base.js"; + +export class ChargeLimitService extends BaseService { + constructor(parent: VehicleAccessory) { + super(parent, parent.platform.Service.Lightbulb, "charge limit", "charge_limit"); + + const on = this.service + .getCharacteristic(this.parent.platform.Characteristic.On) + .onGet(this.getOn.bind(this)); + + const level = this.service + .getCharacteristic(this.parent.platform.Characteristic.ChargingState) + .onGet(this.getLevel.bind(this)) + .onSet(debounce(this.setLevel.bind(this), 3000)); + + this.parent.emitter.on("vehicle_data", () => { + on.updateValue(this.getOn()); + level.updateValue(this.getLevel()); + }); + } + + getOn(): boolean { + return !!this.parent.accessory.context?.charge_state; + } + + getLevel(): number { + return this.parent.accessory.context?.charge_state?.charge_limit_soc ?? 50; + } + + setLevel(value: CharacteristicValue): Promise { + const min = this.parent.accessory.context.charge_state.charge_limit_soc_min ?? 50; + const max = this.parent.accessory.context.charge_state.charge_limit_soc_max ?? 100; + value = Math.max(min, Math.min(max, value as number)); + return this.parent.vehicle.set_charge_limit(value).then(() => value); + } +} diff --git a/src/services/chargeport.ts b/src/services/chargeport.ts index e69c339..fd1eb23 100644 --- a/src/services/chargeport.ts +++ b/src/services/chargeport.ts @@ -1,5 +1,4 @@ import { CharacteristicValue } from "homebridge"; -import { debounce } from "../utils/debounce.js"; import { VehicleAccessory } from "../vehicle.js"; import { BaseService } from "./base.js"; @@ -7,34 +6,36 @@ export class ChargePortService extends BaseService { constructor(parent: VehicleAccessory) { super(parent, parent.platform.Service.LockMechanism, "charge port", "charge_port"); - const on = this.service - .getCharacteristic(this.parent.platform.Characteristic.On) - .onGet(this.getOn.bind(this)); + const currentState = this.service + .getCharacteristic(this.parent.platform.Characteristic.LockCurrentState) + .onGet(this.getState.bind(this)); - const level = this.service - .getCharacteristic(this.parent.platform.Characteristic.ChargingState) - .onGet(this.getLevel.bind(this)) - .onSet(debounce(this.setLevel.bind(this), 3000)); + this.service + .getCharacteristic(this.parent.platform.Characteristic.LockTargetState) + .onGet(this.getState.bind(this)) + .onSet(this.setState.bind(this)); this.parent.emitter.on("vehicle_data", () => { - on.updateValue(this.getOn()); - level.updateValue(this.getLevel()); + currentState.updateValue(this.getState()); }); } - getOn(): boolean { - return this.parent.accessory.context?.charge_state?.user_charge_enable_request - ?? this.parent.accessory.context?.charge_state?.charge_enable_request; + getState(): number { + return this.parent.accessory.context?.charge_state?.charge_port_latch ? + this.parent.platform.Characteristic.LockTargetState.SECURED : + this.parent.platform.Characteristic.LockTargetState.UNSECURED; } - getLevel(): number { - return this.parent.accessory.context?.charge_state?.charge_limit_soc ?? 50; - } + setState(value: CharacteristicValue): Promise { + const open = value === this.parent.platform.Characteristic.LockTargetState.UNSECURED; - setLevel(value: CharacteristicValue): Promise { - const min = this.parent.accessory.context.charge_state.charge_limit_soc_min ?? 50; - const max = this.parent.accessory.context.charge_state.charge_limit_soc_max ?? 100; - value = Math.max(min, Math.min(max, value as number)); - return this.parent.vehicle.set_charge_limit(value).then(() => value); + if (open) { + return this.parent.vehicle.charge_port_door_open().then(() => + this.parent.platform.Characteristic.LockTargetState.SECURED + ); + } + return this.parent.vehicle.charge_port_door_close().then(() => + this.parent.platform.Characteristic.LockTargetState.UNSECURED + ); } } diff --git a/src/services/charge.ts b/src/services/chargeswitch.ts similarity index 100% rename from src/services/charge.ts rename to src/services/chargeswitch.ts