Skip to content
This repository has been archived by the owner on Oct 30, 2021. It is now read-only.

Commit

Permalink
Fixes & cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Van den Abeele committed Jun 2, 2020
1 parent 01c042a commit 8f757a6
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 223 deletions.
11 changes: 9 additions & 2 deletions src/lg-airco-accessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import {
Units
} from "homebridge";

import {AirCooler, HSwingMode, Mode, VSwingMode, WideqAdapter} from "./lg/wideq-adapter";
import {AirCooler, FanSpeed, HSwingMode, Mode, VSwingMode, WideqAdapter} from "./lg/wideq-adapter";
import {LgAircoController} from "./lg/lg-airco-controller";
import {AsyncUtils} from "./utils/async-utils";

export class LgAirCoolerAccessory implements AccessoryPlugin {

Expand All @@ -29,6 +30,8 @@ export class LgAirCoolerAccessory implements AccessoryPlugin {
private airCooler: AirCooler;
private controller: LgAircoController;

private handleRotationSpeedSetWithDebounce: Function;

constructor(log: Logging, config: AccessoryConfig, api: API) {
this.hap = api.hap;
this.log = log;
Expand Down Expand Up @@ -61,8 +64,12 @@ export class LgAirCoolerAccessory implements AccessoryPlugin {
this.log('No air coolers found!');
return;
}

//TODO: Update interval from config, default now is 30 seconds.
this.controller = new LgAircoController(this.airCooler);
this.handleRotationSpeedSetWithDebounce = AsyncUtils.debounce((newFanSpeed: FanSpeed) => {
this.controller.setFanSpeed(newFanSpeed);
}, 5000);

this.heaterCoolerService.getCharacteristic(this.hap.Characteristic.Active)
.on(CharacteristicEventTypes.GET, this.handleActiveGet.bind(this))
Expand Down Expand Up @@ -223,7 +230,7 @@ export class LgAirCoolerAccessory implements AccessoryPlugin {

private handleRotationSpeedSet(value: CharacteristicValue, callback: CharacteristicSetCallback): void {
console.log('Setting FAN SPEED: ' + value);
//TODO: Implement!
this.handleRotationSpeedSetWithDebounce(value);
callback(null);
}

Expand Down
7 changes: 5 additions & 2 deletions src/lg/lg-airco-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,17 @@ export class LgAircoController {
}

public async setFanSpeed(newFanSpeed: FanSpeed): Promise<void> {
if (this.fanSpeed !== newFanSpeed) {
/*if (this.fanSpeed !== newFanSpeed) {
const succeeded: boolean = await this.adapter.setFanSpeed(this.airCooler.deviceId, newFanSpeed);
if (succeeded) {
this.isOn = true;
this.fanSpeed = newFanSpeed;
} else {
throw new Error('Could not set new fan speed of the AC unit!');
}
}
}*/
console.log('SPEED SET!');
this.isOn = true;
this.fanSpeed = newFanSpeed;
}
}
199 changes: 0 additions & 199 deletions src/lg/wideq.js

This file was deleted.

16 changes: 16 additions & 0 deletions src/utils/async-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export class AsyncUtils {

public static debounce = <F extends (...args: any[]) => any>(func: F, waitFor: number) => {
let timeout: ReturnType<typeof setTimeout> | null = null;

const debounced = (...args: Parameters<F>) => {
if (timeout !== null) {
clearTimeout(timeout);
timeout = null;
}
timeout = setTimeout(() => func(...args), waitFor);
};

return debounced as (...args: Parameters<F>) => ReturnType<F>;
};
}
20 changes: 0 additions & 20 deletions src/utils/utils.js

This file was deleted.

0 comments on commit 8f757a6

Please sign in to comment.