@elshaer/homebridge-lg-thinq
Version:
A Homebridge plugin for controlling/monitoring LG ThinQ device via LG ThinQ platform.
268 lines • 13.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AirPurifierStatus = exports.RotateSpeed = void 0;
const baseDevice_1 = require("../baseDevice");
var RotateSpeed;
(function (RotateSpeed) {
RotateSpeed[RotateSpeed["LOW"] = 2] = "LOW";
RotateSpeed[RotateSpeed["MEDIUM"] = 4] = "MEDIUM";
RotateSpeed[RotateSpeed["HIGH"] = 6] = "HIGH";
RotateSpeed[RotateSpeed["EXTRA"] = 7] = "EXTRA";
})(RotateSpeed = exports.RotateSpeed || (exports.RotateSpeed = {}));
// opMode = 14 => normal mode, can rotate speed
class AirPurifier extends baseDevice_1.baseDevice {
constructor(platform, accessory) {
super(platform, accessory);
this.platform = platform;
this.accessory = accessory;
const { Service: { AirPurifier, AirQualitySensor, Lightbulb, FilterMaintenance, Switch, }, Characteristic, } = this.platform;
const device = accessory.context.device;
// get the service if it exists, otherwise create a new service
// you can create multiple services for each accessory
this.serviceAirPurifier = accessory.getService(AirPurifier);
if (!this.serviceAirPurifier) {
this.serviceAirPurifier = accessory.addService(AirPurifier, 'Air Purifier', 'Air Purifier');
this.serviceAirPurifier.addOptionalCharacteristic(Characteristic.ConfiguredName);
this.serviceAirPurifier.updateCharacteristic(Characteristic.ConfiguredName, 'Air Purifier');
}
/**
* Required Characteristics: Active, CurrentAirPurifierState, TargetAirPurifierState
*/
this.serviceAirPurifier.getCharacteristic(Characteristic.Active)
.onGet(() => {
return this.Status.isPowerOn ? Characteristic.Active.ACTIVE : Characteristic.Active.INACTIVE;
})
.onSet(this.setActive.bind(this));
this.serviceAirPurifier.getCharacteristic(Characteristic.TargetAirPurifierState)
.onSet(this.setTargetAirPurifierState.bind(this));
/**
* Optional Characteristics: Name, RotationSpeed, SwingMode
*/
this.serviceAirPurifier.setCharacteristic(Characteristic.Name, device.name);
this.serviceAirPurifier.getCharacteristic(Characteristic.SwingMode).onSet(this.setSwingMode.bind(this));
this.serviceAirPurifier.getCharacteristic(Characteristic.RotationSpeed)
.onSet(this.setRotationSpeed.bind(this))
.setProps({ minValue: 0, maxValue: Object.keys(RotateSpeed).length / 2, minStep: 0.1 });
this.serviceAirQuality = accessory.getService(AirQualitySensor) || accessory.addService(AirQualitySensor);
// check if light is available
if ('airState.lightingState.displayControl' in device.snapshot || 'airState.lightingState.signal' in device.snapshot) {
this.serviceLight = accessory.getService(Lightbulb);
if (!this.serviceLight) {
this.serviceLight = accessory.addService(Lightbulb, device.name + ' - Light');
this.serviceLight.addOptionalCharacteristic(Characteristic.ConfiguredName);
this.serviceLight.updateCharacteristic(Characteristic.ConfiguredName, 'Light');
}
this.serviceLight.getCharacteristic(Characteristic.On).onSet(this.setLight.bind(this));
}
if (this.Status.filterMaxTime) {
this.serviceFilterMaintenance = accessory.getService(FilterMaintenance);
if (!this.serviceFilterMaintenance) {
this.serviceFilterMaintenance = accessory.addService(FilterMaintenance, 'Filter Maintenance', 'Filter Maintenance');
this.serviceFilterMaintenance.addOptionalCharacteristic(Characteristic.ConfiguredName);
this.serviceFilterMaintenance.updateCharacteristic(Characteristic.ConfiguredName, 'Filter Maintenance');
}
this.serviceFilterMaintenance.updateCharacteristic(Characteristic.Name, 'Filter Maintenance');
this.serviceAirPurifier.addLinkedService(this.serviceFilterMaintenance);
}
this.serviceAirFastMode = accessory.getService('Air Fast');
if (this.config.air_fast_mode) {
if (!this.serviceAirFastMode) {
this.serviceAirFastMode = accessory.addService(Switch, 'Air Fast', 'Air Fast');
this.serviceAirFastMode.addOptionalCharacteristic(Characteristic.ConfiguredName);
this.serviceAirFastMode.updateCharacteristic(Characteristic.ConfiguredName, 'Air Fast');
}
this.serviceAirFastMode.updateCharacteristic(Characteristic.Name, 'Air Fast');
this.serviceAirFastMode.getCharacteristic(Characteristic.On)
.onSet(this.setAirFastActive.bind(this));
}
else if (this.serviceAirFastMode) {
accessory.removeService(this.serviceAirFastMode);
this.serviceAirFastMode = null;
}
}
get Status() {
return new AirPurifierStatus(this.accessory.context.device.snapshot);
}
get config() {
return Object.assign({}, {
air_fast_mode: false,
}, super.config);
}
async setAirFastActive(value) {
var _a;
if (!this.Status.isPowerOn) {
return;
}
const device = this.accessory.context.device;
const isOn = value ? 1 : 0;
(_a = this.platform.ThinQ) === null || _a === void 0 ? void 0 : _a.deviceControl(device.id, {
dataKey: 'airState.miscFuncState.airFast',
dataValue: isOn,
}).then(() => {
device.data.snapshot['airState.miscFuncState.airFast'] = isOn;
this.updateAccessoryCharacteristic(device);
});
}
async setActive(value) {
var _a;
const device = this.accessory.context.device;
const isOn = value ? 1 : 0;
if (this.Status.isPowerOn && isOn) {
return; // don't send same status
}
this.platform.log.debug('Set Active State ->', value);
(_a = this.platform.ThinQ) === null || _a === void 0 ? void 0 : _a.deviceControl(device.id, {
dataKey: 'airState.operation',
dataValue: isOn,
}).then(() => {
device.data.snapshot['airState.operation'] = isOn;
this.updateAccessoryCharacteristic(device);
});
}
async setTargetAirPurifierState(value) {
var _a;
const device = this.accessory.context.device;
if (!this.Status.isPowerOn || (!!value !== this.Status.isNormalMode)) {
return; // just skip it
}
this.platform.log.debug('Set Target State ->', value);
(_a = this.platform.ThinQ) === null || _a === void 0 ? void 0 : _a.deviceControl(device.id, {
dataKey: 'airState.opMode',
dataValue: value ? 16 : 14,
});
}
async setRotationSpeed(value) {
var _a;
if (!this.Status.isPowerOn || !this.Status.isNormalMode) {
return;
}
this.platform.log.debug('Set Rotation Speed ->', value);
const device = this.accessory.context.device;
const values = Object.keys(RotateSpeed);
const windStrength = parseInt(values[Math.round(value) - 1]) || RotateSpeed.EXTRA;
(_a = this.platform.ThinQ) === null || _a === void 0 ? void 0 : _a.deviceControl(device.id, {
dataKey: 'airState.windStrength',
dataValue: windStrength,
}).then(() => {
device.data.snapshot['airState.windStrength'] = windStrength;
this.updateAccessoryCharacteristic(device);
});
}
async setSwingMode(value) {
var _a;
if (!this.Status.isPowerOn || !this.Status.isNormalMode) {
return;
}
const device = this.accessory.context.device;
const isSwing = value ? 1 : 0;
(_a = this.platform.ThinQ) === null || _a === void 0 ? void 0 : _a.deviceControl(device.id, {
dataKey: 'airState.circulate.rotate',
dataValue: isSwing,
}).then(() => {
device.data.snapshot['airState.circulate.rotate'] = isSwing;
this.updateAccessoryCharacteristic(device);
});
}
async setLight(value) {
var _a;
if (!this.Status.isPowerOn) {
return;
}
const device = this.accessory.context.device;
const isLightOn = value ? 1 : 0;
let dataKey = '';
if ('airState.lightingState.signal' in device.snapshot) {
dataKey = 'airState.lightingState.signal';
}
else if ('airState.lightingState.displayControl' in device.snapshot) {
dataKey = 'airState.lightingState.displayControl';
}
if (!dataKey) {
return;
}
(_a = this.platform.ThinQ) === null || _a === void 0 ? void 0 : _a.deviceControl(device.id, {
dataKey,
dataValue: isLightOn,
}).then(() => {
device.data.snapshot[dataKey] = isLightOn;
this.updateAccessoryCharacteristic(device);
});
}
updateAccessoryCharacteristic(device) {
super.updateAccessoryCharacteristic(device);
const { Characteristic, Characteristic: { TargetAirPurifierState, FilterChangeIndication, }, } = this.platform;
this.serviceAirPurifier.updateCharacteristic(Characteristic.Active, this.Status.isPowerOn ? 1 : 0);
this.serviceAirPurifier.updateCharacteristic(Characteristic.CurrentAirPurifierState, this.Status.isPowerOn ? 2 : 0);
this.serviceAirPurifier.updateCharacteristic(TargetAirPurifierState, this.Status.isNormalMode ? TargetAirPurifierState.MANUAL : TargetAirPurifierState.AUTO);
this.serviceAirPurifier.updateCharacteristic(Characteristic.SwingMode, this.Status.isSwing ? 1 : 0);
this.serviceAirPurifier.updateCharacteristic(Characteristic.RotationSpeed, this.Status.rotationSpeed);
if (this.Status.filterMaxTime && this.serviceFilterMaintenance) {
this.serviceFilterMaintenance.updateCharacteristic(Characteristic.FilterLifeLevel, this.Status.filterUsedTimePercent);
this.serviceFilterMaintenance.updateCharacteristic(FilterChangeIndication, this.Status.filterUsedTimePercent > 95 ? FilterChangeIndication.CHANGE_FILTER : FilterChangeIndication.FILTER_OK);
}
// airState.quality.sensorMon = 1 mean sensor always running even device not running
this.serviceAirQuality.updateCharacteristic(Characteristic.AirQuality, this.Status.airQuality.overall);
this.serviceAirQuality.updateCharacteristic(Characteristic.PM2_5Density, this.Status.airQuality.PM2);
this.serviceAirQuality.updateCharacteristic(Characteristic.PM10Density, this.Status.airQuality.PM10);
this.serviceAirQuality.updateCharacteristic(Characteristic.StatusActive, this.Status.airQuality.isOn);
if (this.serviceLight) {
this.serviceLight.updateCharacteristic(Characteristic.On, this.Status.isLightOn);
}
if (this.config.air_fast_mode && this.serviceAirFastMode) {
this.serviceAirFastMode.updateCharacteristic(Characteristic.On, this.Status.isAirFastEnable);
}
}
}
exports.default = AirPurifier;
class AirPurifierStatus {
constructor(data) {
this.data = data;
}
get isPowerOn() {
return this.data['airState.operation'];
}
get isLightOn() {
if ('airState.lightingState.signal' in this.data) {
return this.isPowerOn && this.data['airState.lightingState.signal'];
}
if ('airState.lightingState.displayControl' in this.data) {
return this.isPowerOn && this.data['airState.lightingState.displayControl'];
}
return false;
}
get isSwing() {
return (this.data['airState.circulate.rotate'] || 0);
}
get airQuality() {
return {
isOn: this.isPowerOn || !!this.data['airState.quality.sensorMon'],
overall: parseInt(this.data['airState.quality.overall']),
PM2: parseInt(this.data['airState.quality.PM2'] || '0'),
PM10: parseInt(this.data['airState.quality.PM10'] || '0'),
};
}
get rotationSpeed() {
const index = Object.keys(RotateSpeed).indexOf(parseInt(this.data['airState.windStrength']).toString());
return index !== -1 ? index + 1 : Object.keys(RotateSpeed).length / 2;
}
get isNormalMode() {
return this.data['airState.opMode'] === 14;
}
get filterUsedTimePercent() {
if (!this.filterMaxTime) {
return 0;
}
return Math.round((1 - (this.filterUseTime / this.filterMaxTime)) * 100);
}
get filterMaxTime() {
return this.data['airState.filterMngStates.maxTime'] || 0;
}
get filterUseTime() {
return this.data['airState.filterMngStates.useTime'] || 0;
}
get isAirFastEnable() {
return this.data['airState.miscFuncState.airFast'] || 0;
}
}
exports.AirPurifierStatus = AirPurifierStatus;
//# sourceMappingURL=AirPurifier.js.map