UNPKG

@uboness/homebridge-unifi-access

Version:
118 lines 6.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.UnifiAccessPlatform = void 0; const common_js_1 = require("./common.js"); const device_1 = require("./device"); const Logger_js_1 = require("./Logger.js"); const settings_js_1 = require("./settings.js"); const UnifiAccessClient_1 = require("./UnifiAccessClient"); const mqtt_1 = require("./mqtt"); /** * Homebridge Platform * This class is the main constructor for your plugin, this is where you should * parse the user config and discover/register accessories with Homebridge. */ class UnifiAccessPlatform { constructor(log, config, api) { this.accessories = []; this.devices = []; this.logger = new Logger_js_1.ContextLogger(log); this.config = config; this.api = api; this.Service = api.hap.Service; this.Characteristic = api.hap.Characteristic; this.client = new UnifiAccessClient_1.UnifiAccessClient(this.config, this.logger); this.api.on('didFinishLaunching', () => this.init()); this.api.on('shutdown', () => this.dispose()); if (this.config.mqtt) { this.mqtt = new mqtt_1.Mqtt(this, this.client, this.config.mqtt, this.logger); } } configureAccessory(accessory) { this.accessories.push(accessory); } async init() { var _a, _b, _c, _d, _e; await this.client.start(); await ((_a = this.mqtt) === null || _a === void 0 ? void 0 : _a.start()); const devices = await this.client.listDevices(); const deviceById = devices.reduce((deviceById, device) => { deviceById[device.id] = device; return deviceById; }, {}); // first, let's clean up the cached accessories that are no long available const removeAccessories = []; for (let i = 0; i < this.accessories.length; i++) { const accessory = this.accessories[i]; const device = deviceById[accessory.context.deviceId]; if (!device) { this.accessories.splice(i--, 1); removeAccessories.push({ accessory, reason: `Device [${accessory.context.deviceType}] no longer available` }); } else { const asGarageDoor = !!((_c = (_b = this.config.devices) === null || _b === void 0 ? void 0 : _b.find(d => d.id == device.id)) === null || _c === void 0 ? void 0 : _c.asGarageDoor); const deviceType = accessory.context.deviceType; const ignore = !!((_e = (_d = this.config.devices) === null || _d === void 0 ? void 0 : _d.find(d => d.id == device.id)) === null || _e === void 0 ? void 0 : _e.ignore); if (ignore || device.type !== deviceType || asGarageDoor !== accessory.context.asGarageDoor) { this.accessories.splice(i--, 1); removeAccessories.push({ accessory, reason: `Device [${accessory.context.deviceType}] has changed` }); } } } for (const { reason, accessory } of removeAccessories) { this.api.unregisterPlatformAccessories(settings_js_1.PLUGIN_NAME, settings_js_1.PLATFORM_NAME, [accessory]); this.logger.info(`Unregistering cached [${accessory.context.deviceType}] accessory [${accessory.displayName}] (reason: ${reason})`); } devices.forEach(device => this.registerDevice(device)); } async dispose() { var _a; await Promise.all(this.devices.map(device => device.close())); await this.client.close(); await ((_a = this.mqtt) === null || _a === void 0 ? void 0 : _a.close()); } async registerDevice(device) { var _a, _b, _c, _d; if ((0, common_js_1.isUndefined)(device_1.Devices[device.type])) { return; } const ignore = !!((_b = (_a = this.config.devices) === null || _a === void 0 ? void 0 : _a.find(d => d.id == device.id)) === null || _b === void 0 ? void 0 : _b.ignore); if (ignore) { return; } const asGarageDoor = !!((_d = (_c = this.config.devices) === null || _c === void 0 ? void 0 : _c.find(d => d.id == device.id)) === null || _d === void 0 ? void 0 : _d.asGarageDoor); // generate a unique id for the accessory this should be generated from // something globally unique, but constant, for example, the device serial // number or MAC address const uuid = this.api.hap.uuid.generate(`${device.type}:${device.model}:${device.id}:${asGarageDoor ? 'garage' : 'default'}`); // see if an accessory with the same uuid has already been registered and restored from // the cached devices we stored in the `configureAccessory` method above let accessory = this.accessories.find(accessory => accessory.UUID === uuid); if (!accessory) { const deviceName = device.name || device.model || device.type; accessory = new this.api.platformAccessory(deviceName, uuid); accessory.context.deviceId = device.id; accessory.context.deviceType = device.type; accessory.context.asGarageDoor = asGarageDoor; accessory.context.deviceName = deviceName; this.logger.info(`Registering [${device.type}] device [${accessory.displayName}] ID [${device.id}]`); this.api.registerPlatformAccessories(settings_js_1.PLUGIN_NAME, settings_js_1.PLATFORM_NAME, [accessory]); this.accessories.push(accessory); } else { this.logger.info(`Found existing [${device.type}] device [${accessory.displayName}] ID [${device.id}]`); } accessory.on('identify', async () => { await this.client.identifyDevice(device.type, device.id); }); accessory.getService(this.Service.AccessoryInformation) .setCharacteristic(this.Characteristic.Name, accessory.displayName) .setCharacteristic(this.Characteristic.Manufacturer, 'Ubiquiti Unifi') .setCharacteristic(this.Characteristic.Model, device.model) .setCharacteristic(this.Characteristic.FirmwareRevision, '0') .setCharacteristic(this.Characteristic.SerialNumber, device.id); this.devices.push(await device_1.Devices[device.type].create(this, this.client, accessory, device)); } } exports.UnifiAccessPlatform = UnifiAccessPlatform; //# sourceMappingURL=UnifiAccessPlatform.js.map