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,286 @@
1
+ 'use strict';
2
+
3
+ const { Thing, Nameable } = require('abstract-things');
4
+ const { Light, Fading, Colorable, ColorTemperature, ColorFull } = require('abstract-things/lights');
5
+ const { color } = require('abstract-things/values');
6
+ const MiioApi = require('../device');
7
+
8
+ const Power = require('./capabilities/power');
9
+ const Dimmable = require('./capabilities/dimmable');
10
+
11
+ const DEFAULT_EFFECT = 'smooth';
12
+ const DEFAULT_DURATION = 500;
13
+
14
+ const Yeelight = Thing.type(Parent => class Yeelight extends Parent
15
+ .with(Light, Fading, MiioApi, Power, Dimmable, Nameable)
16
+ {
17
+ static get type() {
18
+ return 'miio:yeelight';
19
+ }
20
+
21
+ constructor(options) {
22
+ super(options);
23
+
24
+ this.defineProperty('power', {
25
+ name: 'power',
26
+ mapper: v => v === 'on'
27
+ });
28
+
29
+ this.defineProperty('bright', {
30
+ name: 'brightness',
31
+ mapper: parseInt
32
+ });
33
+
34
+ // Used for scheduling turning off after a while
35
+ this.defineProperty('delayoff', {
36
+ name: 'offDelay',
37
+ mapper: parseInt
38
+ });
39
+
40
+ // Query for the color mode
41
+ this.defineProperty('color_mode', {
42
+ name: 'colorMode',
43
+ mapper: v => {
44
+ v = parseInt(v);
45
+ switch(v) {
46
+ case 1:
47
+ return 'rgb';
48
+ case 2:
49
+ return 'colorTemperature';
50
+ case 3:
51
+ return 'hsv';
52
+ }
53
+ }
54
+ });
55
+
56
+ // Read the name of the light
57
+ this.defineProperty('name');
58
+
59
+ /*
60
+ * Set maximum fade time to 30s - seems to work well, longer values
61
+ * sometimes cause jumps on certain models.
62
+ */
63
+ this.updateMaxChangeTime('30s');
64
+ }
65
+
66
+ propertyUpdated(key, value) {
67
+ if(key === 'name') {
68
+ this.metadata.name = value;
69
+ }
70
+
71
+ super.propertyUpdated(key, value);
72
+ }
73
+
74
+ changePower(power) {
75
+ // TODO: Support for duration
76
+ return this.call(
77
+ 'set_power',
78
+ Yeelight.withEffect(power ? 'on' : 'off'),
79
+ {
80
+ refresh: [ 'power']
81
+ }
82
+ ).then(MiioApi.checkOk);
83
+ }
84
+
85
+ /**
86
+ * Make the current state the default state, meaning the light will restore
87
+ * to this state after power has been cut.
88
+ */
89
+ setDefault() {
90
+ return this.call('set_default')
91
+ .then(MiioApi.checkOk);
92
+ }
93
+
94
+ changeBrightness(brightness, options) {
95
+ if(brightness <= 0) {
96
+ return this.changePower(false);
97
+ } else {
98
+ let promise;
99
+ if(options.powerOn && this.power() === false) {
100
+ // Currently not turned on
101
+ promise = this.changePower(true);
102
+ } else {
103
+ promise = Promise.resolve();
104
+ }
105
+
106
+ return promise.then(() =>
107
+ this.call(
108
+ 'set_bright',
109
+ Yeelight.withEffect(brightness, options.duration), {
110
+ refresh: [ 'brightness' ]
111
+ })
112
+ ).then(MiioApi.checkOk);
113
+ }
114
+ }
115
+
116
+ changeName(name) {
117
+ return this.call('set_name', [ name ])
118
+ .then(MiioApi.checkOk)
119
+ .then(() => this.metadata.name = name);
120
+ }
121
+
122
+ /**
123
+ * Helper method to combine an argument with effect information.
124
+ */
125
+ static withEffect(arg, duration) {
126
+ const result = Array.isArray(arg) ? arg : [ arg ];
127
+
128
+ if(duration) {
129
+ if(duration.ms > 0) {
130
+ result.push(DEFAULT_EFFECT);
131
+ result.push(duration.ms);
132
+ } else {
133
+ result.push('sudden');
134
+ result.push(0);
135
+ }
136
+ } else {
137
+ result.push(DEFAULT_EFFECT);
138
+ result.push(DEFAULT_DURATION);
139
+ }
140
+
141
+ return result;
142
+ }
143
+ });
144
+
145
+ module.exports.Yeelight = Yeelight;
146
+
147
+ module.exports.ColorTemperature = Thing.mixin(Parent => class extends Parent
148
+ .with(MiioApi, Colorable, ColorTemperature)
149
+ {
150
+
151
+ constructor(...args) {
152
+ super(...args);
153
+
154
+ // Color temperature
155
+ this.defineProperty('ct', {
156
+ name: 'colorTemperature',
157
+ mapper: parseInt
158
+ });
159
+
160
+ // TODO: Do all Yeelights use the same color range?
161
+ this.updateColorTemperatureRange(2700, 6500);
162
+ }
163
+
164
+ propertyUpdated(key, value) {
165
+ if(key === 'colorTemperature') {
166
+ this.updateColor(color.temperature(value));
167
+ }
168
+
169
+ super.propertyUpdated(key, value);
170
+ }
171
+
172
+ changeColor(color, options) {
173
+ const range = this.colorTemperatureRange;
174
+ const temperature = Math.min(Math.max(color.temperature.kelvins, range.min), range.max);
175
+
176
+ return this.call('set_ct_abx', Yeelight.withEffect(temperature, options.duration), {
177
+ refresh: [ 'colorTemperature' ]
178
+ }).then(MiioApi.checkOk);
179
+ }
180
+ });
181
+
182
+ module.exports.ColorFull = Thing.mixin(Parent => class extends Parent
183
+ .with(MiioApi, Colorable, ColorTemperature, ColorFull)
184
+ {
185
+
186
+ constructor(...args) {
187
+ super(...args);
188
+
189
+ // Color temperature
190
+ this.defineProperty('ct', {
191
+ name: 'colorTemperature',
192
+ mapper: parseInt
193
+ });
194
+
195
+ this.defineProperty('rgb', {
196
+ name: 'colorRGB',
197
+ mapper: rgb => {
198
+ rgb = parseInt(rgb);
199
+
200
+ return {
201
+ red: (rgb >> 16) & 0xff,
202
+ green: (rgb >> 8) & 0xff,
203
+ blue: rgb & 0xff
204
+ };
205
+ }
206
+ });
207
+
208
+ this.defineProperty('hue', {
209
+ name: 'colorHue',
210
+ mapper: parseInt
211
+ });
212
+
213
+ this.defineProperty('sat', {
214
+ name: 'colorSaturation',
215
+ mapper: parseInt
216
+ });
217
+
218
+ this.metadata.addCapabilities('color:temperature', 'color:full');
219
+
220
+ // TODO: Do all Yeelights use the same color range?
221
+ this.updateColorTemperatureRange(2700, 6500);
222
+ }
223
+
224
+ propertyUpdated(key, value) {
225
+ if(key === 'colorTemperature' || key === 'colorMode' || key === 'colorRGB' || key === 'colorHue' || key === 'colorSaturation' || key === 'brightness') {
226
+ let currentColor = this.color();
227
+ switch(this.property('colorMode')) {
228
+ case 'colorTemperature':
229
+ // Currently using color temperature mode, parse as temperature
230
+ currentColor = color.temperature(this.property('colorTemperature'));
231
+ break;
232
+ case 'rgb': {
233
+ // Using RGB, parse if we have gotten the RGB value
234
+ let rgb = this.property('colorRGB');
235
+ if(rgb) {
236
+ currentColor = color.rgb(rgb.red, rgb.green, rgb.blue);
237
+ }
238
+ break;
239
+ }
240
+ case 'hsv': {
241
+ // Using HSV, parse the hue, saturation and get the brightness to set color
242
+ let hue = this.property('colorHue');
243
+ let saturation = this.property('colorSaturation');
244
+
245
+ if(typeof hue !== 'undefined' && typeof saturation !== 'undefined') {
246
+ currentColor = color.hsv(hue, saturation, this.property('brightness'));
247
+ }
248
+ }
249
+ }
250
+
251
+ this.updateColor(currentColor);
252
+ }
253
+
254
+ super.propertyUpdated(key, value);
255
+ }
256
+
257
+ changeColor(color, options) {
258
+ if(color.is('temperature')) {
259
+ // The user has request a color via temperature
260
+
261
+ const range = this.colorTemperatureRange;
262
+ const temperature = Math.min(Math.max(color.temperature.kelvins, range.min), range.max);
263
+
264
+ return this.call('set_ct_abx', Yeelight.withEffect(temperature, options.duration), {
265
+ refresh: [ 'colorTemperature', 'colorMode' ]
266
+ }).then(MiioApi.checkOk);
267
+ } else if(color.is('hsl')) {
268
+ /*
269
+ * User has requested hue and saturation
270
+ */
271
+ return this.call('set_hsv', Yeelight.withEffect([ color.hue, color.saturation ], options.duration), {
272
+ refresh: [ 'colorHue', 'colorSaturation', 'colorMode' ]
273
+ }).then(MiioApi.checkOk);
274
+ } else {
275
+ /*
276
+ * Fallback to applying via RGB.
277
+ */
278
+ color = color.rgb;
279
+
280
+ const rgb = color.red * 65536 + color.green * 256 + color.blue;
281
+ return this.call('set_rgb', Yeelight.withEffect(rgb, options.duration), {
282
+ refresh: [ 'colorRGB', 'colorMode' ]
283
+ }).then(MiioApi.checkOk);
284
+ }
285
+ }
286
+ });
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+
3
+ const { Yeelight, ColorTemperature } = require('./yeelight');
4
+
5
+ module.exports = class YeelightMono extends Yeelight.with(ColorTemperature) {
6
+ constructor(options) {
7
+ super(options);
8
+ }
9
+ };
@@ -0,0 +1,192 @@
1
+ 'use strict';
2
+
3
+ const {
4
+ TimedDiscovery,
5
+ BasicDiscovery,
6
+ search,
7
+ addService,
8
+ removeService,
9
+ } = require('tinkerhub-discovery');
10
+ const { Children } = require('abstract-things');
11
+
12
+ const util = require('util');
13
+ const dns = require('dns');
14
+
15
+ const network = require('./network');
16
+ const infoFromHostname = require('./infoFromHostname');
17
+
18
+ const connectToDevice = require('./connectToDevice');
19
+
20
+ const tryAdd = Symbol('tryAdd');
21
+
22
+ const Browser = (module.exports.Browser = class Browser extends TimedDiscovery {
23
+ static get type() {
24
+ return 'miio';
25
+ }
26
+
27
+ constructor(options) {
28
+ super({
29
+ maxStaleTime: (options.cacheTime || 1800) * 1000,
30
+ });
31
+
32
+ this.manualTokens = options.tokens || {};
33
+ this[tryAdd] = this[tryAdd].bind(this);
34
+
35
+ this.start();
36
+ }
37
+
38
+ _manualToken(id) {
39
+ return this.manualTokens[id] || null;
40
+ }
41
+
42
+ start() {
43
+ this.handle = network.ref();
44
+ network.on('device', this[tryAdd]);
45
+
46
+ super.start();
47
+ }
48
+
49
+ stop() {
50
+ super.stop();
51
+
52
+ network.removeListener('device', this[tryAdd]);
53
+ this.handle.release();
54
+ }
55
+
56
+ [search]() {
57
+ network.search();
58
+ }
59
+
60
+ [tryAdd](device) {
61
+ const service = {
62
+ id: device.id,
63
+ address: device.address,
64
+ port: device.port,
65
+ token: device.token || this._manualToken(device.id),
66
+ autoToken: device.autoToken,
67
+
68
+ connect: function (options = {}) {
69
+ return connectToDevice(
70
+ Object.assign(
71
+ {
72
+ address: this.address,
73
+ port: this.port,
74
+ model: this.model,
75
+ },
76
+ options
77
+ )
78
+ );
79
+ },
80
+ };
81
+
82
+ const add = () => this[addService](service);
83
+
84
+ // Give us five seconds to try resolve some extras for new devices
85
+ setTimeout(add, 5000);
86
+
87
+ dns.lookupService(service.address, service.port, (err, hostname) => {
88
+ if (err || !hostname) {
89
+ add();
90
+ return;
91
+ }
92
+
93
+ service.hostname = hostname;
94
+ const info = infoFromHostname(hostname);
95
+ if (info) {
96
+ service.model = info.model;
97
+ }
98
+
99
+ add();
100
+ });
101
+ }
102
+
103
+ [util.inspect.custom]() {
104
+ return 'MiioBrowser{}';
105
+ }
106
+ });
107
+
108
+ class Devices extends BasicDiscovery {
109
+ static get type() {
110
+ return 'miio:devices';
111
+ }
112
+
113
+ constructor(options) {
114
+ super();
115
+
116
+ this._filter = options && options.filter;
117
+ this._skipSubDevices = options && options.skipSubDevices;
118
+
119
+ this._browser = new Browser(options).map((reg) => {
120
+ return connectToDevice({
121
+ address: reg.address,
122
+ port: reg.port,
123
+ model: reg.model,
124
+ withPlaceholder: true,
125
+ token: options.token,
126
+ });
127
+ });
128
+
129
+ this._browser.on('available', (s) => {
130
+ this[addService](s);
131
+
132
+ if (s instanceof Children) {
133
+ this._bindSubDevices(s);
134
+ }
135
+ });
136
+
137
+ this._browser.on('unavailable', (s) => {
138
+ this[removeService](s);
139
+ });
140
+ }
141
+
142
+ start() {
143
+ super.start();
144
+
145
+ this._browser.start();
146
+ }
147
+
148
+ stop() {
149
+ super.stop();
150
+
151
+ this._browser.stop();
152
+ }
153
+
154
+ [util.inspect.custom]() {
155
+ return 'MiioDevices{}';
156
+ }
157
+
158
+ _bindSubDevices(device) {
159
+ if (this._skipSubDevices) return;
160
+
161
+ const handleAvailable = (sub) => {
162
+ if (!sub.miioModel) return;
163
+
164
+ const reg = {
165
+ id: sub.internalId,
166
+ model: sub.model,
167
+ type: sub.type,
168
+
169
+ parent: device,
170
+ device: sub,
171
+ };
172
+
173
+ if (this._filter && !this._filter(reg)) {
174
+ // Filter does not match sub device
175
+ return;
176
+ }
177
+
178
+ // Register and emit event
179
+ this[addService](sub);
180
+ };
181
+
182
+ device.on('thing:available', handleAvailable);
183
+ device.on('thing:unavailable', (sub) => this[removeService](sub.id));
184
+
185
+ // Register initial devices
186
+ for (const child of device.children()) {
187
+ handleAvailable(child);
188
+ }
189
+ }
190
+ }
191
+
192
+ module.exports.Devices = Devices;
package/lib/index.js ADDED
@@ -0,0 +1,40 @@
1
+ 'use strict';
2
+
3
+ const discovery = require('./discovery');
4
+
5
+ /**
6
+ * Get information about the models supported. Can be used to extend the models
7
+ * supported.
8
+ */
9
+ module.exports.models = require('./models');
10
+
11
+ /**
12
+ * Resolve a device from the given options.
13
+ *
14
+ * Options:
15
+ * * `address`, **required** the address to the device as an IP or hostname
16
+ * * `port`, optional port number, if not specified the default 54321 will be used
17
+ * * `token`, optional token of the device
18
+ */
19
+ module.exports.device = require('./connectToDevice');
20
+
21
+ /**
22
+ * Extract information about a device from its hostname on the local network.
23
+ */
24
+ module.exports.infoFromHostname = require('./infoFromHostname');
25
+
26
+ /**
27
+ * Browse for devices available on the network. Will not automatically
28
+ * connect to them.
29
+ */
30
+ module.exports.browse = function(options) {
31
+ return new discovery.Browser(options || {});
32
+ };
33
+
34
+ /**
35
+ * Get access to all devices on the current network. Will find and connect to
36
+ * devices automatically.
37
+ */
38
+ module.exports.devices = function(options) {
39
+ return new discovery.Devices(options || {});
40
+ };
@@ -0,0 +1,24 @@
1
+ 'use strict';
2
+
3
+ module.exports = function(hostname) {
4
+ // Extract info via hostname structure
5
+ const m = /(.+)_miio(\d+)/g.exec(hostname);
6
+ if(! m) {
7
+ // Fallback for rockrobo - might break in the future
8
+ if(/rockrobo/g.exec(hostname)) {
9
+ return {
10
+ model: 'rockrobo.vacuum.v1',
11
+ type: 'vacuum'
12
+ };
13
+ }
14
+
15
+ return null;
16
+ }
17
+
18
+ const model = m[1].replace(/-/g, '.');
19
+
20
+ return {
21
+ model: model,
22
+ id: m[2]
23
+ };
24
+ };
@@ -0,0 +1,102 @@
1
+ 'use strict';
2
+
3
+ const tokens = require('./tokens');
4
+
5
+ /**
6
+ * Management of a device. Supports quering it for information and changing
7
+ * the WiFi settings.
8
+ */
9
+ class DeviceManagement {
10
+ constructor(device) {
11
+ this.api = device.handle.api;
12
+ }
13
+
14
+ get model() {
15
+ return this.api.model;
16
+ }
17
+
18
+ get token() {
19
+ const token = this.api.token;
20
+ return token ? token.toString('hex') : null;
21
+ }
22
+
23
+ get autoToken() {
24
+ return this.api.autoToken;
25
+ }
26
+
27
+ get address() {
28
+ return this.api.address;
29
+ }
30
+
31
+ /**
32
+ * Get information about this device. Includes model info, token and
33
+ * connection information.
34
+ */
35
+ info() {
36
+ return this.api.call('miIO.info');
37
+ }
38
+
39
+ /**
40
+ * Update the wireless settings of this device. Needs `ssid` and `passwd`
41
+ * to be set in the options object.
42
+ *
43
+ * `uid` can be set to associate the device with a Mi Home user id.
44
+ */
45
+ updateWireless(options) {
46
+ if(typeof options.ssid !== 'string') {
47
+ throw new Error('options.ssid must be a string');
48
+ }
49
+ if(typeof options.passwd !== 'string') {
50
+ throw new Error('options.passwd must be a string');
51
+ }
52
+
53
+ return this.api.call('miIO.config_router', options)
54
+ .then(result => {
55
+ if(result !== 0 && result !== 'OK' && result !== 'ok') {
56
+ throw new Error('Failed updating wireless');
57
+ }
58
+ return true;
59
+ });
60
+ }
61
+
62
+ /**
63
+ * Get the wireless state of this device. Includes if the device is
64
+ * online and counters for things such as authentication failures and
65
+ * connection success and failures.
66
+ */
67
+ wirelessState() {
68
+ return this.api.call('miIO.wifi_assoc_state');
69
+ }
70
+
71
+ /**
72
+ * Update the token used to connect to this device.
73
+ *
74
+ * @param {string|Buffer} token
75
+ */
76
+ updateToken(token) {
77
+ if(token instanceof Buffer) {
78
+ token = token.toString('hex');
79
+ } else if(typeof token !== 'string') {
80
+ return Promise.reject(new Error('Token must be a hex-string or a Buffer'));
81
+ }
82
+
83
+ // Lazily imported to solve recursive dependencies
84
+ const connectToDevice = require('./connectToDevice');
85
+
86
+ return connectToDevice({
87
+ address: this.address,
88
+ port: this.port,
89
+ token: token
90
+ }).then(device => {
91
+ // Connection to device could be performed
92
+ return tokens.update(this.api.id, token)
93
+ .then(() => device.destroy())
94
+ .then(() => true);
95
+ }).catch(err => {
96
+ // Connection to device failed with the token
97
+ return false;
98
+ });
99
+ }
100
+ }
101
+
102
+ module.exports = DeviceManagement;