homebridge-tuya-plus 3.14.0-dev.4 → 3.14.0-dev.41

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 (53) hide show
  1. package/.github/workflows/publish-dev.yml +298 -31
  2. package/AGENTS.md +162 -0
  3. package/CLAUDE.md +1 -0
  4. package/Changelog.md +16 -3
  5. package/Readme.MD +8 -32
  6. package/config.schema.json +63 -78
  7. package/index.js +599 -358
  8. package/lib/AirPurifierAccessory.js +1 -1
  9. package/lib/BaseAccessory.js +54 -17
  10. package/lib/ConvectorAccessory.js +1 -1
  11. package/lib/CustomMultiOutletAccessory.js +2 -1
  12. package/lib/DoorbellAccessory.js +9 -16
  13. package/lib/GarageDoorAccessory.js +1 -1
  14. package/lib/IrrigationSystemAccessory.js +93 -114
  15. package/lib/MultiOutletAccessory.js +3 -2
  16. package/lib/OilDiffuserAccessory.js +10 -8
  17. package/lib/RGBTWLightAccessory.js +13 -11
  18. package/lib/RGBTWOutletAccessory.js +11 -9
  19. package/lib/SimpleBlindsAccessory.js +5 -5
  20. package/lib/SimpleDimmer2Accessory.js +1 -1
  21. package/lib/SimpleDimmerAccessory.js +10 -6
  22. package/lib/SimpleGarageDoorAccessory.js +78 -24
  23. package/lib/SimpleHeaterAccessory.js +4 -4
  24. package/lib/SimpleLightAccessory.js +1 -1
  25. package/lib/SwitchAccessory.js +3 -2
  26. package/lib/TWLightAccessory.js +2 -2
  27. package/lib/TuyaAccessory.js +75 -42
  28. package/lib/TuyaCloudApi.js +115 -7
  29. package/lib/TuyaCloudDevice.js +351 -28
  30. package/lib/TuyaCloudMessaging.js +28 -41
  31. package/lib/TuyaDevice.js +269 -0
  32. package/lib/TuyaDiscovery.js +25 -9
  33. package/lib/ValveAccessory.js +16 -12
  34. package/lib/VerticalBlindsWithTilt.js +27 -25
  35. package/lib/WledDimmerAccessory.js +46 -81
  36. package/package.json +5 -6
  37. package/scripts/cleanup-pr-tags.js +122 -0
  38. package/test/BaseAccessory.test.js +94 -15
  39. package/test/IrrigationSystemAccessory.test.js +122 -68
  40. package/test/MultiOutletAccessory.test.js +18 -3
  41. package/test/RGBTWLightAccessory.test.js +3 -3
  42. package/test/SimpleFanLightAccessory.test.js +3 -3
  43. package/test/SimpleGarageDoorAccessory.test.js +63 -9
  44. package/test/TuyaAccessory.protocol.test.js +76 -3
  45. package/test/TuyaCloudApi.test.js +110 -1
  46. package/test/TuyaCloudDevice.test.js +438 -2
  47. package/test/TuyaCloudMessaging.test.js +24 -5
  48. package/test/TuyaDevice.test.js +266 -0
  49. package/test/getCategory.test.js +1 -0
  50. package/test/index.test.js +271 -0
  51. package/test/support/mocks.js +13 -0
  52. package/wiki/Supported-Device-Types.md +48 -30
  53. package/wiki/Tuya-Cloud-Setup.md +31 -108
