node-miio 0.0.0-dev-20250810-f1674d0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. package/.editorconfig +21 -0
  2. package/.eslintrc +32 -0
  3. package/.github/workflows/build.yml +50 -0
  4. package/.github/workflows/codecov.yml +35 -0
  5. package/.github/workflows/codeql.yml +24 -0
  6. package/.github/workflows/publish-dev-from-main.yml +92 -0
  7. package/.github/workflows/publish-release-to-npm.yml +48 -0
  8. package/CONTRIBUTING.md +104 -0
  9. package/LICENSE.md +20 -0
  10. package/README.md +216 -0
  11. package/cli/commands/configure.js +69 -0
  12. package/cli/commands/control.js +54 -0
  13. package/cli/commands/discover.js +36 -0
  14. package/cli/commands/inspect.js +197 -0
  15. package/cli/commands/protocol/call.js +66 -0
  16. package/cli/commands/protocol/json-dump.js +52 -0
  17. package/cli/commands/protocol/packet.js +29 -0
  18. package/cli/commands/protocol.js +8 -0
  19. package/cli/commands/tokens/update.js +58 -0
  20. package/cli/commands/tokens.js +8 -0
  21. package/cli/device-finder.js +66 -0
  22. package/cli/index.js +9 -0
  23. package/cli/log.js +89 -0
  24. package/docs/advanced-api.md +103 -0
  25. package/docs/devices/README.md +111 -0
  26. package/docs/devices/air-purifier.md +142 -0
  27. package/docs/devices/controller.md +89 -0
  28. package/docs/devices/gateway.md +41 -0
  29. package/docs/devices/humidifier.md +136 -0
  30. package/docs/devices/light.md +141 -0
  31. package/docs/devices/power-outlet.md +56 -0
  32. package/docs/devices/power-plug.md +69 -0
  33. package/docs/devices/power-strip.md +73 -0
  34. package/docs/devices/sensor.md +74 -0
  35. package/docs/devices/vacuum.md +80 -0
  36. package/docs/devices/wall-switch.md +62 -0
  37. package/docs/management.md +109 -0
  38. package/docs/missing-devices.md +48 -0
  39. package/docs/protocol.md +77 -0
  40. package/docs/reporting-issues.md +47 -0
  41. package/example.js +11 -0
  42. package/lib/checkResult.js +13 -0
  43. package/lib/connectToDevice.js +62 -0
  44. package/lib/device.js +290 -0
  45. package/lib/device_info.js +329 -0
  46. package/lib/device_info.test.js +337 -0
  47. package/lib/devices/air-monitor.js +53 -0
  48. package/lib/devices/air-purifier.js +162 -0
  49. package/lib/devices/capabilities/battery-level.js +16 -0
  50. package/lib/devices/capabilities/buzzer.js +78 -0
  51. package/lib/devices/capabilities/changeable-led-brightness.js +48 -0
  52. package/lib/devices/capabilities/colorable.js +14 -0
  53. package/lib/devices/capabilities/dimmable.js +14 -0
  54. package/lib/devices/capabilities/mode.js +13 -0
  55. package/lib/devices/capabilities/power.js +14 -0
  56. package/lib/devices/capabilities/sensor.js +50 -0
  57. package/lib/devices/capabilities/switchable-led.js +49 -0
  58. package/lib/devices/chuangmi.plug.v1.js +61 -0
  59. package/lib/devices/eyecare-lamp2.js +179 -0
  60. package/lib/devices/gateway/86sw1.js +28 -0
  61. package/lib/devices/gateway/86sw2.js +44 -0
  62. package/lib/devices/gateway/ctrl_ln1.js +44 -0
  63. package/lib/devices/gateway/ctrl_ln2.js +53 -0
  64. package/lib/devices/gateway/ctrl_neutral1.js +44 -0
  65. package/lib/devices/gateway/ctrl_neutral2.js +53 -0
  66. package/lib/devices/gateway/cube.js +55 -0
  67. package/lib/devices/gateway/developer-api.js +120 -0
  68. package/lib/devices/gateway/light-channel.js +32 -0
  69. package/lib/devices/gateway/light-mixin.js +71 -0
  70. package/lib/devices/gateway/magnet.js +29 -0
  71. package/lib/devices/gateway/magnet2.js +13 -0
  72. package/lib/devices/gateway/motion.js +27 -0
  73. package/lib/devices/gateway/motion2.js +31 -0
  74. package/lib/devices/gateway/plug.js +45 -0
  75. package/lib/devices/gateway/sensor_ht.js +17 -0
  76. package/lib/devices/gateway/subdevice.js +227 -0
  77. package/lib/devices/gateway/subdevices.js +37 -0
  78. package/lib/devices/gateway/switch.js +29 -0
  79. package/lib/devices/gateway/switch2.js +27 -0
  80. package/lib/devices/gateway/voltage.js +28 -0
  81. package/lib/devices/gateway/weather.js +22 -0
  82. package/lib/devices/gateway.js +244 -0
  83. package/lib/devices/humidifier.js +162 -0
  84. package/lib/devices/mijiavacuum.js +22 -0
  85. package/lib/devices/philips-light-bulb.js +73 -0
  86. package/lib/devices/power-plug.js +26 -0
  87. package/lib/devices/power-strip.js +38 -0
  88. package/lib/devices/vacuum.js +412 -0
  89. package/lib/devices/viomivacuum.js +250 -0
  90. package/lib/devices/yeelight.color.js +6 -0
  91. package/lib/devices/yeelight.js +286 -0
  92. package/lib/devices/yeelight.mono.js +9 -0
  93. package/lib/discovery.js +192 -0
  94. package/lib/index.js +40 -0
  95. package/lib/infoFromHostname.js +24 -0
  96. package/lib/management.js +102 -0
  97. package/lib/models.js +77 -0
  98. package/lib/network.js +234 -0
  99. package/lib/packet.js +191 -0
  100. package/lib/placeholder.js +12 -0
  101. package/lib/safeishJSON.js +14 -0
  102. package/lib/tokens.js +129 -0
  103. package/npm-shrinkwrap.json +853 -0
  104. package/package.json +40 -0
