UNPKG

homebridge-interlogix

Version:

Homebridge Support for Interlogix Wireless Contact and Motion Sensors using rtl_433

116 lines 5.44 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; const child_process_1 = require("child_process"); const readline_1 = __importDefault(require("readline")); let hap; let Accessory; const PLUGIN_NAME = 'homebridge-interlogix'; const PLATFORM_NAME = 'interlogix'; class InterlogixPlatform { constructor(log, config, api) { this.sensors = new Map(); this.motionTimeouts = new Map(); this.log = log; this.config = config; this.api = api; api.on("didFinishLaunching", this.didFinishLaunching.bind(this)); api.on("shutdown", () => { var _a; (_a = this.rtl433) === null || _a === void 0 ? void 0 : _a.kill('SIGKILL'); }); } didFinishLaunching() { this.log('Starting rtl_433...'); this.rtl433 = child_process_1.spawn('rtl_433', ['-R', '100', '-f', '319508000', '-F', 'json'], { env: process.env }); this.rtl433.on('exit', (code, signal) => { this.log('rtl_433 exited with code: ' + code + ' and signal: ' + signal); }); const stdout = readline_1.default.createInterface({ input: this.rtl433.stdout, terminal: false }); stdout.on('line', (line) => { const message = JSON.parse(line); this.addUpdateAccessory(message); }); } addUpdateAccessory(message) { if (!this.sensors.has(message.id) && !this.config.dontLearn) { this.log.debug('New sensor found - ID: ' + message.id + ', Type: ' + message.subtype); const uuid = hap.uuid.generate(message.id); const newAccessory = new Accessory(message.id, uuid); const accInfo = newAccessory.getService(hap.Service.AccessoryInformation); if (accInfo) { accInfo .setCharacteristic(hap.Characteristic.Manufacturer, message.model) .setCharacteristic(hap.Characteristic.Model, message.subtype) .setCharacteristic(hap.Characteristic.SerialNumber, message.id); } switch (message.subtype) { case 'contact': newAccessory.addService(hap.Service.ContactSensor); break; case 'motion': newAccessory.addService(hap.Service.MotionSensor); break; default: this.log.debug('Only motion and contact sensors are currently supported.'); return; } this.configureAccessory(newAccessory); this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [newAccessory]); } this.updateState(message); } updateState(message) { var _a, _b; const accessory = this.sensors.get(message.id); if (accessory) { const status = !(message.switch1 == 'CLOSED' || message.switch5 == 'CLOSED'); const tamper = message.switch3 == 'OPEN'; const lowBattery = message.battery_ok == 0; this.log.debug(message.id + ' - Status: ' + status + ', Tamper: ' + tamper + ', Low Battery: ' + lowBattery); this.log.debug(JSON.stringify(message)); let sensorService; switch (message.subtype) { case 'contact': sensorService = (_a = accessory.getService(hap.Service.ContactSensor)) === null || _a === void 0 ? void 0 : _a.setCharacteristic(hap.Characteristic.ContactSensorState, status); break; case 'motion': sensorService = (_b = accessory.getService(hap.Service.MotionSensor)) === null || _b === void 0 ? void 0 : _b.setCharacteristic(hap.Characteristic.MotionDetected, status); const oldTimeout = this.motionTimeouts.get(message.id); if (oldTimeout) { clearTimeout(oldTimeout); this.motionTimeouts.delete(message.id); } const newTimeout = setTimeout(() => { this.log.debug(message.id + ' - Motion timeout'); sensorService === null || sensorService === void 0 ? void 0 : sensorService.setCharacteristic(hap.Characteristic.MotionDetected, false); }, 60 * 1000); this.motionTimeouts.set(message.id, newTimeout); break; } if (sensorService) { sensorService .setCharacteristic(hap.Characteristic.StatusTampered, tamper) .setCharacteristic(hap.Characteristic.StatusLowBattery, lowBattery); } } } configureAccessory(accessory) { var _a; accessory.on("identify", () => { this.log(accessory.displayName, 'identify requested!'); }); (_a = accessory.getService(hap.Service.MotionSensor)) === null || _a === void 0 ? void 0 : _a.setCharacteristic(hap.Characteristic.MotionDetected, false); this.sensors.set(accessory.displayName, accessory); } } module.exports = (api) => { hap = api.hap; Accessory = api.platformAccessory; api.registerPlatform(PLUGIN_NAME, PLATFORM_NAME, InterlogixPlatform); }; //# sourceMappingURL=index.js.map