diff --git a/README.md b/README.md index 41712a3..389a781 100644 --- a/README.md +++ b/README.md @@ -118,7 +118,7 @@ const devices = miio.devices({ cacheTime: 300 // 5 minutes. Default is 1800 seconds (30 minutes) }); -browser.on('available', reg => { +devices.on('available', reg => { if(! reg.token) { console.log(reg.id, 'hides its token'); return; @@ -133,13 +133,13 @@ browser.on('available', reg => { // Do something useful with the device }); -browser.on('unavailable', reg => { +devices.on('unavailable', reg => { if(! reg.device) return; // Do whatever you need here }); -browser.on('error', err => { +devices.on('error', err => { // err.device points to info about the device console.log('Something went wrong connecting to device', err); }); @@ -148,9 +148,10 @@ browser.on('error', err => { `miio.devices()` supports these options: * `cacheTime`, the maximum amount of seconds a device can be unreachable before it becomes unavailable. Default: `1800` -* `useTokenStorage`, if tokens should be fetched from storage (see device management). Default: `true` * `filter`, function used to filter what devices are connected to. Default: `reg => true` * `skipSubDevices`, if sub devices on Aqara gateways should be skipped. Default: `false` +* `useTokenStorage`, if tokens should be fetched from storage (see device management). Default: `true` +* `tokens`, object with manual mapping between ids and tokens (advanced, use [Device management](docs/management.md) if possible) Example using `miio.browse()`: diff --git a/lib/discovery.js b/lib/discovery.js index e593b7b..cf81c18 100644 --- a/lib/discovery.js +++ b/lib/discovery.js @@ -21,6 +21,8 @@ class Browser { this.tokens = new Tokens(); } + this.manualTokens = options.tokens || {}; + this._events = new EventEmitter(); this._services = {}; @@ -36,6 +38,10 @@ class Browser { this._events.removeListener(event, cb); } + _manualToken(id) { + return this.manualTokens[id] || null; + } + start() { this._socket = dgram.createSocket('udp4'); this._socket.bind(); @@ -56,7 +62,7 @@ class Browser { id: id, address: rinfo.address, port: rinfo.port, - token: storedToken, + token: storedToken || this._manualToken(id), autoToken: false }); }) @@ -66,7 +72,7 @@ class Browser { id: id, address: rinfo.address, port: rinfo.port, - token: token, + token: token || this._manualToken(id), autoToken: true }); }