homebridge-lg-airco
Version:
Homebridge plugin to control a Smart Thinq enabled LG airco unit. Makes use of WideQ => https://github.com/sampsyo/wideq
225 lines • 10.8 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LgAirCoolerAccessory = void 0;
const wideq_adapter_1 = require("./lg/wideq-adapter");
const lg_airco_controller_1 = require("./lg/lg-airco-controller");
const async_utils_1 = require("./utils/async-utils");
const dummy_controller_1 = require("./lg/dummy-controller");
const python_utils_1 = require("./utils/python-utils");
class LgAirCoolerAccessory {
constructor(log, config, api) {
this.powerStateWillChange = false;
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 : () => { };
python_utils_1.PythonUtils.logDebug = this.logDebug;
this.informationService = new this.hap.Service.AccessoryInformation()
.setCharacteristic(this.hap.Characteristic.Manufacturer, 'LG')
.setCharacteristic(this.hap.Characteristic.Model, 'AIR CONDITIONER')
.setCharacteristic(this.hap.Characteristic.SerialNumber, this.config.model);
this.heaterCoolerService = new this.hap.Service.HeaterCooler(this.config.name);
setTimeout(async () => {
const airCoolers = await wideq_adapter_1.WideqAdapter.listAirCoolers(this.config.country, this.config.language, this.storagePath);
if (airCoolers.length === 1) {
this.airCooler = airCoolers[0];
}
else if (airCoolers.length > 1) {
const filteredAirCoolers = airCoolers.filter((airCooler) => {
return airCooler.deviceId === this.config.deviceId;
});
if (filteredAirCoolers.length === 1) {
this.airCooler = filteredAirCoolers[0];
}
else {
this.log('Cannot find air cooler with id: ' + this.config.deviceId);
return;
}
}
else {
this.log('No air coolers found!');
return;
}
if (this.config.dummy) {
this.controller = new dummy_controller_1.DummyController(this.airCooler, this.config.updateInterval, this.config.debug ? this.log : () => { });
}
else {
this.controller = new lg_airco_controller_1.LgAircoController(this.airCooler, this.config.updateInterval, this.storagePath, this.config.debug, this.logDebug);
}
this.handleRotationSpeedSetWithDebounce = async_utils_1.AsyncUtils.debounce((newFanSpeed) => {
this.controller.setFanSpeed(wideq_adapter_1.WideqAdapter.percentageToFanSpeed(newFanSpeed));
}, 5000);
this.heaterCoolerService.getCharacteristic(this.hap.Characteristic.Active)
.on("get", this.handleActiveGet.bind(this))
.on("set", this.handleActiveSet.bind(this));
this.heaterCoolerService.getCharacteristic(this.hap.Characteristic.CurrentHeaterCoolerState)
.on("get", this.handleCurrentHeaterCoolerStateGet.bind(this));
this.heaterCoolerService.getCharacteristic(this.hap.Characteristic.TargetHeaterCoolerState)
.setProps({
format: "uint8",
maxValue: 2,
minValue: 0,
validValues: [1, 2],
perms: ["pr", "pw", "ev"]
})
.on("get", this.handleTargetHeaterCoolerStateGet.bind(this))
.on("set", this.handleTargetHeaterCoolerStateSet.bind(this));
this.heaterCoolerService.getCharacteristic(this.hap.Characteristic.CurrentTemperature)
.on("get", this.handleCurrentTemperatureGet.bind(this));
this.heaterCoolerService.getCharacteristic(this.hap.Characteristic.CoolingThresholdTemperature)
.setProps({
format: "float",
unit: "celsius",
maxValue: this.config.maxCoolingTemp,
minValue: this.config.minCoolingTemp,
minStep: 1,
perms: ["pr", "pw", "ev"]
})
.on("get", this.handleCoolingThresholdTemperatureGet.bind(this))
.on("set", this.handleCoolingThresholdTemperatureSet.bind(this));
this.heaterCoolerService.getCharacteristic(this.hap.Characteristic.HeatingThresholdTemperature)
.setProps({
format: "float",
unit: "celsius",
maxValue: this.config.maxHeatingTemp,
minValue: this.config.minHeatingTemp,
minStep: 1,
perms: ["pr", "pw", "ev"]
})
.on("get", this.handleHeatingThresholdTemperatureGet.bind(this))
.on("set", this.handleHeatingThresholdTemperatureSet.bind(this));
this.heaterCoolerService.getCharacteristic(this.hap.Characteristic.RotationSpeed)
.on("get", this.handleRotationSpeedGet.bind(this))
.on("set", this.handleRotationSpeedSet.bind(this));
this.heaterCoolerService.getCharacteristic(this.hap.Characteristic.SwingMode)
.on("get", this.handleSwingModeGet.bind(this))
.on("set", this.handleSwingModeSet.bind(this));
});
}
getServices() {
return [
this.informationService,
this.heaterCoolerService
];
}
identify() {
this.log("Identify!");
}
handleActiveGet(callback) {
callback(null, this.controller.isPoweredOn() ? this.hap.Characteristic.Active.ACTIVE : this.hap.Characteristic.Active.INACTIVE);
}
handleActiveSet(value, callback) {
if (!this.powerStateWillChange && !this.controller.isPoweredOn() === (value === this.hap.Characteristic.Active.ACTIVE)) {
this.controller.setPowerState(value === this.hap.Characteristic.Active.ACTIVE);
}
else {
this.powerStateWillChange = false;
}
callback(null);
}
handleCurrentHeaterCoolerStateGet(callback) {
let currentHeaterCoolerState;
if (!this.controller.isPoweredOn()) {
currentHeaterCoolerState = this.hap.Characteristic.CurrentHeaterCoolerState.INACTIVE;
}
else {
switch (this.controller.getMode()) {
case wideq_adapter_1.Mode.COOL:
currentHeaterCoolerState = this.hap.Characteristic.CurrentHeaterCoolerState.COOLING;
break;
case wideq_adapter_1.Mode.HEAT:
currentHeaterCoolerState = this.hap.Characteristic.CurrentHeaterCoolerState.HEATING;
break;
case wideq_adapter_1.Mode.FAN:
case wideq_adapter_1.Mode.DRY:
case wideq_adapter_1.Mode.AI:
case wideq_adapter_1.Mode.AIRCLEAN:
case wideq_adapter_1.Mode.ACO:
case wideq_adapter_1.Mode.AROMA:
case wideq_adapter_1.Mode.ENERGY_SAVING:
case wideq_adapter_1.Mode.ENERGY_SAVER:
currentHeaterCoolerState = this.hap.Characteristic.CurrentHeaterCoolerState.IDLE;
break;
}
}
callback(null, currentHeaterCoolerState);
}
handleTargetHeaterCoolerStateGet(callback) {
this.handleCurrentHeaterCoolerStateGet(callback);
}
handleTargetHeaterCoolerStateSet(value, callback) {
if (!this.controller.isPoweredOn()) {
this.powerStateWillChange = true;
}
switch (value) {
case this.hap.Characteristic.TargetHeaterCoolerState.COOL:
this.controller.setMode(wideq_adapter_1.Mode.COOL);
break;
case this.hap.Characteristic.TargetHeaterCoolerState.HEAT:
this.controller.setMode(wideq_adapter_1.Mode.HEAT);
break;
case this.hap.Characteristic.TargetHeaterCoolerState.AUTO:
break;
}
callback(null);
}
handleCurrentTemperatureGet(callback) {
callback(null, this.controller.getCurrentTemperatureInCelsius());
}
handleCoolingThresholdTemperatureGet(callback) {
callback(null, this.controller.getTargetCoolingTemperatureInCelsius());
}
handleCoolingThresholdTemperatureSet(value, callback) {
if (!this.controller.isPoweredOn()) {
this.powerStateWillChange = true;
}
this.controller.setTargetCoolingTemperatureInCelsius(value);
callback(null);
}
handleHeatingThresholdTemperatureGet(callback) {
callback(null, this.controller.getTargetHeatingTemperatureInCelsius());
}
handleHeatingThresholdTemperatureSet(value, callback) {
if (!this.controller.isPoweredOn()) {
this.powerStateWillChange = true;
}
this.controller.setTargetHeatingTemperatureInCelsius(value);
callback(null);
}
handleRotationSpeedGet(callback) {
callback(null, Math.round(wideq_adapter_1.WideqAdapter.fanSpeedToPercentage(this.controller.getFanSpeed())));
}
handleRotationSpeedSet(value, callback) {
if (!this.controller.isPoweredOn()) {
this.powerStateWillChange = true;
}
this.handleRotationSpeedSetWithDebounce(value);
callback(null);
}
handleSwingModeGet(callback) {
callback(null, this.controller.getHorizontalSwingMode() === wideq_adapter_1.HSwingMode.ALL &&
this.controller.getVerticalSwingMode() === wideq_adapter_1.VSwingMode.ALL ?
this.hap.Characteristic.SwingMode.SWING_ENABLED : this.hap.Characteristic.SwingMode.SWING_DISABLED);
}
handleSwingModeSet(value, callback) {
if (!this.controller.isPoweredOn()) {
this.powerStateWillChange = true;
}
setTimeout(async () => {
if (value === this.hap.Characteristic.SwingMode.SWING_ENABLED) {
await this.controller.setHorizontalSwingMode(wideq_adapter_1.HSwingMode.ALL);
await async_utils_1.AsyncUtils.sleep(2000);
await this.controller.setVerticalSwingMode(wideq_adapter_1.VSwingMode.ALL);
}
else {
await this.controller.setHorizontalSwingMode(wideq_adapter_1.HSwingMode.OFF);
await async_utils_1.AsyncUtils.sleep(2000);
await this.controller.setVerticalSwingMode(wideq_adapter_1.VSwingMode.OFF);
}
});
callback(null);
}
}
exports.LgAirCoolerAccessory = LgAirCoolerAccessory;
//# sourceMappingURL=lg-airco-accessory.js.map