UNPKG

@elshaer/homebridge-lg-thinq

Version:

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

335 lines 17.5 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, FilterMaintenance, }, 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); if (!this.serviceDoorOpened) { this.serviceDoorOpened = accessory.addService(ContactSensor, 'Refrigerator Door Closed'); this.serviceDoorOpened.addOptionalCharacteristic(Characteristic.ConfiguredName); this.serviceDoorOpened.updateCharacteristic(Characteristic.ConfiguredName, '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)) { if (!this.serviceExpressMode) { this.serviceExpressMode = accessory.addService(Switch, 'Express Freezer', 'Express Freezer'); this.serviceExpressMode.addOptionalCharacteristic(Characteristic.ConfiguredName); this.serviceExpressMode.updateCharacteristic(Characteristic.ConfiguredName, '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)) { if (!this.serviceExpressFridge) { this.serviceExpressFridge = accessory.addService(Switch, 'Express Fridge', 'Express Fridge'); this.serviceExpressFridge.addOptionalCharacteristic(Characteristic.ConfiguredName); this.serviceExpressFridge.updateCharacteristic(Characteristic.ConfiguredName, '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)) { if (!this.serviceEcoFriendly) { this.serviceEcoFriendly = accessory.addService(Switch, 'Eco Friendly', 'Eco Friendly'); this.serviceEcoFriendly.addOptionalCharacteristic(Characteristic.ConfiguredName); this.serviceEcoFriendly.updateCharacteristic(Characteristic.ConfiguredName, 'Eco Friendly'); } this.serviceEcoFriendly.getCharacteristic(Characteristic.On).onSet(this.setEcoFriendly.bind(this)); } else if (this.serviceEcoFriendly) { accessory.removeService(this.serviceEcoFriendly); this.serviceEcoFriendly = null; } if (this.Status.hasFeature('waterFilter')) { this.serviceWaterFilter = accessory.getService('Water Filter Maintenance'); if (!this.serviceWaterFilter) { this.serviceWaterFilter = accessory.addService(FilterMaintenance, 'Water Filter Maintenance', 'Water Filter Maintenance'); this.serviceWaterFilter.addOptionalCharacteristic(Characteristic.ConfiguredName); this.serviceWaterFilter.updateCharacteristic(Characteristic.ConfiguredName, 'Water Filter Maintenance'); } this.serviceWaterFilter.updateCharacteristic(Characteristic.Name, 'Water Filter Maintenance'); } } 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, Characteristic: { FilterLifeLevel, FilterChangeIndication, }, } = 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); } if (this.Status.hasFeature('waterFilter') && this.serviceWaterFilter) { this.serviceWaterFilter.updateCharacteristic(FilterLifeLevel, this.Status.waterFilterRemain); this.serviceWaterFilter.updateCharacteristic(FilterChangeIndication, this.Status.waterFilterRemain < 5 ? FilterChangeIndication.CHANGE_FILTER : FilterChangeIndication.FILTER_OK); } } 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) { const device = this.accessory.context.device; if (!this.Status.hasFeature(key)) { return; } const { Characteristic } = this.platform; const isCelsius = this.Status.tempUnit === 'CELSIUS'; let service = this.accessory.getService(name); if (!service) { service = this.accessory.addService(this.platform.Service.Thermostat, name, name); service.addOptionalCharacteristic(Characteristic.ConfiguredName); service.updateCharacteristic(Characteristic.ConfiguredName, name); } // Restrict to Cool only service.updateCharacteristic(Characteristic.CurrentHeatingCoolingState, Characteristic.CurrentHeatingCoolingState.COOL) .getCharacteristic(Characteristic.CurrentHeatingCoolingState) .setProps({ validValues: [Characteristic.CurrentHeatingCoolingState.COOL] // Hide other states }); service.getCharacteristic(Characteristic.TargetHeatingCoolingState) .updateValue(Characteristic.TargetHeatingCoolingState.COOL) .setProps({ validValues: [Characteristic.TargetHeatingCoolingState.COOL] // Hide Heat/Auto/Off }); 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) .updateValue(Math.min(...values)) .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 }); 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'; } get waterFilterRemain() { var _a, _b, _c; if ('waterFilter1RemainP' in this.data) { return ((_a = this.data) === null || _a === void 0 ? void 0 : _a.waterFilter1RemainP) || 0; } if ('waterFilter' in this.data) { const usedInMonth = parseInt((_b = this.data) === null || _b === void 0 ? void 0 : _b.waterFilter.match(/(\d)_/)[1]); if (isNaN(usedInMonth)) { return 0; } return (12 - usedInMonth) / 12 * 100; } return ((_c = this.data) === null || _c === void 0 ? void 0 : _c.waterFilter1RemainP) || 0; } hasFeature(key) { var _a, _b; const visibleItem = (_b = (_a = this.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) { return false; } else if (visibleItem.ControlTitle === undefined && visibleItem.controlTitle === undefined) { return false; } return true; } } exports.RefrigeratorStatus = RefrigeratorStatus; //# sourceMappingURL=Refrigerator.js.map