UNPKG

homebridge-lirc-heater-cooler

Version:
240 lines (239 loc) 10.4 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.HeaterCoolerAccessory = void 0; const fs = __importStar(require("fs")); const settings_1 = require("./settings"); const { version } = require('../package.json'); const nodeLIRC = require('node-lirc'); class HeaterCoolerAccessory { constructor(log, config, api) { this.log = log; this.config = config; this.api = api; this.Service = this.api.hap.Service; this.currentActive = 0; this.currentState = 0; this.targetState = 0; this.currentTemperature = 10; this.coolingThresholdTemperature = 10; this.heatingThresholdTemperature = 10; this.temperatureDisplayUnits = 0; this.swingMode = 0; this.rotationSpeed = 0; this.log = log; this.config = config; this.api = api; this.log.debug('initializing accessory with config: ', this.config); // initialize node lirc nodeLIRCInit(this.log, this.config.lirc); this.informationService = new this.api.hap.Service.AccessoryInformation() .setCharacteristic(this.api.hap.Characteristic.Manufacturer, settings_1.MANU_FACTURER) .setCharacteristic(this.api.hap.Characteristic.Model, settings_1.MODEL) .setCharacteristic(this.api.hap.Characteristic.SerialNumber, 'Version ' + version); this.service = new this.api.hap.Service.HeaterCooler(this.config.name); this.service.getCharacteristic(this.api.hap.Characteristic.Active) .onGet(this.handleActiveGet.bind(this)) .onSet(this.handleActiveSet.bind(this)); this.service.getCharacteristic(this.api.hap.Characteristic.CurrentHeaterCoolerState) .onGet(this.handleCurrentHeaterCoolerStateGet.bind(this)); this.service.getCharacteristic(this.api.hap.Characteristic.CurrentTemperature) .onGet(this.handleCurrentTemperatureGet.bind(this)); this.service.getCharacteristic(this.api.hap.Characteristic.TargetHeaterCoolerState) .onGet(this.handleTargetHeaterCoolerStateGet.bind(this)) .onSet(this.handleTargetHeaterCoolerStateSet.bind(this)); this.service.getCharacteristic(this.api.hap.Characteristic.CoolingThresholdTemperature) .onGet(this.handleCoolingThresholdTemperatureGet.bind(this)) .onSet(this.handleCoolingThresholdTemperatureSet.bind(this)); this.service.getCharacteristic(this.api.hap.Characteristic.HeatingThresholdTemperature) .onGet(this.handleHeatingThresholdTemperatureGet.bind(this)) .onSet(this.handleHeatingThresholdTemperatureSet.bind(this)); this.service.getCharacteristic(this.api.hap.Characteristic.TemperatureDisplayUnits) .onGet(this.handleTemperatureDisplayUnitsGet.bind(this)) .onSet(this.handleTemperatureDisplayUnitsSet.bind(this)); this.service.getCharacteristic(this.api.hap.Characteristic.SwingMode) .onGet(this.handleSwingModeGet.bind(this)) .onSet(this.handleSwingModeSet.bind(this)); this.service.getCharacteristic(this.api.hap.Characteristic.RotationSpeed) .onGet(this.handleRotationSpeedGet.bind(this)) .onSet(this.handleRotationSpeedSet.bind(this)); } getServices() { return [ this.informationService, this.service ]; } /** * active */ handleActiveGet() { this.log.info('Getting current active: ', this.currentActive); return this.currentActive; } handleActiveSet(value) { this.log.info('Setting current active: ', value); const config = this.config; const { active, inactive } = config.activeCommands; const activeCommand = value ? active : inactive; this.log.info('active command: ', activeCommand); nodeLIRC.send(activeCommand); this.currentActive = value; } /** * state */ handleCurrentHeaterCoolerStateGet() { this.log.info('Getting current heating cooling state: ', this.currentState); return this.currentState; } handleTargetHeaterCoolerStateGet() { this.log.info('Getting target heating cooling state: ', this.targetState); return this.targetState; } handleTargetHeaterCoolerStateSet(value) { this.log.info('Setting target heating cooling state to: ', value); const config = this.config; if (!config.stateCommands) { this.log.error('stateCommands config not found.'); return; } const state = ['AUTO', 'HEAT', 'COOL'][value]; const stateCommand = config.stateCommands[state]; if (!state || !stateCommand) { this.log.debug('target state command not found.'); return; } this.log.info('state command: ', stateCommand); nodeLIRC.send(stateCommand); this.currentState = value; this.targetState = value; } /** * temperature */ handleCurrentTemperatureGet() { this.log.info('Getting current temperature: ', this.currentTemperature); return this.currentTemperature; } handleHeatingThresholdTemperatureGet() { this.log.info('Getting heating threshold temperature: ', this.heatingThresholdTemperature); return this.heatingThresholdTemperature; } handleHeatingThresholdTemperatureSet(value) { this.log.info('Setting heating threshold temperature: ', value); const config = this.config; const tempNum = parseInt(value.toString()); // temp command template const tempCommand = config.heatTempsCommands.template ? config.heatTempsCommands.template.replace(/\{tempNum\}/, tempNum.toString()) : config.heatTempsCommands[tempNum]; if (!tempCommand) { this.log.debug('tempature set command not found.'); return; } this.log.info('temp command: ', tempCommand); nodeLIRC.send(tempCommand); this.heatingThresholdTemperature = value; this.currentTemperature = value; } handleCoolingThresholdTemperatureGet() { this.log.info('Getting cooling threshold temperature: ', this.coolingThresholdTemperature); return this.coolingThresholdTemperature; } handleCoolingThresholdTemperatureSet(value) { this.log.info('Setting cooling threshold temperature: ', value); const config = this.config; const tempNum = parseInt(value.toString()); // temp command template const tempCommand = config.coolTempsCommands.template ? config.coolTempsCommands.template.replace(/\{tempNum\}/, tempNum.toString()) : config.coolTempsCommands[tempNum]; if (!tempCommand) { this.log.debug('tempature set command not found.'); return; } this.log.info('temp command: ', tempCommand); nodeLIRC.send(tempCommand); this.coolingThresholdTemperature = value; this.currentTemperature = value; } handleTemperatureDisplayUnitsGet() { this.log.info('Getting temperature display units :', this.temperatureDisplayUnits); return this.temperatureDisplayUnits; } handleTemperatureDisplayUnitsSet(value) { this.log.info('Setting temperature display units to:', value); this.temperatureDisplayUnits = value; } handleSwingModeGet() { this.log.info('Getting swing mode :', this.swingMode); return this.swingMode; } handleSwingModeSet(value) { this.log.info('Setting swing mode to:', value); const config = this.config; const modeCommand = value ? config.swingModeCommands.disabled : config.swingModeCommands.enabled; if (!modeCommand) { this.log.debug('swing mode set command not found.'); return; } this.log.info('swing mode command: ', modeCommand); nodeLIRC.send(modeCommand); this.swingMode = value; } handleRotationSpeedGet() { this.log.info('Getting rotation speed :', this.rotationSpeed); return this.rotationSpeed; } handleRotationSpeedSet(value) { this.log.info('Setting rotation speed to:', value); const config = this.config; const speedNum = parseInt(value.toString()); // speed command template const speedCommand = config.rotationSpeedCommands.template ? config.rotationSpeedCommands.template.replace(/\{speedValue\}/, speedNum.toString()) : config.rotationSpeedCommands[speedNum]; if (!speedCommand) { this.log.debug('rotation speed set command not found.'); return; } this.log.info('rotation speed command: ', speedCommand); nodeLIRC.send(speedCommand); this.rotationSpeed = value; } } exports.HeaterCoolerAccessory = HeaterCoolerAccessory; const nodeLIRCInit = (log, config) => { if (config) { fs.writeFileSync('./config.json', JSON.stringify(config)); } nodeLIRC.init(); nodeLIRC.on('stdout', (event) => { log.info('nodeLIRC on stdout event: ' + event.instructions); }); nodeLIRC.on('stderr', (data) => { log.info('irrecord output stderr: ' + data.toString()); }); nodeLIRC.on('exit', (code) => { log.info('irrecord exited with code ' + (code ? code.toString() : '(unknown)')); }); };