UNPKG

homebridge-lg-thinq-fanfix

Version:

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

185 lines 9.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WasherDryerStatus = exports.NOT_RUNNING_STATUS = void 0; const baseDevice_1 = require("../baseDevice"); const constants_1 = require("../lib/constants"); exports.NOT_RUNNING_STATUS = ['COOLDOWN', 'POWEROFF', 'POWERFAIL', 'INITIAL', 'PAUSE', 'AUDIBLE_DIAGNOSIS', 'FIRMWARE', 'COURSE_DOWNLOAD', 'ERROR', 'END']; class WasherDryer extends baseDevice_1.baseDevice { constructor(platform, accessory) { var _a; super(platform, accessory); this.platform = platform; this.accessory = accessory; this.isRunning = false; this.isServiceTubCleanMaintenanceTriggered = false; const { Service: { OccupancySensor, LockMechanism, Valve, StatelessProgrammableSwitch, }, Characteristic, Characteristic: { LockCurrentState, }, } = this.platform; const device = accessory.context.device; this.serviceWasherDryer = accessory.getService(Valve) || accessory.addService(Valve, device.name); this.serviceWasherDryer.getCharacteristic(Characteristic.Active) .onSet(this.setActive.bind(this)) .setProps({ perms: [ "pr" /* Perms.PAIRED_READ */, "ev" /* Perms.NOTIFY */, ], }) .updateValue(Characteristic.Active.INACTIVE); this.serviceWasherDryer.setCharacteristic(Characteristic.Name, device.name); this.serviceWasherDryer.setCharacteristic(Characteristic.ValveType, Characteristic.ValveType.WATER_FAUCET); this.serviceWasherDryer.setCharacteristic(Characteristic.InUse, Characteristic.InUse.NOT_IN_USE); this.serviceWasherDryer.getCharacteristic(Characteristic.RemainingDuration).setProps({ maxValue: 86400, // 1 day }); // only thinq2 support door lock status this.serviceDoorLock = accessory.getService(LockMechanism); if (this.config.washer_door_lock && device.platform === constants_1.PlatformType.ThinQ2 && 'doorLock' in ((_a = device.snapshot) === null || _a === void 0 ? void 0 : _a.washerDryer)) { this.serviceDoorLock = this.serviceDoorLock || accessory.addService(LockMechanism, device.name + ' - Door'); this.serviceDoorLock.getCharacteristic(Characteristic.LockCurrentState) .onSet(this.setActive.bind(this)) .setProps({ minValue: 0, maxValue: 3, validValues: [LockCurrentState.UNSECURED, LockCurrentState.SECURED], }) .updateValue(LockCurrentState.UNSECURED); this.serviceDoorLock.getCharacteristic(Characteristic.LockTargetState) .onSet(this.setActive.bind(this)) .updateValue(Characteristic.LockTargetState.UNSECURED); } else if (this.serviceDoorLock) { accessory.removeService(this.serviceDoorLock); } this.serviceEventFinished = accessory.getService(OccupancySensor); if (this.config.washer_trigger) { this.serviceEventFinished = this.serviceEventFinished || accessory.addService(OccupancySensor, device.name + ' - Program Finished'); // eslint-disable-next-line max-len this.serviceEventFinished.updateCharacteristic(Characteristic.OccupancyDetected, Characteristic.OccupancyDetected.OCCUPANCY_NOT_DETECTED); } else if (this.serviceEventFinished) { accessory.removeService(this.serviceEventFinished); } // tub clean coach this.serviceTubCleanMaintenance = accessory.getService('Tub Clean Coach') || accessory.addService(StatelessProgrammableSwitch, 'Tub Clean Coach', 'Tub Clean Coach'); this.serviceTubCleanMaintenance.updateCharacteristic(Characteristic.Name, 'Tub Clean Coach'); this.serviceTubCleanMaintenance.getCharacteristic(Characteristic.ProgrammableSwitchEvent) .setProps({ validValues: [0], // single press }); this.serviceWasherDryer.addLinkedService(this.serviceTubCleanMaintenance); } get Status() { var _a; return new WasherDryerStatus((_a = this.accessory.context.device.snapshot) === null || _a === void 0 ? void 0 : _a.washerDryer, this.accessory.context.device.deviceModel); } get config() { return Object.assign({}, { washer_trigger: false, washer_door_lock: false, }, super.config); } async setActive(value) { if (!this.Status.isRemoteStartEnable) { return; } return; } updateAccessoryCharacteristic(device) { super.updateAccessoryCharacteristic(device); const { Characteristic, } = this.platform; this.serviceWasherDryer.updateCharacteristic(Characteristic.Active, this.Status.isPowerOn ? 1 : 0); this.serviceWasherDryer.updateCharacteristic(Characteristic.InUse, this.Status.isRunning ? 1 : 0); const prevRemainDuration = this.serviceWasherDryer.getCharacteristic(Characteristic.RemainingDuration).value; if (this.Status.remainDuration !== prevRemainDuration) { this.serviceWasherDryer.updateCharacteristic(Characteristic.RemainingDuration, this.Status.remainDuration); } this.serviceWasherDryer.updateCharacteristic(Characteristic.StatusFault, this.Status.isError ? Characteristic.StatusFault.GENERAL_FAULT : Characteristic.StatusFault.NO_FAULT); if (this.config.washer_door_lock && this.serviceDoorLock) { this.serviceDoorLock.updateCharacteristic(Characteristic.LockCurrentState, this.Status.isDoorLocked ? Characteristic.LockCurrentState.SECURED : Characteristic.LockCurrentState.UNSECURED); this.serviceDoorLock.updateCharacteristic(Characteristic.LockTargetState, this.Status.isDoorLocked ? 1 : 0); } } update(snapshot) { super.update(snapshot); const washerDryer = snapshot.washerDryer; if (!washerDryer) { return; } const { Characteristic: { OccupancyDetected, ProgrammableSwitchEvent, }, } = this.platform; // when washer state is changed if (this.config.washer_trigger && this.serviceEventFinished && ('preState' in washerDryer || 'processState' in washerDryer) && 'state' in washerDryer) { // detect if washer program in done if ((['END', 'COOLDOWN'].includes(washerDryer.state) && !exports.NOT_RUNNING_STATUS.includes(washerDryer.preState || washerDryer.processState)) || (this.isRunning && !this.Status.isRunning)) { this.serviceEventFinished.updateCharacteristic(OccupancyDetected, OccupancyDetected.OCCUPANCY_DETECTED); this.isRunning = false; // marked device as not running // turn it off after 10 minute setTimeout(() => { this.serviceEventFinished.updateCharacteristic(OccupancyDetected, OccupancyDetected.OCCUPANCY_NOT_DETECTED); }, 10000 * 60); } // detect if washer program is start if (this.Status.isRunning && !this.isRunning) { this.serviceEventFinished.updateCharacteristic(OccupancyDetected, OccupancyDetected.OCCUPANCY_NOT_DETECTED); this.isRunning = true; } } if ('TCLCount' in washerDryer) { // detect if tub clean coach counter is reached if (this.Status.TCLCount >= 30) { // trigger tub clean coach if not triggered yet if (!this.isServiceTubCleanMaintenanceTriggered) { this.serviceTubCleanMaintenance.updateCharacteristic(ProgrammableSwitchEvent, ProgrammableSwitchEvent.SINGLE_PRESS); this.isServiceTubCleanMaintenanceTriggered = true; } } else { // reset tub clean coach trigger flag this.isServiceTubCleanMaintenanceTriggered = false; } } } } exports.default = WasherDryer; class WasherDryerStatus { constructor(data, deviceModel) { this.data = data; this.deviceModel = deviceModel; } get isPowerOn() { var _a; return !['POWEROFF', 'POWERFAIL'].includes((_a = this.data) === null || _a === void 0 ? void 0 : _a.state); } get isRunning() { var _a; return this.isPowerOn && !exports.NOT_RUNNING_STATUS.includes((_a = this.data) === null || _a === void 0 ? void 0 : _a.state); } get isError() { var _a; return ((_a = this.data) === null || _a === void 0 ? void 0 : _a.state) === 'ERROR'; } get isRemoteStartEnable() { return this.data.remoteStart === this.deviceModel.lookupMonitorName('remoteStart', '@CP_ON_EN_W'); } get isDoorLocked() { return this.data.doorLock === this.deviceModel.lookupMonitorName('doorLock', '@CP_ON_EN_W'); } get remainDuration() { var _a, _b; const remainTimeHour = ((_a = this.data) === null || _a === void 0 ? void 0 : _a.remainTimeHour) || 0, remainTimeMinute = ((_b = this.data) === null || _b === void 0 ? void 0 : _b.remainTimeMinute) || 0; let remainingDuration = 0; if (this.isRunning) { remainingDuration = remainTimeHour * 3600 + remainTimeMinute * 60; } return remainingDuration; } get TCLCount() { var _a; return Math.min(parseInt(((_a = this.data) === null || _a === void 0 ? void 0 : _a.TCLCount) || 0), 30); } } exports.WasherDryerStatus = WasherDryerStatus; //# sourceMappingURL=WasherDryer.js.map