node-miio 0.3.0-beta.1 → 0.3.0-beta.4

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 (253) hide show
  1. package/AGENTS.md +115 -0
  2. package/README.md +41 -2
  3. package/dist/cli/commands/configure.js +60 -0
  4. package/dist/cli/commands/configure.js.map +1 -0
  5. package/dist/cli/commands/control.js +47 -0
  6. package/dist/cli/commands/control.js.map +1 -0
  7. package/dist/cli/commands/discover.js +32 -0
  8. package/dist/cli/commands/discover.js.map +1 -0
  9. package/dist/cli/commands/inspect.js +183 -0
  10. package/dist/cli/commands/inspect.js.map +1 -0
  11. package/dist/cli/commands/miot-spec.js +60 -0
  12. package/dist/cli/commands/miot-spec.js.map +1 -0
  13. package/dist/cli/commands/protocol/call.js +59 -0
  14. package/dist/cli/commands/protocol/call.js.map +1 -0
  15. package/dist/cli/commands/protocol/json-dump.js +56 -0
  16. package/dist/cli/commands/protocol/json-dump.js.map +1 -0
  17. package/dist/cli/commands/protocol/packet.js +30 -0
  18. package/dist/cli/commands/protocol/packet.js.map +1 -0
  19. package/dist/cli/commands/protocol.js +49 -0
  20. package/dist/cli/commands/protocol.js.map +1 -0
  21. package/dist/cli/commands/tokens/update.js +53 -0
  22. package/dist/cli/commands/tokens/update.js.map +1 -0
  23. package/dist/cli/commands/tokens.js +44 -0
  24. package/dist/cli/commands/tokens.js.map +1 -0
  25. package/dist/cli/device-finder.js +72 -0
  26. package/dist/cli/device-finder.js.map +1 -0
  27. package/dist/cli/index.js +64 -0
  28. package/dist/cli/index.js.map +1 -0
  29. package/dist/cli/log.js +69 -0
  30. package/dist/cli/log.js.map +1 -0
  31. package/dist/lib/check_result.js +18 -0
  32. package/dist/lib/check_result.js.map +1 -0
  33. package/dist/lib/connect_to_device.js +123 -0
  34. package/dist/lib/connect_to_device.js.map +1 -0
  35. package/dist/lib/device.js +259 -0
  36. package/dist/lib/device.js.map +1 -0
  37. package/dist/lib/device_info.js +297 -0
  38. package/dist/lib/device_info.js.map +1 -0
  39. package/dist/lib/devices/air-monitor.js +42 -0
  40. package/dist/lib/devices/air-monitor.js.map +1 -0
  41. package/dist/lib/devices/air-purifier.js +144 -0
  42. package/dist/lib/devices/air-purifier.js.map +1 -0
  43. package/dist/lib/devices/capabilities/battery-level.js +13 -0
  44. package/dist/lib/devices/capabilities/battery-level.js.map +1 -0
  45. package/dist/lib/devices/capabilities/buzzer.js +69 -0
  46. package/dist/lib/devices/capabilities/buzzer.js.map +1 -0
  47. package/dist/lib/devices/capabilities/changeable-led-brightness.js +44 -0
  48. package/dist/lib/devices/capabilities/changeable-led-brightness.js.map +1 -0
  49. package/dist/lib/devices/capabilities/colorable.js +14 -0
  50. package/dist/lib/devices/capabilities/colorable.js.map +1 -0
  51. package/dist/lib/devices/capabilities/dimmable.js +14 -0
  52. package/dist/lib/devices/capabilities/dimmable.js.map +1 -0
  53. package/dist/lib/devices/capabilities/mode.js +13 -0
  54. package/dist/lib/devices/capabilities/mode.js.map +1 -0
  55. package/dist/lib/devices/capabilities/power.js +13 -0
  56. package/dist/lib/devices/capabilities/power.js.map +1 -0
  57. package/dist/lib/devices/capabilities/sensor.js +46 -0
  58. package/dist/lib/devices/capabilities/sensor.js.map +1 -0
  59. package/dist/lib/devices/capabilities/switchable-led.js +48 -0
  60. package/dist/lib/devices/capabilities/switchable-led.js.map +1 -0
  61. package/dist/lib/devices/chuangmi.plug.v1.js +47 -0
  62. package/dist/lib/devices/chuangmi.plug.v1.js.map +1 -0
  63. package/dist/lib/devices/eyecare-lamp2.js +151 -0
  64. package/dist/lib/devices/eyecare-lamp2.js.map +1 -0
  65. package/dist/lib/devices/gateway/86sw1.js +20 -0
  66. package/dist/lib/devices/gateway/86sw1.js.map +1 -0
  67. package/dist/lib/devices/gateway/86sw2.js +36 -0
  68. package/dist/lib/devices/gateway/86sw2.js.map +1 -0
  69. package/dist/lib/devices/gateway/ctrl_ln1.js +37 -0
  70. package/dist/lib/devices/gateway/ctrl_ln1.js.map +1 -0
  71. package/dist/lib/devices/gateway/ctrl_ln2.js +45 -0
  72. package/dist/lib/devices/gateway/ctrl_ln2.js.map +1 -0
  73. package/dist/lib/devices/gateway/ctrl_neutral1.js +37 -0
  74. package/dist/lib/devices/gateway/ctrl_neutral1.js.map +1 -0
  75. package/dist/lib/devices/gateway/ctrl_neutral2.js +45 -0
  76. package/dist/lib/devices/gateway/ctrl_neutral2.js.map +1 -0
  77. package/dist/lib/devices/gateway/cube.js +49 -0
  78. package/dist/lib/devices/gateway/cube.js.map +1 -0
  79. package/dist/lib/devices/gateway/developer-api.js +104 -0
  80. package/dist/lib/devices/gateway/developer-api.js.map +1 -0
  81. package/dist/lib/devices/gateway/light-channel.js +24 -0
  82. package/dist/lib/devices/gateway/light-channel.js.map +1 -0
  83. package/dist/lib/devices/gateway/light-mixin.js +59 -0
  84. package/dist/lib/devices/gateway/light-mixin.js.map +1 -0
  85. package/dist/lib/devices/gateway/magnet.js +23 -0
  86. package/dist/lib/devices/gateway/magnet.js.map +1 -0
  87. package/dist/lib/devices/gateway/magnet2.js +9 -0
  88. package/dist/lib/devices/gateway/magnet2.js.map +1 -0
  89. package/dist/lib/devices/gateway/motion.js +21 -0
  90. package/dist/lib/devices/gateway/motion.js.map +1 -0
  91. package/dist/lib/devices/gateway/motion2.js +25 -0
  92. package/dist/lib/devices/gateway/motion2.js.map +1 -0
  93. package/dist/lib/devices/gateway/plug.js +34 -0
  94. package/dist/lib/devices/gateway/plug.js.map +1 -0
  95. package/dist/lib/devices/gateway/sensor_ht.js +13 -0
  96. package/dist/lib/devices/gateway/sensor_ht.js.map +1 -0
  97. package/dist/lib/devices/gateway/subdevice.js +203 -0
  98. package/dist/lib/devices/gateway/subdevice.js.map +1 -0
  99. package/dist/lib/devices/gateway/subdevices.js +33 -0
  100. package/dist/lib/devices/gateway/subdevices.js.map +1 -0
  101. package/dist/lib/devices/gateway/switch.js +24 -0
  102. package/dist/lib/devices/gateway/switch.js.map +1 -0
  103. package/dist/lib/devices/gateway/switch2.js +19 -0
  104. package/dist/lib/devices/gateway/switch2.js.map +1 -0
  105. package/dist/lib/devices/gateway/voltage.js +21 -0
  106. package/dist/lib/devices/gateway/voltage.js.map +1 -0
  107. package/dist/lib/devices/gateway/weather.js +16 -0
  108. package/dist/lib/devices/gateway/weather.js.map +1 -0
  109. package/dist/lib/devices/gateway.js +212 -0
  110. package/dist/lib/devices/gateway.js.map +1 -0
  111. package/dist/lib/devices/humidifier.js +141 -0
  112. package/dist/lib/devices/humidifier.js.map +1 -0
  113. package/dist/lib/devices/philips-light-bulb.js +63 -0
  114. package/dist/lib/devices/philips-light-bulb.js.map +1 -0
  115. package/dist/lib/devices/power-plug.js +22 -0
  116. package/dist/lib/devices/power-plug.js.map +1 -0
  117. package/dist/lib/devices/power-strip.js +27 -0
  118. package/dist/lib/devices/power-strip.js.map +1 -0
  119. package/dist/lib/devices/vacuums/dreamevacuum.js +312 -0
  120. package/dist/lib/devices/vacuums/dreamevacuum.js.map +1 -0
  121. package/dist/lib/devices/vacuums/mijiavacuum.js +21 -0
  122. package/dist/lib/devices/vacuums/mijiavacuum.js.map +1 -0
  123. package/dist/lib/devices/vacuums/miot-vacuum.js +395 -0
  124. package/dist/lib/devices/vacuums/miot-vacuum.js.map +1 -0
  125. package/dist/lib/devices/vacuums/vacuum.js +669 -0
  126. package/dist/lib/devices/vacuums/vacuum.js.map +1 -0
  127. package/dist/lib/devices/vacuums/vacuum_modes.js +171 -0
  128. package/dist/lib/devices/vacuums/vacuum_modes.js.map +1 -0
  129. package/dist/lib/devices/vacuums/viomivacuum.js +222 -0
  130. package/dist/lib/devices/vacuums/viomivacuum.js.map +1 -0
  131. package/dist/lib/devices/yeelight.color.js +5 -0
  132. package/dist/lib/devices/yeelight.color.js.map +1 -0
  133. package/dist/lib/devices/yeelight.js +242 -0
  134. package/dist/lib/devices/yeelight.js.map +1 -0
  135. package/dist/lib/devices/yeelight.mono.js +5 -0
  136. package/dist/lib/devices/yeelight.mono.js.map +1 -0
  137. package/dist/lib/discovery.js +147 -0
  138. package/dist/lib/discovery.js.map +1 -0
  139. package/dist/lib/index.js +89 -0
  140. package/dist/lib/index.js.map +1 -0
  141. package/dist/lib/info_from_hostname.js +27 -0
  142. package/dist/lib/info_from_hostname.js.map +1 -0
  143. package/dist/lib/management.js +101 -0
  144. package/dist/lib/management.js.map +1 -0
  145. package/dist/lib/miio_error.js +23 -0
  146. package/dist/lib/miio_error.js.map +1 -0
  147. package/dist/lib/miot/miot_api.js +73 -0
  148. package/dist/lib/miot/miot_api.js.map +1 -0
  149. package/dist/lib/miot/miot_spec.js +105 -0
  150. package/dist/lib/miot/miot_spec.js.map +1 -0
  151. package/dist/lib/miot/spec_client.js +290 -0
  152. package/dist/lib/miot/spec_client.js.map +1 -0
  153. package/dist/lib/models.js +69 -0
  154. package/dist/lib/models.js.map +1 -0
  155. package/dist/lib/network.js +194 -0
  156. package/dist/lib/network.js.map +1 -0
  157. package/dist/lib/packet.js +177 -0
  158. package/dist/lib/packet.js.map +1 -0
  159. package/dist/lib/placeholder.js +16 -0
  160. package/dist/lib/placeholder.js.map +1 -0
  161. package/dist/lib/protocol.js +3 -0
  162. package/dist/lib/protocol.js.map +1 -0
  163. package/dist/lib/safeish_json.js +22 -0
  164. package/dist/lib/safeish_json.js.map +1 -0
  165. package/dist/lib/tokens.js +30 -0
  166. package/dist/lib/tokens.js.map +1 -0
  167. package/docs/devices/vacuum.md +53 -3
  168. package/example.js +1 -1
  169. package/package.json +49 -16
  170. package/snapshots/vacuum-specs.json.gz +0 -0
  171. package/.editorconfig +0 -26
  172. package/.eslintrc +0 -32
  173. package/.github/release.yml +0 -20
  174. package/.github/workflows/build.yml +0 -50
  175. package/.github/workflows/codecov.yml +0 -35
  176. package/.github/workflows/codeql.yml +0 -24
  177. package/.github/workflows/publish-dev-from-main.yml +0 -92
  178. package/.github/workflows/publish-release-to-npm.yml +0 -45
  179. package/cli/commands/configure.js +0 -69
  180. package/cli/commands/control.js +0 -54
  181. package/cli/commands/discover.js +0 -36
  182. package/cli/commands/inspect.js +0 -197
  183. package/cli/commands/protocol/call.js +0 -66
  184. package/cli/commands/protocol/json-dump.js +0 -52
  185. package/cli/commands/protocol/packet.js +0 -29
  186. package/cli/commands/protocol.js +0 -8
  187. package/cli/commands/tokens/update.js +0 -58
  188. package/cli/commands/tokens.js +0 -8
  189. package/cli/device-finder.js +0 -69
  190. package/cli/index.js +0 -9
  191. package/cli/log.js +0 -89
  192. package/lib/checkResult.js +0 -13
  193. package/lib/connectToDevice.js +0 -66
  194. package/lib/device.js +0 -290
  195. package/lib/device_info.js +0 -329
  196. package/lib/device_info.test.js +0 -337
  197. package/lib/devices/air-monitor.js +0 -53
  198. package/lib/devices/air-purifier.js +0 -162
  199. package/lib/devices/capabilities/battery-level.js +0 -16
  200. package/lib/devices/capabilities/buzzer.js +0 -78
  201. package/lib/devices/capabilities/changeable-led-brightness.js +0 -48
  202. package/lib/devices/capabilities/colorable.js +0 -14
  203. package/lib/devices/capabilities/dimmable.js +0 -14
  204. package/lib/devices/capabilities/mode.js +0 -13
  205. package/lib/devices/capabilities/power.js +0 -14
  206. package/lib/devices/capabilities/sensor.js +0 -50
  207. package/lib/devices/capabilities/switchable-led.js +0 -49
  208. package/lib/devices/chuangmi.plug.v1.js +0 -61
  209. package/lib/devices/eyecare-lamp2.js +0 -179
  210. package/lib/devices/gateway/86sw1.js +0 -28
  211. package/lib/devices/gateway/86sw2.js +0 -44
  212. package/lib/devices/gateway/ctrl_ln1.js +0 -44
  213. package/lib/devices/gateway/ctrl_ln2.js +0 -53
  214. package/lib/devices/gateway/ctrl_neutral1.js +0 -44
  215. package/lib/devices/gateway/ctrl_neutral2.js +0 -53
  216. package/lib/devices/gateway/cube.js +0 -55
  217. package/lib/devices/gateway/developer-api.js +0 -120
  218. package/lib/devices/gateway/light-channel.js +0 -32
  219. package/lib/devices/gateway/light-mixin.js +0 -71
  220. package/lib/devices/gateway/magnet.js +0 -29
  221. package/lib/devices/gateway/magnet2.js +0 -13
  222. package/lib/devices/gateway/motion.js +0 -27
  223. package/lib/devices/gateway/motion2.js +0 -31
  224. package/lib/devices/gateway/plug.js +0 -45
  225. package/lib/devices/gateway/sensor_ht.js +0 -17
  226. package/lib/devices/gateway/subdevice.js +0 -227
  227. package/lib/devices/gateway/subdevices.js +0 -37
  228. package/lib/devices/gateway/switch.js +0 -29
  229. package/lib/devices/gateway/switch2.js +0 -27
  230. package/lib/devices/gateway/voltage.js +0 -28
  231. package/lib/devices/gateway/weather.js +0 -22
  232. package/lib/devices/gateway.js +0 -244
  233. package/lib/devices/humidifier.js +0 -162
  234. package/lib/devices/philips-light-bulb.js +0 -73
  235. package/lib/devices/power-plug.js +0 -26
  236. package/lib/devices/power-strip.js +0 -38
  237. package/lib/devices/vacuums/dreamevacuum.js +0 -349
  238. package/lib/devices/vacuums/mijiavacuum.js +0 -22
  239. package/lib/devices/vacuums/vacuum.js +0 -518
  240. package/lib/devices/vacuums/viomivacuum.js +0 -250
  241. package/lib/devices/yeelight.color.js +0 -6
  242. package/lib/devices/yeelight.js +0 -286
  243. package/lib/devices/yeelight.mono.js +0 -9
  244. package/lib/discovery.js +0 -192
  245. package/lib/index.js +0 -40
  246. package/lib/infoFromHostname.js +0 -24
  247. package/lib/management.js +0 -102
  248. package/lib/models.js +0 -76
  249. package/lib/network.js +0 -234
  250. package/lib/packet.js +0 -191
  251. package/lib/placeholder.js +0 -12
  252. package/lib/safeishJSON.js +0 -14
  253. package/lib/tokens.js +0 -129
