homebridge-tuya-plus 4.2.0-dev.81 → 4.2.0

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
@@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file. This projec
4
4
 
5
5
  ## Unreleased
6
6
 
7
+ * [+] **PercentBlinds: animate Opening/Closing mid-travel via an optional work-state data-point.** Many Tuya curtain/roller motors report their position (`dpPercentState`) only once travel finishes, so the Home app jumped straight to the target instead of showing "Opening…"/"Closing…". Set `"dpWorkState"` (Tuya `work_state`/`control`, usually `7`) and the accessory drives HomeKit's `PositionState` from the live direction the motor reports (`opening`/`closing`, with `open`/`up` and `close`/`down` also recognised, overridable via `opening`/`closing`), then settles to STOPPED when the position report arrives. `flipState` inverts the direction to match a flipped position range. Purely opt-in: with no `dpWorkState` the behaviour is unchanged. Also adds `PercentBlinds`, its `dpPercentControl`/`dpPercentState`/`flipState`/`dpWorkState` options, and the new work-state values to `config.schema.json` (the type existed in code but was missing from the Config UI form).
7
8
  * [+] **Garage door: support DASPI "Passage" openers.** Set `"manufacturer": "Daspi"` on a `GarageDoor` device to select its data-points (`dpAction` `1`, `dpStatus` `7`, still overridable). DASPI reports state as strings on DP `7` (`full_opened`/`opening`/`open_softstop`/`full_closed`/`closing`/`close_softstop`) and takes capitalized `Open`/`Close` commands on DP `1`; the soft-stop phases map to HomeKit's Opening/Closing so the tile animates correctly mid-travel.
8
9
  * [+] **Tuya Cloud is now a transparent, global fallback for every device.** What started as an experiment to reach battery-powered "sleepy" irrigation timers (which never appear on the LAN) is now a general, optional fallback: add a top-level `cloud` credentials block once and the plugin keeps a single Tuya Cloud/MQTT session alive in the background, falling back to it whenever a device can't be reached on the LAN — a flaky moment, or hardware that's never local. The plugin stays **LAN-first** (local is always tried first and preferred) and cloud remains **strictly opt-in** (nothing runs unless credentials are present).
9
10
  * **No per-device configuration.** There is one global session and no per-device cloud settings. Every device automatically uses the LAN first and the cloud as a fallback; a device with no local `key` is reached over the cloud only. Existing LAN configs (numeric data-points) keep working over the cloud — the data-point id↔code map is learned from Tuya's device shadow (`/v2.0/cloud/thing/{id}/shadow/properties`) — and if both transports fail, HomeKit shows "No Response" as before. This mirrors the official Tuya/Smart Life app (LAN when possible, cloud as a backup).
@@ -147,6 +147,10 @@
147
147
  "title": "Simple Blinds",
148
148
  "enum": ["SimpleBlinds"]
149
149
  },