@@ -0,0 +1,69 @@
1
+ 'use strict';
2
+
3
+ const log = require('../log');
4
+ const deviceFinder = require('../device-finder');
5
+
6
+ const tokens = require('../../lib/tokens');
7
+
8
+ exports.command = 'configure <idOrIp>';
9
+ exports.description = 'Control a device by invoking the given method';
10
+ exports.builder = {
11
+ ssid: {
12
+ required: true,
13
+ description: 'SSID of the WiFi network',
14
+ type: 'string'
15
+ },
16
+
17
+ passwd: {
18
+ required: true,
19
+ description: 'Password of WiFi-network',
20
+ type: 'string'
21
+ }
22
+ };
23
+
24
+ exports.handler = function(argv) {
25
+ let target = argv.idOrIp;
26
+ log.info('Attempting to configure', target);
27
+
28
+ let foundDevice = false;
29
+ let pending = 0;
30
+ const browser = deviceFinder({
31
+ filter: target
32
+ });
33
+ browser.on('available', device => {
34
+ pending++;
35
+
36
+ log.plain();
37
+
38
+ device.management.updateWireless({
39
+ ssid: argv.ssid,
40
+ passwd: argv.passwd
41
+ })
42
+ .then(result => {
43
+ log.plain('Updated wireless configuration');
44
+
45
+ return tokens.update(device.id, device.management.token);
46
+ })
47
+ .catch(err => {
48
+ log.error('Encountered an error while updating wireless');
49
+ log.plain();
50
+ log.plain('Error was:');
51
+ log.plain(err.message);
52
+ })
53
+ .then(() => {
54
+ pending--;
55
+ process.exit(0); // eslint-disable-line
56
+ });
57
+ });
58
+
59
+ const doneHandler = () => {
60
+ if(pending === 0) {
61
+ if(! foundDevice) {
62
+ log.warn('Could not find device');
63
+ }
64
+ process.exit(0); // eslint-disable-line
65
+ }
66
+ };
67
+ setTimeout(doneHandler, 5000);
68
+ browser.on('done', doneHandler);
69
+ };
@@ -0,0 +1,54 @@
1
+ 'use strict';
2
+
3
+ const log = require('../log');
4
+ const deviceFinder = require('../device-finder');
5
+
6
+ exports.command = 'control <idOrIp> <method> [params..]';
7
+ exports.description = 'Control a device by invoking the given method';
8
+ exports.builder = {
9
+ };
10
+
11
+ exports.handler = function(argv) {
12
+ let target = argv.idOrIp;
13
+ log.info('Attempting to invoke', argv.method, 'on', target);
14
+
15
+ let foundDevice = false;
16
+ let pending = 0;
17
+ const browser = deviceFinder({
18
+ filter: target
19
+ });
20
+ browser.on('available', device => {
21
+ if(! device[argv.method]) {
22
+ log.error('The method ' + argv.method + ' is not available');
23
+ process.exit(0); // eslint-disable-line
24
+ }
25
+
26
+ pending++;
27
+
28
+ Promise.resolve(device[argv.method](...argv.params))
29
+ .then(result => {
30
+ log.plain(JSON.stringify(result, null, ' '));
31
+ })
32
+ .catch(err => {
33
+ log.error('Encountered an error while controlling device');
34
+ log.plain();
35
+ log.plain('Error was:');
36
+ log.plain(err.message);
37
+ })
38
+ .then(() => {
39
+ pending--;
40
+ process.exit(0); // eslint-disable-line
41
+ });
42
+ });
43
+
44
+ const doneHandler = () => {
45
+ if(pending === 0) {
46
+ if(! foundDevice) {
47
+ log.warn('Could not find device');
48
+ }
49
+ process.exit(0); // eslint-disable-line
50
+ }
51
+ };
52
+ setTimeout(doneHandler, 5000);
53
+ browser.on('done', doneHandler);
54
+ };
@@ -0,0 +1,36 @@
1
+ 'use strict';
2
+
3
+ const log = require('../log');
4
+ const deviceFinder = require('../device-finder');
5
+ const tokens = require('../../lib/tokens');
6
+
7
+ exports.command = 'discover';
8
+ exports.description = 'Discover devices on the local network';
9
+ exports.builder = {
10
+ 'sync': {
11
+ type: 'boolean',
12
+ description: 'Synchronize tokens'
13
+ }
14
+ };
15
+
16
+ exports.handler = function(argv) {
17
+ log.info('Discovering devices. Press Ctrl+C to stop.');
18
+ log.plain();
19
+
20
+ const browser = deviceFinder();
21
+ browser.on('available', device => {
22
+ try {
23
+ log.device(device);
24
+ } catch(ex) {
25
+ log.error(ex);
26
+ }
27
+
28
+ const mgmt = device.management;
29
+ if(argv.sync && mgmt.token && mgmt.autoToken) {
30
+ tokens.update(device.id, mgmt.token)
31
+ .catch(err => {
32
+ log.error('Could not update token for', device.id, ':', err);
33
+ });
34
+ }
35
+ });
36
+ };
@@ -0,0 +1,197 @@
1
+ 'use strict';
2
+
3
+ const chalk = require('chalk');
4
+ const log = require('../log');
5
+ const deviceFinder = require('../device-finder');
6
+
7
+ const GROUPS = [
8
+ { name: 'Power', tags: [ 'cap:power', 'cap:switchable-power' ] },
9
+ { name: 'Mode', tags: [ 'cap:mode', 'cap:switchable-mode' ] },
10
+ { name: 'Sensor', tags: [
11
+ 'type:sensor',
12
+ 'cap:temperature',
13
+ 'cap:relativeHumidity',
14
+ 'cap:pressure',
15
+ 'cap:pm2.5',
16
+ 'cap:illuminance',
17
+ 'cap:contact',
18
+ 'cap:motion',
19
+ ] },
20
+ { name: 'Brightness', tags: [ 'cap:brightness', 'cap:dimmable' ] },
21
+ { name: 'Color', tags: [ 'cap:colorable' ] },
22
+ { name: 'LED', tags: [ 'cap:miio:switchable-led', 'cap:miio:led-brightness' ] },
23
+ { name: 'Buzzer', tags: [ 'cap:miio:buzzer' ] },
24
+ { name: 'Children', tags: [ 'cap:children' ] },
25
+ { name: 'miIO', tags: [ 'type:miio' ]}
26
+ ];
27
+
28
+ exports.command = 'inspect <idOrIp>';
29
+ exports.description = 'Inspect a device';
30
+ exports.builder = {
31
+ };
32
+
33
+ exports.handler = function(argv) {
34
+ let target = argv.idOrIp;
35
+ log.info('Attempting to inspect', target);
36
+
37
+ let foundDevice = false;
38
+ let pending = 0;
39
+ const browser = deviceFinder({
40
+ instances: true,
41
+ filter: target
42
+ });
43
+ browser.on('available', device => {
44
+ pending++;
45
+
46
+ // TODO: Error handling
47
+ const mgmt = device.management;
48
+ mgmt.info()
49
+ .then(info => {
50
+ log.plain();
51
+ log.device(device, true);
52
+
53
+ if(! mgmt.parent) {
54
+ log.plain(chalk.bold('Firmware version:'), info.fw_ver);
55
+ log.plain(chalk.bold('Hardware version:'), info.hw_ver);
56
+ if(info.mcu_fw_ver) {
57
+ log.plain(chalk.bold('MCU firmware version:'), info.mcu_fw_ver);
58
+ }
59
+ log.plain();
60
+
61
+ if(info.ap) {
62
+ log.plain(chalk.bold('WiFi:'), info.ap.ssid, chalk.dim('(' + info.ap.bssid + ')'), chalk.bold('RSSI:'), info.ap.rssi);
63
+ } else {
64
+ log.plain(chalk.bold('WiFi:'), 'Not Connected');
65
+ }
66
+ if(info.wifi_fw_ver) {
67
+ log.plain(chalk.bold('WiFi firmware version:'), info.wifi_fw_ver);
68
+ }
69
+ log.plain();
70
+
71
+ if(info.ot) {
72
+ let type;
73
+ switch(info.ot) {
74
+ case 'otu':
75
+ type = 'UDP';
76
+ break;
77
+ case 'ott':
78
+ type = 'TCP';
79
+ break;
80
+ default:
81
+ type = 'Unknown (' + info.ot + ')';
82
+ }
83
+ log.plain(chalk.bold('Remote access (Mi Home App):'), type);
84
+ } else {
85
+ log.plain(chalk.bold('Remote access (Mi Home App):'), 'Maybe');
86
+ }
87
+ log.plain();
88
+ }
89
+
90
+ const props = device.properties;
91
+ const keys = Object.keys(props);
92
+ if(keys.length > 0) {
93
+ log.plain(chalk.bold('Properties:'));
94
+ for(const key of keys) {
95
+ log.plain(' -', key + ':', props[key]);
96
+ }
97
+ log.plain();
98
+ }
99
+
100
+ if(mgmt.parent) {
101
+ log.plain(chalk.bold('Parent:'));
102
+ log.group(() => {
103
+ log.device(mgmt.parent);
104
+ });
105
+ }
106
+
107
+ log.plain(chalk.bold('Actions:'));
108
+ log.group(() => {
109
+ const actions = device.metadata.actions;
110
+ const handled = new Set();
111
+ for(const group of GROUPS) {
112
+ let seenTags = new Set();
113
+ let actionsToPrint = [];
114
+
115
+ /*
116
+ * Go through all actions and collect those that
117
+ * belong to this group.
118
+ */
119
+ for(const name of Object.keys(actions)) {
120
+ if(handled.has(name)) continue;
121
+ const action = actions[name];
122
+
123
+ for(const t of group.tags) {
124
+ if(action.tags.indexOf(t) >= 0) {
125
+ seenTags.add(t);
126
+ actionsToPrint.push(action);
127
+ break;
128
+ }
129
+ }
130
+ }
131
+
132
+ if(actionsToPrint.length > 0) {
133
+ log.plain(chalk.bold(group.name), '-', Array.from(seenTags).join(', '));
134
+
135
+ for(const action of actionsToPrint) {
136
+ printAction(action);
137
+ handled.add(action.name);
138
+ }
139
+
140
+ log.plain();
141
+ }
142
+ }
143
+
144
+ let isFirst = true;
145
+ for(const name of Object.keys(actions)) {
146
+ if(handled.has(name)) continue;
147
+
148
+ if(isFirst) {
149
+ log.plain(chalk.bold('Other actions'));
150
+ isFirst = false;
151
+ }
152
+
153
+ printAction(actions[name]);
154
+ }
155
+ });
156
+ })
157
+ .catch(err => {
158
+ log.error('Could inspect device. Error was:', err.message);
159
+ })
160
+ .then(() => {
161
+ pending--;
162
+ process.exit(0); // eslint-disable-line
163
+ });
164
+ });
165
+
166
+ const doneHandler = () => {
167
+ if(pending === 0) {
168
+ if(! foundDevice) {
169
+ log.warn('Could not find device');
170
+ }
171
+ process.exit(0); // eslint-disable-line
172
+ }
173
+ };
174
+ setTimeout(doneHandler, 5000);
175
+ browser.on('done', doneHandler);
176
+ };
177
+
178
+ function printAction(action) {
179
+ log.group(() => {
180
+ let args = action.name;
181
+ for(const arg of action.arguments) {
182
+ args += ' ';
183
+
184
+ if(arg.optional) {
185
+ args += '[';
186
+ }
187
+
188
+ args += arg.type;
189
+
190
+ if(arg.optional) {
191
+ args += ']';
192
+ }
193
+ }
194
+
195
+ log.plain(args);
196
+ });
197
+ }
@@ -0,0 +1,66 @@
1
+ 'use strict';
2
+
3
+ const log = require('../../log');
4
+ const deviceFinder = require('../../device-finder');
5
+
6
+ exports.command = 'call <idOrIp> <method> [params...] --token token-as-hex';
7
+ exports.description = 'Call a raw method on a device';
8
+ exports.builder = {
9
+ token: {
10
+ type: 'string',
11
+ description: 'The known token of the device to communicate with',
12
+ },
13
+ };
14
+
15
+ exports.handler = function (argv) {
16
+ let target = argv.idOrIp;
17
+ log.info('Attempting to call', argv.method, 'on', target);
18
+
19
+ let foundDevice = false;
20
+ let pending = 0;
21
+ const browser = deviceFinder({
22
+ filter: target,
23
+ token: argv.token,
24
+ });
25
+ browser.on('available', (device) => {
26
+ pending++;
27
+
28
+ log.plain();
29
+ log.info('Device found, making call');
30
+ log.plain();
31
+
32
+ const parsedArgs = argv.params
33
+ ? Array.isArray(argv.params)
34
+ ? argv.params
35
+ : JSON.parse(argv.params)
36
+ : [];
37
+ log.plain(parsedArgs);
38
+ device
39
+ .miioCall(argv.method, parsedArgs)
40
+ .then((result) => {
41
+ log.info('Got result:');
42
+ log.plain(JSON.stringify(result, null, ' '));
43
+ })
44
+ .catch((err) => {
45
+ log.error('Encountered an error while controlling device');
46
+ log.plain();
47
+ log.plain('Error was:');
48
+ log.plain(err.message);
49
+ })
50
+ .then(() => {
51
+ pending--;
52
+ process.exit(0); // eslint-disable-line
53
+ });
54
+ });
55
+
56
+ const doneHandler = () => {
57
+ if (pending === 0) {
58
+ if (!foundDevice) {
59
+ log.warn('Could not find device');
60
+ }
61
+ process.exit(0); // eslint-disable-line
62
+ }
63
+ };
64
+ setTimeout(doneHandler, 5000);
65
+ browser.on('done', doneHandler);
66
+ };
@@ -0,0 +1,52 @@
1
+ 'use strict';
2
+
3
+ const fs = require('fs');
4
+ const chalk = require('chalk');
5
+ const log = require('../../log');
6
+ const Packet = require('../../../lib/packet');
7
+
8
+ exports.command = 'json-dump <file>';
9
+ exports.description = 'Extract packets from a Wireshark JSON dump';
10
+ exports.builder = {
11
+ token: {
12
+ required: true,
13
+ description: 'Token to use for decoding'
14
+ }
15
+ };
16
+
17
+ exports.handler = function(argv) {
18
+ const data = fs.readFileSync(argv.file);
19
+ const packets = JSON.parse(data.toString());
20
+
21
+ const packet = new Packet();
22
+ packet.token = Buffer.from(argv.token, 'hex');
23
+
24
+ packets.forEach(p => {
25
+ const source = p._source;
26
+ if(! source) return;
27
+
28
+ const layers = source.layers;
29
+
30
+ const udp = layers.udp;
31
+ if(! udp) return;
32
+
33
+ let out;
34
+ if(udp['udp.dstport'] === '54321') {
35
+ // Packet that is being sent to the device
36
+ out = true;
37
+ } else if(udp['udp.srcport'] === '54321') {
38
+ // Packet coming from the device
39
+ out = false;
40
+ } else {
41
+ // Unknown, skip it
42
+ return;
43
+ }
44
+
45
+
46
+ const rawString = layers.data['data.data'];
47
+ const raw = Buffer.from(rawString.replace(/:/g, ''), 'hex');
48
+ packet.raw = raw;
49
+
50
+ log.plain(out ? chalk.bgBlue.white.bold(' -> ') : chalk.bgMagenta.white.bold(' <- '), chalk.yellow(layers.ip['ip.src']), chalk.dim('data='), packet.data ? packet.data.toString() : chalk.dim('N/A'));
51
+ });
52
+ };
@@ -0,0 +1,29 @@
1
+ 'use strict';
2
+
3
+ const log = require('../../log');
4
+ const Packet = require('../../../lib/packet');
5
+
6
+ exports.command = 'packet <hexData>';
7
+ exports.description = 'Decode a miIO UDP packet';
8
+ exports.builder = {
9
+ token: {
10
+ required: true,
11
+ description: 'Token to use for decoding'
12
+ }
13
+ };
14
+
15
+ exports.handler = function(argv) {
16
+ const packet = new Packet();
17
+ packet.token = Buffer.from(argv.token, 'hex');
18
+
19
+ const raw = Buffer.from(argv.hexData, 'hex');
20
+ packet.raw = raw;
21
+
22
+ const data = packet.data;
23
+ if(! data) {
24
+ log.error('Could not extract data from packet, check your token and packet data');
25
+ } else {
26
+ log.plain('Hex: ', data.toString('hex'));
27
+ log.plain('String: ', data.toString());
28
+ }
29
+ };
@@ -0,0 +1,8 @@
1
+ 'use strict';
2
+
3
+ const path = require('path');
4
+
5
+ exports.command = 'protocol <command>';
6
+ exports.description = 'Inspect and test raw miIO-commands';
7
+ exports.builder = yargs => yargs.commandDir(path.join(__dirname, 'protocol'));
8
+ exports.handler = () => {};
@@ -0,0 +1,58 @@
1
+ 'use strict';
2
+
3
+ const log = require('../../log');
4
+ const deviceFinder = require('../../device-finder');
5
+
6
+ exports.command = 'update <idOrIp>';
7
+ exports.description = 'Update the token to use for the given device';
8
+ exports.builder = {
9
+ token: {
10
+ required: true,
11
+ description: 'The token to set'
12
+ }
13
+ };
14
+
15
+ exports.handler = function(argv) {
16
+ let target = argv.idOrIp;
17
+ log.info('Updating token for', target);
18
+
19
+ let foundDevice = false;
20
+ let pending = 0;
21
+ const browser = deviceFinder({
22
+ filter: target
23
+ });
24
+ browser.on('available', device => {
25
+ pending++;
26
+
27
+ log.plain();
28
+ log.info('Connected to', device.id, ' - trying to change token');
29
+ log.plain();
30
+
31
+ device.management.updateToken(argv.token)
32
+ .then(status => {
33
+ if(status) {
34
+ log.plain('Token has been updated');
35
+ } else {
36
+ log.error('Could not update token, double-check the given token');
37
+ }
38
+ })
39
+ .catch(err => {
40
+ log.error('Could update token. Error was:', err.message);
41
+ })
42
+ .then(() => {
43
+ pending--;
44
+ process.exit(0); // eslint-disable-line
45
+ });
46
+ });
47
+
48
+ const doneHandler = () => {
49
+ if(pending === 0) {
50
+ if(! foundDevice) {
51
+ log.warn('Could not find device');
52
+ }
53
+ process.exit(0); // eslint-disable-line
54
+ }
55
+ };
56
+ setTimeout(doneHandler, 5000);
57
+ browser.on('done', doneHandler);
58
+ };
@@ -0,0 +1,8 @@
1
+ 'use strict';
2
+
3
+ const path = require('path');
4
+
5
+ exports.command = 'tokens <command>';
6
+ exports.description = 'Manage tokens of devices';
7
+ exports.builder = yargs => yargs.commandDir(path.join(__dirname, 'tokens'));
8
+ exports.handler = () => {};
@@ -0,0 +1,66 @@
1
+ 'use strict';
2
+
3
+ const { EventEmitter } = require('events');
4
+ const { Devices } = require('../lib/discovery');
5
+ const connectToDevice = require('../lib/connectToDevice');
6
+
7
+ const IP = /^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
8
+
9
+ function asFilter(filter) {
10
+ if(typeof filter === 'number') {
11
+ filter = String(filter);
12
+ }
13
+
14
+ if(typeof filter === 'string') {
15
+ return reg => {
16
+ // Assign management if this filter is filtering a device
17
+ const mgmt = reg.management ? reg.management : reg;
18
+
19
+ // Make sure to remove `miio:` prefix from ids when matching
20
+ let id = String(reg.id);
21
+ if(id.indexOf('miio:') === 0) {
22
+ id = id.substring(5);
23
+ }
24
+
25
+ // Match id, address or model
26
+ return id === filter || mgmt.address === filter || mgmt.model === filter;
27
+ };
28
+ } else if(typeof filter === 'function') {
29
+ return filter;
30
+ } else {
31
+ return () => true;
32
+ }
33
+ }
34
+
35
+ function asToplevelFilter(filter) {
36
+ return reg => reg.type === 'gateway' || filter(reg);
37
+ }
38
+
39
+ module.exports = function(options={}) {
40
+ const result = new EventEmitter();
41
+
42
+ if(typeof options.filter === 'string' && options.filter.match(IP)) {
43
+ // IP-address specified
44
+ connectToDevice({
45
+ address: options.filter,
46
+ withPlaceholder: true
47
+ })
48
+ .then(device => result.emit('available', device))
49
+ .catch(() => result.emit('done'));
50
+ } else {
51
+ //
52
+ const filter = asFilter(options.filter);
53
+ const browser = new Devices({
54
+ cacheTime: options.cacheTime || 300,
55
+ filter: options.filter ? asToplevelFilter(filter) : null,
56
+ });
57
+
58
+ browser.on('available', device => {
59
+ if(filter(device)) {
60
+ result.emit('available', device);
61
+ }
62
+ });
63
+ browser.on('unavailable', device => result.emit('unavailable', device));
64
+ }
65
+ return result;
66
+ };
package/cli/index.js ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+
4
+ const path = require('path');
5
+ require('yargs')
6
+ .commandDir(path.join(__dirname, 'commands'))
7
+ .recommendCommands()
8
+ .demandCommand()
9
+ .argv;