@@ -1,337 +0,0 @@
1
- 'use strict';
2
-
3
- const TOKEN = 'e381c97af00985577e8e6a3d94732bbe';
4
-
5
- const loggerMock = jest.fn().mockImplementation((a, b) => {
6
- // Uncomment when debugging tests
7
- // console.debug(a, b);
8
- });
9
- const debugMock = jest.fn().mockImplementation((label) => {
10
- return loggerMock;
11
- });
12
-
13
- jest.doMock('debug', () => debugMock);
14
-
15
- function createParentMock() {
16
- return {
17
- socket: {
18
- send: jest
19
- .fn()
20
- .mockImplementation((data, size, length, port, address, cb) => {
21
- cb();
22
- }),
23
- },
24
- };
25
- }
26
-
27
- const DeviceInfo = require('./device_info');
28
- const Packet = require('./packet');
29
-
30
- describe('DeviceInfo', () => {
31
- jest.useFakeTimers();
32
-
33
- describe('constructor', () => {
34
- test('creates a new DeviceInfo without id', () => {
35
- new DeviceInfo({}, null, 'localhost', 1234);
36
- expect(debugMock).toHaveBeenCalledWith('thing:miio:pending');
37
- });
38
- test('creates a new DeviceInfo with id', () => {
39
- new DeviceInfo({}, 'MY-ID', 'localhost', 1234);
40
- expect(debugMock).toHaveBeenCalledWith('thing:miio:MY-ID');
41
- });
42
- });
43
-
44
- describe('token', () => {
45
- const device = new DeviceInfo({}, 'MY-ID', 'localhost', 1234);
46
- test('initial token does not exist', () => {
47
- expect(device.token).toBeNull();
48
- });
49
- test('token stored', () => {
50
- expect(device.tokenChanged).toBe(false);
51
- device.token = Buffer.from('my-token');
52
- expect(device.tokenChanged).toBe(true);
53
- expect(device.token.toString('utf8')).toBe('my-token');
54
- });
55
- });
56
-
57
- describe('enrich', () => {
58
- test('fails if the id is not populated yet', async () => {
59
- const device = new DeviceInfo({}, null, 'localhost', 1234);
60
- await expect(device.enrich()).rejects.toThrow(
61
- 'Device has no identifier yet, handshake needed'
62
- );
63
- });
64
-
65
- test('returns undefined because everything is properly initialised already', async () => {
66
- const device = new DeviceInfo({}, 'MY-ID', 'localhost', 1234);
67
- device.token = TOKEN;
68
- device.tokenChanged = false;
69
- device.model = {};
70
- await expect(device.enrich()).resolves.toBeUndefined();
71
- });
72
-
73
- test('happy path', async () => {
74
- const model = { test: 1 };
75
- const device = new DeviceInfo({}, 'MY-ID', 'localhost', 1234);
76
- device.token = TOKEN;
77
- device.call = jest.fn().mockImplementation(async () => ({ model }));
78
- await expect(device.enrich()).resolves.toBeUndefined();
79
- expect(device.tokenChanged).toBe(false);
80
- expect(device.model).toBe(model);
81
- expect(device.enriched).toBe(true);
82
- expect(device.enrichPromise).toBeNull();
83
- });
84
-
85
- test('errors with \'missing-token\'', async () => {
86
- const device = new DeviceInfo({}, 'MY-ID', 'localhost', 1234);
87
- device.token = TOKEN;
88
- device.enrichPromise = Promise.reject({ code: 'missing-token' });
89
- await expect(device.enrich()).rejects.toStrictEqual({
90
- code: 'missing-token',
91
- device,
92
- });
93
- expect(device.tokenChanged).toBe(true);
94
- expect(device.enriched).toBe(true);
95
- expect(device.enrichPromise).toBeNull();
96
- });
97
-
98
- test('errors with \'connection-failure\'', async () => {
99
- const device = new DeviceInfo({}, 'MY-ID', 'localhost', 1234);
100
- device.token = TOKEN;
101
- device.tokenChanged = false;
102
- device.enrichPromise = Promise.reject('Something went terribly wrong');
103
- await expect(device.enrich()).rejects.toThrow(
104
- 'Could not connect to device, token might be wrong'
105
- );
106
- expect(device.tokenChanged).toBe(false);
107
- expect(device.enriched).toBe(true);
108
- expect(device.enrichPromise).toBeNull();
109
- });
110
-
111
- test('errors with \'missing-token\' custom', async () => {
112
- const device = new DeviceInfo({}, 'MY-ID', 'localhost', 1234);
113
- device.enrichPromise = Promise.reject('Something went terribly wrong');
114
- await expect(device.enrich()).rejects.toThrow(
115
- 'Could not connect to device, token needs to be specified'
116
- );
117
- expect(device.tokenChanged).toBe(false);
118
- expect(device.enriched).toBe(true);
119
- expect(device.enrichPromise).toBeNull();
120
- });
121
- });
122
-
123
- describe('handshake', () => {
124
- test('happy-path: runs the handshake', async () => {
125
- const packet = new Packet(false);
126
- const parent = createParentMock();
127
- const device = new DeviceInfo(parent, 'MY-ID', 'localhost', 1234);
128
- device.token = TOKEN;
129
- packet.token = TOKEN;
130
- parent.socket.send.mockImplementation(
131
- (data, size, length, port, address, cb) => {
132
- cb();
133
- packet.data = null;
134
- device.onMessage(packet.raw);
135
- }
136
- );
137
- const token = await device.handshake();
138
- expect(token.toString()).toBe(TOKEN);
139
- // Second call is not triggered
140
- const token2 = await device.handshake();
141
- expect(token2.toString()).toBe(TOKEN);
142
- });
143
-
144
- test('fails because \'missing-token\'', async () => {
145
- const packet = new Packet(false);
146
- const parent = createParentMock();
147
- const device = new DeviceInfo(parent, 'MY-ID', 'localhost', 1234);
148
- packet.token = TOKEN;
149
- parent.socket.send.mockImplementation(
150
- (data, size, length, port, address, cb) => {
151
- cb();
152
- packet.data = null;
153
- device.onMessage(packet.raw);
154
- }
155
- );
156
- await expect(device.handshake()).rejects.toThrow(
157
- 'Could not connect to device, token needs to be specified'
158
- );
159
- });
160
-
161
- test('fails because to send the handshake request', async () => {
162
- const packet = new Packet(false);
163
- const parent = createParentMock();
164
- const device = new DeviceInfo(parent, 'MY-ID', 'localhost', 1234);
165
- packet.token = TOKEN;
166
- parent.socket.send.mockImplementation(
167
- (data, size, length, port, address, cb) => {
168
- cb(new Error('Something went terribly wrong'));
169
- }
170
- );
171
- await expect(device.handshake()).rejects.toThrow(
172
- 'Something went terribly wrong'
173
- );
174
- });
175
-
176
- test('timesout after 2 secs', async () => {
177
- const parent = createParentMock();
178
- const device = new DeviceInfo(parent, 'MY-ID', 'localhost', 1234);
179
- device.token = TOKEN;
180
- const promise = device.handshake();
181
- jest.advanceTimersByTime(2 * 1000);
182
- await expect(promise).rejects.toThrow(
183
- 'Could not connect to device, handshake timeout'
184
- );
185
- });
186
- });
187
-
188
- describe('onMessage', () => {
189
- test('swallows error to parse the message', async () => {
190
- const device = new DeviceInfo({}, 'MY-ID', 'localhost', 1234);
191
- expect(device.onMessage('test')).toBeUndefined();
192
- });
193
-
194
- test('handshake message but no handler', async () => {
195
- const device = new DeviceInfo({}, 'MY-ID', 'localhost', 1234);
196
- const packet = new Packet();
197
- device.token = TOKEN;
198
- packet.token = TOKEN;
199
- packet.data = null;
200
- expect(device.onMessage(packet.raw)).toBeUndefined();
201
- });
202
-
203
- test('swallows error because data is not a valid JSON', async () => {
204
- const device = new DeviceInfo({}, 'MY-ID', 'localhost', 1234);
205
- const packet = new Packet();
206
- device.token = TOKEN;
207
- packet.token = TOKEN;
208
- packet.data = Buffer.from('test').fill(0);
209
- expect(device.onMessage(packet.raw)).toBeUndefined();
210
- });
211
-
212
- test('does not call the promise resolvers because there\'s none', async () => {
213
- const device = new DeviceInfo({}, 'MY-ID', 'localhost', 1234);
214
- const packet = new Packet();
215
- device.token = TOKEN;
216
- packet.token = TOKEN;
217
- const response = { id: '1', result: 'something' };
218
- packet.data = Buffer.from(JSON.stringify(response));
219
- expect(device.onMessage(packet.raw)).toBeUndefined();
220
- });
221
-
222
- test('calls the promise resolver when there\'s a result', async () => {
223
- const device = new DeviceInfo({}, 'MY-ID', 'localhost', 1234);
224
- const packet = new Packet();
225
- device.token = TOKEN;
226
- packet.token = TOKEN;
227
- const response = { id: '1', result: 'something' };
228
- const promiseResolvers = { resolve: jest.fn(), reject: jest.fn() };
229
- device.promises.set('1', promiseResolvers);
230
- packet.data = Buffer.from(JSON.stringify(response));
231
- expect(device.onMessage(packet.raw)).toBeUndefined();
232
- expect(promiseResolvers.resolve).toHaveBeenCalledWith('something');
233
- });
234
-
235
- test('calls the promise resolver\'s reject when there\'s an error', async () => {
236
- const device = new DeviceInfo({}, 'MY-ID', 'localhost', 1234);
237
- const packet = new Packet();
238
- device.token = TOKEN;
239
- packet.token = TOKEN;
240
- const response = { id: '1', error: 'something' };
241
- const promiseResolvers = { resolve: jest.fn(), reject: jest.fn() };
242
- device.promises.set('1', promiseResolvers);
243
- packet.data = Buffer.from(JSON.stringify(response));
244
- expect(device.onMessage(packet.raw)).toBeUndefined();
245
- expect(promiseResolvers.reject).toHaveBeenCalledWith('something');
246
- });
247
- });
248
-
249
- describe('call', () => {
250
- test('happy-path: sends a method \'miIO.info\'', async () => {
251
- const packet = new Packet(false);
252
- const parent = createParentMock();
253
- const device = new DeviceInfo(parent, 'MY-ID', 'localhost', 1234);
254
- device.token = TOKEN;
255
- packet.token = TOKEN;
256
- parent.socket.send.mockImplementation(
257
- (data, size, length, port, address, cb) => {
258
- cb();
259
- // Return what you get embedded in `result`
260
- packet.raw = Buffer.from(data);
261
- const str = packet.data;
262
- if (str !== null) {
263
- const json = JSON.parse(str);
264
- packet.data = JSON.stringify(Object.assign({}, json, {result: json}));
265
- }
266
- device.onMessage(packet.raw);
267
- }
268
- );
269
- const response = await device.call('miIO.info');
270
- expect(response).toStrictEqual({
271
- id: device.lastId,
272
- method: 'miIO.info',
273
- params: [],
274
- });
275
- });
276
-
277
- test('timeout error', async () => {
278
- const packet = new Packet(false);
279
- const parent = createParentMock();
280
- const device = new DeviceInfo(parent, 'MY-ID', 'localhost', 1234);
281
- packet.token = TOKEN;
282
- device.token = TOKEN;
283
- device.lastId = 10000;
284
-
285
- parent.socket.send.mockImplementation(
286
- (data, size, length, port, address, cb) => {
287
- cb();
288
- // Reply to handshakes only
289
- packet.raw = data;
290
- const str = packet.data;
291
- if (str === null) {
292
- device.onMessage(packet.raw);
293
- } else {
294
- jest.advanceTimersByTime(2 * 1000);
295
- }
296
- }
297
- );
298
- const promise = device.call('miIO.info', [], { retries: 2 });
299
- await expect(promise).rejects.toThrow('Call to device timed out');
300
- expect(device.lastId).toBe(101);
301
- });
302
-
303
- describe('known errors', () => {
304
- const packet = new Packet(false);
305
- packet.token = TOKEN;
306
-
307
- test.each`
308
- code | message | expected
309
- ${'-5001'} | ${'invalid_arg'} | ${'Invalid argument'}
310
- ${'-5001'} | ${'Test msg'} | ${'Test msg'}
311
- ${'-5005'} | ${'params error'} | ${'Invalid argument'}
312
- ${'-5005'} | ${'Test msg'} | ${'Test msg'}
313
- ${'-10000'} | ${'Test msg'} | ${'Method `miIO.info` is not supported'}
314
- ${'OTHER'} | ${'Test msg'} | ${'Test msg'}
315
- `('$code | $message', async ({ code, message, expected }) => {
316
- const parent = createParentMock();
317
- const device = new DeviceInfo(parent, 'MY-ID', 'localhost', 1234);
318
- device.token = TOKEN;
319
-
320
- parent.socket.send.mockImplementation(
321
- (data, size, length, port, address, cb) => {
322
- cb();
323
- // Return what you get embedded in `result`
324
- packet.raw = data;
325
- const str = packet.data;
326
- if (str !== null) {
327
- const json = JSON.parse(str);
328
- packet.data = JSON.stringify(Object.assign({}, json, { error: { code, message } }));
329
- }
330
- device.onMessage(packet.raw);
331
- }
332
- );
333
- await expect(device.call('miIO.info')).rejects.toThrow(expected);
334
- });
335
- });
336
- });
337
- });
@@ -1,53 +0,0 @@
1
- 'use strict';
2
-
3
- const { ChargingState } = require('abstract-things');
4
- const { AirMonitor } = require('abstract-things/climate');
5
- const MiioApi = require('../device');
6
-
7
- const Power = require('./capabilities/power');
8
- const BatteryLevel = require('./capabilities/battery-level');
9
- const { AQI } = require('./capabilities/sensor');
10
-
11
- module.exports = class extends AirMonitor
12
- .with(MiioApi, Power, AQI, BatteryLevel, ChargingState)
13
- {
14
-
15
- static get type() {
16
- return 'miio:air-monitor';
17
- }
18
-
19
- constructor(options) {
20
- super(options);
21
-
22
- // Define the power property
23
- this.defineProperty('power', v => v === 'on');
24
-
25
- // Sensor value used for AQI (PM2.5) capability
26
- this.defineProperty('aqi');
27
-
28
- this.defineProperty('battery', {
29
- name: 'batteryLevel'
30
- });
31
-
32
- this.defineProperty('usb_state', {
33
- name: 'charging',
34
- mapper: v => v === 'on'
35
- });
36
- }
37
-
38
- propertyUpdated(key, value, oldValue) {
39
- if(key === 'charging') {
40
- this.updateCharging(value);
41
- }
42
-
43
- super.propertyUpdated(key, value, oldValue);
44
- }
45
-
46
- changePower(power) {
47
- return this.call('set_power', [ power ? 'on' : 'off' ], {
48
- refresh: [ 'power', 'mode' ],
49
- refreshDelay: 200
50
- });
51
- }
52
-
53
- };
@@ -1,162 +0,0 @@
1
- 'use strict';
2
-
3
- const { AirPurifier } = require('abstract-things/climate');
4
- const MiioApi = require('../device');
5
-
6
- const Power = require('./capabilities/power');
7
- const Mode = require('./capabilities/mode');
8
- const SwitchableLED = require('./capabilities/switchable-led');
9
- const LEDBrightness = require('./capabilities/changeable-led-brightness');
10
- const Buzzer = require('./capabilities/buzzer');
11
- const { Temperature, Humidity, AQI } = require('./capabilities/sensor');
12
-
13
- /**
14
- * Abstraction over a Mi Air Purifier.
15
- *
16
- * Air Purifiers have a mode that indicates if is on or not. Changing the mode
17
- * to `idle` will power off the device, all other modes will power on the
18
- * device.
19
- */
20
- module.exports = class extends AirPurifier
21
- .with(MiioApi, Power, Mode, Temperature, Humidity, AQI,
22
- SwitchableLED, LEDBrightness, Buzzer)
23
- {
24
-
25
- static get type() {
26
- return 'miio:air-purifier';
27
- }
28
-
29
- constructor(options) {
30
- super(options);
31
-
32
- // Define the power property
33
- this.defineProperty('power', v => v === 'on');
34
-
35
- // Set the mode property and supported modes
36
- this.defineProperty('mode');
37
- this.updateModes([
38
- 'idle',
39
-
40
- 'auto',
41
- 'silent',
42
- 'favorite'
43
- ]);
44
-
45
- // Sensor value for Temperature capability
46
- this.defineProperty('temp_dec', {
47
- name: 'temperature',
48
- mapper: v => v / 10.0
49
- });
50
-
51
- // Sensor value for RelativeHumidity capability
52
- this.defineProperty('humidity');
53
-
54
- // Sensor value used for AQI (PM2.5) capability
55
- this.defineProperty('aqi');
56
-
57
- // The favorite level
58
- this.defineProperty('favorite_level', {
59
- name: 'favoriteLevel'
60
- });
61
-
62
- // Info about usage
63
- this.defineProperty('filter1_life', {
64
- name: 'filterLifeRemaining'
65
- });
66
- this.defineProperty('f1_hour_used', {
67
- name: 'filterHoursUsed'
68
- });
69
- this.defineProperty('use_time', {
70
- name: 'useTime'
71
- });
72
-
73
- // State for SwitchableLED capability
74
- this.defineProperty('led', {
75
- mapper: v => v === 'on'
76
- });
77
-
78
- this.defineProperty('led_b', {
79
- name: 'ledBrightness',
80
- mapper: v => {
81
- switch(v) {
82
- case 0:
83
- return 'bright';
84
- case 1:
85
- return 'dim';
86
- case 2:
87
- return 'off';
88
- default:
89
- return 'unknown';
90
- }
91
- }
92
- });
93
-
94
- // Buzzer and beeping
95
- this.defineProperty('buzzer', {
96
- mapper: v => v === 'on'
97
- });
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
- * Perform a mode change as requested by `mode(string)` or
109
- * `setMode(string)`.
110
- */
111
- changeMode(mode) {
112
- return this.call('set_mode', [ mode ], {
113
- refresh: [ 'power', 'mode' ],
114
- refreshDelay: 200
115
- })
116
- .then(MiioApi.checkOk)
117
- .catch(err => {
118
- throw err.code === -5001 ? new Error('Mode `' + mode + '` not supported') : err;
119
- });
120
- }
121
-
122
- /**
123
- * Get the favorite level used when the mode is `favorite`. Between 0 and 16.
124
- */
125
- favoriteLevel(level=undefined) {
126
- if(typeof level === 'undefined') {
127
- return Promise.resolve(this.property('favoriteLevel'));
128
- }
129
-
130
- return this.setFavoriteLevel(level);
131
- }
132
-
133
- /**
134
- * Set the favorite level used when the mode is `favorite`, should be
135
- * between 0 and 16.
136
- */
137
- setFavoriteLevel(level) {
138
- return this.call('set_level_favorite', [ level ])
139
- .then(() => null);
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
- };
@@ -1,16 +0,0 @@
1
- 'use strict';
2
-
3
- const { Thing, BatteryLevel } = require('abstract-things');
4
-
5
- module.exports = Thing.mixin(
6
- (Parent) =>
7
- class extends Parent.with(BatteryLevel) {
8
- propertyUpdated(key, value) {
9
- if (key === 'batteryLevel') {
10
- this.updateBatteryLevel(value);
11
- }
12
-
13
- super.propertyUpdated(key, value);
14
- }
15
- }
16
- );
@@ -1,78 +0,0 @@
1
- 'use strict';
2
-
3
- const { Thing, State } = require('abstract-things');
4
- const { boolean } = require('abstract-things/values');
5
-
6
- const MiioApi = require('../../device');
7
-
8
- module.exports = Thing.mixin(Parent => class extends Parent.with(State) {
9
- static get capability() {
10
- return 'miio:buzzer';
11
- }
12
-
13
- static availableAPI(builder) {
14
- builder.event('buzzerChanged')
15
- .type('boolean')
16
- .description('Buzzer state has changed')
17
- .done();
18
-
19
- builder.action('buzzer')
20
- .description('Get or set if the buzzer is active')
21
- .argument('boolean', true, 'If the device should beep')
22
- .returns('boolean', 'If the buzzer is on')
23
- .done();
24
-
25
- builder.action('setBuzzer')
26
- .description('Set if the buzzer is active')
27
- .argument('boolean', false, 'If the device should beep')
28
- .returns('boolean', 'If the buzzer is on')
29
- .done();
30
-
31
- builder.action('getBuzzer')
32
- .description('Get if the buzzer is active')
33
- .returns('boolean', 'If the buzzer is on')
34
- .done();
35
- }
36
-
37
- propertyUpdated(key, value) {
38
- if(key === 'buzzer') {
39
- if(this.updateState('buzzer', value)) {
40
- this.emitEvent('buzzerChanged', value);
41
- }
42
- }
43
-
44
- super.propertyUpdated(key, value);
45
- }
46
-
47
- /**
48
- * Get or set if the buzzer is active.
49
- *
50
- * @param {boolean} active
51
- * Optional boolean to switch buzzer to.
52
- */
53
- buzzer(active) {
54
- if(typeof active === 'undefined') {
55
- return this.getBuzzer();
56
- }
57
-
58
- return this.setBuzzer(active);
59
- }
60
-
61
- getBuzzer() {
62
- return this.getState('buzzer');
63
- }
64
-
65
- setBuzzer(active) {
66
- active = boolean(active);
67
-
68
- return this.changeBuzzer(active)
69
- .then(() => this.getBuzzer());
70
- }
71
-
72
- changeBuzzer(active) {
73
- return this.call('set_buzzer', [ active ? 'on' : 'off' ], {
74
- refresh: [ 'buzzer' ]
75
- })
76
- .then(MiioApi.checkOk);
77
- }
78
- });
@@ -1,48 +0,0 @@
1
- 'use strict';
2
-
3
- const { Thing, State } = require('abstract-things');
4
- const { string } = require('abstract-things/values');
5
-
6
- module.exports = Thing.mixin(Parent => class extends Parent.with(State) {
7
- static get capability() {
8
- return 'miio:led-brightness';
9
- }
10
-
11
- static availableAPI(builder) {
12
- builder.action('ledBrightness')
13
- .description('Get or set the LED brightness')
14
- .argument('string', true, 'If provided, set the LED brightness to this value')
15
- .returns('string', 'The LED brightness')
16
- .done();
17
- }
18
-
19
- propertyUpdated(key, value) {
20
- if(key === 'ledBrightness') {
21
- this.updateState('ledBrightness', value);
22
- }
23
-
24
- super.propertyUpdated(key, value);
25
- }
26
-
27
- /**
28
- * Get or set if the LED brightness.
29
- *
30
- * @param {string} brightness The LED brightness
31
- */
32
- ledBrightness(brightness) {
33
- if(typeof brightness === 'undefined') {
34
- return this.getState('ledBrightness');
35
- }
36
-
37
- brightness = string(brightness);
38
- return this.changeLEDBrightness(brightness)
39
- .then(() => this.getState('ledBrightness'));
40
- }
41
-
42
- /**
43
- * Set the LED brightness.
44
- */
45
- changeLEDBrightness(brightness) {
46
- throw new Error('changeLEDBrightness not implemented');
47
- }
48
- });