homebridge-tuya-plus 3.14.0-dev.42 → 3.14.0-dev.43

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.
@@ -87,6 +87,10 @@ class TuyaCloudApi {
87
87
  this.uid = null; // linked app-account uid (needed for realtime MQTT)
88
88
  this._tokenExpiry = 0; // ms epoch when the token must be refreshed
89
89
  this._tokenPromise = null; // in-flight token request (de-dupes concurrent callers)
90
+
91
+ // Reuse TLS connections across requests instead of a fresh handshake each
92
+ // time — noticeably cuts per-command latency and the startup read burst.
93
+ this._agent = new https.Agent({keepAlive: true, maxSockets: 8});
90
94
  }
91
95
 
92
96
  isConfigured() {
@@ -365,7 +369,8 @@ class TuyaCloudApi {
365
369
  port: url.port || 443,
366
370
  path: url.pathname + url.search,
367
371
  method,
368
- headers
372
+ headers,
373
+ agent: this._agent
369
374
  }, resp => {
370
375
  let data = '';
371
376
  resp.setEncoding('utf8');
@@ -76,6 +76,7 @@ class TuyaCloudDevice extends EventEmitter {
76
76
  this._propertiesTried = new Set();
77
77
  this._announcedProperties = false;
78
78
  this._catchupTimers = null; // post-write state re-reads (see _scheduleStateCatchup)
79
+ this._writeChain = null; // serializes cloud writes to this device (see update)
79
80
 
80
81
  // Bidirectional data-point maps, learned from the device's thing-shadow on
81
82
  // connect. They let this cloud transport (and the LAN+cloud TuyaDevice that
@@ -366,7 +367,16 @@ class TuyaCloudDevice extends EventEmitter {
366
367
  if (undeliverable.length) this._reportUndeliverableWrite(undeliverable);
367
368
  if (!sendable.length) return false;
368
369
 
369
- return this._dispatch(sendable);
370
+ // Serialize writes to this device. HomeKit frequently double-sends a
371
+ // characteristic write, and rapid changes (dragging a thermostat) fire a
372
+ // burst; running them concurrently both races the per-code endpoint
373
+ // learning (a duplicate could see a code "tried" but not yet learned and
374
+ // fail spuriously) and bursts the cloud's QPS limit (uneven latency). One
375
+ // at a time keeps learning coherent and the request rate smooth. _dispatch
376
+ // never rejects, so the chain can't break.
377
+ const run = () => this._dispatch(sendable);
378
+ this._writeChain = this._writeChain ? this._writeChain.then(run, run) : run();
379
+ return this._writeChain;
370
380
  }
371
381
 
372
382
  // Send a write, routing each data-point to the cloud endpoint it accepts. Codes
@@ -418,7 +428,7 @@ class TuyaCloudDevice extends EventEmitter {
418
428
  }
419
429
  if (!this._announcedProperties) {
420
430
  this._announcedProperties = true;
421
- this.log.info(`${this.context.name}: controlling some of this device's data-points over the Tuya thing-model property endpoint (they aren't in its standard instruction set).`);
431
+ this.log.debug(`${this.context.name}: controlling some of this device's data-points over the Tuya thing-model property endpoint (they aren't in its standard instruction set).`);
422
432
  }
423
433
  return this._learn(fresh, 'properties');
424
434
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "homebridge-tuya-plus",
3
- "version": "3.14.0-dev.42",
3
+ "version": "3.14.0-dev.43",
4
4
  "description": "A community-maintained Homebridge plugin for controlling Tuya devices in HomeKit. LAN-first, with an optional Tuya Cloud fallback for any device the LAN can't reach.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -240,6 +240,29 @@ describe('TuyaCloudDevice', () => {
240
240
  expect(api.sendCommands).toHaveBeenCalledWith('dev1', [{code: 'switch_1', value: false}]);
241
241
  expect(api.sendProperties).toHaveBeenCalledWith('dev1', [{code: 'wfh_open', value: true}]);
242
242
  });
243
+
244
+ // HomeKit often double-sends a write; running the two concurrently used to
245
+ // race the learning (the 2nd saw the code "tried" but not yet learned and
246
+ // failed). Serializing writes per device fixes it.
247
+ test('concurrent duplicate writes of an unknown code are serialized, not raced', async () => {
248
+ const api = makeApi();
249
+ api.getShadowProperties = jest.fn().mockResolvedValue([{code: 'Power', dp_id: 1, value: false}]);
250
+ api.sendCommands.mockImplementation((id, cmds) =>
251
+ cmds.some(c => c.code === 'Power')
252
+ ? Promise.reject(new Error('command or value not support (code 2008)'))
253
+ : Promise.resolve(true));
254
+ const dev = makeDevice(api, null, {key: 'abc'});
255
+ await dev._connect();
256
+ const warn = jest.spyOn(log, 'warn');
257
+
258
+ const [a, b] = await Promise.all([dev.update({'1': true}), dev.update({'1': true})]);
259
+
260
+ expect(a).toBe(true);
261
+ expect(b).toBe(true);
262
+ expect(warn).not.toHaveBeenCalled(); // the 2nd write didn't spuriously fail
263
+ expect(api.sendCommands).toHaveBeenCalledTimes(1); // only the 1st probed commands
264
+ jest.restoreAllMocks();
265
+ });
243
266
  });
244
267
 
245
268
  test('realtime code-only deltas are mirrored to numeric ids via the learned map', async () => {