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

Commit

Permalink
Cleaned up logging and added debug config flag. wideq state now store…
Browse files Browse the repository at this point in the history
…d inside the homebridge folder!
  • Loading branch information
Kevin Van den Abeele committed Jun 4, 2020
1 parent b5c84d2 commit c2d38c6
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 63 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ Next open the config.json that contains your Homebridge configuration and add a
"minCoolingTemp": 18,
"maxHeatingTemp": 30,
"minHeatingTemp": 5,
"updateInterval": 60000
"updateInterval": 60000,
"debug": false,
"dummy": false
}
```

Expand All @@ -32,6 +34,8 @@ The `minCoolingTemp` field is the minimum settable temperature when in COOLING m
The `maxHeatingTemp` field is the maximum settable temperature when in HEATING mode.
The `minHeatingTemp` field is the minimum settable temperature when in HEATING mode.
The `updateInterval` field is the interval that is used to fetch new state data from the AC unit. In milliseconds!
The `debug` field is the boolean that enables or disables debug logging, set this to false unless collecting logs.
The `dummy` field is the boolean that enables mocking out the LG API and will instead use a dummy AC unit with no network calls, only for development & testing!

The initial state will be fetched shortly after booting your Homebridge instance.
After that an update of the state is performed every minute.
Expand All @@ -47,14 +51,12 @@ After that an update of the state is performed every minute.
- Open a terminal on the device where you installed this plugin and type: `npm root -g`
- Navigate to the path that the previous command has printed out
- Enter the folder of the plugin to where the wideq files are: `cd homebridge-lg-airco/resources/wideq`
- Execute the command `sudo python3 example.py -c country-code -l language-code` where you should replace `country-code` and `language-code` with the respective values.
For example: `sudo python3 example.py -c BE -l en-UK`
- Execute the command `python3 example.py -c country-code -l language-code -p path-to-homebridge-folder` where you should replace `country-code`, `language-code` and `path-to-homebridge-folder` with the respective values.
For example: `python3 example.py -c BE -l en-UK -p /home/pi/.homebridge`
- Follow the instructions on the screen, and paste the resulting URL back into the terminal.
The command will now print out a list of all known devices for your account. If wanted select the one you want and paste the value in the `config.json` file at the `deviceId` field of the corresponding accessory definition.
It will also generate a file in which the session is stored.
- Set the file permissions on the state file with this command (osx/linux): `sudo chmod a+rw wideq_state.json`
It will also generate a file in which the session is stored in the Homebridge folder.
- The plugin is now fully ready to be used in Homebridge!
- Note! If you update the plugin, make sure to re-execute the command above with your specific parameters! (because updating the plugin removes the file in which the session is stored!)

This code makes use of the `WideQ` library, more information [here](https://github.com/sampsyo/wideq).
Some changes have been made to the included version of the WideQ library.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "homebridge-lg-airco",
"version": "0.1.0-beta2",
"version": "0.1.0-beta3",
"description": "Homebridge plugin to control a Smart Thinq enabled LG airco unit. Makes use of WideQ => https://github.com/sampsyo/wideq",
"main": "src/index.js",
"scripts": {
Expand Down
11 changes: 8 additions & 3 deletions resources/wideq/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import logging
from typing import List

STATE_FILE = 'wideq_state.json'
LOGGER = logging.getLogger("wideq.example")


Expand Down Expand Up @@ -207,11 +206,12 @@ def example_command(client, cmd, args):
func(client, *args)


def example(country: str, language: str, verbose: bool,
def example(country: str, language: str, path: str, verbose: bool,
cmd: str, args: List[str]) -> None:
if verbose:
wideq.set_log_level(logging.DEBUG)

STATE_FILE = path
# Load the current state for the example.
try:
with open(STATE_FILE) as f:
Expand Down Expand Up @@ -279,6 +279,11 @@ def main() -> None:
help='verbose mode to help debugging',
action='store_true', default=False
)
parser.add_argument(
'--path', '-p',
help='path parameter specifying where to store the wideq state',
default='wideq_state.json'
)

args = parser.parse_args()
country_regex = re.compile(r"^[A-Z]{2,3}$")
Expand All @@ -294,7 +299,7 @@ def main() -> None:
" got: '%s'",
args.language)
exit(1)
example(args.country, args.language, args.verbose, args.cmd, args.args)
example(args.country, args.language, args.path, args.verbose, args.cmd, args.args)


if __name__ == '__main__':
Expand Down
17 changes: 13 additions & 4 deletions src/lg-airco-accessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ import {LgAircoController} from "./lg/lg-airco-controller";
import {AsyncUtils} from "./utils/async-utils";
import {DummyController} from "./lg/dummy-controller";
import {Controller} from "./lg/controller";
import {PythonUtils} from "./utils/python-utils";

export class LgAirCoolerAccessory implements AccessoryPlugin {

private readonly hap: HAP;
private readonly log: Logging;
private readonly config: AccessoryConfig;
private readonly storagePath: string;
private readonly logDebug: Function;

private readonly informationService: Service;
private readonly heaterCoolerService: Service;
Expand All @@ -39,6 +42,9 @@ export class LgAirCoolerAccessory implements AccessoryPlugin {
this.hap = api.hap;
this.log = log;
this.config = config;
this.storagePath = api.user.storagePath() + '/wideq_state.json';
this.logDebug = this.config.debug ? this.log : () => {};
PythonUtils.logDebug = this.logDebug;

this.informationService = new this.hap.Service.AccessoryInformation()
.setCharacteristic(this.hap.Characteristic.Manufacturer, 'LG')
Expand All @@ -47,8 +53,7 @@ export class LgAirCoolerAccessory implements AccessoryPlugin {
this.heaterCoolerService = new this.hap.Service.HeaterCooler(this.config.name);

setTimeout(async () => {
console.log(this.config);
const airCoolers: AirCooler[] = await WideqAdapter.listAirCoolers(this.config.country, this.config.language);
const airCoolers: AirCooler[] = await WideqAdapter.listAirCoolers(this.config.country, this.config.language, this.storagePath);

if (airCoolers.length === 1) {
this.airCooler = airCoolers[0];
Expand All @@ -68,8 +73,12 @@ export class LgAirCoolerAccessory implements AccessoryPlugin {
return;
}

this.controller = new LgAircoController(this.airCooler, config.updateInterval);
//this.controller = new DummyController(this.airCooler, config.updateInterval);
if (this.config.dummy) {
this.controller = new DummyController(this.airCooler, this.config.updateInterval, this.config.debug ? this.log : () => {});
} else {
this.controller = new LgAircoController(this.airCooler, this.config.updateInterval, this.storagePath, this.config.debug, this.logDebug);
}

this.handleRotationSpeedSetWithDebounce = AsyncUtils.debounce((newFanSpeed: number) => {
this.controller.setFanSpeed(WideqAdapter.percentageToFanSpeed(newFanSpeed));
}, 5000);
Expand Down
32 changes: 18 additions & 14 deletions src/lg/dummy-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import {Controller} from "./controller";

export class DummyController extends Controller {

constructor(airCooler: AirCooler, updateInterval: number = 30000) {
private logDebug: Function;

constructor(airCooler: AirCooler, updateInterval: number, debugLogger: Function) {
super();

this.airCooler = airCooler;
Expand All @@ -19,6 +21,8 @@ export class DummyController extends Controller {

this.targetCoolingTemperatureInCelsius = 18;
this.targetHeatingTemperatureInCelsius = 21;

this.logDebug = debugLogger;
}

public isPoweredOn(): boolean {
Expand All @@ -28,33 +32,33 @@ export class DummyController extends Controller {
public async setPowerState(powerOn: boolean): Promise<void> {
if (this.isOn !== powerOn) {
this.isOn = powerOn;
console.log('Setting power value: ' + (powerOn ? 'ON' : 'OFF'));
this.logDebug('Setting power value: ' + (powerOn ? 'ON' : 'OFF'));
}
}

public getMode(): Mode {
console.log('Getting mode value: ' + this.mode);
this.logDebug('Getting mode value: ' + this.mode);
return this.mode;
}

public async setMode(newTargetMode: Mode): Promise<void> {
if (this.mode !== newTargetMode) {
this.isOn = true;
this.mode = newTargetMode;
console.log('Setting mode value: ' + newTargetMode);
this.logDebug('Setting mode value: ' + newTargetMode);
await this.setTargetTemperatureInCelsius(this.mode === Mode.COOL ? this.targetCoolingTemperatureInCelsius : this.targetHeatingTemperatureInCelsius);
} else {
this.setPowerState(true);
}
}

public getCurrentTemperatureInCelsius(): number {
console.log('Getting current temperature value: ' + this.currentTemperatureInCelsius);
this.logDebug('Getting current temperature value: ' + this.currentTemperatureInCelsius);
return this.currentTemperatureInCelsius;
}

public getTargetCoolingTemperatureInCelsius(): number {
console.log('Getting target temperature value: ' + this.targetCoolingTemperatureInCelsius);
this.logDebug('Getting target temperature value: ' + this.targetCoolingTemperatureInCelsius);
return this.targetCoolingTemperatureInCelsius;
}

Expand All @@ -70,7 +74,7 @@ export class DummyController extends Controller {
}

public getTargetHeatingTemperatureInCelsius(): number {
console.log('Getting target heating temperature value: ' + this.targetHeatingTemperatureInCelsius);
this.logDebug('Getting target heating temperature value: ' + this.targetHeatingTemperatureInCelsius);
return this.targetHeatingTemperatureInCelsius;
}

Expand All @@ -89,46 +93,46 @@ export class DummyController extends Controller {
if (this.targetTemperatureInCelsius !== newTargetTemperatureInCelsius) {
this.isOn = true;
this.targetTemperatureInCelsius = newTargetTemperatureInCelsius;
console.log('Setting temperature value: ' + newTargetTemperatureInCelsius);
this.logDebug('Setting temperature value: ' + newTargetTemperatureInCelsius);
}
}

public getVerticalSwingMode(): VSwingMode {
console.log('Getting v-swing value: ' + this.swingModeV);
this.logDebug('Getting v-swing value: ' + this.swingModeV);
return this.swingModeV;
}

public async setVerticalSwingMode(newVerticalSwingMode: VSwingMode): Promise<void> {
if (this.swingModeV !== newVerticalSwingMode) {
this.isOn = true;
this.swingModeV = newVerticalSwingMode;
console.log('Setting swing V value: ' + newVerticalSwingMode);
this.logDebug('Setting swing V value: ' + newVerticalSwingMode);
}
}

public getHorizontalSwingMode(): HSwingMode {
console.log('Getting h-swing value: ' + this.swingModeH);
this.logDebug('Getting h-swing value: ' + this.swingModeH);
return this.swingModeH;
}

public async setHorizontalSwingMode(newHorizontalSwingMode: HSwingMode): Promise<void> {
if (this.swingModeH !== newHorizontalSwingMode) {
this.isOn = true;
this.swingModeH = newHorizontalSwingMode;
console.log('Setting swing H value: ' + newHorizontalSwingMode);
this.logDebug('Setting swing H value: ' + newHorizontalSwingMode);
}
}

public getFanSpeed(): FanSpeed {
console.log('Getting fan speed value: ' + this.fanSpeed);
this.logDebug('Getting fan speed value: ' + this.fanSpeed);
return this.fanSpeed;
}

public async setFanSpeed(newFanSpeed: FanSpeed): Promise<void> {
if (this.fanSpeed !== newFanSpeed) {
this.isOn = true;
this.fanSpeed = newFanSpeed;
console.log('Setting fan speed value: ' + newFanSpeed);
this.logDebug('Setting fan speed value: ' + newFanSpeed);
}
}
}
4 changes: 2 additions & 2 deletions src/lg/lg-airco-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ export class LgAircoController extends Controller {

private readonly adapter: WideqAdapter;

constructor(airCooler: AirCooler, updateInterval: number = 30000) {
constructor(airCooler: AirCooler, updateInterval: number, storagePath: string, debugEnabled: boolean, debugLogger: Function) {
super();

this.adapter = new WideqAdapter(airCooler.country, airCooler.language);
this.adapter = new WideqAdapter(airCooler.country, airCooler.language, storagePath, debugEnabled, debugLogger);
this.airCooler = airCooler;

this.update();
Expand Down
Loading

0 comments on commit c2d38c6

Please sign in to comment.