UNPKG

homebridge-calypshome

Version:
122 lines 6.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CalypshomePlatform = void 0; const settings_js_1 = require("./settings.js"); const calypshomeAPI_1 = require("./calypshomeAPI"); class CalypshomePlatform { log; config; api; // this is used to track restored cached accessories accessories = []; calypshome; constructor(log, config, api) { this.log = log; this.config = config; this.api = api; this.log.info('Booting CalypsHome platform'); this.calypshome = new calypshomeAPI_1.CalypshomeAPI(config, log); // eslint-disable-next-line @typescript-eslint/no-misused-promises this.api.on("didFinishLaunching" /* APIEvent.DID_FINISH_LAUNCHING */, this.discoverDevices.bind(this)); this.api.on("shutdown" /* APIEvent.SHUTDOWN */, () => { this.calypshome.close(); }); this.calypshome.on(calypshomeAPI_1.CalypshomeAPI.DEVICE_UPDATE, this.updateDevice.bind(this)); } configureAccessory(accessory) { this.accessories.push(accessory); } updateDevice(device, type) { const accessory = this.accessories.find((acc) => acc.context.id === device.id)?.getService(this.api.hap.Service.WindowCovering); if (!accessory) { this.log.warn('Accessory not found', device.id); return; } switch (type) { case 'level': accessory.getCharacteristic(this.api.hap.Characteristic.CurrentPosition).updateValue(device.level); break; case 'angle': accessory.getCharacteristic(this.api.hap.Characteristic.CurrentHorizontalTiltAngle).updateValue(device.angle); break; case 'status': // accessory.getCharacteristic(this.api.hap.Characteristic.PositionState).updateValue(this.api.hap.Characteristic.PositionState.STOPPED); break; default: this.log.warn('Unknown type:', type); } } async discoverDevices() { return this.calypshome .devices() .then((devices) => { if (!devices.length) { return { add: [], update: [], remove: [] }; } this.calypshome.connectWebSocket(); const remove = this.accessories.filter((acc) => !devices.some((device) => this.api.hap.uuid.generate(device.id) === acc.UUID)); this.log.info('Devices:', devices.map((d) => d.id)); return devices.reduce((acc, device) => { const uuid = this.api.hap.uuid.generate(device.id); let accessory = this.accessories.find((obj) => obj.UUID === uuid); if (accessory) { acc.update.push(accessory); } else { accessory = new this.api.platformAccessory(device.name, uuid, 14 /* Categories.WINDOW_COVERING */); acc.add.push(accessory); } accessory.context = device; this.hookupAccessory(accessory); return acc; }, { add: [], update: [], remove }); }) .then(({ add, update, remove }) => { if (add.length) { this.api.registerPlatformAccessories(settings_js_1.PLUGIN_NAME, settings_js_1.PLATFORM_NAME, add); } if (update.length) { this.api.updatePlatformAccessories(update); } if (remove.length) { this.log.warn('Removing accessories', remove.map((a) => a.displayName).join(', ')); this.api.unregisterPlatformAccessories(settings_js_1.PLUGIN_NAME, settings_js_1.PLATFORM_NAME, remove); } }); } hookupAccessory(accessory) { const characteristics = this.api.hap.Characteristic; // https://developers.homebridge.io/#/service/WindowCovering const wcService = accessory.getService(this.api.hap.Service.WindowCovering) ?? accessory.addService(this.api.hap.Service.WindowCovering); const aiService = accessory.getService(this.api.hap.Service.AccessoryInformation) ?? accessory.addService(this.api.hap.Service.AccessoryInformation); const { id } = accessory.context; aiService .setCharacteristic(characteristics.Manufacturer, accessory.context.manufacturerName) .setCharacteristic(characteristics.Model, accessory.context.actions.includes('TILT') ? 'BSO' : 'Shutter') .setCharacteristic(characteristics.SerialNumber, accessory.context.serialNumber); wcService.setCharacteristic(characteristics.Name, accessory.context.name); wcService.getCharacteristic(characteristics.CurrentPosition).onGet(() => this.calypshome.device(id)?.level ?? 0); wcService.getCharacteristic(characteristics.PositionState).onGet(() => characteristics.PositionState.STOPPED); wcService .getCharacteristic(characteristics.TargetPosition) .onGet(() => this.calypshome.device(id)?.level ?? 0) .onSet((value) => { void this.calypshome.action(id, 'LEVEL', { level: value.toString() }); }); // if accessory has tilt support if (accessory.context.actions.includes('TILT')) { wcService.getCharacteristic(characteristics.CurrentHorizontalTiltAngle).onGet(() => this.calypshome.device(id)?.angle ?? 0); wcService .getCharacteristic(characteristics.TargetHorizontalTiltAngle) .onGet(() => this.calypshome.device(id)?.angle ?? 0) .onSet((value) => { void this.calypshome.action(id, 'TILT', { angle: value.toString() }); }); } wcService.getCharacteristic(characteristics.HoldPosition).onSet(() => { void this.calypshome.action(id, 'STOP'); }); } } exports.CalypshomePlatform = CalypshomePlatform; //# sourceMappingURL=platform.js.map