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,244 @@
1
+ 'use strict';
2
+
3
+ const { Thing, Children } = require('abstract-things');
4
+ const { ChildSyncer } = require('abstract-things/children');
5
+
6
+ const MiioApi = require('../device');
7
+ const { Illuminance } = require('./capabilities/sensor');
8
+
9
+ const DeveloperApi = require('./gateway/developer-api');
10
+ const CHARS = '0123456789abcdefghijklmnopqrstuvwxyz';
11
+
12
+ const LightMixin = require('./gateway/light-mixin');
13
+ const SubDevice = require('./gateway/subdevice');
14
+ const types = require('./gateway/subdevices');
15
+
16
+ /**
17
+ * Generate a key for use with the local developer API. This does not generate
18
+ * a secure key, but the Mi Home app does not seem to do that either.
19
+ */
20
+ function generateKey() {
21
+ let result = '';
22
+ for(let i=0; i<16; i++) {
23
+ let idx = Math.floor(Math.random() * CHARS.length);
24
+ result += CHARS[idx];
25
+ }
26
+ return result;
27
+ }
28
+
29
+ /**
30
+ * Abstraction over a Mi Smart Home Gateway.
31
+ *
32
+ * This device is very different from most other miIO-devices in that it has
33
+ * devices on its own. There seems to be a way to talk to these devices by
34
+ * setting `sid` in the requests, which means we can poll them. The problem
35
+ * is that certain devices also send events, but these seem to only be
36
+ * accessible over a local developer connection.
37
+ *
38
+ * So to work around this we activate the local developer connection and use
39
+ * that together with the regular calls for changing things on the gateway.
40
+ */
41
+ const Gateway = Thing.type(Parent => class Gateway extends Parent.with(MiioApi, Children) {
42
+ static get type() {
43
+ return 'miio:gateway';
44
+ }
45
+
46
+ static get SubDevice() { return SubDevice; }
47
+
48
+ static registerSubDevice(id, deviceClass) {
49
+ types[id] = deviceClass;
50
+ }
51
+
52
+ static availableAPI(builder) {
53
+ builder.action('addDevice')
54
+ .done();
55
+
56
+ builder.action('stopAddDevice')
57
+ .done();
58
+
59
+ builder.action('removeDevice')
60
+ .done();
61
+ }
62
+
63
+ constructor(options) {
64
+ super(options);
65
+
66
+ this.defineProperty('illumination', {
67
+ name: 'illuminance'
68
+ });
69
+
70
+ this.defineProperty('rgb', {
71
+ handler: (result, rgba) => {
72
+ result['rgb'] = {
73
+ red: 0xff & (rgba >> 16),
74
+ green: 0xff & (rgba >> 8),
75
+ blue: 0xff & rgba
76
+ };
77
+ result['brightness'] = Math.round(0xff & (rgba >> 24));
78
+ }
79
+ });
80
+
81
+ this.extraChildren = [];
82
+
83
+ // Monitor every minute right now
84
+ this._monitorInterval = 60 * 1000;
85
+
86
+ this.syncer = new ChildSyncer(this, (def, current) => {
87
+ // Shortcut to detect extra child
88
+ if(def.metadata && def.id) return def;
89
+
90
+ if(! def.online) return;
91
+ if(current) return current;
92
+
93
+ const type = types[def.type] || SubDevice;
94
+ const device = new type(this, {
95
+ id: def.id,
96
+ model: 'lumi.generic.' + def.type
97
+ });
98
+
99
+ return device;
100
+ });
101
+ }
102
+
103
+ _report(properties) {
104
+ Object.keys(properties).forEach(key => {
105
+ this.setRawProperty(key, properties[key]);
106
+ });
107
+ }
108
+
109
+ initCallback() {
110
+ return super.initCallback()
111
+ .then(() => this._findDeveloperKey())
112
+ .then(() => this._updateDeviceList())
113
+ .then(() => {
114
+ // Update devices once every half an hour
115
+ this._deviceListTimer = setInterval(this._updateDeviceList.bind(this), 30 * 60 * 1000);
116
+ });
117
+ }
118
+
119
+ destroyCallback() {
120
+ return super.destroyCallback()
121
+ .then(() => {
122
+
123
+ if(this.devApi) {
124
+ this.devApi.destroy();
125
+ }
126
+
127
+ clearInterval(this._deviceListTimer);
128
+ });
129
+ }
130
+
131
+ addExtraChild(child) {
132
+ this.extraChildren.push(child);
133
+ }
134
+
135
+ _updateDeviceList() {
136
+ function stripLumiFromId(id) {
137
+ if(id.indexOf('lumi.') === 0) {
138
+ return id.substring(5);
139
+ }
140
+ return id;
141
+ }
142
+
143
+ return this.call('get_device_prop', [ 'lumi.0', 'device_list' ])
144
+ .then(list => {
145
+ const defs = [ ...this.extraChildren ];
146
+ for(let i=0; i<list.length; i+=5) {
147
+ const id = stripLumiFromId(list[i]);
148
+ const type = list[i+1];
149
+ const online = list[i+2] === 1;
150
+
151
+ // Sanity check, skip gateway if found
152
+ if(id === '0') continue;
153
+
154
+ defs.push({
155
+ id: 'miio:' + id,
156
+ type: type,
157
+ online: online
158
+ });
159
+ }
160
+
161
+ return this.syncer.update(defs);
162
+ });
163
+ }
164
+
165
+ /**
166
+ * Either resolve the developer key or setup a new one.
167
+ */
168
+ _findDeveloperKey() {
169
+ // We already have a developer key
170
+ if(this._developerKey) return Promise.resolve(this._developerKey);
171
+
172
+ // Call and fetch the key or set a new key
173
+ return this.call('get_lumi_dpf_aes_key')
174
+ .then(result => {
175
+ let key = result[0];
176
+ if(key && key.length === 16) {
177
+ // This is a valid key, store it
178
+ return key;
179
+ }
180
+
181
+ key = generateKey();
182
+ return this.call('set_lumi_dpf_aes_key', [ key ]);
183
+ })
184
+ .then(key => this._setDeveloperKey(key));
185
+ }
186
+
187
+ /**
188
+ * Set the developer key and start listenting for events from the gateway.
189
+ */
190
+ _setDeveloperKey(key) {
191
+ this._developerKey = key;
192
+
193
+ // If we are already connected to the Developer API skip init
194
+ if(this.devApi) return Promise.resolve();
195
+
196
+ return new Promise((resolve, reject) => {
197
+ this.devApi = new DeveloperApi(this, this.management.address);
198
+
199
+ this.devApi.on('propertiesChanged', e => {
200
+ if(e.id === '0') {
201
+ // Data for the local gateway
202
+ this._report(e.data);
203
+ }
204
+ });
205
+ this.devApi.on('ready', resolve);
206
+
207
+ setTimeout(reject, 10000);
208
+ });
209
+ }
210
+
211
+ /**
212
+ * Start the process of adding a device. Allows Zigbee devices to join for
213
+ * 30 seconds.
214
+ */
215
+ addDevice() {
216
+ return this.call('start_zigbee_join', [ 30 ])
217
+ .then(MiioApi.checkOk)
218
+ .then(() => setTimeout(this._updateDeviceList.bind(this), 30000))
219
+ .then(() => undefined);
220
+ }
221
+
222
+ /**
223
+ * Stop allowing devices to join.
224
+ */
225
+ stopAddDevice() {
226
+ return this.call('start_zigbee_join', [ 0 ])
227
+ .then(MiioApi.checkOk)
228
+ .then(() => undefined);
229
+ }
230
+
231
+ /**
232
+ * Remove a device from the gateway using the identifier of the device.
233
+ */
234
+ removeDevice(id) {
235
+ return this.call('remove_device', [ id ])
236
+ .then(MiioApi.checkOk)
237
+ .then(this._updateDeviceList.bind(this))
238
+ .then(() => undefined);
239
+ }
240
+
241
+ });
242
+
243
+ module.exports.Basic = Gateway;
244
+ module.exports.WithLightAndSensor = Gateway.with(LightMixin, Illuminance);
@@ -0,0 +1,162 @@
1
+ 'use strict';
2
+
3
+ const { SwitchableMode } = require('abstract-things');
4
+ const { Humidifier, AdjustableTargetHumidity } = require('abstract-things/climate');
5
+
6
+ const MiioApi = require('../device');
7
+
8
+ const Power = require('./capabilities/power');
9
+ const Buzzer = require('./capabilities/buzzer');
10
+ const LEDBrightness = require('./capabilities/changeable-led-brightness');
11
+ const { Temperature, Humidity } = require('./capabilities/sensor');
12
+
13
+ /**
14
+ * Abstraction over a Mi Humidifier.
15
+ *
16
+ */
17
+ module.exports = class extends Humidifier
18
+ .with(MiioApi, Power, SwitchableMode,
19
+ AdjustableTargetHumidity, Temperature, Humidity,
20
+ Buzzer, LEDBrightness)
21
+ {
22
+ static get type() {
23
+ return 'miio:humidifier';
24
+ }
25
+
26
+ constructor(options) {
27
+ super(options);
28
+
29
+ this.defineProperty('power', v => v === 'on');
30
+
31
+ // Set the mode property and supported modes
32
+ this.defineProperty('mode');
33
+ this.updateModes([
34
+ 'idle',
35
+ 'silent',
36
+ 'medium',
37
+ 'high'
38
+ ]);
39
+
40
+ // Humidity limit
41
+ this.defineProperty('limit_hum', {
42
+ name: 'targetHumidity',
43
+ });
44
+
45
+ // Sensor values reported by the device
46
+ this.defineProperty('temp_dec', {
47
+ name: 'temperature',
48
+ mapper: v => v / 10.0
49
+ });
50
+
51
+ this.defineProperty('humidity');
52
+
53
+ // Buzzer and beeping
54
+ this.defineProperty('buzzer', {
55
+ mapper: v => v === 'on'
56
+ });
57
+
58
+ this.defineProperty('led_b', {
59
+ name: 'ledBrightness',
60
+ mapper: v => {
61
+ switch(v) {
62
+ case 0:
63
+ return 'bright';
64
+ case 1:
65
+ return 'dim';
66
+ case 2:
67
+ return 'off';
68
+ default:
69
+ return 'unknown';
70
+ }
71
+ }
72
+ });
73
+ }
74
+
75
+ propertyUpdated(key, value) {
76
+ /*
77
+ * Emulate that the humidifier has an idle mode.
78
+ */
79
+ if(key === 'power') {
80
+ if(value) {
81
+ const mode = this.property('mode');
82
+ this.updateMode(mode);
83
+ } else {
84
+ this.updateMode('idle');
85
+ }
86
+ } else if(key === 'mode') {
87
+ const power = this.property('power');
88
+ if(power) {
89
+ this.updateMode(value);
90
+ } else {
91
+ this.updateMode('idle');
92
+ }
93
+ } else if(key === 'targetHumidity') {
94
+ this.updateTargetHumidity(value);
95
+ }
96
+
97
+ super.propertyUpdated(key, value);
98
+ }
99
+
100
+ changePower(power) {
101
+ return this.call('set_power', [ power ? 'on' : 'off' ], {
102
+ refresh: [ 'power', 'mode' ],
103
+ refreshDelay: 200
104
+ });
105
+ }
106
+
107
+ /**
108
+ * Set the mode of this device.
109
+ */
110
+ changeMode(mode) {
111
+ let promise;
112
+ if(mode === 'idle') {
113
+ return this.setPower(false);
114
+ } else {
115
+ if(this.power()) {
116
+ // Already powered on, do nothing
117
+ promise = Promise.resolve();
118
+ } else {
119
+ // Not powered on, power on
120
+ promise = this.setPower(true);
121
+ }
122
+ }
123
+
124
+ return promise.then(() => {
125
+ return this.call('set_mode', [ mode ], {
126
+ refresh: [ 'power', 'mode' ],
127
+ refreshDelay: 200
128
+ })
129
+ .then(MiioApi.checkOk)
130
+ .catch(err => {
131
+ throw err.code === -5001 ? new Error('Mode `' + mode + '` not supported') : err;
132
+ });
133
+ });
134
+ }
135
+
136
+ changeTargetHumidity(humidity) {
137
+ return this.call('set_limit_hum', [ humidity ], {
138
+ refresh: [ 'targetHumidity' ]
139
+ }).then(MiioApi.checkOk);
140
+ }
141
+
142
+ /**
143
+ * Set the LED brightness to either `bright`, `dim` or `off`.
144
+ */
145
+ changeLEDBrightness(level) {
146
+ switch(level) {
147
+ case 'bright':
148
+ level = 0;
149
+ break;
150
+ case 'dim':
151
+ level = 1;
152
+ break;
153
+ case 'off':
154
+ level = 2;
155
+ break;
156
+ default:
157
+ return Promise.reject(new Error('Invalid LED brigthness: ' + level));
158
+ }
159
+ return this.call('set_led_b', [ level ], { refresh: [ 'ledBrightness' ] })
160
+ .then(() => null);
161
+ }
162
+ };
@@ -0,0 +1,22 @@
1
+ 'use strict';
2
+
3
+ const Vacuum = require('./vacuum');
4
+ const checkResult = require('../checkResult');
5
+
6
+ module.exports = class extends Vacuum {
7
+ async cleanRooms(listOfRooms) {
8
+ try {
9
+ await super.cleanRooms(listOfRooms);
10
+ } catch (err) {
11
+ // From https://github.com/rytilahti/python-miio/issues/550#issuecomment-552780952
12
+ return this.call(
13
+ 'set_mode_withroom',
14
+ [0, 1, listOfRooms.length].concat(listOfRooms),
15
+ {
16
+ refresh: ['state'],
17
+ refreshDelay: 1000,
18
+ }
19
+ ).then(checkResult);
20
+ }
21
+ }
22
+ };
@@ -0,0 +1,73 @@
1
+ 'use strict';
2
+
3
+ const { LightBulb, ColorTemperature } = require('abstract-things/lights');
4
+ const { color } = require('abstract-things/values');
5
+ const MiioApi = require('../device');
6
+
7
+ const Power = require('./capabilities/power');
8
+ const Dimmable = require('./capabilities/dimmable');
9
+ const Colorable = require('./capabilities/colorable');
10
+
11
+ const MIN_TEMP = 3000;
12
+ const MAX_TEMP = 5700;
13
+
14
+ module.exports = class BallLamp extends LightBulb
15
+ .with(MiioApi, Power, Dimmable, Colorable, ColorTemperature)
16
+ {
17
+ static get type() {
18
+ return 'miio:philiphs-ball-lamp';
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
+ this.defineProperty('cct', {
35
+ name: 'color',
36
+ mapper: v => {
37
+ v = parseInt(v);
38
+ return color.temperature(MIN_TEMP + (v / 100) * (MAX_TEMP - MIN_TEMP));
39
+ }
40
+ });
41
+
42
+ this.updateColorTemperatureRange(MIN_TEMP, MAX_TEMP);
43
+ }
44
+
45
+ changePower(power) {
46
+ return this.call('set_power', [ power ? 'on' : 'off' ], {
47
+ refresh: [ 'power' ]
48
+ }).then(MiioApi.checkOk);
49
+ }
50
+
51
+ changeBrightness(brightness) {
52
+ return this.call('set_bright', [ brightness ], {
53
+ refresh: [ 'brightness' ]
54
+ }).then(MiioApi.checkOk);
55
+ }
56
+
57
+ changeColor(color) {
58
+ const kelvins = color.temperature.kelvins;
59
+ let temp;
60
+ if(kelvins <= MIN_TEMP) {
61
+ temp = 1;
62
+ } else if(kelvins >= MAX_TEMP) {
63
+ temp = 100;
64
+ } else {
65
+ temp = Math.round((kelvins - MIN_TEMP) / (MAX_TEMP - MIN_TEMP) * 100);
66
+ }
67
+
68
+ return this.call('set_cct', [ temp ], {
69
+ refresh: [ 'color']
70
+ }).then(MiioApi.checkOk);
71
+ }
72
+
73
+ };
@@ -0,0 +1,26 @@
1
+ 'use strict';
2
+
3
+ const { Thing } = require('abstract-things');
4
+ const { PowerPlug, PowerOutlet } = require('abstract-things/electrical');
5
+ const MiioApi = require('../device');
6
+ const Power = require('./capabilities/power');
7
+
8
+ module.exports = class extends Thing
9
+ .with(PowerPlug, PowerOutlet, MiioApi, Power)
10
+ {
11
+ static get type() {
12
+ return 'miio:power-plug';
13
+ }
14
+
15
+ constructor(options) {
16
+ super(options);
17
+
18
+ this.defineProperty('power', {
19
+ mapper: v => v === 'on'
20
+ });
21
+ }
22
+
23
+ changePower(power) {
24
+ return this.call('set_power', [ power ? 'on' : 'off' ], { refresh: [ 'power' ] });
25
+ }
26
+ };
@@ -0,0 +1,38 @@
1
+ 'use strict';
2
+
3
+ const { PowerStrip } = require('abstract-things/electrical');
4
+
5
+ const MiioApi = require('../device');
6
+ const Power = require('./capabilities/power');
7
+ const Mode = require('./capabilities/mode');
8
+
9
+ module.exports = class extends PowerStrip
10
+ .with(MiioApi, Power, Mode)
11
+ {
12
+ static get type() {
13
+ return 'miio:power-strip';
14
+ }
15
+
16
+ constructor(options) {
17
+ super(options);
18
+
19
+ this.defineProperty('power', {
20
+ mapper: v => v === 'n'
21
+ });
22
+
23
+ this.updateModes([
24
+ 'green',
25
+ 'normal'
26
+ ]);
27
+
28
+ this.defineProperty('mode');
29
+ }
30
+
31
+ changePower(power) {
32
+ return this.call('set_power', [ power ? 'on' : 'off' ], { refresh: [ 'power' ] });
33
+ }
34
+
35
+ changeMode(mode) {
36
+ return this.call('set_power_mode', [ mode ]);
37
+ }
38
+ };