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,IAAI,0DAA2D,EAAO,gBACrE,OAAO,WAAW,EAAO,SACjC,CAAE,QACA,KAAK,IAAI,2BAA4B,EAAO,UAC5C,KAAK,gBAAgB,EAAO,UAC9B,CACF","file":"/npm/homebridge-zigbee@1.3.0/index.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/homebridge-zigbee@1.3.0/index.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","const path = require('path')\nconst get = require('lodash.get')\nconst retry = require('async-retry')\nconst requireDir = require('require-dir')\nconst zigbee = require('./lib/zigbee')\nconst sleep = require('./lib/utils/sleep')\nconst castArray = require('./lib/utils/castArray')\nconst parseModel = require('./lib/utils/parseModel')\nconst routerPolling = require('./lib/utils/routerPolling')\nconst findSerialPort = require('./lib/utils/findSerialPort')\nconst addCustomTypes = require('./lib/utils/customTypes')\nconst PermitJoinAccessory = require('./lib/PermitJoinAccessory')\n\nconst PLUGIN_NAME = 'homebridge-zigbee'\nconst PLATFORM_NAME = 'ZigBeePlatform'\n\nconst devices = Object.values(requireDir('./lib/devices'))\n\n// Only for beta period\nprocess.on('uncaughtException', (error) => {\n  console.error('Uncaught Exception', error) // eslint-disable-line no-console\n})\nprocess.on('unhandledRejection', (reason) => {\n  console.error('Unhandled Rejection', reason) // eslint-disable-line no-console\n})\n\n// eslint-disable-next-line one-var, one-var-declaration-per-line\nlet Accessory, Service, Characteristic, UUIDGen\n\nmodule.exports = function main(homebridge) {\n  addCustomTypes(homebridge.hap)\n  Accessory = homebridge.platformAccessory\n  Service = homebridge.hap.Service\n  Characteristic = homebridge.hap.Characteristic\n  UUIDGen = homebridge.hap.uuid\n  homebridge.registerPlatform(PLUGIN_NAME, PLATFORM_NAME, ZigBeePlatform, true)\n}\n\nclass ZigBeePlatform {\n  constructor(log, config, api) {\n    this.log = log\n    this.api = api\n    this.config = config\n    this.devices = {}\n    this.accessories = {}\n    this.permitJoinAccessory = null\n\n    // Bind handlers\n    this.handleZigBeeError = this.handleZigBeeError.bind(this)\n    this.handleZigBeeReady = this.handleZigBeeReady.bind(this)\n    this.handleZigBeeIndication = this.handleZigBeeIndication.bind(this)\n    this.handleInitialization = this.handleInitialization.bind(this)\n    this.configureAccessory = this.configureAccessory.bind(this)\n\n    // Listen events\n    this.api.on('didFinishLaunching', this.handleInitialization)\n\n    this.log('ZigBee platform initialization')\n  }\n\n  handleInitialization() {\n    this.startZigBee().catch(this.log)\n  }\n\n  async startZigBee() {\n    zigbee.init({\n      port: this.config.port || await findSerialPort(),\n      db: this.config.database || path.join(this.api.user.storagePath(), './zigbee.db'),\n      panId: this.config.panId || 0xFFFF,\n      channel: this.config.channel || 11,\n    })\n\n    zigbee.on('ready', this.handleZigBeeReady)\n    zigbee.on('error', this.handleZigBeeError)\n    zigbee.on('ind', this.handleZigBeeIndication)\n\n    const retrier = async () => {\n      try {\n        await zigbee.start()\n      } catch (error) {\n        await zigbee.stop()\n        throw error\n      }\n    }\n\n    try {\n      await retry(retrier, {\n        retries: 20,\n        minTimeout: 5000,\n        maxTimeout: 5000,\n        onRetry: () => this.log('Retrying connect to hardware'),\n      })\n    } catch (error) {\n      this.log('error:', error)\n    }\n  }\n\n  handleZigBeeError(error) {\n    this.log('error:', error)\n  }\n\n  handleZigBeeIndication(message) { // eslint-disable-line consistent-return\n    switch (message.type) {\n      // Supported indication messages\n      case 'attReport':\n      case 'statusChange':\n        return this.handleZigBeeAttrChange(message)\n      case 'devInterview':\n        return this.handleZigBeeDevInterview(message)\n      case 'devIncoming':\n        return this.handleZigBeeDevIncoming(message)\n      case 'devLeaving':\n        return this.handleZigBeeDevLeaving(message)\n      default:\n        // Do nothing\n    }\n  }\n\n  handleZigBeeAttrChange(message) {\n    const ieeeAddr = get(message, 'endpoints[0].device.ieeeAddr')\n\n    if (!ieeeAddr) {\n      return this.log('Unable to parse device ieeeAddr from message:', message)\n    }\n\n    const device = this.getDevice(ieeeAddr)\n\n    if (!device) {\n      return this.log('Received message from unknown device:', ieeeAddr)\n    }\n\n    device.zigbee.handleIndicationMessage(message)\n  }\n\n  handleZigBeeDevInterview(message) {\n    const endpoint = get(message, 'status.endpoint.current')\n    const endpointTotal = get(message, 'status.endpoint.total')\n    const cluster = get(message, 'status.endpoint.cluster.current')\n    const clusterTotal = get(message, 'status.endpoint.cluster.total')\n    this.log(\n      `Join progress: interview endpoint ${endpoint} of ${endpointTotal} `\n      + `and cluster ${cluster} of ${clusterTotal}`\n    )\n  }\n\n  async handleZigBeeDevIncoming(message) {\n    const ieeeAddr = message.data\n    // Stop permit join\n    this.permitJoinAccessory.setPermitJoin(false)\n    this.log(`Device announced incoming and is added, id: ${ieeeAddr}`)\n    // Ignore if the device exists\n    if (!this.getDevice(ieeeAddr)) {\n      // Wait a little bit for a database sync\n      await sleep(1500)\n      const data = zigbee.device(ieeeAddr)\n      this.initDevice(data)\n    }\n  }\n\n  handleZigBeeDevLeaving(message) {\n    const ieeeAddr = message.data\n    // Stop permit join\n    this.permitJoinAccessory.setPermitJoin(false)\n    this.log(`Device announced leaving and is removed, id: ${ieeeAddr}`)\n    const uuid = UUIDGen.generate(ieeeAddr)\n    const accessory = this.getAccessory(uuid)\n    // Sometimes we can unpair device which doesn't exist in HomeKit\n    if (accessory) {\n      this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory])\n      delete this.devices[ieeeAddr]\n      delete this.accessories[uuid]\n    }\n  }\n\n  handleZigBeeReady() {\n    const info = zigbee.info()\n    this.log('ZigBee platform initialized, info:')\n    this.log('------------------------------------')\n    this.log('channel:', info.net.channel)\n    this.log('pan id:', info.net.panId)\n    this.log('extended pan id:', info.net.extPanId)\n    this.log('ieee address:', info.net.ieeeAddr)\n    this.log('nwk address:', info.net.nwkAddr)\n    this.log('firmware version:', info.firmware.version)\n    this.log('firmware revision:', info.firmware.revision)\n    this.log('------------------------------------')\n    // Set led indicator\n    zigbee.request('UTIL', 'ledControl', {\n      ledid: 3, mode: this.config.disableLed ? 0 : 1,\n    }).catch(() => {\n      /* Unable to set led indicator, may be your device doesn\\'t support it */\n    })\n    // Init permit join accessory\n    this.initPermitJoinAccessory()\n    // Init devices\n    zigbee.list().forEach(data => this.initDevice(data))\n    // Init log for router polling service\n    if (!this.config.disablePingLog) {\n      routerPolling.log = this.log\n    }\n    // Some routers need polling to prevent them from sleeping.\n    routerPolling.start(this.config.routerPollingInterval)\n  }\n\n  setDevice(device) {\n    this.devices[device.ieeeAddr] = device\n  }\n\n  getDevice(ieeeAddr) {\n    return this.devices[ieeeAddr]\n  }\n\n  setAccessory(accessory) {\n    this.accessories[accessory.UUID] = accessory\n  }\n\n  getAccessory(uuid) {\n    return this.accessories[uuid]\n  }\n\n  registerAccessory(accessory) {\n    this.setAccessory(accessory)\n    this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory])\n  }\n\n  recognizeDevice({ model, manufacturer }) {\n    for (const Device of devices) {\n      if (!Device.description) {\n        continue // eslint-disable-line no-continue\n      }\n      if (\n        castArray(Device.description.model).includes(model)\n        && castArray(Device.description.manufacturer).includes(manufacturer)\n      ) {\n        return Device\n      }\n    }\n  }\n\n  initDevice(data) {\n    try {\n      const platform = this\n      const model = parseModel(data.modelId)\n      const manufacturer = data.manufName\n      const ieeeAddr = data.ieeeAddr\n      const uuid = UUIDGen.generate(ieeeAddr)\n      const accessory = this.getAccessory(uuid)\n      const log = (...args) => this.log(manufacturer, model, ieeeAddr, ...args)\n      const Device = this.recognizeDevice({ model, manufacturer })\n      const name = get(Device, 'description.name')\n\n      if (!Device) {\n        return this.log('Unrecognized device:', ieeeAddr, manufacturer, model)\n      }\n\n      const device = new Device({\n        name,\n        model,\n        manufacturer,\n        ieeeAddr,\n        accessory,\n        platform,\n        log,\n        Accessory,\n        Service,\n        Characteristic,\n        UUIDGen,\n      })\n\n      this.setDevice(device)\n      this.log('Registered device:', ieeeAddr, manufacturer, model)\n    } catch (error) {\n      this.log(\n        `Unable to initialize device ${data && data.ieeeAddr}, `\n        + 'try to remove it and add it again.\\n')\n      this.log('Reason:', error)\n    }\n  }\n\n  initPermitJoinAccessory() {\n    const platform = this\n    const uuid = UUIDGen.generate('zigbee:permit-join')\n    const accessory = this.getAccessory(uuid)\n    const log = (...args) => this.log('[PermitJoinAccessory]', ...args)\n    this.permitJoinAccessory = new PermitJoinAccessory({\n      accessory,\n      platform,\n      log,\n      Accessory,\n      Service,\n      Characteristic,\n      UUIDGen,\n    })\n  }\n\n  configureAccessory(accessory) {\n    this.setAccessory(accessory)\n  }\n\n  removeAccessory(accessory) {\n    try {\n      this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory])\n    } catch (error) {\n      // Do nothing\n    }\n  }\n\n  async removeDevice(device) {\n    try {\n      this.log('Removing device:', device.ieeeAddr)\n      await zigbee.remove(device.ieeeAddr)\n    } catch (error) {\n      this.log('Unable to remove properly, trying to unregister device:', device.ieeeAddr)\n      await zigbee.unregister(device.ieeeAddr)\n    } finally {\n      this.log('Device has been removed:', device.ieeeAddr)\n      this.removeAccessory(device.accessory)\n    }\n  }\n}\n"]}