Skip to content

Commit

Permalink
Merge pull request #7 from diginize/testing
Browse files Browse the repository at this point in the history
v0.2.0
  • Loading branch information
silviokennecke authored Jul 24, 2021
2 parents 6c09291 + be80c00 commit f71f68e
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 20 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
1 change: 0 additions & 1 deletion src/accessories/advanced-charging.accessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
8 changes: 3 additions & 5 deletions src/accessories/advanced-lighting.accessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
Expand All @@ -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;

Expand Down
1 change: 0 additions & 1 deletion src/accessories/charging.accessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
4 changes: 2 additions & 2 deletions src/models/api/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ export interface Status {
*/
cfi: number;

lse: LedSaveEnergyEnum | number;
lse: LedSaveEnergyEnum | number | boolean;

ust: UnlockStateEnum | number;

Expand Down Expand Up @@ -562,7 +562,7 @@ export interface Status {

export type StatusWritable = Writable<Status> & {
/**
* Setter for lse
* Setter for lse in v1 api
*/
r2x: LedSaveEnergyEnum | number;
};
30 changes: 21 additions & 9 deletions src/services/go-e-charger-local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class GoEChargerLocal {
this._lastUpdate = Date.now();
}

async getStatus(hostname: string = this.hostname, cacheTtlMs: number = 2500): Promise<Status> {
async getStatus(cacheTtlMs: number = 2500, hostname: string = this.hostname): Promise<Status> {
// prevent multiple simultaneous api calls
if (this._currentRequest) {
return await this._currentRequest;
Expand All @@ -57,25 +57,31 @@ export class GoEChargerLocal {
return this._status as Status;
}

async updateValue<T extends StatusWritable, K extends keyof T>(hostname: string = this.hostname, payloadKey?: K, payloadValue?: T[K]): Promise<Status> {
const status = await this.performRequest(hostname, '/mqtt', payloadKey, payloadValue);
async updateValue<T extends StatusWritable, K extends keyof T>(payloadKey?: K, payloadValue?: T[K], hostname: string = this.hostname): Promise<Status> {
const status = await this.performRequest(hostname, '/mqtt?payload=' + this.transformGetParameter(payloadKey as any, payloadValue as any));
this.setStatus(status);

return status;
}

async updateValueV2<T extends StatusWritable, K extends keyof T>(payloadKey?: K, payloadValue?: T[K], hostname: string = this.hostname): Promise<Status> {
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<R = Status, I = StatusWritable, K extends keyof I = never>(hostname: string, path?: string, payloadKey?: K, payloadValue?: I[K]): Promise<R> {
protected transformGetParameter(key: string, value: string): string {
return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
}

protected performRequest<R = Status, I = StatusWritable, K extends keyof I = never>(hostname: string, path?: string, parseResponse: boolean = true): Promise<R> {
return new Promise<R>((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) => {
Expand All @@ -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);
Expand Down

0 comments on commit f71f68e

Please sign in to comment.