homebridge-tuya-plus 4.3.0-dev.108 → 4.3.0-dev.110

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.
@@ -589,6 +589,46 @@
589
589
  "functionBody": "return model.devices && model.devices[arrayIndices] && ['Fan'].includes(model.devices[arrayIndices].type);"
590
590
  }
591
591
  },
592
+ "swingOnValue": {
593
+ "title": "Swing/oscillation on value",
594
+ "description": "Value to send to the swing DP to start oscillating, for fans whose oscillation DP is an enum of angles (e.g. 120). Set together with the swing off value. Leave empty for a boolean DP.",
595
+ "type": "string",
596
+ "condition": {
597
+ "functionBody": "return model.devices && model.devices[arrayIndices] && ['Fan'].includes(model.devices[arrayIndices].type);"
598
+ }
599
+ },
600
+ "swingOffValue": {
601
+ "title": "Swing/oscillation off value",
602
+ "description": "Value of the swing DP that means not oscillating (e.g. OFF). Any other value is shown as oscillating. Set together with the swing on value. Leave empty for a boolean DP.",
603
+ "type": "string",
604
+ "condition": {
605
+ "functionBody": "return model.devices && model.devices[arrayIndices] && ['Fan'].includes(model.devices[arrayIndices].type);"
606
+ }
607
+ },
608
+ "dpSwingVertical": {
609
+ "title": "Vertical swing DP",
610
+ "description": "Data point of a second (vertical) oscillation axis. When set, a separate switch is shown in the Home app. Leave empty if the fan has no vertical oscillation.",
611
+ "type": "integer",
612
+ "condition": {
613
+ "functionBody": "return model.devices && model.devices[arrayIndices] && ['Fan'].includes(model.devices[arrayIndices].type);"
614
+ }
615
+ },
616
+ "swingVerticalOnValue": {
617
+ "title": "Vertical swing on value",
618
+ "description": "Value to send to the vertical swing DP to start oscillating (e.g. 60). Set together with the vertical swing off value. Leave empty for a boolean DP.",
619
+ "type": "string",
620
+ "condition": {
621
+ "functionBody": "return model.devices && model.devices[arrayIndices] && ['Fan'].includes(model.devices[arrayIndices].type);"
622
+ }
623
+ },
624
+ "swingVerticalOffValue": {
625
+ "title": "Vertical swing off value",
626
+ "description": "Value of the vertical swing DP that means not oscillating (e.g. OFF). Set together with the vertical swing on value. Leave empty for a boolean DP.",
627
+ "type": "string",
628
+ "condition": {
629
+ "functionBody": "return model.devices && model.devices[arrayIndices] && ['Fan'].includes(model.devices[arrayIndices].type);"
630
+ }
631
+ },
592
632
  "noDirection": {
593
633
  "title": "Hide the rotation direction control",
594
634
  "description": "Enable if the fan has no forward/reverse direction, or if the direction data point is actually a mode enum. Prevents writing forward/reverse into that data point.",
@@ -38,6 +38,7 @@ class SimpleFanAccessory extends BaseAccessory {
38
38
  this.dpRotationSpeed = this._getCustomDP(this.device.context.dpRotationSpeed) || '3';
39
39
  this.dpFanDirection = this._getCustomDP(this.device.context.dpFanDirection) || '2';
40
40
  this.dpSwing = this._getCustomDP(this.device.context.dpSwing);
41
+ this.dpSwingVertical = this._getCustomDP(this.device.context.dpSwingVertical);
41
42
 
42
43
  this.dpCurrentTemperature = this._getCustomDP(this.device.context.dpCurrentTemperature);
43
44
 
@@ -81,6 +82,23 @@ class SimpleFanAccessory extends BaseAccessory {
81
82
  .onSet(value => this.setSwingMode(value));
82
83
  } else this._removeCharacteristic(serviceFan, Characteristic.SwingMode);
83
84
 
85
+ let serviceSwingVertical = this.accessory.getServiceById(Service.Switch, 'swing-vertical');
86
+ if (this.dpSwingVertical && !serviceSwingVertical) {
87
+ serviceSwingVertical = this.accessory.addService(Service.Switch, this._getSwingVerticalName(), 'swing-vertical');
88
+ } else if (!this.dpSwingVertical && serviceSwingVertical) {
89
+ this.accessory.removeService(serviceSwingVertical);
90
+ serviceSwingVertical = null;
91
+ }
92
+
93
+ let characteristicSwingVertical;
94
+ if (serviceSwingVertical) {
95
+ this._checkServiceName(serviceSwingVertical, this._getSwingVerticalName());
96
+ characteristicSwingVertical = serviceSwingVertical.getCharacteristic(Characteristic.On)
97
+ .updateValue(this._getSwingVertical(dps[this.dpSwingVertical]))
98
+ .onGet(() => this.getSwingVertical())
99
+ .onSet(value => this.setSwingVertical(value));
100
+ }
101
+
84
102
  let characteristicTemperature;
85
103
  if (this.dpCurrentTemperature && serviceTemp) {
86
104
  characteristicTemperature = serviceTemp.getCharacteristic(Characteristic.CurrentTemperature)
@@ -105,6 +123,11 @@ class SimpleFanAccessory extends BaseAccessory {
105
123
  if (characteristicSwingMode.value !== swing) characteristicSwingMode.updateValue(swing);
106
124
  }
107
125
 
126
+ if (characteristicSwingVertical && changes.hasOwnProperty(this.dpSwingVertical)) {
127
+ const swingVertical = this._getSwingVertical(changes[this.dpSwingVertical]);
128
+ if (characteristicSwingVertical.value !== swingVertical) characteristicSwingVertical.updateValue(swingVertical);
129
+ }
130
+
108
131
  if (characteristicTemperature && changes.hasOwnProperty(this.dpCurrentTemperature)) {
109
132
  const temp = this._getCurrentTemperature(changes[this.dpCurrentTemperature]);
110
133
  if (characteristicTemperature.value !== temp) characteristicTemperature.updateValue(temp);
@@ -195,12 +218,41 @@ class SimpleFanAccessory extends BaseAccessory {
195
218
 
196
219
  _getSwingMode(dp) {
197
220
  const {Characteristic} = this.hap;
198
- return dp ? Characteristic.SwingMode.SWING_ENABLED : Characteristic.SwingMode.SWING_DISABLED;
221
+ return this._isSwinging(dp, this.device.context.swingOffValue) ? Characteristic.SwingMode.SWING_ENABLED : Characteristic.SwingMode.SWING_DISABLED;
199
222
  }
200
223
 
201
224
  setSwingMode(value) {
202
225
  const {Characteristic} = this.hap;
203
- return this.setStateAsync(this.dpSwing, value === Characteristic.SwingMode.SWING_ENABLED);
226
+ const enabled = value === Characteristic.SwingMode.SWING_ENABLED;
227
+ return this.setStateAsync(this.dpSwing, this._getSwingDPValue(enabled, this.device.context.swingOnValue, this.device.context.swingOffValue));
228
+ }
229
+
230
+ getSwingVertical() {
231
+ return this._getSwingVertical(this.getStateAsync(this.dpSwingVertical));
232
+ }
233
+
234
+ _getSwingVertical(dp) {
235
+ return this._isSwinging(dp, this.device.context.swingVerticalOffValue);
236
+ }
237
+
238
+ setSwingVertical(value) {
239
+ return this.setStateAsync(this.dpSwingVertical, this._getSwingDPValue(!!value, this.device.context.swingVerticalOnValue, this.device.context.swingVerticalOffValue));
240
+ }
241
+
242
+ _getSwingVerticalName() {
243
+ return this.device.context.name + ' Vertical Swing';
244
+ }
245
+
246
+ // Many fans model oscillation as an enum of sweep angles (e.g. "OFF"/"30"/"120")
247
+ // rather than a boolean; with an off value configured, any other value means swinging.
248
+ _isSwinging(dp, offValue) {
249
+ if (offValue == null || offValue === '') return !!dp;
250
+ return dp != null && String(dp) !== String(offValue);
251
+ }
252
+
253
+ _getSwingDPValue(enabled, onValue, offValue) {
254
+ const value = enabled ? onValue : offValue;
255
+ return (value == null || value === '') ? enabled : value;
204
256
  }
205
257
 
206
258
  getCurrentTemperature() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "homebridge-tuya-plus",
3
- "version": "4.3.0-dev.108",
3
+ "version": "4.3.0-dev.110",
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": {
@@ -17,6 +17,7 @@ function makeFan(state = {}, context = {}) {
17
17
  instance.dpRotationSpeed = '3';
18
18
  instance.dpFanDirection = '2';
19
19
  instance.dpSwing = instance._getCustomDP(context.dpSwing);
20
+ instance.dpSwingVertical = instance._getCustomDP(context.dpSwingVertical);
20
21
  return result;
21
22
  }
22
23
 
@@ -67,6 +68,75 @@ describe('SimpleFanAccessory.getSwingMode / setSwingMode', () => {
67
68
  });
68
69
  });
69
70
 
71
+ // ---------------------------------------------------------------------------
72
+ // Enum swing DPs — fans that model oscillation as sweep angles ("OFF"/"30"/"120")
73
+ // ---------------------------------------------------------------------------
74
+ describe('SimpleFanAccessory swing on/off values', () => {
75
+ const enumSwing = { dpSwing: 7, swingOnValue: '120', swingOffValue: 'OFF' };
76
+
77
+ test('the off value disables swing and any other value enables it', () => {
78
+ const { instance } = makeFan({}, enumSwing);
79
+ expect(instance._getSwingMode('OFF')).toBe(SwingMode.SWING_DISABLED);
80
+ expect(instance._getSwingMode('30')).toBe(SwingMode.SWING_ENABLED);
81
+ expect(instance._getSwingMode('120')).toBe(SwingMode.SWING_ENABLED);
82
+ expect(instance._getSwingMode(undefined)).toBe(SwingMode.SWING_DISABLED);
83
+ });
84
+
85
+ test('setSwingMode writes the configured on value', () => {
86
+ const { instance, device } = makeFan({ '7': 'OFF' }, enumSwing);
87
+ instance.setSwingMode(SwingMode.SWING_ENABLED);
88
+ expect(device.update).toHaveBeenCalledWith({ '7': '120' });
89
+ });
90
+
91
+ test('setSwingMode writes the configured off value', () => {
92
+ const { instance, device } = makeFan({ '7': '120' }, enumSwing);
93
+ instance.setSwingMode(SwingMode.SWING_DISABLED);
94
+ expect(device.update).toHaveBeenCalledWith({ '7': 'OFF' });
95
+ });
96
+
97
+ test('empty on/off values fall back to booleans', () => {
98
+ const { instance, device } = makeFan({ '4': false }, { dpSwing: 4, swingOnValue: '', swingOffValue: '' });
99
+ instance.setSwingMode(SwingMode.SWING_ENABLED);
100
+ expect(device.update).toHaveBeenCalledWith({ '4': true });
101
+ expect(instance._getSwingMode(false)).toBe(SwingMode.SWING_DISABLED);
102
+ });
103
+ });
104
+
105
+ // ---------------------------------------------------------------------------
106
+ // Vertical swing — a second oscillation axis exposed as a Switch
107
+ // ---------------------------------------------------------------------------
108
+ describe('SimpleFanAccessory vertical swing', () => {
109
+ const vertical = { dpSwingVertical: 6, swingVerticalOnValue: '60', swingVerticalOffValue: 'OFF' };
110
+
111
+ test('reads the vertical swing DP through its off value', () => {
112
+ const { instance } = makeFan({ '6': '30' }, vertical);
113
+ expect(instance.getSwingVertical()).toBe(true);
114
+ expect(instance._getSwingVertical('OFF')).toBe(false);
115
+ });
116
+
117
+ test('setSwingVertical writes the configured on value', () => {
118
+ const { instance, device } = makeFan({ '6': 'OFF' }, vertical);
119
+ instance.setSwingVertical(true);
120
+ expect(device.update).toHaveBeenCalledWith({ '6': '60' });
121
+ });
122
+
123
+ test('setSwingVertical writes the configured off value', () => {
124
+ const { instance, device } = makeFan({ '6': '60' }, vertical);
125
+ instance.setSwingVertical(false);
126
+ expect(device.update).toHaveBeenCalledWith({ '6': 'OFF' });
127
+ });
128
+
129
+ test('a boolean vertical swing DP works without on/off values', () => {
130
+ const { instance, device } = makeFan({ '6': false }, { dpSwingVertical: 6 });
131
+ instance.setSwingVertical(true);
132
+ expect(device.update).toHaveBeenCalledWith({ '6': true });
133
+ });
134
+
135
+ test('the switch is named after the device', () => {
136
+ expect(makeFan({}, vertical).instance._getSwingVerticalName()).toBe('Test Device Vertical Swing');
137
+ });
138
+ });
139
+
70
140
  // ---------------------------------------------------------------------------
71
141
  // Registration — SwingMode is opt-in and RotationDirection is opt-out
72
142
  // ---------------------------------------------------------------------------
@@ -89,6 +159,21 @@ describe('SimpleFanAccessory._registerCharacteristics', () => {
89
159
  expect(service.getCharacteristic).not.toHaveBeenCalledWith(SwingMode);
90
160
  });
91
161
 
162
+ test('adds the vertical swing Switch when dpSwingVertical is configured', () => {
163
+ const { instance, accessory } = makeInstance(SimpleFanAccessory, {}, { type: 'Fan', dpSwingVertical: 6 });
164
+ instance._registerCharacteristics(instance.device.state);
165
+ expect(accessory.addService).toHaveBeenCalledWith(HAP.Service.Switch, 'Test Device Vertical Swing', 'swing-vertical');
166
+ });
167
+
168
+ test('removes a cached vertical swing Switch when dpSwingVertical is dropped', () => {
169
+ const { instance, accessory } = makeInstance(SimpleFanAccessory, {}, { type: 'Fan' });
170
+ const cached = { UUID: 'switch' };
171
+ accessory.getServiceById.mockReturnValue(cached);
172
+ instance._registerCharacteristics(instance.device.state);
173
+ expect(accessory.removeService).toHaveBeenCalledWith(cached);
174
+ expect(accessory.addService).not.toHaveBeenCalled();
175
+ });
176
+
92
177
  test('registers RotationDirection by default', () => {
93
178
  const service = register({});
94
179
  expect(service.getCharacteristic).toHaveBeenCalledWith(RotationDirection);
@@ -878,6 +878,22 @@ Every option below is optional — the defaults match a common 3-speed fan. The
878
878
  (oscillation) control appears in the Home app. Omit if the fan can't oscillate. */
879
879
  "dpSwing": 4,
880
880
 
881
+ /* For fans whose oscillation DP is an enum of sweep angles rather than a
882
+ boolean (e.g. "OFF" / "30" / "75" / "120"): the value to send when
883
+ oscillation is switched on, and the value that means "not oscillating".
884
+ Any other value is shown as oscillating. Set both or neither.
885
+ Default: true / false */
886
+ "swingOnValue": "120",
887
+ "swingOffValue": "OFF",
888
+
889
+ /* Data-point of a second, vertical oscillation axis. When set, a separate
890
+ switch named "<name> Vertical Swing" appears in the Home app. Accepts the
891
+ same on/off values as above.
892
+ Omit if the fan has a single oscillation axis. */
893
+ "dpSwingVertical": 6,
894
+ "swingVerticalOnValue": "60",
895
+ "swingVerticalOffValue": "OFF",
896
+
881
897
  /* Drop the direction control entirely — for fans whose "direction" DP is
882
898
  actually a mode enum (e.g. nature/sleep/smart), so HomeKit won't write
883
899
  forward/reverse into it. Default: false */