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

Commit

Permalink
AC update state now working correctly!
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Van den Abeele committed Aug 13, 2019
1 parent f0ed39e commit c5b4014
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
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.0.975",
"version": "0.0.998",
"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
58 changes: 43 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ function HomebridgeLgAirco(log, config) {
this.utils = new Utils();
this.debouncedRotationHandler = this.utils.debounce(this.setActualRotationSpeed, 5000);

this.intervalWhenOn = null;
this.intervalWhenOff = null;

this.state = {
isOn: false,
isCooling: false,
Expand All @@ -44,7 +47,11 @@ function HomebridgeLgAirco(log, config) {
.then((status) => {
if (status) {
if (status.Operation) {
this.state.isOn = this.wideq.paramConversion.isOn(status.Operation.value);
const newValue = this.wideq.paramConversion.isOn(status.Operation.value);
this.state.isOn = newValue;
if (newValue !== this.state.isOn) {
this.setCorrectInterval();
}
}
if (status.OpMode) {
this.state.isCooling = this.wideq.paramConversion.isCooling(status.OpMode.value);
Expand All @@ -63,7 +70,7 @@ function HomebridgeLgAirco(log, config) {
this.state.targetTemp = status.TempCfg.value;
}

console.log(this.state);
//console.log(this.state);

this.getActive((unknown, value) => {
this.service.getCharacteristic(Characteristic.Active).updateValue(value);
Expand All @@ -80,9 +87,24 @@ function HomebridgeLgAirco(log, config) {
});
};

setInterval(() => {
this.updateState();
}, 60 * 1000);
this.setCorrectInterval = () => {
clearInterval(this.intervalWhenOff);
clearInterval(this.intervalWhenOn);

if (this.state.isOn) {
console.log('Setting fast ac status interval');
this.intervalWhenOn = setInterval(() => {
this.updateState()
}, 60 * 1000);
} else {
console.log('Setting slow ac status interval');
this.intervalWhenOff = setInterval(() => {
this.updateState();
}, 10 * 60 * 1000);
}
};

this.setCorrectInterval();
this.updateState();
}

Expand All @@ -94,11 +116,13 @@ HomebridgeLgAirco.prototype = {
setActive: function (shouldBeActive, callback) {
const me = this;

if (shouldBeActive !== (me.state.isOn ? 1 : 0)) {
const isOn = shouldBeActive === Characteristic.Active.ACTIVE;
me.wideq.turnOnOrOff(me.deviceId, isOn)
const turnOnOrOff = shouldBeActive === Characteristic.Active.ACTIVE;
if (me.state.isOn !== turnOnOrOff) {
console.log('Turning ac ' + (turnOnOrOff ? 'on' : 'off') + '!');
me.wideq.turnOnOrOff(me.deviceId, turnOnOrOff)
.then((done) => {
me.state.isOn = isOn;
me.state.isOn = turnOnOrOff;
me.setCorrectInterval();
});
}

Expand Down Expand Up @@ -137,15 +161,19 @@ HomebridgeLgAirco.prototype = {
setTargetHeaterCoolerState: function (targetState, callback) {
const me = this;

let turnOn = false;
let turnOnOrOff = false;
if (targetState !== Characteristic.TargetHeaterCoolerState.AUTO) {
turnOn = true;
turnOnOrOff = true;
}

me.wideq.turnOnOrOff(me.deviceId, turnOn).then((done) => {
me.state.isOn = turnOn;
me.state.isOn = turnOn;
});
if(me.state.isOn !== turnOnOrOff) {
console.log('Turning ac ' + (turnOnOrOff ? 'on' : 'off') + '!');
me.wideq.turnOnOrOff(me.deviceId, turnOnOrOff)
.then((done) => {
me.state.isOn = turnOnOrOff;
me.setCorrectInterval();
});
}

callback(null);
},
Expand Down

0 comments on commit c5b4014

Please sign in to comment.