homebridge-tuya-plus 4.3.0-dev.103 → 4.3.0-dev.106

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/Changelog.md CHANGED
@@ -12,6 +12,7 @@ All notable changes to this project will be documented in this file. This projec
12
12
  * Works with both **Custom** and **Smart Home** Cloud projects (the latter via app-account login).
13
13
  * The **IrrigationSystem** accessory no longer has any cloud-specific handling — like every other accessory it's transport-agnostic, with the LAN+cloud fallback handled underneath.
14
14
  * If your devices sometimes drop off the LAN and show "No Response", adding cloud credentials is an easy way to smooth that over. See the wiki: **[Tuya Cloud Setup](https://github.com/adrianjagielak/homebridge-tuya-plus/blob/main/wiki/Tuya-Cloud-Setup.md)**.
15
+ * [*] **SimpleBlinds: keep the estimated position within 0–100.** The time-based position estimate wasn't clamped, so a stale run start (e.g. the device never echoed a command because its data-point already held that value) extrapolated to positions like `286` or `147382`. HomeKit rejected them (`characteristic was supplied illegal value … exceeded maximum of 100`), and percentage control then started its timed move from that bogus position. The estimate is now bounded by the physical travel range (`minPosition`–100, so `timeToTighten` still works), and reported positions are clamped to 0–100.
15
16
  * [*] **Fix realtime (MQTT) cloud updates being silently dropped** — external changes (physical buttons, the Tuya app, the device's own timers) now show up in HomeKit within a second or two. The decryptor was verifying the AES-GCM auth tag (`decipher.final()`), but Tuya's real status frames don't carry a tag that verifies against the documented AAD, so every realtime message was being thrown away. Now decrypts with `update()` only, matching the official `tuya/tuya-homebridge` and `0x5e/homebridge-tuya-platform` implementations.
16
17
  * [*] **Fix cloud irrigation valves that could be turned on but not off** — the per-zone write coalescer was dropping any command that matched the last-known `device.state`. Cloud devices never optimistically advance `state` (it only moves once the realtime stream confirms the device), so an "off" issued before the "on" was echoed matched the stale "off" and was discarded — HomeKit showed the zone closed while it kept running. Queued commands are now sent as-is (callers already queue only genuine changes).
17
18
  * [*] **IrrigationSystem: remove the rain sensor.** It never reported reliably on these devices, and bundling a sensor (a different HomeKit category) in the same accessory forced the Home app to fragment the sprinkler into "sub-accessories" — blocking control from the main tile and hiding the system master on/off. The accessory is now a single, clean sprinkler tile (IrrigationSystem + valves + optional battery); any leftover Contact/Leak sensor service from a previous build is removed automatically on restart. The `noRainSensor`, `rainSensorType`, `rainInverted`, `dpRain` and `rainOnValue` options are gone.
package/Readme.MD CHANGED
@@ -104,14 +104,11 @@ If you have new accessory logic for a new device, please add a function defined
104
104
 
105
105
  ## Credits
106
106
 
107
- * [Eliran Sapir](https://github.com/sapireli) - ZY-M100 occupancy and ambient light support ([original implementation](https://github.com/sapireli/homebridge-tuya-local/commit/08e223b2f4ecfe80031ee5a4b16393a88fdd0b5b)), and the contact, smoke, and leak sensor port with battery support and regression tests.
108
-
109
107
  * [iRayanKhan](https://github.com/iRayanKhan) - developer of the original [homebridge-tuya](https://github.com/iRayanKhan/homebridge-tuya) plugin.
110
108
  * [AMoo-Miki](https://github.com/AMoo-Miki) - developer of the [Tuya-Lan](https://github.com/AMoo-Miki/homebridge-tuya-lan) plugin which this plugin is based off.
111
109
  * mxDanger - Plugin branding.
112
110
  * [CodeTheWeb](https://github.com/CodeTheWeb) - developer of [TuyaApi](https://github.com/codetheweb/tuyapi), who gratiously provided this repo's name.
113
111
  * [Oznu](https://github.com/oznu) - developer of Homebridge, added ```config.schema.json``` , fixed dependencies, and helped inspire this readME off his [gsh](https://github.com/oznu/homebridge-gsh) plugin.
114
- * [0x5e](https://github.com/0x5e) and the [Tuya Homebridge contributors](https://github.com/0x5e/homebridge-tuya-platform) - contact, smoke, leak, and battery mappings, adapted under the MIT license. See [third-party notices](THIRD_PARTY_NOTICES.md).
115
112
 
116
113
  #
117
114
 
@@ -152,15 +152,10 @@ class SimpleBlindsAccessory extends BaseAccessory {
152
152
  this.log.debug('[TuyaAccessory] Blinds last change was ' + this.changeTime + '; ' + (Date.now() - this.changeTime) + 'ms ago');
153
153
 
154
154
  if (this.changeTime) {
155
- const disposition = ((Date.now() - this.changeTime) / (10 * this.duration));
156
- if (this.assumedState === BLINDS_CLOSING) {
157
- this.assumedPosition = BLINDS_OPEN - disposition;
158
- } else {
159
- this.assumedPosition = this.minPosition + disposition;
160
- }
155
+ this.assumedPosition = this._estimatePosition();
161
156
  }
162
157
 
163
- const adjustedPosition = Math.max(0, Math.round(this.assumedPosition));
158
+ const adjustedPosition = this._reportedPosition();
164
159
  characteristicCurrentPosition.updateValue(adjustedPosition);
165
160
  characteristicTargetPosition.updateValue(adjustedPosition);
166
161
  characteristicPositionState.updateValue(Characteristic.PositionState.STOPPED);
@@ -186,7 +181,7 @@ class SimpleBlindsAccessory extends BaseAccessory {
186
181
  case this.cmdClose:
187
182
  return BLINDS_CLOSED;
188
183
  default:
189
- return Math.max(BLINDS_CLOSED, Math.round(this.assumedPosition));
184
+ return this._reportedPosition();
190
185
  }
191
186
  }
192
187
 
@@ -201,7 +196,7 @@ class SimpleBlindsAccessory extends BaseAccessory {
201
196
  case this.cmdClose:
202
197
  return BLINDS_CLOSED;
203
198
  default:
204
- return Math.max(BLINDS_CLOSED, Math.round(this.assumedPosition));
199
+ return this._reportedPosition();
205
200
  }
206
201
  }
207
202
 
@@ -213,12 +208,7 @@ class SimpleBlindsAccessory extends BaseAccessory {
213
208
 
214
209
  if (this.changeTime !== false) {
215
210
  this.log.debug('[TuyaAccessory] Blinds ' + (this.assumedState === BLINDS_CLOSING ? 'closing' : 'opening') + ' had started ' + this.changeTime + '; ' + (Date.now() - this.changeTime) + 'ms ago');
216
- const disposition = ((Date.now() - this.changeTime) / (10 * this.duration));
217
- if (this.assumedState === BLINDS_CLOSING) {
218
- this.assumedPosition = BLINDS_OPEN - disposition;
219
- } else {
220
- this.assumedPosition = this.minPosition + disposition;
221
- }
211
+ this.assumedPosition = this._estimatePosition();
222
212
  this.log.debug('[TuyaAccessory] Blinds\' adjusted assumedPosition is ' + this.assumedPosition);
223
213
  }
224
214
 
@@ -253,6 +243,22 @@ class SimpleBlindsAccessory extends BaseAccessory {
253
243
  }
254
244
  }
255
245
 
246
+ // Time-based guess at where the blind is now, given when the current run
247
+ // started. Clamped to the physical travel range: the motor stops at its end
248
+ // limits, and changeTime can go stale (e.g. the device never echoed a
249
+ // command because its DP already held that value), which otherwise
250
+ // extrapolates to positions like 147382 that HomeKit rejects.
251
+ _estimatePosition() {
252
+ const disposition = (Date.now() - this.changeTime) / (10 * this.duration);
253
+ const position = this.assumedState === BLINDS_CLOSING ? BLINDS_OPEN - disposition : this.minPosition + disposition;
254
+ return Math.min(BLINDS_OPEN, Math.max(this.minPosition, position));
255
+ }
256
+
257
+ // assumedPosition dips below 0 when timeToTighten is set; HomeKit only takes 0-100.
258
+ _reportedPosition() {
259
+ return Math.min(BLINDS_OPEN, Math.max(BLINDS_CLOSED, Math.round(this.assumedPosition)));
260
+ }
261
+
256
262
  _getPositionState() {
257
263
  const {Characteristic} = this.hap;
258
264
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "homebridge-tuya-plus",
3
- "version": "4.3.0-dev.103",
3
+ "version": "4.3.0-dev.106",
4
4
  "description": "A community-maintained Homebridge plugin for controlling Tuya devices locally over LAN, with an optional Tuya Cloud fallback for whatever the LAN can't reach. Includes new features, fixes, and updated device support. PRs welcome: if it runs, it ships (almost).",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -0,0 +1,108 @@
1
+ 'use strict';
2
+
3
+ const SimpleBlindsAccessory = require('../lib/SimpleBlindsAccessory');
4
+ const { HAP, makeInstance, makeMockCharacteristic } = require('./support/mocks');
5
+
6
+ const { CurrentPosition, TargetPosition } = HAP.Characteristic;
7
+
8
+ const HOUR = 60 * 60 * 1000;
9
+
10
+ // Wire up the accessory the way the platform does (first state report), and
11
+ // hand back the device 'change' listener. Each characteristic gets its own mock
12
+ // (the shared default one can't tell CurrentPosition from PositionState).
13
+ function makeBlinds(context = {}, dps = { 1: 'stop' }) {
14
+ const { instance, device, accessory } = makeInstance(SimpleBlindsAccessory, dps, { type: 'SimpleBlinds', ...context });
15
+ // echo writes into state like a real device, so setMultiState's
16
+ // "already at that value" short-circuit behaves as it does in production
17
+ device.update = jest.fn(change => { Object.assign(device.state, change); return true; });
18
+ const chars = new Map();
19
+ accessory._mockService.getCharacteristic = jest.fn(c => {
20
+ if (!chars.has(c)) chars.set(c, makeMockCharacteristic());
21
+ return chars.get(c);
22
+ });
23
+ instance._registerCharacteristics(dps);
24
+ const onChange = device.on.mock.calls.find(([event]) => event === 'change')[1];
25
+ return { instance, device, onChange, current: chars.get(CurrentPosition), target: chars.get(TargetPosition) };
26
+ }
27
+
28
+ beforeEach(() => jest.useFakeTimers({ now: new Date('2026-10-24T08:00:00Z') }));
29
+ afterEach(() => jest.useRealTimers());
30
+
31
+ // ---------------------------------------------------------------------------
32
+ // The time-based position estimate must stay within the physical travel range.
33
+ // A stale changeTime (the device never echoed the command because its DP
34
+ // already held that value) used to extrapolate to values like 147382, which
35
+ // HomeKit rejects — breaking percentage control.
36
+ // ---------------------------------------------------------------------------
37
+ describe('SimpleBlindsAccessory position estimate', () => {
38
+ test('a stop long after opening started reports fully open, not >100', () => {
39
+ const { instance, onChange, current, target } = makeBlinds();
40
+ instance.assumedState = 'opening';
41
+ instance.changeTime = Date.now() - 18 * HOUR;
42
+
43
+ onChange({ 1: 'stop' });
44
+
45
+ expect(current.value).toBe(100);
46
+ expect(target.value).toBe(100);
47
+ expect(instance.assumedPosition).toBe(100);
48
+ });
49
+
50
+ test('a stop long after closing started reports fully closed, not <0', () => {
51
+ const { instance, onChange, current, target } = makeBlinds();
52
+ instance.assumedState = 'closing';
53
+ instance.changeTime = Date.now() - 18 * HOUR;
54
+
55
+ onChange({ 1: 'stop' });
56
+
57
+ expect(current.value).toBe(0);
58
+ expect(target.value).toBe(0);
59
+ expect(instance.assumedPosition).toBe(0);
60
+ });
61
+
62
+ test('a stop mid-travel still reports the proportional position', () => {
63
+ const { instance, onChange, current } = makeBlinds({ timeToOpen: 40 });
64
+ instance.assumedState = 'opening';
65
+ instance.changeTime = Date.now() - 10 * 1000; // 10s of a 40s run
66
+
67
+ onChange({ 1: 'stop' });
68
+
69
+ expect(current.value).toBe(25);
70
+ });
71
+
72
+ test('closing clamps to minPosition when timeToTighten is set', () => {
73
+ const { instance, onChange } = makeBlinds({ timeToOpen: 45, timeToTighten: 5 });
74
+ instance.assumedState = 'closing';
75
+ instance.changeTime = Date.now() - 18 * HOUR;
76
+
77
+ onChange({ 1: 'stop' });
78
+
79
+ expect(instance.assumedPosition).toBe(instance.minPosition);
80
+ });
81
+
82
+ test('setting a percentage after a stale run moves from the real end position', async () => {
83
+ const { instance, device } = makeBlinds({ timeToOpen: 40 });
84
+ instance.assumedState = 'opening';
85
+ instance.changeTime = Date.now() - 18 * HOUR;
86
+
87
+ await instance.setTargetPosition(50);
88
+
89
+ expect(instance.assumedPosition).toBe(100);
90
+ expect(device.update).toHaveBeenLastCalledWith({ 1: 'close' });
91
+
92
+ // 50% of a 40s run, not hours
93
+ jest.advanceTimersByTime(20 * 1000);
94
+ expect(device.update).toHaveBeenLastCalledWith({ 1: 'stop' });
95
+ });
96
+
97
+ test('reported positions are clamped to the HomeKit 0–100 range', () => {
98
+ const { instance } = makeBlinds({ timeToOpen: 45, timeToTighten: 5 });
99
+
100
+ instance.assumedPosition = 368;
101
+ expect(instance._getCurrentPosition('stop')).toBe(100);
102
+ expect(instance._getTargetPosition('stop')).toBe(100);
103
+
104
+ instance.assumedPosition = instance.minPosition;
105
+ expect(instance._getCurrentPosition('stop')).toBe(0);
106
+ expect(instance._getTargetPosition('stop')).toBe(0);
107
+ });
108
+ });
@@ -15,6 +15,8 @@ const HAP = {
15
15
  CurrentHeaterCoolerState: { INACTIVE: 0, IDLE: 1, HEATING: 2, COOLING: 3 },
16
16
  TargetHeaterCoolerState: { AUTO: 0, HEAT: 1, COOL: 2 },
17
17
  PositionState: { DECREASING: 0, INCREASING: 1, STOPPED: 2 },
18
+ CurrentPosition: { UUID: '6D' },
19
+ TargetPosition: { UUID: '7C' },
18
20
  LockPhysicalControls: { CONTROL_LOCK_DISABLED: 0, CONTROL_LOCK_ENABLED: 1 },
19
21
  TemperatureDisplayUnits: { CELSIUS: 0, FAHRENHEIT: 1 },
20
22
  SwingMode: { SWING_DISABLED: 0, SWING_ENABLED: 1 },
@@ -1,42 +0,0 @@
1
- # Third-party notices
2
-
3
- ## Sensor mappings from homebridge-tuya-platform
4
-
5
- The contact, smoke, and leak sensor mappings and shared battery mappings in
6
- `lib/TuyaSensorAccessory.js`, `lib/ContactSensorAccessory.js`,
7
- `lib/SmokeSensorAccessory.js`, and `lib/LeakSensorAccessory.js` are adapted from
8
- [0x5e/homebridge-tuya-platform](https://github.com/0x5e/homebridge-tuya-platform),
9
- revision `ab74ea4eee3c9df0be2ecc04c0fc56d9c0b02207`:
10
-
11
- - `src/accessory/ContactSensorAccessory.ts`
12
- - `src/accessory/SmokeSensorAccessory.ts`
13
- - `src/accessory/LeakSensorAccessory.ts`
14
- - `src/accessory/BaseAccessory.ts`
15
-
16
- Credit goes to 0x5e and the Tuya Homebridge contributors for those implementations.
17
- Eliran Sapir ([sapireli](https://github.com/sapireli)) adapted them for Tuya Plus's
18
- accessory lifecycle, cloud updates, configuration UI, and regression tests.
19
-
20
- The source project's MIT license notice follows:
21
-
22
- The MIT License (MIT)
23
-
24
- Copyright (c) 2014-2021 Tuya Inc.
25
-
26
- Permission is hereby granted, free of charge, to any person obtaining a copy
27
- of this software and associated documentation files (the "Software"), to deal
28
- in the Software without restriction, including without limitation the rights
29
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
30
- copies of the Software, and to permit persons to whom the Software is
31
- furnished to do so, subject to the following conditions:
32
-
33
- The above copyright notice and this permission notice shall be included in
34
- all copies or substantial portions of the Software.
35
-
36
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
37
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
38
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
39
- THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
40
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
41
- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
42
- DEALINGS IN THE SOFTWARE.