UNPKG

homebridge-lg-thinq-fanfix

Version:

A Homebridge plugin for controlling/monitoring LG ThinQ device via LG ThinQ platform.

276 lines 14.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RefrigeratorStatus = void 0; const baseDevice_1 = require("../baseDevice"); const helper_1 = require("../helper"); class Refrigerator extends baseDevice_1.baseDevice { constructor(platform, accessory) { var _a, _b, _c; super(platform, accessory); this.platform = platform; this.accessory = accessory; const { Service: { ContactSensor, Switch, ServiceLabel, }, Characteristic, } = this.platform; const device = accessory.context.device; const serviceLabel = accessory.getService(ServiceLabel); if (serviceLabel) { accessory.removeService(serviceLabel); } this.serviceFridge = this.createThermostat('Fridge', 'fridgeTemp'); if (this.serviceFridge) { this.serviceFridge.updateCharacteristic(Characteristic.TargetTemperature, this.Status.fridgeTemperature); } this.serviceFreezer = this.createThermostat('Freezer', 'freezerTemp'); if (this.serviceFreezer) { this.serviceFreezer.updateCharacteristic(Characteristic.TargetTemperature, this.Status.freezerTemperature); } // Door open state this.serviceDoorOpened = accessory.getService(ContactSensor) || accessory.addService(ContactSensor, 'Refrigerator Door Closed'); this.serviceExpressMode = accessory.getService('Express Freezer'); if (this.config.ref_express_freezer && 'expressMode' in ((_a = device.snapshot) === null || _a === void 0 ? void 0 : _a.refState)) { this.serviceExpressMode = this.serviceExpressMode || accessory.addService(Switch, 'Express Freezer', 'Express Freezer'); this.serviceExpressMode.getCharacteristic(Characteristic.On).onSet(this.setExpressMode.bind(this)); } else if (this.serviceExpressMode) { accessory.removeService(this.serviceExpressMode); this.serviceExpressMode = null; } this.serviceExpressFridge = accessory.getService('Express Fridge'); if (this.config.ref_express_fridge && 'expressFridge' in ((_b = device.snapshot) === null || _b === void 0 ? void 0 : _b.refState)) { // eslint-disable-next-line max-len this.serviceExpressFridge = this.serviceExpressFridge || accessory.addService(Switch, 'Express Fridge', 'Express Fridge'); this.serviceExpressFridge.getCharacteristic(Characteristic.On).onSet(this.setExpressFridge.bind(this)); } else if (this.serviceExpressFridge) { accessory.removeService(this.serviceExpressFridge); this.serviceExpressFridge = null; } this.serviceEcoFriendly = accessory.getService('Eco Friendly'); if (this.config.ref_eco_friendly && 'ecoFriendly' in ((_c = device.snapshot) === null || _c === void 0 ? void 0 : _c.refState)) { this.serviceEcoFriendly = this.serviceEcoFriendly || accessory.addService(Switch, 'Eco Friendly', 'Eco Friendly'); this.serviceEcoFriendly.getCharacteristic(Characteristic.On).onSet(this.setEcoFriendly.bind(this)); } else if (this.serviceEcoFriendly) { accessory.removeService(this.serviceEcoFriendly); this.serviceEcoFriendly = null; } } get config() { return Object.assign({}, { ref_express_freezer: false, ref_express_fridge: false, ref_eco_friendly: false, }, super.config); } get Status() { var _a; return new RefrigeratorStatus((_a = this.accessory.context.device.snapshot) === null || _a === void 0 ? void 0 : _a.refState, this.accessory.context.device.deviceModel); } /** * update accessory characteristic by device */ updateAccessoryCharacteristic(device) { var _a, _b, _c; super.updateAccessoryCharacteristic(device); const { Characteristic } = this.platform; const tempBetween = (props, value) => { return Math.min(Math.max(props.minValue, value), props.maxValue); }; if (this.serviceFreezer) { const t = tempBetween(this.serviceFreezer.getCharacteristic(Characteristic.TargetTemperature).props, this.Status.freezerTemperature); this.serviceFreezer.updateCharacteristic(Characteristic.CurrentTemperature, t); this.serviceFreezer.updateCharacteristic(Characteristic.TargetTemperature, t); } if (this.serviceFridge) { const t = tempBetween(this.serviceFridge.getCharacteristic(Characteristic.TargetTemperature).props, this.Status.fridgeTemperature); this.serviceFridge.updateCharacteristic(Characteristic.CurrentTemperature, t); this.serviceFridge.updateCharacteristic(Characteristic.TargetTemperature, t); } if (this.serviceDoorOpened) { const contactSensorValue = this.Status.isDoorClosed ? Characteristic.ContactSensorState.CONTACT_DETECTED : Characteristic.ContactSensorState.CONTACT_NOT_DETECTED; this.serviceDoorOpened.updateCharacteristic(Characteristic.ContactSensorState, contactSensorValue); } if (this.config.ref_express_freezer && 'expressMode' in ((_a = device.snapshot) === null || _a === void 0 ? void 0 : _a.refState) && this.serviceExpressMode) { this.serviceExpressMode.updateCharacteristic(Characteristic.On, this.Status.isExpressModeOn); } if (this.config.ref_express_fridge && 'expressFridge' in ((_b = device.snapshot) === null || _b === void 0 ? void 0 : _b.refState) && this.serviceExpressFridge) { this.serviceExpressFridge.updateCharacteristic(Characteristic.On, this.Status.isExpressFridgeOn); } if (this.config.ref_eco_friendly && 'ecoFriendly' in ((_c = device.snapshot) === null || _c === void 0 ? void 0 : _c.refState) && this.serviceEcoFriendly) { this.serviceEcoFriendly.updateCharacteristic(Characteristic.On, this.Status.isEcoFriendlyOn); } } async setExpressMode(value) { var _a; const device = this.accessory.context.device; const On = device.deviceModel.lookupMonitorName('expressMode', '@CP_ON_EN_W'); const Off = device.deviceModel.lookupMonitorName('expressMode', '@CP_OFF_EN_W'); (_a = this.platform.ThinQ) === null || _a === void 0 ? void 0 : _a.deviceControl(device.id, { dataKey: null, dataValue: null, dataSetList: { refState: { expressMode: value ? On : Off, tempUnit: this.Status.tempUnit, }, }, dataGetList: null, }); this.platform.log.debug('Set Express Freezer ->', value); } async setExpressFridge(value) { var _a; const device = this.accessory.context.device; const On = device.deviceModel.lookupMonitorName('expressFridge', '@CP_ON_EN_W'); const Off = device.deviceModel.lookupMonitorName('expressFridge', '@CP_OFF_EN_W'); (_a = this.platform.ThinQ) === null || _a === void 0 ? void 0 : _a.deviceControl(device.id, { dataKey: null, dataValue: null, dataSetList: { refState: { expressFridge: value ? On : Off, tempUnit: this.Status.tempUnit, }, }, dataGetList: null, }); this.platform.log.debug('Set Express Fridge ->', value); } async setEcoFriendly(value) { var _a; const device = this.accessory.context.device; const On = device.deviceModel.lookupMonitorName('ecoFriendly', '@CP_ON_EN_W'); const Off = device.deviceModel.lookupMonitorName('ecoFriendly', '@CP_OFF_EN_W'); (_a = this.platform.ThinQ) === null || _a === void 0 ? void 0 : _a.deviceControl(device.id, { dataKey: null, dataValue: null, dataSetList: { refState: { ecoFriendly: value ? On : Off, tempUnit: this.Status.tempUnit, }, }, dataGetList: null, }); this.platform.log.debug('Set Eco Friendly ->', value); } async tempUnit() { const { Characteristic: { TemperatureDisplayUnits, }, } = this.platform; return this.Status.tempUnit === 'CELSIUS' ? TemperatureDisplayUnits.CELSIUS : TemperatureDisplayUnits.FAHRENHEIT; } /** * create a thermostat service */ createThermostat(name, key) { var _a, _b; const device = this.accessory.context.device; const visibleItem = (_b = (_a = device.deviceModel.data.Config) === null || _a === void 0 ? void 0 : _a.visibleItems) === null || _b === void 0 ? void 0 : _b.find(item => item.Feature === key || item.feature === key); if (visibleItem && (visibleItem.ControlTitle === undefined && visibleItem.controlTitle === undefined)) { return; } const { Characteristic } = this.platform; const isCelsius = this.Status.tempUnit === 'CELSIUS'; const service = this.accessory.getService(name) || this.accessory.addService(this.platform.Service.Thermostat, name, name); // cool only service.setCharacteristic(Characteristic.CurrentHeatingCoolingState, Characteristic.CurrentHeatingCoolingState.COOL); service.getCharacteristic(Characteristic.TargetHeatingCoolingState) .setProps({ minValue: Characteristic.TargetHeatingCoolingState.COOL, maxValue: Characteristic.TargetHeatingCoolingState.COOL, }) .updateValue(Characteristic.TargetHeatingCoolingState.COOL); service.setCharacteristic(Characteristic.TargetHeatingCoolingState, Characteristic.TargetHeatingCoolingState.COOL); service.getCharacteristic(Characteristic.TemperatureDisplayUnits).setProps({ minValue: Characteristic.TemperatureDisplayUnits.CELSIUS, maxValue: Characteristic.TemperatureDisplayUnits.FAHRENHEIT, }).onGet(this.tempUnit.bind(this)); const valueMapping = device.deviceModel.monitoringValueMapping(key + '_C') || device.deviceModel.monitoringValueMapping(key); const values = Object.values(valueMapping) .map(value => { if (value && typeof value === 'object' && 'label' in value) { return parseInt(value['label']); } return parseInt(value); }) .filter(value => { return !isNaN(value); }); service.getCharacteristic(Characteristic.TargetTemperature) .onSet(async (value) => { let indexValue; if (this.Status.tempUnit === 'FAHRENHEIT') { indexValue = device.deviceModel.lookupMonitorName(key + '_F', (0, helper_1.cToF)(value).toString()) || device.deviceModel.lookupMonitorName(key, (0, helper_1.cToF)(value).toString()); } else { indexValue = device.deviceModel.lookupMonitorName(key + '_C', value.toString()) || device.deviceModel.lookupMonitorName(key, value.toString()); } if (!indexValue) { throw new this.platform.api.hap.HapStatusError(-70410 /* this.platform.api.hap.HAPStatus.INVALID_VALUE_IN_REQUEST */); } await this.setTemperature(key, indexValue); }) .setProps({ minValue: Math.min(...values), maxValue: Math.max(...values), minStep: isCelsius ? 1 : 0.1 }) .updateValue(Math.min(...values)); return service; } async setTemperature(key, temp) { var _a; const device = this.accessory.context.device; await ((_a = this.platform.ThinQ) === null || _a === void 0 ? void 0 : _a.deviceControl(device.id, { dataKey: null, dataValue: null, dataSetList: { refState: { [key]: parseInt(temp), tempUnit: this.Status.tempUnit, }, }, dataGetList: null, })); } } exports.default = Refrigerator; class RefrigeratorStatus { constructor(data, deviceModel) { this.data = data; this.deviceModel = deviceModel; } get freezerTemperature() { var _a, _b; if (this.tempUnit === 'FAHRENHEIT') { return (0, helper_1.fToC)(parseInt(this.deviceModel.lookupMonitorValue('freezerTemp_F', (_a = this.data) === null || _a === void 0 ? void 0 : _a.freezerTemp, '0'))); } return parseInt(this.deviceModel.lookupMonitorValue('freezerTemp_C', (_b = this.data) === null || _b === void 0 ? void 0 : _b.freezerTemp, '0')); } get fridgeTemperature() { var _a, _b; if (this.tempUnit === 'FAHRENHEIT') { return (0, helper_1.fToC)(parseInt(this.deviceModel.lookupMonitorValue('fridgeTemp_F', (_a = this.data) === null || _a === void 0 ? void 0 : _a.fridgeTemp, '0'))); } return parseInt(this.deviceModel.lookupMonitorValue('fridgeTemp_C', (_b = this.data) === null || _b === void 0 ? void 0 : _b.fridgeTemp, '0')); } get isDoorClosed() { var _a; return ((_a = this.data) === null || _a === void 0 ? void 0 : _a.atLeastOneDoorOpen) === 'CLOSE'; } get isExpressModeOn() { var _a; return ((_a = this.data) === null || _a === void 0 ? void 0 : _a.expressMode) === this.deviceModel.lookupMonitorName('expressMode', '@CP_ON_EN_W'); } get isExpressFridgeOn() { var _a; return ((_a = this.data) === null || _a === void 0 ? void 0 : _a.expressFridge) === this.deviceModel.lookupMonitorName('expressFridge', '@CP_ON_EN_W'); } get isEcoFriendlyOn() { var _a; return ((_a = this.data) === null || _a === void 0 ? void 0 : _a.ecoFriendly) === this.deviceModel.lookupMonitorName('ecoFriendly', '@CP_ON_EN_W'); } get tempUnit() { var _a; return ((_a = this.data) === null || _a === void 0 ? void 0 : _a.tempUnit) || 'CELSIUS'; } } exports.RefrigeratorStatus = RefrigeratorStatus; //# sourceMappingURL=Refrigerator.js.map