diff --git a/CHANGELOG.md b/CHANGELOG.md index 31e34e6..c51703d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## v0.2.0 + +- change led save energy accessory to use api v2 #5 + ## v0.1.1 - fix led brightness updates diff --git a/README.md b/README.md index 9a2bae4..4e3ef08 100644 --- a/README.md +++ b/README.md @@ -21,12 +21,15 @@ A homebridge integration for the go-eCharger wallbox. - go-eCharger installed and connected to the same network - activated HTTP API v1 (App / Internet / Erweiterte Einstellungen / Aktiviere lokale HTTP API v1) +- activated HTTP API v2 (App / Internet / Erweiterte Einstellungen / Aktiviere lokale HTTP API v2) ## Configuration You can use this plugin to add multiple go-eChargers to HomeKit. Therefore, multiple platform configurations are needed. If you are using [homebridge-config-ui-x](https://github.com/oznu/homebridge-config-ui-x) you can simply use the ui to configure the plugin. +> :warning: Adding more than one charger is not fully tested. Please provide feedback if issues with multiple chargers occur. + Basically each platform configuration consists of three configuration values: | key | type | description | diff --git a/package-lock.json b/package-lock.json index 6570081..0100d8f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "homebridge-go-e-charger", - "version": "0.1.1", + "version": "0.2.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 8054fd3..1749c1b 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "displayName": "Homebridge go-eCharger", "name": "homebridge-go-e-charger", - "version": "0.1.1", + "version": "0.2.0", "description": "A homebridge integration for the go-e charger wallbox.", "license": "AGPL-3.0-or-later", "repository": { diff --git a/src/accessories/advanced-charging.accessory.ts b/src/accessories/advanced-charging.accessory.ts index 0ff3516..4358624 100644 --- a/src/accessories/advanced-charging.accessory.ts +++ b/src/accessories/advanced-charging.accessory.ts @@ -80,7 +80,6 @@ export class AdvancedChargingAccessory extends AbstractAccessory { const service = GoEChargerLocal.getService(this.instanceId); const state = await service .updateValue( - service.hostname, 'ust', value === this.platform.Characteristic.LockTargetState.UNSECURED ? UnlockStateEnum.lockWhileCarPluggedIn : UnlockStateEnum.alwaysLocked ); diff --git a/src/accessories/advanced-lighting.accessory.ts b/src/accessories/advanced-lighting.accessory.ts index 2b5465a..b09ca27 100644 --- a/src/accessories/advanced-lighting.accessory.ts +++ b/src/accessories/advanced-lighting.accessory.ts @@ -96,7 +96,6 @@ export class AdvancedLightingAccessory extends AbstractAccessory { const service = GoEChargerLocal.getService(this.instanceId); const state = await service .updateValue( - service.hostname, 'lbr', Math.round((value as number) / 100 * 255) ); @@ -108,10 +107,9 @@ export class AdvancedLightingAccessory extends AbstractAccessory { async setLedSaveEnergy(value: CharacteristicValue) { const service = GoEChargerLocal.getService(this.instanceId); const state = await service - .updateValue( - service.hostname, - 'r2x', - value ? LedSaveEnergyEnum.activated : LedSaveEnergyEnum.deactivated + .updateValueV2( + 'lse', + value as boolean ); this._switchLedSaveEnergyStatus = state.lse == LedSaveEnergyEnum.activated; diff --git a/src/accessories/charging.accessory.ts b/src/accessories/charging.accessory.ts index 2b3e871..ea26d75 100644 --- a/src/accessories/charging.accessory.ts +++ b/src/accessories/charging.accessory.ts @@ -80,7 +80,6 @@ export class ChargingAccessory extends AbstractAccessory { const service = GoEChargerLocal.getService(this.instanceId); const state = await service .updateValue( - service.hostname, 'ast', value === this.platform.Characteristic.LockTargetState.UNSECURED ? AccessStateEnum.open : AccessStateEnum.rfidOrAppNeeded ); diff --git a/src/models/api/status.ts b/src/models/api/status.ts index 423e015..dd49e78 100644 --- a/src/models/api/status.ts +++ b/src/models/api/status.ts @@ -238,7 +238,7 @@ export interface Status { */ cfi: number; - lse: LedSaveEnergyEnum | number; + lse: LedSaveEnergyEnum | number | boolean; ust: UnlockStateEnum | number; @@ -562,7 +562,7 @@ export interface Status { export type StatusWritable = Writable & { /** - * Setter for lse + * Setter for lse in v1 api */ r2x: LedSaveEnergyEnum | number; }; \ No newline at end of file diff --git a/src/services/go-e-charger-local.ts b/src/services/go-e-charger-local.ts index 46dc5c0..cdff2fc 100644 --- a/src/services/go-e-charger-local.ts +++ b/src/services/go-e-charger-local.ts @@ -35,7 +35,7 @@ export class GoEChargerLocal { this._lastUpdate = Date.now(); } - async getStatus(hostname: string = this.hostname, cacheTtlMs: number = 2500): Promise { + async getStatus(cacheTtlMs: number = 2500, hostname: string = this.hostname): Promise { // prevent multiple simultaneous api calls if (this._currentRequest) { return await this._currentRequest; @@ -57,25 +57,31 @@ export class GoEChargerLocal { return this._status as Status; } - async updateValue(hostname: string = this.hostname, payloadKey?: K, payloadValue?: T[K]): Promise { - const status = await this.performRequest(hostname, '/mqtt', payloadKey, payloadValue); + async updateValue(payloadKey?: K, payloadValue?: T[K], hostname: string = this.hostname): Promise { + const status = await this.performRequest(hostname, '/mqtt?payload=' + this.transformGetParameter(payloadKey as any, payloadValue as any)); this.setStatus(status); return status; } + async updateValueV2(payloadKey?: K, payloadValue?: T[K], hostname: string = this.hostname): Promise { + await this.performRequest(hostname, '/api/set?' + this.transformGetParameter(payloadKey as string, JSON.stringify(payloadValue)), false); + + return await this.getStatus(0); + } + protected getBaseUrl(hostname: string, path?: string): string { return `${this.protocol}://${hostname}${this.basePath}${path || ''}`; } - protected performRequest(hostname: string, path?: string, payloadKey?: K, payloadValue?: I[K]): Promise { + protected transformGetParameter(key: string, value: string): string { + return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`; + } + + protected performRequest(hostname: string, path?: string, parseResponse: boolean = true): Promise { return new Promise((resolve, reject) => { let url = this.getBaseUrl(hostname, path); - if (payloadKey !== undefined && payloadValue !== undefined) { - url += `?payload=${encodeURIComponent(payloadKey as string)}=${encodeURIComponent(payloadValue as any)}`; - } - this.log?.debug('Performing request to:', url); this.httpModule.get(url, (response) => { @@ -89,7 +95,13 @@ export class GoEChargerLocal { try { this.log?.debug(`Received response for request to "${url}":`, data); - const result = JSON.parse(data); + let result: any; + if (parseResponse) { + result = JSON.parse(data); + } else { + result = data; + } + resolve(result); } catch (e) { this.log?.error(`Error parsing response from request to "${url}":`, data);