150
+ {
151
+ "title": "Percentage Blinds (position %)",
152
+ "enum": ["PercentBlinds"]
153
+ },
150
154
  {
151
155
  "title": "Vertical Blinds with Tilt",
152
156
  "enum": ["VerticalBlindsWithTilt"]
@@ -789,7 +793,47 @@
789
793
  "flipState": {
790
794
  "type": "boolean",
791
795
  "condition": {
792
- "functionBody": "return model.devices && model.devices[arrayIndices] && ['GarageDoor','SimpleBlinds'].includes(model.devices[arrayIndices].type);"
796
+ "functionBody": "return model.devices && model.devices[arrayIndices] && ['GarageDoor','SimpleBlinds','PercentBlinds'].includes(model.devices[arrayIndices].type);"
797
+ }
798
+ },
799
+ "dpPercentControl": {
800
+ "type": "integer",
801
+ "placeholder": 2,
802
+ "description": "Data-point that sets the target position (0-100). Defaults to 2.",
803
+ "condition": {
804
+ "functionBody": "return model.devices && model.devices[arrayIndices] && ['PercentBlinds'].includes(model.devices[arrayIndices].type);"
805
+ }
806
+ },
807
+ "dpPercentState": {
808
+ "type": "integer",
809
+ "placeholder": 2,
810
+ "description": "Data-point that reports the current position (0-100). Defaults to the control data-point. Many motors report position on a separate data-point (e.g. 3).",
811
+ "condition": {
812
+ "functionBody": "return model.devices && model.devices[arrayIndices] && ['PercentBlinds'].includes(model.devices[arrayIndices].type);"
813
+ }
814
+ },
815
+ "dpWorkState": {
816
+ "type": "integer",
817
+ "placeholder": 7,
818
+ "description": "Optional. Data-point reporting live travel direction (Tuya 'work_state'/'control', usually 7). When set, HomeKit shows opening/closing while the motor moves. Leave empty if unsure.",
819
+ "condition": {
820
+ "functionBody": "return model.devices && model.devices[arrayIndices] && ['PercentBlinds'].includes(model.devices[arrayIndices].type);"
821
+ }
822
+ },
823
+ "opening": {
824
+ "type": "string",
825
+ "placeholder": "opening",
826
+ "description": "Optional. Comma is not used; single work-state value that means opening. Defaults recognise opening/open/up.",
827
+ "condition": {
828
+ "functionBody": "return model.devices && model.devices[arrayIndices] && ['PercentBlinds'].includes(model.devices[arrayIndices].type) && model.devices[arrayIndices].dpWorkState;"
829
+ }
830
+ },
831
+ "closing": {
832
+ "type": "string",
833
+ "placeholder": "closing",
834
+ "description": "Optional. Single work-state value that means closing. Defaults recognise closing/close/down.",
835
+ "condition": {
836
+ "functionBody": "return model.devices && model.devices[arrayIndices] && ['PercentBlinds'].includes(model.devices[arrayIndices].type) && model.devices[arrayIndices].dpWorkState;"
793
837
  }
794
838
  },
795
839
  "timeToOpen": {
@@ -24,6 +24,20 @@ class PercentBlindsAccessory extends BaseAccessory {
24
24
  this.dpPercentState = this._getCustomDP(this.device.context.dpPercentState) || '2';
25
25
  this.flipState = !!this.device.context.flipState;
26
26
 
27
+ // Optional work-state data point. Many Tuya curtain motors report their
28
+ // live travel direction on a separate DP (Tuya code 'work_state' or
29
+ // 'control', typically DP 7) with string values like 'opening'/'closing'.
30
+ // When it is configured, PositionState follows the real direction of
31
+ // travel and resets to STOPPED once movement finishes; without it the
32
+ // behaviour is unchanged and PositionState stays STOPPED.
33
+ this.dpWorkState = this.device.context.dpWorkState ? this._getCustomDP(this.device.context.dpWorkState) : undefined;
34
+ const toList = (value, fallback) => (value === undefined ? fallback : [].concat(value)).map(v => ('' + v).toLowerCase());
35
+ this._openingValues = toList(this.device.context.opening, ['opening', 'open', 'up']);
36
+ this._closingValues = toList(this.device.context.closing, ['closing', 'close', 'down']);
37
+
38
+ const PositionState = Characteristic.PositionState;
39
+ this._positionState = PositionState.STOPPED;
40
+
27
41
  const characteristicCurrentPosition = service.getCharacteristic(Characteristic.CurrentPosition)
28
42
  .updateValue(this._mapPosition(dps[this.dpPercentState] !== undefined ? dps[this.dpPercentState] : 0))
29
43
  .on('get', this.getCurrentPosition.bind(this));
@@ -33,16 +47,35 @@ class PercentBlindsAccessory extends BaseAccessory {
33
47
  .on('get', this.getTargetPosition.bind(this))
34
48
  .on('set', this.setTargetPosition.bind(this));
35
49
 
36
- service.getCharacteristic(Characteristic.PositionState)
37
- .updateValue(Characteristic.PositionState.STOPPED)
38
- .on('get', callback => callback(null, Characteristic.PositionState.STOPPED));
50
+ const characteristicPositionState = service.getCharacteristic(Characteristic.PositionState)
51
+ .updateValue(this._positionState)
52
+ .on('get', callback => callback(null, this._positionState));
39
53
 
40
54
  this.device.on('change', changes => {
55
+ if (this.dpWorkState !== undefined && changes.hasOwnProperty(this.dpWorkState)) {
56
+ const state = this._workStateToPositionState(changes[this.dpWorkState]);
57
+ if (state !== undefined) {
58
+ this._positionState = state;
59
+ this.log.debug(`[TuyaAccessory] Blind position state updated to ${state}`);
60
+ characteristicPositionState.updateValue(state);
61
+ }
62
+ }
63
+
41
64
  if (changes.hasOwnProperty(this.dpPercentState)) {
42
65
  const position = this._mapPosition(changes[this.dpPercentState]);
43
66
  this.log.debug(`[TuyaAccessory] Blind current position updated to ${position}`);
44
67
  characteristicCurrentPosition.updateValue(position);
68
+
69
+ // Motors that only report their position once travel ends use this
70
+ // report as the "movement finished" signal: settle the state and
71
+ // align the target so HomeKit stops showing an in-progress move.
72
+ if (this.dpWorkState !== undefined) {
73
+ this._positionState = PositionState.STOPPED;
74
+ characteristicPositionState.updateValue(PositionState.STOPPED);
75
+ characteristicTargetPosition.updateValue(position);
76
+ }
45
77
  }
78
+
46
79
  if (changes.hasOwnProperty(this.dpPercentControl)) {
47
80
  const position = this._mapPosition(changes[this.dpPercentControl]);
48
81
  this.log.debug(`[TuyaAccessory] Blind target position updated to ${position}`);
@@ -51,6 +84,19 @@ class PercentBlindsAccessory extends BaseAccessory {
51
84
  });
52
85
  }
53
86
 
87
+ _workStateToPositionState(value) {
88
+ const {PositionState} = this.hap.Characteristic;
89
+ const raw = ('' + value).toLowerCase();
90
+ let state;
91
+ if (this._openingValues.includes(raw)) state = PositionState.INCREASING;
92
+ else if (this._closingValues.includes(raw)) state = PositionState.DECREASING;
93
+ else return PositionState.STOPPED;
94
+ // A flipped device travels toward the opposite HomeKit position, so the
95
+ // increasing/decreasing sense is inverted too.
96
+ if (this.flipState) state = state === PositionState.INCREASING ? PositionState.DECREASING : PositionState.INCREASING;
97
+ return state;
98
+ }
99
+
54
100
  _mapPosition(value) {
55
101
  const position = parseInt(value) || 0;
56
102
  return this.flipState ? 100 - position : position;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "homebridge-tuya-plus",
3
- "version": "4.2.0-dev.81",
3
+ "version": "4.2.0",
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,81 @@
1
+ 'use strict';
2
+
3
+ const PercentBlindsAccessory = require('../lib/PercentBlindsAccessory');
4
+ const { HAP, makeInstance } = require('./support/mocks');
5
+
6
+ const { PositionState: PS } = HAP.Characteristic;
7
+
8
+ // Helper: build an instance and set the fields _registerCharacteristics would
9
+ // normally populate, so the pure helpers can be tested without wiring up the
10
+ // mock WindowCovering service (whose getCharacteristic returns a single shared
11
+ // mock, indistinguishable per-characteristic).
12
+ function makeBlinds({ flipState = false, opening, closing } = {}) {
13
+ const { instance, device } = makeInstance(PercentBlindsAccessory, {}, { type: 'PercentBlinds' });
14
+
15
+ const toList = (value, fallback) => (value === undefined ? fallback : [].concat(value)).map(v => ('' + v).toLowerCase());
16
+ instance.flipState = flipState;
17
+ instance._openingValues = toList(opening, ['opening', 'open', 'up']);
18
+ instance._closingValues = toList(closing, ['closing', 'close', 'down']);
19
+
20
+ return { instance, device };
21
+ }
22
+
23
+ // ---------------------------------------------------------------------------
24
+ // _workStateToPositionState — direction mapping
25
+ // ---------------------------------------------------------------------------
26
+ describe('PercentBlindsAccessory._workStateToPositionState — defaults', () => {
27
+ let instance;
28
+ beforeEach(() => ({ instance } = makeBlinds()));
29
+
30
+ test('"opening" → INCREASING', () => expect(instance._workStateToPositionState('opening')).toBe(PS.INCREASING));
31
+ test('"open" → INCREASING', () => expect(instance._workStateToPositionState('open')).toBe(PS.INCREASING));
32
+ test('"up" → INCREASING', () => expect(instance._workStateToPositionState('up')).toBe(PS.INCREASING));
33
+ test('"closing" → DECREASING', () => expect(instance._workStateToPositionState('closing')).toBe(PS.DECREASING));
34
+ test('"close" → DECREASING', () => expect(instance._workStateToPositionState('close')).toBe(PS.DECREASING));
35
+ test('"down" → DECREASING', () => expect(instance._workStateToPositionState('down')).toBe(PS.DECREASING));
36
+ test('unknown value → STOPPED', () => expect(instance._workStateToPositionState('paused')).toBe(PS.STOPPED));
37
+ test('mixed case "OPENING" → INCREASING', () => expect(instance._workStateToPositionState('OPENING')).toBe(PS.INCREASING));
38
+ });
39
+
40
+ describe('PercentBlindsAccessory._workStateToPositionState — flipState inverts direction', () => {
41
+ let instance;
42
+ beforeEach(() => ({ instance } = makeBlinds({ flipState: true })));
43
+
44
+ test('"opening" → DECREASING', () => expect(instance._workStateToPositionState('opening')).toBe(PS.DECREASING));
45
+ test('"closing" → INCREASING', () => expect(instance._workStateToPositionState('closing')).toBe(PS.INCREASING));
46
+ test('unknown value stays STOPPED', () => expect(instance._workStateToPositionState('paused')).toBe(PS.STOPPED));
47
+ });
48
+
49
+ describe('PercentBlindsAccessory._workStateToPositionState — custom work-state values', () => {
50
+ test('custom opening/closing tokens are honoured', () => {
51
+ const { instance } = makeBlinds({ opening: 'FZ', closing: 'ZF' });
52
+ expect(instance._workStateToPositionState('fz')).toBe(PS.INCREASING);
53
+ expect(instance._workStateToPositionState('zf')).toBe(PS.DECREASING);
54
+ // the built-in defaults no longer apply once overridden
55
+ expect(instance._workStateToPositionState('opening')).toBe(PS.STOPPED);
56
+ });
57
+ });
58
+
59
+ // ---------------------------------------------------------------------------
60
+ // _mapPosition — flip handling
61
+ // ---------------------------------------------------------------------------
62
+ describe('PercentBlindsAccessory._mapPosition', () => {
63
+ test('passes value through when not flipped', () => {
64
+ const { instance } = makeBlinds();
65
+ expect(instance._mapPosition(0)).toBe(0);
66
+ expect(instance._mapPosition(30)).toBe(30);
67
+ expect(instance._mapPosition(100)).toBe(100);
68
+ });
69
+
70
+ test('inverts value when flipped', () => {
71
+ const { instance } = makeBlinds({ flipState: true });
72
+ expect(instance._mapPosition(0)).toBe(100);
73
+ expect(instance._mapPosition(30)).toBe(70);
74
+ expect(instance._mapPosition(100)).toBe(0);
75
+ });
76
+
77
+ test('coerces string data-point values', () => {
78
+ const { instance } = makeBlinds();
79
+ expect(instance._mapPosition('45')).toBe(45);
80
+ });
81
+ });
@@ -702,7 +702,20 @@ These are blinds or roller shades that natively report their current position an
702
702
  "dpPercentState": "2",
703
703
 
704
704
  /* If the device reports 0 as fully open instead of fully closed, flip the range */
705
- "flipState": true
705
+ "flipState": true,
706
+
707
+ /* Optional. Datapoint that reports live travel direction (Tuya "work_state" /
708
+ "control", usually "7"). When set, the Home app shows "Opening…"/"Closing…"
709
+ while the motor is moving and settles to the resting position when it stops.
710
+ Many motors only report their position (dpPercentState) once travel ends, so
711
+ without this the tile jumps straight to the target. Leave it out if your
712
+ device doesn't have such a datapoint. */
713
+ "dpWorkState": "7",
714
+
715
+ /* Optional. Only used with dpWorkState, to override the reported values that
716
+ mean opening/closing. Defaults recognise opening/open/up and closing/close/down. */
717
+ "opening": "opening",
718
+ "closing": "closing"
706
719
  }
707
720
  ```
708
721