@@ -52,6 +52,8 @@ function makeSimpleGarage(initialContext = {}) {
52
52
  instance.stopBeforeCloseMs = 1500;
53
53
  instance.partialStopTimer = null;
54
54
  instance.pendingCloseTimer = null;
55
+ instance.partialPending = false;
56
+ instance.partialGeneration = 0;
55
57
  instance.currentDoorState = CDS.CLOSED;
56
58
  instance.characteristicCurrentDoorState = {
57
59
  value: CDS.CLOSED,
@@ -373,11 +375,13 @@ describe('SimpleGarageDoorAccessory.setTargetDoorState — close (stop-before-cl
373
375
  // Disconnect handling
374
376
  // ---------------------------------------------------------------------------
375
377
  describe('SimpleGarageDoorAccessory — disconnected', () => {
376
- test('Skips writes when the device is disconnected', () => {
378
+ test('Surfaces a No Response error and writes nothing when disconnected', () => {
377
379
  const { instance, device } = makeSimpleGarage();
378
380
  device.connected = false;
379
381
 
380
- instance.setTargetDoorState(TDS.OPEN);
382
+ // A tap on an unreachable gate must fail in HomeKit instead of being
383
+ // silently dropped while HomeKit believes the open/close succeeded.
384
+ expect(() => instance.setTargetDoorState(TDS.OPEN)).toThrow(HAP.HapStatusError);
381
385
 
382
386
  expect(device.update).not.toHaveBeenCalled();
383
387
  });
@@ -407,35 +411,71 @@ describe('SimpleGarageDoorAccessory._handlePartialOpen', () => {
407
411
  beforeEach(() => jest.useFakeTimers());
408
412
  afterEach(() => jest.useRealTimers());
409
413
 
410
- test('Fires open, then fires stop after partialOpenMs', () => {
414
+ test('Stop is anchored to the gate reporting it is moving, not the button press', () => {
411
415
  const { instance, device } = makeSimpleGarage();
412
416
  instance.partialOpenMs = 2000;
417
+ // Gate starts closed, so the open command needs time to reach it and
418
+ // get the gate moving before a stop can do anything.
419
+ device.state['105'] = STATE_CLOSING_OR_CLOSED;
413
420
 
414
421
  instance._handlePartialOpen();
415
422
 
416
- // Open goes out immediately.
423
+ // Open goes out immediately, but the auto-stop is NOT armed yet.
417
424
  expect(device.update).toHaveBeenCalledTimes(1);
418
425
  expect(device.update).toHaveBeenNthCalledWith(1, { '101': true });
419
426
  expect(instance.characteristicTargetDoorState.value).toBe(TDS.OPEN);
427
+ expect(instance.partialStopTimer).toBeNull();
428
+ expect(instance.partialPending).toBe(true);
429
+
430
+ // No amount of waiting fires a stop until the gate reports it's moving —
431
+ // this is what stops a too-early stop from being a dropped no-op.
432
+ jest.advanceTimersByTime(10_000);
433
+ expect(device.update).toHaveBeenCalledTimes(1);
434
+
435
+ // Controller reports it has started opening: now the countdown arms.
436
+ emitState(device, STATE_OPENING_OR_OPEN);
437
+ expect(instance.partialStopTimer).not.toBeNull();
438
+ expect(instance.partialPending).toBe(false);
420
439
 
421
- // Nothing happens until the timer elapses.
422
440
  jest.advanceTimersByTime(2000 - 1);
423
441
  expect(device.update).toHaveBeenCalledTimes(1);
424
442
 
425
- // Then a single stop is fired.
443
+ // The stop fires, and is re-sent a couple of times to survive a dropped
444
+ // write (the controller occasionally drops a lone command).
426
445
  jest.advanceTimersByTime(1);
427
446
  expect(device.update).toHaveBeenCalledTimes(2);
428
447
  expect(device.update).toHaveBeenNthCalledWith(2, { '103': true });
448
+ jest.advanceTimersByTime(300);
449
+ expect(device.update).toHaveBeenNthCalledWith(3, { '103': true });
450
+ jest.advanceTimersByTime(300);
451
+ expect(device.update).toHaveBeenNthCalledWith(4, { '103': true });
429
452
 
430
- // And nothing more after that.
453
+ // And nothing more after the re-sends.
431
454
  jest.advanceTimersByTime(10_000);
432
- expect(device.update).toHaveBeenCalledTimes(2);
455
+ expect(device.update).toHaveBeenCalledTimes(4);
433
456
  expect(instance.partialStopTimer).toBeNull();
434
457
  });
435
458
 
436
- test('Re-entrant press while armed is ignored (idempotent)', () => {
459
+ test('Arms straight away when the gate already reports open/opening', () => {
460
+ const { instance, device } = makeSimpleGarage();
461
+ instance.partialOpenMs = 2000;
462
+ device.state['105'] = STATE_OPENING_OR_OPEN;
463
+
464
+ instance._handlePartialOpen();
465
+
466
+ // Open fired and the countdown armed without waiting for a fresh report.
467
+ expect(device.update).toHaveBeenNthCalledWith(1, { '101': true });
468
+ expect(instance.partialStopTimer).not.toBeNull();
469
+ expect(instance.partialPending).toBe(false);
470
+
471
+ jest.advanceTimersByTime(2000);
472
+ expect(device.update).toHaveBeenNthCalledWith(2, { '103': true });
473
+ });
474
+
475
+ test('Re-entrant press while in progress is ignored (idempotent)', () => {
437
476
  const { instance, device } = makeSimpleGarage();
438
477
  instance.partialOpenMs = 2000;
478
+ device.state['105'] = STATE_OPENING_OR_OPEN; // arms immediately
439
479
 
440
480
  instance._handlePartialOpen();
441
481
  expect(device.update).toHaveBeenCalledTimes(1);
@@ -452,6 +492,20 @@ describe('SimpleGarageDoorAccessory._handlePartialOpen', () => {
452
492
  expect(device.update).toHaveBeenNthCalledWith(2, { '103': true });
453
493
  });
454
494
 
495
+ test('Re-entrant press while waiting for movement is ignored', () => {
496
+ const { instance, device } = makeSimpleGarage();
497
+ instance.partialOpenMs = 2000;
498
+ device.state['105'] = STATE_CLOSING_OR_CLOSED;
499
+
500
+ instance._handlePartialOpen();
501
+ expect(device.update).toHaveBeenCalledTimes(1); // open
502
+ expect(instance.partialPending).toBe(true);
503
+
504
+ // A second press before the gate reports moving must not fire another open.
505
+ instance._handlePartialOpen();
506
+ expect(device.update).toHaveBeenCalledTimes(1);
507
+ });
508
+
455
509
  test('A direct close cancels the partial auto-stop and runs stop-before-close', () => {
456
510
  const { instance, device } = makeSimpleGarage();
457
511
  instance.partialOpenMs = 2000;
@@ -155,6 +155,78 @@ describe('protocol version normalization', () => {
155
155
  });
156
156
  });
157
157
 
158
+ // A wrong-length local key made crypto.createCipheriv throw "Invalid key length"
159
+ // from inside a socket callback (see _send_3_3), which goes unhandled and crashes
160
+ // Homebridge into a restart loop. An out-of-spec key must be refused clearly,
161
+ // up front, without ever reaching the cipher or taking the process down.
162
+ describe('invalid local key handling', () => {
163
+ describe('isValidLocalKey', () => {
164
+ test.each([
165
+ ['0123456789abcdef', true], // 16 ASCII chars
166
+ ['short', false],
167
+ ['0123456789abcdefXX', false], // 18 chars
168
+ ['', false],
169
+ ['012345678901234é', false], // 16 chars but 17 UTF-8 bytes
170
+ [Buffer.alloc(16), true],
171
+ [Buffer.alloc(8), false],
172
+ [null, false],
173
+ [undefined, false],
174
+ [12345678901234567, false], // not a string/Buffer
175
+ ])('isValidLocalKey(%p) === %p', (key, expected) => {
176
+ expect(TuyaAccessory.isValidLocalKey(key)).toBe(expected);
177
+ });
178
+ });
179
+
180
+ test('a wrong-length key is flagged and logged without throwing', () => {
181
+ let device;
182
+ expect(() => { device = makeDevice('3.3', {key: 'too-short'}); }).not.toThrow();
183
+ expect(device._invalidKey).toBe(true);
184
+ expect(device.log.error).toHaveBeenCalledWith(expect.stringContaining('16 characters'));
185
+ const logged = device.log.error.mock.calls.flat().join(' ');
186
+ expect(logged).toContain('Protocol Test Device');
187
+ });
188
+
189
+ test('a valid 16-character key is accepted and logs no error', () => {
190
+ const device = makeDevice('3.3');
191
+ expect(device._invalidKey).toBe(false);
192
+ expect(device.log.error).not.toHaveBeenCalled();
193
+ });
194
+
195
+ test('_connect() opens no socket for an invalid-key device', () => {
196
+ const device = makeDevice('3.5', {key: 'bad'});
197
+ device._connect();
198
+ expect(device._socket).toBeUndefined();
199
+ expect(device.connected).toBe(false);
200
+ });
201
+
202
+ // The exact path that used to crash: a connected device whose update() flows
203
+ // into _send -> _send_3_x -> createCipheriv. It must short-circuit to false.
204
+ test.each(['3.1', '3.3', '3.4', '3.5'])(
205
+ 'update() never reaches the cipher for an invalid-key %s device, even when connected',
206
+ version => {
207
+ const device = makeDevice(version, {key: 'nope'});
208
+ const written = attachSocketStub(device); // also forces connected = true
209
+
210
+ let result;
211
+ expect(() => { result = device.update({1: true}); }).not.toThrow();
212
+ expect(result).toBe(false);
213
+ expect(written).toHaveLength(0);
214
+ }
215
+ );
216
+
217
+ test('a fake device without a key is never treated as invalid', () => {
218
+ const device = new TuyaAccessory({
219
+ id: 'bf1234567890abcdef12',
220
+ name: 'Fake Device',
221
+ fake: true,
222
+ connect: false,
223
+ log: makeLog(),
224
+ });
225
+ expect(device._invalidKey).toBe(false);
226
+ expect(device.log.error).not.toHaveBeenCalled();
227
+ });
228
+ });
229
+
158
230
  describe('message handler routing', () => {
159
231
  const HANDLERS = ['_msgHandler_3_1', '_msgHandler_3_3', '_msgHandler_3_4', '_msgHandler_3_5'];
160
232
 
@@ -607,9 +679,10 @@ describe('graceful disconnect handling', () => {
607
679
  jest.advanceTimersByTime(1000);
608
680
  expect(pingErrors).toHaveLength(0);
609
681
 
610
- // The log reflects a clean disconnect, never a ping failure.
611
- expect(device.log.info).toHaveBeenCalledWith('Disconnected from', device.context.name);
612
- const logged = device.log.info.mock.calls.flat().join(' ');
682
+ // The log reflects a clean disconnect, never a ping failure. (A device
683
+ // recycling its socket is routine, so the disconnect is logged at debug.)
684
+ expect(device.log.debug).toHaveBeenCalledWith('Disconnected from', device.context.name);
685
+ const logged = device.log.debug.mock.calls.flat().join(' ');
613
686
  expect(logged).not.toMatch(/ERR_PING_TIMED_OUT/);
614
687
  });
615
688
 
@@ -138,6 +138,31 @@ describe('TuyaCloudApi — endpoints', () => {
138
138
  await expect(api.getStatus('dev')).resolves.toEqual([{code: 'switch_1', value: true}]);
139
139
  });
140
140
 
141
+ test('getShadowProperties returns the properties array (code + dp_id)', async () => {
142
+ const api = ready();
143
+ let path;
144
+ api._httpsRequest = async (method, p) => {
145
+ path = p;
146
+ return {success: true, result: {properties: [{code: 'switch_led', dp_id: 20, value: true}]}};
147
+ };
148
+ await expect(api.getShadowProperties('dev')).resolves.toEqual([{code: 'switch_led', dp_id: 20, value: true}]);
149
+ expect(path).toBe('/v2.0/cloud/thing/dev/shadow/properties');
150
+ });
151
+
152
+ test('getShadowProperties returns null (never throws) when the shadow API is unavailable', async () => {
153
+ const api = ready();
154
+ api._httpsRequest = async () => ({success: false, code: 1106, msg: 'permission deny'});
155
+ await expect(api.getShadowProperties('dev')).resolves.toBeNull();
156
+ });
157
+
158
+ test('getDeviceInfo returns the device record (with online status)', async () => {
159
+ const api = ready();
160
+ let path;
161
+ api._httpsRequest = async (method, p) => { path = p; return {success: true, result: {id: 'dev', online: false, name: 'X'}}; };
162
+ await expect(api.getDeviceInfo('dev')).resolves.toEqual({id: 'dev', online: false, name: 'X'});
163
+ expect(path).toBe('/v1.0/devices/dev');
164
+ });
165
+
141
166
  test('sendCommands posts the commands and reports success', async () => {
142
167
  const api = ready();
143
168
  let captured;
@@ -148,7 +173,7 @@ describe('TuyaCloudApi — endpoints', () => {
148
173
  const ok = await api.sendCommands('dev', [{code: 'switch_1', value: true}]);
149
174
  expect(ok).toBe(true);
150
175
  expect(captured.method).toBe('POST');
151
- expect(captured.path).toBe('/v1.0/devices/dev/commands');
176
+ expect(captured.path).toBe('/v1.0/iot-03/devices/dev/commands');
152
177
  expect(captured.body).toEqual({commands: [{code: 'switch_1', value: true}]});
153
178
  });
154
179
 
@@ -159,6 +184,35 @@ describe('TuyaCloudApi — endpoints', () => {
159
184
  expect(api._httpsRequest).not.toHaveBeenCalled();
160
185
  });
161
186
 
187
+ test('getStatus reads from the iot-03 device endpoint', async () => {
188
+ const api = ready();
189
+ let path;
190
+ api._httpsRequest = async (method, p) => { path = p; return {success: true, result: []}; };
191
+ await api.getStatus('dev');
192
+ expect(path).toBe('/v1.0/iot-03/devices/dev/status');
193
+ });
194
+
195
+ test('sendProperties issues thing-model properties as a JSON-string body', async () => {
196
+ const api = ready();
197
+ let captured;
198
+ api._httpsRequest = async (method, path, headers, bodyStr) => {
199
+ captured = {method, path, body: JSON.parse(bodyStr)};
200
+ return {success: true, result: true};
201
+ };
202
+ const ok = await api.sendProperties('dev', [{code: 'wfh_close', value: true}]);
203
+ expect(ok).toBe(true);
204
+ expect(captured.method).toBe('POST');
205
+ expect(captured.path).toBe('/v2.0/cloud/thing/dev/shadow/properties/issue');
206
+ expect(captured.body).toEqual({properties: JSON.stringify({wfh_close: true})});
207
+ });
208
+
209
+ test('sendProperties short-circuits on an empty command list', async () => {
210
+ const api = ready();
211
+ api._httpsRequest = jest.fn();
212
+ await expect(api.sendProperties('dev', [])).resolves.toBe(true);
213
+ expect(api._httpsRequest).not.toHaveBeenCalled();
214
+ });
215
+
162
216
  test('getMqttConfig posts the expected body and returns the broker config', async () => {
163
217
  const api = ready();
164
218
  api.uid = 'UID';
@@ -194,3 +248,58 @@ describe('TuyaCloudApi — endpoints', () => {
194
248
  await expect(api.request('GET', '/x')).rejects.toThrow(/no permissions/);
195
249
  });
196
250
  });
251
+
252
+ describe('TuyaCloudApi — HTTP trace (debug.logCloudHttp)', () => {
253
+ const makeLog = () => ({info: () => {}, warn: () => {}, error: () => {}, debug: jest.fn()});
254
+
255
+ test('logHttp is off unless explicitly enabled', () => {
256
+ expect(makeApi().logHttp).toBe(false);
257
+ expect(makeApi({logHttp: true}).logHttp).toBe(true);
258
+ });
259
+
260
+ test('redaction masks credentials but keeps device data', () => {
261
+ const api = makeApi();
262
+ const redacted = api._redactForLog({
263
+ client_id: 'aid1234567890', access_token: 'tok_abcdef123456', sign: 'SIGNATUREVALUE', t: '1700', nonce: 'n-1',
264
+ result: {access_token: 'inner-token-xyz', refresh_token: 'refresh-xyz', uid: 'uid-1234567', online: true},
265
+ commands: [{code: 'switch_1', value: true}]
266
+ });
267
+
268
+ expect(redacted.client_id).not.toBe('aid1234567890');
269
+ expect(redacted.access_token).not.toBe('tok_abcdef123456');
270
+ expect(redacted.sign).not.toBe('SIGNATUREVALUE');
271
+ expect(redacted.result.access_token).not.toBe('inner-token-xyz');
272
+ expect(redacted.result.refresh_token).not.toBe('refresh-xyz');
273
+ expect(redacted.result.uid).not.toBe('uid-1234567');
274
+ // Non-secret fields and the actual device data-points pass through intact.
275
+ expect(redacted.t).toBe('1700');
276
+ expect(redacted.nonce).toBe('n-1');
277
+ expect(redacted.result.online).toBe(true);
278
+ expect(redacted.commands).toEqual([{code: 'switch_1', value: true}]);
279
+ });
280
+
281
+ test('no trace is logged when the switch is off', () => {
282
+ const log = makeLog();
283
+ const api = makeApi({log});
284
+ api._traceHttp('POST', '/v1.0/devices/dev/commands', {sign: 'X'}, '{"commands":[]}', 200, {success: true});
285
+ expect(log.debug).not.toHaveBeenCalled();
286
+ });
287
+
288
+ test('a trace carries the request/response but never the raw token or signature', () => {
289
+ const log = makeLog();
290
+ const api = makeApi({log, logHttp: true});
291
+ const headers = {client_id: 'aid', sign: 'SIGN_SECRET_VALUE', access_token: 'TOKEN_SECRET_VALUE', t: '1', nonce: 'n', 'Content-Type': 'application/json'};
292
+ const body = JSON.stringify({commands: [{code: 'wfh_open', value: true}]});
293
+
294
+ api._traceHttp('POST', '/v1.0/devices/dev/commands', headers, body, 200, {success: false, code: 2008, msg: 'command or value not support'});
295
+
296
+ expect(log.debug).toHaveBeenCalledTimes(1);
297
+ const line = log.debug.mock.calls[0][0];
298
+ expect(line).toContain('POST /v1.0/devices/dev/commands');
299
+ expect(line).toContain('wfh_open'); // the attempted command is visible
300
+ expect(line).toContain('2008'); // and the cloud's verdict
301
+ expect(line).toContain('command or value not support');
302
+ expect(line).not.toContain('TOKEN_SECRET_VALUE'); // …but secrets are not
303
+ expect(line).not.toContain('SIGN_SECRET_VALUE');
304
+ });
305
+ });