@george.talusan/eufy-robovac-js 0.0.13 → 0.0.15

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.
package/lib/RoboVac.js CHANGED
@@ -1,7 +1,7 @@
1
- const debug = require('debug');
2
- const api = require('tuyapi');
3
- const { EventEmitter } = require('events');
4
- const { Buffer } = require('buffer');
1
+ import debug from 'debug';
2
+ import TuyAPI from 'tuyapi';
3
+ import { EventEmitter } from 'node:events';
4
+ import { Buffer } from 'node:buffer';
5
5
 
6
6
  const COMMAND = {
7
7
  playPause: '2',
@@ -65,27 +65,29 @@ const ERRORS = {
65
65
  Stuck_5_min: 'device trapped',
66
66
  Fan_stuck: 'fan stuck',
67
67
  S_brush_stuck: 'side brush stuck',
68
+ no_error: 'no error',
68
69
  };
69
70
 
70
71
  class RoboVac extends EventEmitter
71
72
  {
72
73
  dps = {};
73
74
 
74
- constructor({ ip, deviceId, localKey }) {
75
+ constructor({ ip, deviceId, localKey, version = '3.3' }) {
75
76
  super();
76
77
  this.ip = ip;
77
78
  this.deviceId = deviceId;
78
79
  this.localKey = localKey;
80
+ this.version = version;
79
81
  this.dps = {};
80
82
  this.connected = false;
81
83
  }
82
84
 
83
85
  async initialize() {
84
- this.device = new api({
86
+ this.device = new TuyAPI({
85
87
  id: this.deviceId,
86
88
  key: this.localKey,
87
89
  ip: this.ip,
88
- version: '3.3',
90
+ version: this.version,
89
91
  issueRefreshOnConnect: true,
90
92
  });
91
93
 
@@ -542,4 +544,4 @@ class RoboVac extends EventEmitter
542
544
  }
543
545
  }
544
546
 
545
- module.exports = { RoboVac };
547
+ export { RoboVac };
@@ -0,0 +1,745 @@
1
+ import { describe, it, before, mock } from 'node:test';
2
+ import { deepStrictEqual, strictEqual, throws, rejects, ok } from 'node:assert/strict';
3
+ import { Buffer } from 'node:buffer';
4
+ import { RoboVac } from './RoboVac.js';
5
+
6
+ function b64(obj) {
7
+ return Buffer.from(JSON.stringify(obj)).toString('base64');
8
+ }
9
+
10
+ function makeVac(overrides = {}) {
11
+ return new RoboVac({
12
+ ip: '192.168.1.100',
13
+ deviceId: 'device-1',
14
+ localKey: 'key-1',
15
+ ...overrides,
16
+ });
17
+ }
18
+
19
+ describe('RoboVac', () => {
20
+ describe('constructor', () => {
21
+ it('sets properties from options', () => {
22
+ const vac = makeVac();
23
+ strictEqual(vac.ip, '192.168.1.100');
24
+ strictEqual(vac.deviceId, 'device-1');
25
+ strictEqual(vac.localKey, 'key-1');
26
+ deepStrictEqual(vac.dps, {});
27
+ strictEqual(vac.connected, false);
28
+ });
29
+ });
30
+
31
+ describe('getters with missing dps keys', () => {
32
+ it('workMode throws when key is missing', () => {
33
+ throws(() => makeVac().workMode(), { message: 'RoboVac does not support this command' });
34
+ });
35
+
36
+ it('activity throws when key is missing', () => {
37
+ throws(() => makeVac().activity(), { message: 'RoboVac does not support this command' });
38
+ });
39
+
40
+ it('batteryLevel throws when key is missing', () => {
41
+ throws(() => makeVac().batteryLevel(), { message: 'RoboVac does not support this command' });
42
+ });
43
+
44
+ it('runtime throws when key is missing', () => {
45
+ throws(() => makeVac().runtime(), { message: 'RoboVac does not support this command' });
46
+ });
47
+
48
+ it('coverage throws when key is missing', () => {
49
+ throws(() => makeVac().coverage(), { message: 'RoboVac does not support this command' });
50
+ });
51
+
52
+ it('consumables throws when key is missing', () => {
53
+ throws(() => makeVac().consumables(), { message: 'RoboVac does not support this command' });
54
+ });
55
+
56
+ it('status throws when key is missing', () => {
57
+ throws(() => makeVac().status(), { message: 'RoboVac does not support this command' });
58
+ });
59
+
60
+ it('volume throws when key is missing', () => {
61
+ throws(() => makeVac().volume(), { message: 'RoboVac does not support this command' });
62
+ });
63
+
64
+ it('boostIQ throws when key is missing', () => {
65
+ throws(() => makeVac().boostIQ(), { message: 'RoboVac does not support this command' });
66
+ });
67
+
68
+ it('autoReturn throws when key is missing', () => {
69
+ throws(() => makeVac().autoReturn(), { message: 'RoboVac does not support this command' });
70
+ });
71
+
72
+ it('multimaps throws when key is missing', () => {
73
+ throws(() => makeVac().multimaps(), { message: 'RoboVac does not support this command' });
74
+ });
75
+
76
+ it('rooms throws when key is missing', () => {
77
+ throws(() => makeVac().rooms(), { message: 'RoboVac does not support this command' });
78
+ });
79
+
80
+ it('voice throws when key is missing', () => {
81
+ throws(() => makeVac().voice(), { message: 'RoboVac does not support this command' });
82
+ });
83
+
84
+ it('hello throws when key is missing', () => {
85
+ throws(() => makeVac().hello(), { message: 'RoboVac does not support this command' });
86
+ });
87
+
88
+ it('error returns "no error" when dps is null', () => {
89
+ const vac = makeVac();
90
+ vac.dps = null;
91
+ strictEqual(vac.error(), 'no error');
92
+ });
93
+ });
94
+
95
+ describe('getters (with dps populated)', () => {
96
+ it('workMode reads from dps', () => {
97
+ const vac = makeVac();
98
+ vac.dps = { '5': 'auto' };
99
+ strictEqual(vac.workMode(), 'auto');
100
+ });
101
+
102
+ it('activity reads from dps', () => {
103
+ const vac = makeVac();
104
+ vac.dps = { '15': 'Cleaning' };
105
+ strictEqual(vac.activity(), 'Cleaning');
106
+ });
107
+
108
+ it('batteryLevel reads from dps', () => {
109
+ const vac = makeVac();
110
+ vac.dps = { '104': 85 };
111
+ strictEqual(vac.batteryLevel(), 85);
112
+ });
113
+
114
+ it('runtime reads from dps', () => {
115
+ const vac = makeVac();
116
+ vac.dps = { '109': 42 };
117
+ strictEqual(vac.runtime(), 42);
118
+ });
119
+
120
+ it('coverage reads from dps', () => {
121
+ const vac = makeVac();
122
+ vac.dps = { '110': 30 };
123
+ strictEqual(vac.coverage(), 30);
124
+ });
125
+
126
+ it('volume reads from dps', () => {
127
+ const vac = makeVac();
128
+ vac.dps = { '111': 50 };
129
+ strictEqual(vac.volume(), 50);
130
+ });
131
+
132
+ it('boostIQ reads from dps', () => {
133
+ const vac = makeVac();
134
+ vac.dps = { '118': true };
135
+ strictEqual(vac.boostIQ(), true);
136
+ });
137
+
138
+ it('autoReturn reads from dps', () => {
139
+ const vac = makeVac();
140
+ vac.dps = { '135': true };
141
+ strictEqual(vac.autoReturn(), true);
142
+ });
143
+ });
144
+
145
+ describe('error()', () => {
146
+ it('returns human-readable string for known numeric codes', () => {
147
+ const vac = makeVac();
148
+ vac.dps = { '106': 1 };
149
+ strictEqual(vac.error(), 'front bumper stuck');
150
+ });
151
+
152
+ it('returns human-readable string for known string codes', () => {
153
+ const vac = makeVac();
154
+ vac.dps = { '106': 'Wheel_stuck' };
155
+ strictEqual(vac.error(), 'wheel stuck');
156
+ });
157
+
158
+ it('returns raw value for unknown codes', () => {
159
+ const vac = makeVac();
160
+ vac.dps = { '106': 999 };
161
+ strictEqual(vac.error(), 999);
162
+ });
163
+
164
+ it('returns "no error" for code 0', () => {
165
+ const vac = makeVac();
166
+ vac.dps = { '106': 0 };
167
+ strictEqual(vac.error(), 'no error');
168
+ });
169
+
170
+ it('resolves S-prefix error codes', () => {
171
+ const vac = makeVac();
172
+ const cases = {
173
+ S1: 'battery',
174
+ S2: 'wheel module',
175
+ S3: 'side brush',
176
+ S4: 'suction fan',
177
+ S5: 'rolling brush',
178
+ S8: 'path tracking sensor',
179
+ };
180
+ for (const [code, expected] of Object.entries(cases)) {
181
+ vac.dps = { '106': code };
182
+ strictEqual(vac.error(), expected, code);
183
+ }
184
+ });
185
+
186
+ it('returns undefined when error key is missing', () => {
187
+ const vac = makeVac();
188
+ vac.dps = { '5': 'auto' };
189
+ strictEqual(vac.error(), undefined);
190
+ });
191
+ });
192
+
193
+ describe('base64 decoders', () => {
194
+ it('consumables decodes base64 JSON', () => {
195
+ const vac = makeVac();
196
+ const data = { consumable: { duration: { SB: 100, RB: 200 } } };
197
+ vac.dps = { '116': b64(data) };
198
+ deepStrictEqual(vac.consumables(), data);
199
+ });
200
+
201
+ it('status decodes base64 JSON', () => {
202
+ const vac = makeVac();
203
+ const data = { bat: 85, mode: 'auto' };
204
+ vac.dps = { '142': b64(data) };
205
+ deepStrictEqual(vac.status(), data);
206
+ });
207
+
208
+ it('multimaps decodes base64 JSON', () => {
209
+ const vac = makeVac();
210
+ const data = { maps: [{ id: 1, name: 'Floor 1' }] };
211
+ vac.dps = { '117': b64(data) };
212
+ deepStrictEqual(vac.multimaps(), data);
213
+ });
214
+
215
+ it('rooms decodes base64 JSON', () => {
216
+ const vac = makeVac();
217
+ const data = { rooms: [{ id: 1, name: 'Kitchen' }] };
218
+ vac.dps = { '124': b64(data) };
219
+ deepStrictEqual(vac.rooms(), data);
220
+ });
221
+
222
+ it('voice decodes base64 JSON', () => {
223
+ const vac = makeVac();
224
+ const data = { lang: 'en', enabled: true };
225
+ vac.dps = { '125': b64(data) };
226
+ deepStrictEqual(vac.voice(), data);
227
+ });
228
+
229
+ it('hello decodes base64 JSON', () => {
230
+ const vac = makeVac();
231
+ const data = { message: 'hi' };
232
+ vac.dps = { '126': b64(data) };
233
+ deepStrictEqual(vac.hello(), data);
234
+ });
235
+ });
236
+
237
+ describe('docked()', () => {
238
+ it('returns true when Sleeping', () => {
239
+ const vac = makeVac();
240
+ vac.dps = { '15': 'Sleeping' };
241
+ strictEqual(vac.docked(), true);
242
+ });
243
+
244
+ it('returns true when Charging', () => {
245
+ const vac = makeVac();
246
+ vac.dps = { '15': 'Charging' };
247
+ strictEqual(vac.docked(), true);
248
+ });
249
+
250
+ it('returns true when completed', () => {
251
+ const vac = makeVac();
252
+ vac.dps = { '15': 'completed' };
253
+ strictEqual(vac.docked(), true);
254
+ });
255
+
256
+ it('returns false when Cleaning', () => {
257
+ const vac = makeVac();
258
+ vac.dps = { '15': 'Cleaning' };
259
+ strictEqual(vac.docked(), false);
260
+ });
261
+ });
262
+
263
+ describe('goingHome()', () => {
264
+ it('returns true when activity is Recharge', () => {
265
+ const vac = makeVac();
266
+ vac.dps = { '15': 'Recharge', '101': false };
267
+ strictEqual(vac.goingHome(), true);
268
+ });
269
+
270
+ it('returns goHome dps value when not Recharge', () => {
271
+ const vac = makeVac();
272
+ vac.dps = { '15': 'Cleaning', '101': true };
273
+ strictEqual(vac.goingHome(), true);
274
+ });
275
+
276
+ it('returns false when goHome is false and not Recharge', () => {
277
+ const vac = makeVac();
278
+ vac.dps = { '15': 'Cleaning', '101': false };
279
+ strictEqual(vac.goingHome(), false);
280
+ });
281
+
282
+ it('throws when activity key is missing', () => {
283
+ throws(() => makeVac().goingHome(), { message: 'RoboVac does not support this command' });
284
+ });
285
+ });
286
+
287
+ describe('commands (via set)', () => {
288
+ let vac;
289
+
290
+ before(() => {
291
+ vac = makeVac();
292
+ vac.connected = true;
293
+ vac.device = { set: mock.fn((opts) => Promise.resolve(opts)) };
294
+ vac.dps = {
295
+ '2': false,
296
+ '5': 'auto',
297
+ '101': false,
298
+ '102': 'Standard',
299
+ '103': false,
300
+ '111': 50,
301
+ '118': true,
302
+ '135': true,
303
+ '15': 'Cleaning',
304
+ };
305
+ });
306
+
307
+ it('clean() sets workMode to auto', async () => {
308
+ await vac.clean();
309
+ const call = vac.device.set.mock.calls.at(-1);
310
+ deepStrictEqual(call.arguments[0], { multiple: true, data: { '5': 'auto' } });
311
+ });
312
+
313
+ it('pause() sets playPause to false', async () => {
314
+ await vac.pause();
315
+ const call = vac.device.set.mock.calls.at(-1);
316
+ deepStrictEqual(call.arguments[0], { multiple: true, data: { '2': false } });
317
+ });
318
+
319
+ it('resume() sets playPause to true', async () => {
320
+ await vac.resume();
321
+ const call = vac.device.set.mock.calls.at(-1);
322
+ deepStrictEqual(call.arguments[0], { multiple: true, data: { '2': true } });
323
+ });
324
+
325
+ it('cleanRooms() sends base64-encoded room selection', async () => {
326
+ await vac.cleanRooms([1, 2, 3]);
327
+ const call = vac.device.set.mock.calls.at(-1);
328
+ const payload = call.arguments[0];
329
+ strictEqual(payload.multiple, true);
330
+ const decoded = JSON.parse(Buffer.from(payload.data['124'], 'base64').toString());
331
+ strictEqual(decoded.method, 'selectRoomsClean');
332
+ deepStrictEqual(decoded.data.roomIds, [1, 2, 3]);
333
+ strictEqual(decoded.data.cleanTimes, 1);
334
+ ok(typeof decoded.timestamp === 'number');
335
+ });
336
+
337
+ it('cleanRooms() defaults to room 1 when no rooms provided', async () => {
338
+ await vac.cleanRooms();
339
+ const call = vac.device.set.mock.calls.at(-1);
340
+ const decoded = JSON.parse(Buffer.from(call.arguments[0].data['124'], 'base64').toString());
341
+ deepStrictEqual(decoded.data.roomIds, [1]);
342
+ });
343
+
344
+ it('setCleanSpeedQuiet() sets correct speed', async () => {
345
+ await vac.setCleanSpeedQuiet();
346
+ const call = vac.device.set.mock.calls.at(-1);
347
+ deepStrictEqual(call.arguments[0].data, { '102': 'Quiet' });
348
+ });
349
+
350
+ it('setCleanSpeedStandard() sets correct speed', async () => {
351
+ await vac.setCleanSpeedStandard();
352
+ const call = vac.device.set.mock.calls.at(-1);
353
+ deepStrictEqual(call.arguments[0].data, { '102': 'Standard' });
354
+ });
355
+
356
+ it('setCleanSpeedTurbo() sets correct speed', async () => {
357
+ await vac.setCleanSpeedTurbo();
358
+ const call = vac.device.set.mock.calls.at(-1);
359
+ deepStrictEqual(call.arguments[0].data, { '102': 'Turbo' });
360
+ });
361
+
362
+ it('setCleanSpeedMax() sets correct speed', async () => {
363
+ await vac.setCleanSpeedMax();
364
+ const call = vac.device.set.mock.calls.at(-1);
365
+ deepStrictEqual(call.arguments[0].data, { '102': 'Max' });
366
+ });
367
+
368
+ it('setBoostIQ() sends when value differs', async () => {
369
+ await vac.setBoostIQ(false);
370
+ const call = vac.device.set.mock.calls.at(-1);
371
+ deepStrictEqual(call.arguments[0].data, { '118': false });
372
+ });
373
+
374
+ it('setBoostIQ() skips when value is same', async () => {
375
+ const prevCalls = vac.device.set.mock.calls.length;
376
+ await vac.setBoostIQ(true);
377
+ strictEqual(vac.device.set.mock.calls.length, prevCalls);
378
+ });
379
+
380
+ it('setAutoReturn() sends command', async () => {
381
+ await vac.setAutoReturn(false);
382
+ const call = vac.device.set.mock.calls.at(-1);
383
+ deepStrictEqual(call.arguments[0].data, { '135': false });
384
+ });
385
+ });
386
+
387
+ describe('locate()', () => {
388
+ it('sends findMyRobot when flag differs', async () => {
389
+ const vac = makeVac();
390
+ vac.connected = true;
391
+ vac.device = { set: mock.fn(() => Promise.resolve()) };
392
+ vac.dps = { '103': false, '15': 'Cleaning' };
393
+ await vac.locate(true);
394
+ const call = vac.device.set.mock.calls.at(-1);
395
+ deepStrictEqual(call.arguments[0].data, { '103': true });
396
+ });
397
+
398
+ it('skips when flag is same', async () => {
399
+ const vac = makeVac();
400
+ vac.connected = true;
401
+ vac.device = { set: mock.fn(() => Promise.resolve()) };
402
+ vac.dps = { '103': true, '15': 'Cleaning' };
403
+ await vac.locate(true);
404
+ strictEqual(vac.device.set.mock.calls.length, 0);
405
+ });
406
+
407
+ it('throws when docked', async () => {
408
+ const vac = makeVac();
409
+ vac.dps = { '103': false, '15': 'Sleeping' };
410
+ await rejects(() => vac.locate(true), { message: 'RoboVac is on the charging base' });
411
+ });
412
+
413
+ it('throws when findMyRobot is unsupported', async () => {
414
+ const vac = makeVac();
415
+ vac.dps = { '15': 'Cleaning' };
416
+ await rejects(() => vac.locate(true), { message: 'RoboVac does not support this command' });
417
+ });
418
+ });
419
+
420
+ describe('goHome()', () => {
421
+ it('sends goHome when flag differs', async () => {
422
+ const vac = makeVac();
423
+ vac.connected = true;
424
+ vac.device = { set: mock.fn(() => Promise.resolve()) };
425
+ vac.dps = { '101': false, '15': 'Cleaning' };
426
+ await vac.goHome(true);
427
+ const call = vac.device.set.mock.calls.at(-1);
428
+ deepStrictEqual(call.arguments[0].data, { '101': true });
429
+ });
430
+
431
+ it('skips when flag is same', async () => {
432
+ const vac = makeVac();
433
+ vac.connected = true;
434
+ vac.device = { set: mock.fn(() => Promise.resolve()) };
435
+ vac.dps = { '101': false, '15': 'Cleaning' };
436
+ await vac.goHome(false);
437
+ strictEqual(vac.device.set.mock.calls.length, 0);
438
+ });
439
+
440
+ it('throws when already docked', async () => {
441
+ const vac = makeVac();
442
+ vac.dps = { '101': false, '15': 'Sleeping' };
443
+ await rejects(() => vac.goHome(true), { message: 'RoboVac is already home' });
444
+ });
445
+
446
+ it('throws when goHome is unsupported', async () => {
447
+ const vac = makeVac();
448
+ vac.dps = { '15': 'Cleaning' };
449
+ await rejects(() => vac.goHome(true), { message: 'RoboVac does not support this command' });
450
+ });
451
+ });
452
+
453
+ describe('setVolume()', () => {
454
+ it('sends volume command for valid input', async () => {
455
+ const vac = makeVac();
456
+ vac.connected = true;
457
+ vac.device = { set: mock.fn(() => Promise.resolve()) };
458
+ vac.dps = { '111': 50 };
459
+ await vac.setVolume(75);
460
+ const call = vac.device.set.mock.calls.at(-1);
461
+ deepStrictEqual(call.arguments[0].data, { '111': 75 });
462
+ });
463
+
464
+ it('throws for value < 0', async () => {
465
+ const vac = makeVac();
466
+ vac.dps = { '111': 50 };
467
+ await rejects(() => vac.setVolume(-1), { message: 'expecting value between 0 to 100' });
468
+ });
469
+
470
+ it('throws for value > 100', async () => {
471
+ const vac = makeVac();
472
+ vac.dps = { '111': 50 };
473
+ await rejects(() => vac.setVolume(101), { message: 'expecting value between 0 to 100' });
474
+ });
475
+
476
+ it('throws when volume is unsupported', async () => {
477
+ const vac = makeVac();
478
+ vac.dps = { '5': 'auto' };
479
+ await rejects(() => vac.setVolume(50), { message: 'RoboVac does not support this command' });
480
+ });
481
+ });
482
+
483
+ describe('set() and get()', () => {
484
+ it('set throws when disconnected', async () => {
485
+ await rejects(() => makeVac().set({}), { message: 'RoboVac is disconnected' });
486
+ });
487
+
488
+ it('get throws when disconnected', async () => {
489
+ await rejects(() => makeVac().get({}), { message: 'RoboVac is disconnected' });
490
+ });
491
+ });
492
+
493
+ describe('connect() and disconnect()', () => {
494
+ it('connect calls device.find and device.connect', async () => {
495
+ const vac = makeVac();
496
+ vac.device = {
497
+ find: mock.fn(() => Promise.resolve()),
498
+ connect: mock.fn(() => Promise.resolve()),
499
+ };
500
+ await vac.connect();
501
+ strictEqual(vac.device.find.mock.calls.length, 1);
502
+ strictEqual(vac.device.connect.mock.calls.length, 1);
503
+ });
504
+
505
+ it('disconnect calls device.disconnect', async () => {
506
+ const vac = makeVac();
507
+ vac.device = { disconnect: mock.fn(() => Promise.resolve()) };
508
+ await vac.disconnect();
509
+ strictEqual(vac.device.disconnect.mock.calls.length, 1);
510
+ });
511
+ });
512
+
513
+ describe('refresh()', () => {
514
+ it('calls get with schema:true', async () => {
515
+ const vac = makeVac();
516
+ vac.connected = true;
517
+ vac.device = { get: mock.fn(() => Promise.resolve({})) };
518
+ await vac.refresh();
519
+ strictEqual(vac.device.get.mock.calls.length, 1);
520
+ deepStrictEqual(vac.device.get.mock.calls[0].arguments[0], { schema: true });
521
+ });
522
+ });
523
+
524
+ describe('commands with dps null', () => {
525
+ it('clean returns unknown when dps is null', async () => {
526
+ const vac = makeVac();
527
+ vac.dps = null;
528
+ vac.connected = true;
529
+ vac.device = { set: mock.fn(() => Promise.resolve()) };
530
+ strictEqual(await vac.clean(), 'unknown');
531
+ });
532
+
533
+ it('resume returns unknown when dps is null', async () => {
534
+ const vac = makeVac();
535
+ vac.dps = null;
536
+ vac.connected = true;
537
+ vac.device = { set: mock.fn(() => Promise.resolve()) };
538
+ strictEqual(await vac.resume(), 'unknown');
539
+ });
540
+
541
+ it('pause returns unknown when dps is null', async () => {
542
+ const vac = makeVac();
543
+ vac.dps = null;
544
+ vac.connected = true;
545
+ vac.device = { set: mock.fn(() => Promise.resolve()) };
546
+ strictEqual(await vac.pause(), 'unknown');
547
+ });
548
+
549
+ it('cleanRooms returns undefined when dps is null', async () => {
550
+ const vac = makeVac();
551
+ vac.dps = null;
552
+ vac.connected = true;
553
+ vac.device = { set: mock.fn(() => Promise.resolve()) };
554
+ strictEqual(await vac.cleanRooms(), undefined);
555
+ });
556
+ });
557
+
558
+ describe('dp-refresh event handling', () => {
559
+ // Simulates the event handler registration that initialize() would do.
560
+ // We bypass initialize() to avoid new TuyAPI() with fake credentials.
561
+ function wireHandlers(vac) {
562
+ vac.device.on('connected', () => {
563
+ vac.connected = true;
564
+ vac.emit('tuya.connected');
565
+ });
566
+ vac.device.on('disconnected', () => {
567
+ vac.connected = false;
568
+ vac.emit('tuya.disconnected');
569
+ });
570
+ vac.device.on('error', (error) => {
571
+ vac.emit('tuya.error', error);
572
+ });
573
+ vac.device.on('data', (data) => {
574
+ vac.dps = { ...vac.dps, ...data.dps };
575
+ vac.emit('tuya.data', data);
576
+ });
577
+ vac.device.on('dp-refresh', (data) => {
578
+ vac.dps = { ...vac.dps, ...data.dps };
579
+ vac.emit('tuya.dp-refresh', data);
580
+ Object.keys(data.dps).forEach((command) => {
581
+ const value = data.dps[command];
582
+ if (command === '2') {
583
+ vac.emit('event', { command: 'playPause', value });
584
+ } else if (command === '116') {
585
+ const consumables = vac.consumables();
586
+ vac.emit('event', { command: 'consumables', value: consumables });
587
+ if (consumables.consumable?.duration?.SB >= 250) {
588
+ vac.emit('alert', { consumable: 'side_brush', duration: consumables.consumable?.duration?.SB });
589
+ }
590
+ if (consumables.consumable?.duration?.RB >= 450) {
591
+ vac.emit('alert', { consumable: 'rolling_brush', duration: consumables.consumable?.duration?.RB });
592
+ }
593
+ if (consumables.consumable?.duration?.FM >= 200) {
594
+ vac.emit('alert', { consumable: 'filter', duration: consumables.consumable?.duration?.FM });
595
+ }
596
+ if (consumables.consumable?.duration?.SS >= 35) {
597
+ vac.emit('alert', { consumable: 'sensors', duration: consumables.consumable?.duration?.SS });
598
+ }
599
+ if (consumables.consumable?.duration?.BatteryStatus !== 1) {
600
+ vac.emit('alert', { consumable: 'battery', duration: consumables.consumable?.duration?.BatteryStatus });
601
+ }
602
+ } else if (command === '106') {
603
+ vac.emit('error', vac.error());
604
+ }
605
+ });
606
+ });
607
+ }
608
+
609
+ it('emits event for playPause via dp-refresh', () => {
610
+ const vac = makeVac();
611
+ vac.dps = {};
612
+ const events = [];
613
+ vac.on('event', (e) => events.push(e));
614
+ vac.device = {
615
+ on: mock.fn((_event, handler) => {
616
+ handler({ dps: { '2': true } });
617
+ return vac.device;
618
+ }),
619
+ };
620
+ wireHandlers(vac);
621
+ strictEqual(events.length, 1);
622
+ deepStrictEqual(events[0], { command: 'playPause', value: true });
623
+ });
624
+
625
+ it('emits alerts when consumable thresholds exceeded', () => {
626
+ const vac = makeVac();
627
+ vac.dps = {};
628
+ const alerts = [];
629
+ vac.on('alert', (a) => alerts.push(a));
630
+ const consumableData = b64({
631
+ consumable: {
632
+ duration: { SB: 300, RB: 500, FM: 250, SS: 40, BatteryStatus: 0 },
633
+ },
634
+ });
635
+ vac.device = {
636
+ on: mock.fn((_event, handler) => {
637
+ handler({ dps: { '116': consumableData } });
638
+ return vac.device;
639
+ }),
640
+ };
641
+ wireHandlers(vac);
642
+ const names = alerts.map((a) => a.consumable);
643
+ ok(names.includes('side_brush'));
644
+ ok(names.includes('rolling_brush'));
645
+ ok(names.includes('filter'));
646
+ ok(names.includes('sensors'));
647
+ ok(names.includes('battery'));
648
+ });
649
+
650
+ it('emits no alerts when consumables below threshold', () => {
651
+ const vac = makeVac();
652
+ vac.dps = {};
653
+ const alerts = [];
654
+ vac.on('alert', (a) => alerts.push(a));
655
+ const consumableData = b64({
656
+ consumable: {
657
+ duration: { SB: 100, RB: 200, FM: 100, SS: 20, BatteryStatus: 1 },
658
+ },
659
+ });
660
+ vac.device = {
661
+ on: mock.fn((_event, handler) => {
662
+ handler({ dps: { '116': consumableData } });
663
+ return vac.device;
664
+ }),
665
+ };
666
+ wireHandlers(vac);
667
+ strictEqual(alerts.length, 0);
668
+ });
669
+
670
+ it('emits tuya.connected on device connect', () => {
671
+ const vac = makeVac();
672
+ let emitted = false;
673
+ vac.on('tuya.connected', () => { emitted = true; });
674
+ vac.device = {
675
+ on: mock.fn((event, handler) => {
676
+ if (event === 'connected') handler();
677
+ return vac.device;
678
+ }),
679
+ };
680
+ wireHandlers(vac);
681
+ strictEqual(emitted, true);
682
+ strictEqual(vac.connected, true);
683
+ });
684
+
685
+ it('emits tuya.disconnected on device disconnect', () => {
686
+ const vac = makeVac();
687
+ vac.connected = true;
688
+ let emitted = false;
689
+ vac.on('tuya.disconnected', () => { emitted = true; });
690
+ vac.device = {
691
+ on: mock.fn((event, handler) => {
692
+ if (event === 'disconnected') handler();
693
+ return vac.device;
694
+ }),
695
+ };
696
+ wireHandlers(vac);
697
+ strictEqual(emitted, true);
698
+ strictEqual(vac.connected, false);
699
+ });
700
+
701
+ it('emits tuya.error on device error', () => {
702
+ const vac = makeVac();
703
+ let lastError;
704
+ vac.on('tuya.error', (e) => { lastError = e; });
705
+ vac.device = {
706
+ on: mock.fn((event, handler) => {
707
+ if (event === 'error') handler(new Error('boom'));
708
+ return vac.device;
709
+ }),
710
+ };
711
+ wireHandlers(vac);
712
+ ok(lastError instanceof Error);
713
+ strictEqual(lastError.message, 'boom');
714
+ });
715
+
716
+ it('merges dps on data event', () => {
717
+ const vac = makeVac();
718
+ vac.dps = { '5': 'auto' };
719
+ vac.device = {
720
+ on: mock.fn((event, handler) => {
721
+ if (event === 'data') handler({ dps: { '15': 'Cleaning' } });
722
+ return vac.device;
723
+ }),
724
+ };
725
+ wireHandlers(vac);
726
+ deepStrictEqual(vac.dps, { '5': 'auto', '15': 'Cleaning' });
727
+ });
728
+
729
+ it('emits error event when error dps received', () => {
730
+ const vac = makeVac();
731
+ const errors = [];
732
+ vac.on('error', (e) => errors.push(e));
733
+ vac.dps = {};
734
+ vac.device = {
735
+ on: mock.fn((_event, handler) => {
736
+ handler({ dps: { '106': 2 } });
737
+ return vac.device;
738
+ }),
739
+ };
740
+ wireHandlers(vac);
741
+ strictEqual(errors.length, 1);
742
+ strictEqual(errors[0], 'wheel stuck');
743
+ });
744
+ });
745
+ });
package/package.json CHANGED
@@ -1,11 +1,21 @@
1
1
  {
2
2
  "name": "@george.talusan/eufy-robovac-js",
3
- "version": "0.0.13",
4
- "main": "./lib/RoboVac.js",
3
+ "version": "0.0.15",
4
+ "type": "module",
5
+ "exports": {
6
+ ".": "./lib/RoboVac.js"
7
+ },
8
+ "files": [
9
+ "lib",
10
+ "README.md"
11
+ ],
12
+ "engines": {
13
+ "node": ">=22"
14
+ },
5
15
  "scripts": {
6
16
  "lint": "eslint lib",
7
17
  "prepack": "npm run lint",
8
- "test": "echo \"Error: no test specified\" && exit 1"
18
+ "test": "node --test lib/*.test.js"
9
19
  },
10
20
  "author": "",
11
21
  "license": "ISC",
@@ -18,8 +28,8 @@
18
28
  },
19
29
  "description": "",
20
30
  "devDependencies": {
21
- "@eslint/js": "^9.9.1",
22
- "eslint": "^9.9.1",
23
- "globals": "^15.9.0"
31
+ "@eslint/js": "^10.0.1",
32
+ "eslint": "^10.3.0",
33
+ "globals": "^17.5.0"
24
34
  }
25
35
  }
@@ -1,12 +0,0 @@
1
- version: 2
2
- updates:
3
- - package-ecosystem: "npm"
4
- directory: "/"
5
- schedule:
6
- interval: "weekly"
7
- open-pull-requests-limit: 10
8
-
9
- - package-ecosystem: "github-actions"
10
- directory: "/"
11
- schedule:
12
- interval: "weekly"
@@ -1,33 +0,0 @@
1
- # This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
2
- # For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages
3
-
4
- name: publish eufy-robovac-js
5
-
6
- on:
7
- release:
8
- types: [created]
9
-
10
- jobs:
11
- build:
12
- runs-on: ubuntu-latest
13
- steps:
14
- - uses: actions/checkout@v4
15
- - uses: actions/setup-node@v4
16
- with:
17
- node-version: 20
18
- - run: npm ci
19
- - run: npm test
20
-
21
- publish-npm:
22
- needs: build
23
- runs-on: ubuntu-latest
24
- steps:
25
- - uses: actions/checkout@v4
26
- - uses: actions/setup-node@v4
27
- with:
28
- node-version: 20
29
- registry-url: https://registry.npmjs.org/
30
- - run: npm ci
31
- - run: npm publish
32
- env:
33
- NODE_AUTH_TOKEN: ${{secrets.npm_token}}
package/eslint.config.mjs DELETED
@@ -1,8 +0,0 @@
1
- import globals from "globals";
2
- import pluginJs from "@eslint/js";
3
-
4
- export default [
5
- {files: ["**/*.js"], languageOptions: {sourceType: "commonjs"}},
6
- {languageOptions: { globals: globals.browser }},
7
- pluginJs.configs.recommended,
8
- ];