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,179 @@
1
+ 'use strict';
2
+
3
+ const MiioApi = require('../device');
4
+
5
+ const { Children } = require('abstract-things');
6
+ const { Light, Dimmable, SwitchablePower } = require('abstract-things/lights');
7
+ const { percentage } = require('abstract-things/values');
8
+
9
+ const MiioDimmable = require('./capabilities/dimmable');
10
+ const MiioPower = require('./capabilities/power');
11
+
12
+ module.exports = class EyecareLamp2 extends Light
13
+ .with(MiioPower, MiioDimmable, Children, MiioApi)
14
+ {
15
+
16
+ constructor(options) {
17
+ super(options);
18
+
19
+ this.defineProperty('power', {
20
+ name: 'power',
21
+ mapper: v => v === 'on'
22
+ });
23
+
24
+ this.defineProperty('bright', {
25
+ name: 'brightness',
26
+ mapper: parseInt
27
+ });
28
+
29
+ // Get if the
30
+ this.defineProperty('notifystatus', {
31
+ name: 'notifyStatus',
32
+ mapper: v => v === 'on'
33
+ });
34
+
35
+ // Power of the secondary light
36
+ this.defineProperty('ambstatus', {
37
+ name: 'ambientPower',
38
+ mapper: v => v === 'on'
39
+ });
40
+
41
+ // Brightness of the secondary light
42
+ this.defineProperty('ambvalue', {
43
+ name: 'ambientBrightness',
44
+ mapper: parseInt
45
+ });
46
+
47
+ // If the main light is on
48
+ this.defineProperty('eyecare', {
49
+ name: 'eyeCare',
50
+ mapper: v => v === 'on'
51
+ });
52
+
53
+ // The current user-defined scene
54
+ this.defineProperty('scene_num', {
55
+ name: 'userScene',
56
+ mapper: v => {
57
+ switch (v) {
58
+ case 1:
59
+ return 'study';
60
+ case 2:
61
+ return 'reading';
62
+ case 3:
63
+ return 'phone';
64
+ }
65
+ }
66
+ });
67
+
68
+ this.setModes([ 'study', 'reading', 'phone' ]);
69
+
70
+ this.defineProperty('bls', {
71
+ name: 'bls',
72
+ mapper: v => v === 'on'
73
+ });
74
+
75
+ this.defineProperty('dvalue', {
76
+ name: 'dvalue',
77
+ mapper: parseInt
78
+ });
79
+
80
+ this.ambient = new AmbientLight(this);
81
+ this.addChild(this.ambient);
82
+ }
83
+
84
+ changePower(power) {
85
+ return this.call('set_power', [power ? 'on' : 'off'], {
86
+ refresh: true
87
+ });
88
+ }
89
+
90
+ changeBrightness(brightness) {
91
+ return this.call('set_bright', [ brightness ], {
92
+ refresh: true
93
+ }).then(MiioApi.checkOk);
94
+ }
95
+
96
+ /**
97
+ * Is eyeCare active?
98
+ */
99
+ eyeCare() {
100
+ return Promise.resolve(this.property('eyeCare'));
101
+ }
102
+
103
+ /**
104
+ * Enable the eyeCare
105
+ */
106
+ setEyeCare(enable) {
107
+ return this.call('set_eyecare', [enable ? 'on' : 'off'], {
108
+ refresh: true
109
+ })
110
+ .then(() => null);
111
+ }
112
+
113
+ /**
114
+ * Set the current eyeCare mode
115
+ *
116
+ changeMode(mode) {
117
+
118
+ switch (mode) {
119
+ case 'study':
120
+ mode = 1;
121
+ break;
122
+ case 'reading':
123
+ mode = 2;
124
+ break;
125
+ case 'phone':
126
+ mode = 3;
127
+ break;
128
+ default:
129
+ return Promise.reject(new Error('Invalid EyeCare Mode: ' + mode));
130
+ }
131
+ return this.call('set_user_scene', [mode], {
132
+ refresh: true
133
+ });
134
+ }
135
+ */
136
+
137
+ setAmbientPower(power) {
138
+ return this.call.send('enable_amb', [ power ? 'on' : 'off' ])
139
+ .then(MiioApi.checkOk);
140
+ }
141
+
142
+ setAmbientBrightness(brightness) {
143
+ brightness = percentage(brightness, { min: 0, max: 100 });
144
+
145
+ return this.call.send('set_amb_bright', [ brightness ])
146
+ .then(MiioApi.checkOk);
147
+ }
148
+
149
+ propertyUpdated(key, value) {
150
+ super.propertyUpdated(key, value);
151
+
152
+ switch(key) {
153
+ case 'ambientPower':
154
+ this.ambient.updatePower(value);
155
+ break;
156
+ case 'ambientBrightness':
157
+ this.ambient.updateBrightness(value);
158
+ break;
159
+ }
160
+ }
161
+ };
162
+
163
+ class AmbientLight extends Light.with(Dimmable, SwitchablePower) {
164
+
165
+ constructor(parent) {
166
+ super();
167
+
168
+ this.parent = parent;
169
+ this.id = parent.id + ':ambient';
170
+ }
171
+
172
+ changePower(power) {
173
+ return this.parent.setAmbientPower(power);
174
+ }
175
+
176
+ changeBrightness(brightness) {
177
+ return this.parent.setAmbientBrightness(brightness);
178
+ }
179
+ }
@@ -0,0 +1,28 @@
1
+ 'use strict';
2
+
3
+ const SubDevice = require('./subdevice');
4
+ const { WallController, Actions } = require('abstract-things/controllers');
5
+
6
+ module.exports = class Switch1 extends SubDevice.with(WallController, Actions) {
7
+ constructor(parent, info) {
8
+ super(parent, info);
9
+
10
+ this.type = 'controller';
11
+ this.miioModel = 'lumi.86sw1';
12
+
13
+ this.updateActions([
14
+ 'click',
15
+ 'double_click'
16
+ ]);
17
+ }
18
+
19
+ _report(data) {
20
+ super._report(data);
21
+
22
+ if(typeof data['channel_0'] !== 'undefined') {
23
+ const action = data['channel_0'];
24
+ this.debug('Action performed:', action);
25
+ this.emitAction(action);
26
+ }
27
+ }
28
+ };
@@ -0,0 +1,44 @@
1
+ 'use strict';
2
+
3
+ const SubDevice = require('./subdevice');
4
+ const { WallController, Actions } = require('abstract-things/controllers');
5
+
6
+ module.exports = class Switch2 extends SubDevice.with(WallController, Actions) {
7
+ constructor(parent, info) {
8
+ super(parent, info);
9
+
10
+ this.type = 'controller';
11
+ this.miioModel = 'lumi.86sw2';
12
+
13
+ this.updateActions([
14
+ 'btn0-click',
15
+ 'btn0-double_click',
16
+ 'btn1-click',
17
+ 'btn1-double_click',
18
+ 'both_click'
19
+ ]);
20
+ }
21
+
22
+ _report(data) {
23
+ super._report(data);
24
+
25
+ if(typeof data['channel_0'] !== 'undefined') {
26
+ const action = 'btn0-' + data['channel_0'];
27
+ this.debug('Action performed:', action);
28
+ this.emitAction(action);
29
+ }
30
+
31
+ if(typeof data['channel_1'] !== 'undefined') {
32
+ const action = 'btn1-' + data['channel_1'];
33
+ this.debug('Action performed:', action);
34
+ this.emitAction(action);
35
+ }
36
+
37
+ if(typeof data['dual_channel'] !== 'undefined') {
38
+ const action = data['dual_channel'];
39
+
40
+ this.debug('Action performed:', action);
41
+ this.emitAction(action);
42
+ }
43
+ }
44
+ };
@@ -0,0 +1,44 @@
1
+ 'use strict';
2
+
3
+ const { Children } = require('abstract-things');
4
+ const { WallSwitch } = require('abstract-things/electrical');
5
+
6
+ const SubDevice = require('./subdevice');
7
+ const LightChannel = require('./light-channel');
8
+
9
+ /**
10
+ * Single-channel L-N version of light switch.
11
+ */
12
+ module.exports = class CtrlLN1 extends WallSwitch.with(SubDevice, Children) {
13
+
14
+ static get type() {
15
+ return 'miio:power-switch';
16
+ }
17
+
18
+ constructor(parent, info) {
19
+ super(parent, info);
20
+
21
+ this.miioModel = 'lumi.ctrl_ln1';
22
+
23
+ this.defineProperty('channel_0', {
24
+ name: 'powerChannel0',
25
+ mapper: v => v === 'on'
26
+ });
27
+
28
+ this.addChild(new LightChannel(this, 0));
29
+ }
30
+
31
+ changePowerChannel(channel, power) {
32
+ return this.call('toggle_ctrl_neutral', [ 'neutral_' + channel, power ? 'on' : 'off' ]);
33
+ }
34
+
35
+ propertyUpdated(key, value) {
36
+ super.propertyUpdated(key, value);
37
+
38
+ switch(key) {
39
+ case 'powerChannel0':
40
+ this.child('0').updatePower(value);
41
+ break;
42
+ }
43
+ }
44
+ };
@@ -0,0 +1,53 @@
1
+ 'use strict';
2
+
3
+ const { Children } = require('abstract-things');
4
+ const { WallSwitch } = require('abstract-things/electrical');
5
+
6
+ const SubDevice = require('./subdevice');
7
+ const LightChannel = require('./light-channel');
8
+
9
+
10
+ /**
11
+ * Dual-channel L-N version of light switch.
12
+ */
13
+ module.exports = class CtrlLN2 extends WallSwitch.with(SubDevice, Children) {
14
+
15
+ static get type() {
16
+ return 'miio:power-switch';
17
+ }
18
+
19
+ constructor(parent, info) {
20
+ super(parent, info);
21
+
22
+ this.miioModel = 'lumi.ctrl_ln2';
23
+
24
+ this.defineProperty('channel_0', {
25
+ name: 'powerChannel0',
26
+ mapper: v => v === 'on'
27
+ });
28
+ this.defineProperty('channel_1', {
29
+ name: 'powerChannel1',
30
+ mapper: v => v === 'on'
31
+ });
32
+
33
+ this.addChild(new LightChannel(this, 0));
34
+ this.addChild(new LightChannel(this, 1));
35
+ }
36
+
37
+ changePowerChannel(channel, power) {
38
+ return this.call('toggle_ctrl_neutral', [ 'neutral_' + channel, power ? 'on' : 'off' ]);
39
+ }
40
+
41
+ propertyUpdated(key, value) {
42
+ super.propertyUpdated(key, value);
43
+
44
+ switch(key) {
45
+ case 'powerChannel0':
46
+ this.child('0').updatePower(value);
47
+ break;
48
+ case 'powerChannel1':
49
+ this.child('1').updatePower(value);
50
+ break;
51
+ }
52
+ }
53
+ };
@@ -0,0 +1,44 @@
1
+ 'use strict';
2
+
3
+ const { Children } = require('abstract-things');
4
+ const { WallSwitch } = require('abstract-things/electrical');
5
+
6
+ const SubDevice = require('./subdevice');
7
+ const LightChannel = require('./light-channel');
8
+
9
+ /**
10
+ * Single-channel light switch.
11
+ */
12
+ module.exports = class CtrlNeutral1 extends WallSwitch.with(SubDevice, Children) {
13
+
14
+ static get type() {
15
+ return 'miio:power-switch';
16
+ }
17
+
18
+ constructor(parent, info) {
19
+ super(parent, info);
20
+
21
+ this.miioModel = 'lumi.ctrl_neutral1';
22
+
23
+ this.defineProperty('channel_0', {
24
+ name: 'powerChannel0',
25
+ mapper: v => v === 'on'
26
+ });
27
+
28
+ this.addChild(new LightChannel(this, 0));
29
+ }
30
+
31
+ changePowerChannel(channel, power) {
32
+ return this.call('toggle_ctrl_neutral', [ 'neutral_' + channel, power ? 'on' : 'off' ]);
33
+ }
34
+
35
+ propertyUpdated(key, value) {
36
+ super.propertyUpdated(key, value);
37
+
38
+ switch(key) {
39
+ case 'powerChannel0':
40
+ this.child('0').updatePower(value);
41
+ break;
42
+ }
43
+ }
44
+ };
@@ -0,0 +1,53 @@
1
+ 'use strict';
2
+
3
+ const { Children } = require('abstract-things');
4
+ const { WallSwitch } = require('abstract-things/electrical');
5
+
6
+ const SubDevice = require('./subdevice');
7
+ const LightChannel = require('./light-channel');
8
+
9
+ /**
10
+ * Dual-channel light switch.
11
+ */
12
+ module.exports = class CtrlNeutral2 extends WallSwitch.with(SubDevice, Children) {
13
+
14
+ static get type() {
15
+ return 'miio:power-switch';
16
+ }
17
+
18
+ constructor(parent, info) {
19
+ super(parent, info);
20
+
21
+ this.miioModel = 'lumi.ctrl_neutral2';
22
+
23
+ this.defineProperty('channel_0', {
24
+ name: 'powerChannel0',
25
+ mapper: v => v === 'on'
26
+ });
27
+ this.defineProperty('channel_1', {
28
+ name: 'powerChannel1',
29
+ mapper: v => v === 'on'
30
+ });
31
+
32
+ this.addChild(new LightChannel(this, 0));
33
+ this.addChild(new LightChannel(this, 1));
34
+ }
35
+
36
+ changePowerChannel(channel, power) {
37
+ return this.call('toggle_ctrl_neutral', [ 'neutral_' + channel, power ? 'on' : 'off' ]);
38
+ }
39
+
40
+ propertyUpdated(key, value) {
41
+ super.propertyUpdated(key, value);
42
+
43
+ switch(key) {
44
+ case 'powerChannel0':
45
+ this.child('0').updatePower(value);
46
+ break;
47
+ case 'powerChannel1':
48
+ this.child('1').updatePower(value);
49
+ break;
50
+ }
51
+ }
52
+ };
53
+
@@ -0,0 +1,55 @@
1
+ 'use strict';
2
+
3
+ const SubDevice = require('./subdevice');
4
+ const { Controller, Actions } = require('abstract-things/controllers');
5
+
6
+ /**
7
+ * Cube device, emits `action` with an object when someone interacts with
8
+ * the cube.
9
+ *
10
+ * Supports the actions:
11
+ * * `alert` - when the cube has been idle for a while and it wakes up
12
+ * * `flip90` - when the cube is flipped 90 degrees in any direction
13
+ * * `flip180` - when the cube is flipped 180 degress in any direction
14
+ * * `move` - when the cube is moved on its current surface (slide)
15
+ * * `shake_air` - when the cube is shaken in the air
16
+ * * `free_fall` - when the cube is dropped in a free fall
17
+ * * `swing` - when the cube is held in hand and the fake thrown
18
+ */
19
+ module.exports = class Cube extends SubDevice.with(Controller, Actions) {
20
+ constructor(parent, info) {
21
+ super(parent, info);
22
+
23
+ this.miioModel = 'lumi.cube';
24
+
25
+ this.updateActions([
26
+ 'alert',
27
+ 'flip90',
28
+ 'flip180',
29
+ 'move',
30
+ 'tap_twice',
31
+ 'shake_air',
32
+ 'free_fall',
33
+ 'rotate'
34
+ ]);
35
+ }
36
+
37
+ _report(data) {
38
+ super._report(data);
39
+
40
+ if(typeof data.status !== 'undefined') {
41
+ this.debug('Action performed:', data.status);
42
+ this.emitAction(data.status);
43
+ }
44
+
45
+ if(typeof data.rotate !== 'undefined') {
46
+ const r = data.rotate;
47
+ const idx = r.indexOf(',');
48
+ const amount = parseInt(r.substring(0, idx));
49
+ this.debug('Action performed:', 'rotate', amount);
50
+ this.emitAction('rotate', {
51
+ amount: amount
52
+ });
53
+ }
54
+ }
55
+ };
@@ -0,0 +1,120 @@
1
+ 'use strict';
2
+
3
+ const EventEmitter = require('events');
4
+ const dgram = require('dgram');
5
+ const os = require('os');
6
+
7
+ const MULTICAST_ADDRESS = '224.0.0.50';
8
+ const MULTICAST_PORT = 4321;
9
+
10
+ const SERVER_PORT = 9898;
11
+
12
+ /**
13
+ * Local Developer API for the gateway. Used to read data from the gateway
14
+ * and connected devices.
15
+ *
16
+ * TODO: Retries for discovery
17
+ */
18
+ module.exports = class DeveloperApi extends EventEmitter {
19
+ constructor(parent, address) {
20
+ super();
21
+
22
+ this.address = address;
23
+ this.port = SERVER_PORT;
24
+
25
+ // Bind a custom function instead of using directly to skip resolving debug before id is set
26
+ this.debug = function() { parent.debug.apply(parent, arguments); };
27
+
28
+ this.socket = dgram.createSocket({
29
+ type: 'udp4',
30
+ reuseAddr: true
31
+ });
32
+
33
+ this.socket.on('message', this._onMessage.bind(this));
34
+ this.socket.on('listening', () => {
35
+ // Add membership to the multicast addresss for all network interfaces
36
+ const interfaces = os.networkInterfaces();
37
+ for(const name of Object.keys(interfaces)) {
38
+ const addresses = interfaces[name];
39
+
40
+ for(const addr of addresses) {
41
+ if(addr.family === 'IPv4') {
42
+ this.socket.addMembership(MULTICAST_ADDRESS, addr.address);
43
+ }
44
+ }
45
+ }
46
+
47
+ // Broadcast a whois to find all gateways
48
+ const json = JSON.stringify({
49
+ cmd: 'whois'
50
+ });
51
+ this.debug('DEV BROADCAST ->', json);
52
+ this.socket.send(json, 0, json.length, MULTICAST_PORT, MULTICAST_ADDRESS);
53
+
54
+ // Give us one second to discover the gateway
55
+ setTimeout(() => {
56
+ this.debug('DEV <- Timeout for whois');
57
+ this.emit('ready');
58
+ }, 1000);
59
+ });
60
+ this.socket.bind({
61
+ port: SERVER_PORT,
62
+ exclusive: true
63
+ });
64
+ }
65
+
66
+ destroy() {
67
+ this.socket.close();
68
+ }
69
+
70
+ send(data) {
71
+ const json = JSON.stringify(data);
72
+ this.debug('DEV ->', json);
73
+
74
+ this.socket.send(json, 0, json.length, this.port, this.address);
75
+ }
76
+
77
+ read(sid) {
78
+ this.send({
79
+ cmd: 'read',
80
+ sid: sid
81
+ });
82
+ }
83
+
84
+ _onMessage(msg) {
85
+ let data;
86
+ try {
87
+ this.debug('DEV <-', msg.toString());
88
+ data = JSON.parse(msg.toString());
89
+ } catch(ex) {
90
+ this.emit('error', ex);
91
+ return;
92
+ }
93
+
94
+ switch(data.cmd) {
95
+ case 'iam':
96
+ if(data.ip === this.address) {
97
+ this.port = data.port;
98
+ this.sid = data.sid;
99
+
100
+ this.emit('ready');
101
+ }
102
+ break;
103
+ case 'read_ack':
104
+ case 'heartbeat':
105
+ case 'report': {
106
+ if(! this.sid && data.model === 'gateway') {
107
+ this.sid = data.sid;
108
+ }
109
+
110
+ const parsed = JSON.parse(data.data);
111
+ this.emit('propertiesChanged', {
112
+ id: this.sid === data.sid ? '0' : data.sid,
113
+ data: parsed
114
+ });
115
+
116
+ this.emit('properties:' + data.sid, parsed);
117
+ }
118
+ }
119
+ }
120
+ };
@@ -0,0 +1,32 @@
1
+ 'use strict';
2
+
3
+ const { Light, SwitchablePower } = require('abstract-things/lights');
4
+
5
+ /**
6
+ * Representation of a power channel.
7
+ */
8
+ module.exports = class LightChannel extends Light.with(SwitchablePower) {
9
+ static get types() {
10
+ return [ 'miio:subdevice', 'miio:power-channel', 'miio:light' ];
11
+ }
12
+
13
+ constructor(parent, channel) {
14
+ super();
15
+
16
+ this.id = parent.id + ':' + channel;
17
+
18
+ this.parent = parent;
19
+ this.channel = channel;
20
+
21
+ this.updateName();
22
+ }
23
+
24
+ updateName() {
25
+ this.metadata.name = this.parent.name + ' - ' + this.channel;
26
+ }
27
+
28
+ changePower(power) {
29
+ return this.parent.changePowerChannel(this.channel, power);
30
+ }
31
+
32
+ };