homebridge-tuya-plus 4.1.0-dev.76 → 4.1.0-dev.78

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.
@@ -505,9 +505,10 @@
505
505
  },
506
506
  "dpCurrentTemperature": {
507
507
  "type": "integer",
508
+ "title": "Current Temperature DP",
508
509
  "placeholder": "3",
509
510
  "condition": {
510
- "functionBody": "return model.devices && model.devices[arrayIndices] && ['Convector', 'SimpleHeater'].includes(model.devices[arrayIndices].type);"
511
+ "functionBody": "return model.devices && model.devices[arrayIndices] && ['Convector', 'SimpleHeater', 'Fan', 'FanLight'].includes(model.devices[arrayIndices].type);"
511
512
  }
512
513
  },
513
514
  "dpLightOn": {
@@ -555,6 +556,14 @@
555
556
  "functionBody": "return model.devices && model.devices[arrayIndices] && ['Fan'].includes(model.devices[arrayIndices].type);"
556
557
  }
557
558
  },
559
+ "directionUseBool": {
560
+ "title": "Use Boolean for Direction",
561
+ "type": "boolean",
562
+ "description": "Sends true/false instead of forward/reverse strings for oscillation/direction.",
563
+ "condition": {
564
+ "functionBody": "return model.devices && model.devices[arrayIndices] && ['Fan', 'FanLight'].includes(model.devices[arrayIndices].type);"
565
+ }
566
+ },
558
567
  "maxSpeed": {
559
568
  "type": "integer",
560
569
  "placeholder": "3",
@@ -65,6 +65,14 @@ class BaseAccessory {
65
65
  return typeof b === 'boolean' ? b : (typeof b === 'string' ? b.toLowerCase().trim() === 'true' : (typeof b === 'number' ? b !== 0 : df));
66
66
  }
67
67
 
68
+ // Numbers reach us as numbers from the Homebridge UI but as strings from a
69
+ // hand-edited config.json, and as anything at all from a device that reports
70
+ // an unexpected data point. Anything non-numeric falls back to the default.
71
+ _coerceNumber(n, defaultValue) {
72
+ const parsed = parseInt(n);
73
+ return isNaN(parsed) ? defaultValue : parsed;
74
+ }
75
+
68
76
  // A HapStatusError to throw from a get/set handler when the value can't be
69
77
  // served (device unreachable, or an unusable reading). HomeKit shows the
70
78
  // accessory as "No Response". Preferred over a plain `throw new Error(...)`,
@@ -240,20 +248,34 @@ class BaseAccessory {
240
248
  return Math.round(value * scale)
241
249
  }
242
250
 
251
+ // The mired window HomeKit sees and the raw scale the device speaks. A zero
252
+ // is meaningless for all three, so it keeps falling back to the default.
253
+ _getWhiteColorRange() {
254
+ return {
255
+ min: parseInt(this.device.context.minWhiteColor) || 140,
256
+ max: parseInt(this.device.context.maxWhiteColor) || 400,
257
+ scale: parseInt(this.device.context.scaleWhiteColor) || 255
258
+ };
259
+ }
260
+
243
261
  convertColorTemperatureFromHomeKitToTuya(value) {
244
- const min = this.device.context.minWhiteColor || 140;
245
- const max = this.device.context.maxWhiteColor || 400;
246
- const scale = this.device.context.scaleWhiteColor || 255;
247
- const result = Math.round(Math.max(Math.min(scale-(scale-1)/(max-min)*(value-min),scale),1));
248
- return result;
262
+ const {min, max, scale} = this._getWhiteColorRange();
263
+ const mireds = this._coerceNumber(value, min);
264
+ return Math.round(Math.max(Math.min(scale-(scale-1)/(max-min)*(mireds-min),scale),1));
249
265
  }
250
266
 
251
267
  convertColorTemperatureFromTuyaToHomeKit(value) {
252
- const min = this.device.context.minWhiteColor || 140;
253
- const max = this.device.context.maxWhiteColor || 400;
254
- const scale = this.device.context.scaleWhiteColor || 255;
255
- const result = Math.round(Math.max(Math.min((scale-value)/((scale-1)/(max-min))+min,max),min));
256
- return Math.min(600, Math.max(71, result));
268
+ const {min, max, scale} = this._getWhiteColorRange();
269
+ // A device that hasn't reported its colour-temperature data point yet —
270
+ // or reports a non-numeric one — used to convert to NaN, which HomeKit
271
+ // rejects with a characteristic warning on every read (see issue #34).
272
+ // Fall back to the coolest mired, what the lights already report while
273
+ // they're not in white mode.
274
+ const tuya = this._coerceNumber(value, scale);
275
+ const mireds = (scale-tuya)/((scale-1)/(max-min))+min;
276
+ // Clamped to the configured window last: that window is what the lights
277
+ // publish through setProps(), and a read outside it warns just as loudly.
278
+ return Math.round(Math.max(Math.min(mireds,max),min));
257
279
  }
258
280
 
259
281
  convertColorFromHomeKitToTuya(value, dpValue) {
@@ -61,6 +61,9 @@ class OilDiffuserAccessory extends BaseAccessory {
61
61
  this.dpLight = this._getCustomDP(this.device.context.dpLight) || '5';
62
62
  this.dpMode = this._getCustomDP(this.device.context.dpMode) || '6';
63
63
  this.dpColor = this._getCustomDP(this.device.context.dpColor) || '8';
64
+ // No default: these diffusers have no colour-temperature data point, and
65
+ // setColorTemperature emulates one by writing the equivalent RGB colour.
66
+ this.dpColorTemperature = this._getCustomDP(this.device.context.dpColorTemperature);
64
67
  this.dpActive = this._getCustomDP(this.device.context.dpActive) || '1';
65
68
  this.dpRotationSpeed = this._getCustomDP(this.device.context.dpRotationSpeed) || '2';
66
69
  this.dpWaterLevel = this._getCustomDP(this.device.context.dpWaterLevel) || '9';
@@ -111,7 +114,7 @@ class OilDiffuserAccessory extends BaseAccessory {
111
114
 
112
115
  const characteristicColorTemperature = lightService.getCharacteristic(Characteristic.ColorTemperature)
113
116
  .setProps({ minValue: 0, maxValue: 600 })
114
- .updateValue(dps[this.dpMode] === this.cmdWhite ? this.convertColorTemperatureFromTuyaToHomeKit(dps[this.dpColorTemperature]) : 0)
117
+ .updateValue(this.dpColorTemperature && dps[this.dpMode] === this.cmdWhite ? this.convertColorTemperatureFromTuyaToHomeKit(dps[this.dpColorTemperature]) : 0)
115
118
  .onGet(() => this.getColorTemperature())
116
119
  .onSet(value => this.setColorTemperature(value));
117
120
 
@@ -218,7 +221,11 @@ class OilDiffuserAccessory extends BaseAccessory {
218
221
  }
219
222
 
220
223
  getColorTemperature() {
221
- if (this.getStateAsync(this.dpMode) !== this.cmdWhite) return 0;
224
+ const mode = this.getStateAsync(this.dpMode);
225
+ // Without a colour-temperature data point there is nothing to read back:
226
+ // reading the undefined one returned NaN, which HomeKit rejects with a
227
+ // characteristic warning on every read while the light is white.
228
+ if (!this.dpColorTemperature || mode !== this.cmdWhite) return 0;
222
229
  return this.convertColorTemperatureFromTuyaToHomeKit(this.getStateAsync(this.dpColorTemperature));
223
230
  }
224
231
 
@@ -31,8 +31,8 @@ class RGBTWLightAccessory extends BaseAccessory {
31
31
  this.dpColorTemperature = this._getCustomDP(this.device.context.dpColorTemperature) || '4';
32
32
  this.dpColor = this._getCustomDP(this.device.context.dpColor) || '5';
33
33
 
34
- this.minWhiteColor = this.device.context.minWhiteColor ?? DEFAULT_MIN_WHITE_COLOR;
35
- this.maxWhiteColor = this.device.context.maxWhiteColor ?? DEFAULT_MAX_WHITE_COLOR;
34
+ this.minWhiteColor = this._coerceNumber(this.device.context.minWhiteColor, DEFAULT_MIN_WHITE_COLOR);
35
+ this.maxWhiteColor = this._coerceNumber(this.device.context.maxWhiteColor, DEFAULT_MAX_WHITE_COLOR);
36
36
 
37
37
  this._detectColorFunction(dps[this.dpColor]);
38
38
 
@@ -12,23 +12,37 @@ class SimpleFanAccessory extends BaseAccessory {
12
12
  _registerPlatformAccessory() {
13
13
  const {Service} = this.hap;
14
14
  this.accessory.addService(Service.Fan, this.device.context.name);
15
+
16
+ if (this.device.context.dpCurrentTemperature) {
17
+ this.accessory.addService(Service.TemperatureSensor, this.device.context.name + ' Temperature');
18
+ }
19
+
15
20
  super._registerPlatformAccessory();
16
21
  }
17
22
 
18
23
  _registerCharacteristics(dps) {
19
24
  const {Service, Characteristic} = this.hap;
25
+
20
26
  const serviceFan = this.accessory.getService(Service.Fan);
21
27
  this._checkServiceName(serviceFan, this.device.context.name);
28
+
29
+ let serviceTemp;
30
+ if (this.device.context.dpCurrentTemperature) {
31
+ serviceTemp = this.accessory.getService(Service.TemperatureSensor);
32
+ if (serviceTemp) {
33
+ this._checkServiceName(serviceTemp, this.device.context.name + ' Temperature');
34
+ }
35
+ }
36
+
22
37
  this.dpFanOn = this._getCustomDP(this.device.context.dpFanOn) || '1';
23
38
  this.dpRotationSpeed = this._getCustomDP(this.device.context.dpRotationSpeed) || '3';
24
39
  this.dpFanDirection = this._getCustomDP(this.device.context.dpFanDirection) || '2';
25
- // Oscillation is opt-in: SwingMode is only exposed when the fan's boolean
26
- // swing DP is configured, so existing setups are unchanged (issue #96).
27
40
  this.dpSwing = this._getCustomDP(this.device.context.dpSwing);
28
- // Some fans use DP 2 for a mode enum (nature/sleep/…) rather than a
29
- // rotation direction; RotationDirection would then write forward/reverse
30
- // into that enum. noDirection drops the characteristic for those fans.
41
+
42
+ this.dpCurrentTemperature = this._getCustomDP(this.device.context.dpCurrentTemperature);
43
+
31
44
  this.noDirection = this._coerceBoolean(this.device.context.noDirection, false);
45
+ this.directionUseBool = this._coerceBoolean(this.device.context.directionUseBool, false);
32
46
 
33
47
  this.maxSpeed = parseInt(this.device.context.maxSpeed) || 3;
34
48
  this.fanDefaultSpeed = parseInt(this.device.context.fanDefaultSpeed) || 1;
@@ -67,6 +81,13 @@ class SimpleFanAccessory extends BaseAccessory {
67
81
  .onSet(value => this.setSwingMode(value));
68
82
  } else this._removeCharacteristic(serviceFan, Characteristic.SwingMode);
69
83
 
84
+ let characteristicTemperature;
85
+ if (this.dpCurrentTemperature && serviceTemp) {
86
+ characteristicTemperature = serviceTemp.getCharacteristic(Characteristic.CurrentTemperature)
87
+ .updateValue(this._getCurrentTemperature(dps[this.dpCurrentTemperature]))
88
+ .onGet(() => this.getCurrentTemperature());
89
+ }
90
+
70
91
  this.device.on('change', (changes, state) => {
71
92
  if (changes.hasOwnProperty(this.dpFanOn) && characteristicFanOn.value !== changes[this.dpFanOn])
72
93
  characteristicFanOn.updateValue(changes[this.dpFanOn]);
@@ -84,6 +105,11 @@ class SimpleFanAccessory extends BaseAccessory {
84
105
  if (characteristicSwingMode.value !== swing) characteristicSwingMode.updateValue(swing);
85
106
  }
86
107
 
108
+ if (characteristicTemperature && changes.hasOwnProperty(this.dpCurrentTemperature)) {
109
+ const temp = this._getCurrentTemperature(changes[this.dpCurrentTemperature]);
110
+ if (characteristicTemperature.value !== temp) characteristicTemperature.updateValue(temp);
111
+ }
112
+
87
113
  this.log.debug('SimpleFan changed: ' + JSON.stringify(state));
88
114
  });
89
115
  }
@@ -148,11 +174,17 @@ class SimpleFanAccessory extends BaseAccessory {
148
174
 
149
175
  _getFanDirection(dp) {
150
176
  const {Characteristic} = this.hap;
177
+ if (this.directionUseBool) {
178
+ return dp ? Characteristic.RotationDirection.CLOCKWISE : Characteristic.RotationDirection.COUNTER_CLOCKWISE;
179
+ }
151
180
  return (dp === 'reverse') ? Characteristic.RotationDirection.COUNTER_CLOCKWISE : Characteristic.RotationDirection.CLOCKWISE;
152
181
  }
153
182
 
154
183
  setFanDirection(value) {
155
184
  const {Characteristic} = this.hap;
185
+ if (this.directionUseBool) {
186
+ return this.setStateAsync(this.dpFanDirection, value !== Characteristic.RotationDirection.COUNTER_CLOCKWISE);
187
+ }
156
188
  const tuyaVal = (value === Characteristic.RotationDirection.COUNTER_CLOCKWISE) ? 'reverse' : 'forward';
157
189
  return this.setStateAsync(this.dpFanDirection, tuyaVal);
158
190
  }
@@ -171,6 +203,15 @@ class SimpleFanAccessory extends BaseAccessory {
171
203
  return this.setStateAsync(this.dpSwing, value === Characteristic.SwingMode.SWING_ENABLED);
172
204
  }
173
205
 
206
+ getCurrentTemperature() {
207
+ return this._getCurrentTemperature(this.getStateAsync(this.dpCurrentTemperature));
208
+ }
209
+
210
+ _getCurrentTemperature(dp) {
211
+ const v = parseFloat(dp);
212
+ return isNaN(v) ? 0 : v;
213
+ }
214
+
174
215
  convertRotationSpeedFromTuyaToHomeKit(value) {
175
216
  const v = parseInt(value) || 0;
176
217
  if (v <= 0) return 0;
@@ -185,4 +226,4 @@ class SimpleFanAccessory extends BaseAccessory {
185
226
  }
186
227
  }
187
228
 
188
- module.exports = SimpleFanAccessory;
229
+ module.exports = SimpleFanAccessory;
@@ -41,8 +41,8 @@ class TWLightAccessory extends BaseAccessory {
41
41
 
42
42
  const characteristicColorTemperature = service.getCharacteristic(Characteristic.ColorTemperature)
43
43
  .setProps({
44
- minValue: this.device.context.minWhiteColor ?? DEFAULT_MIN_WHITE_COLOR,
45
- maxValue: this.device.context.maxWhiteColor ?? DEFAULT_MAX_WHITE_COLOR
44
+ minValue: this._coerceNumber(this.device.context.minWhiteColor, DEFAULT_MIN_WHITE_COLOR),
45
+ maxValue: this._coerceNumber(this.device.context.maxWhiteColor, DEFAULT_MAX_WHITE_COLOR)
46
46
  })
47
47
  .updateValue(this.convertColorTemperatureFromTuyaToHomeKit(dps[this.dpColorTemperature]))
48
48
  .onGet(() => this.getColorTemperature())
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "homebridge-tuya-plus",
3
- "version": "4.1.0-dev.76",
3
+ "version": "4.1.0-dev.78",
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": {
@@ -66,10 +66,13 @@ describe('convertColorTemperatureFromTuyaToHomeKit', () => {
66
66
  expect(instance.convertColorTemperatureFromTuyaToHomeKit(0)).toBe(400);
67
67
  });
68
68
 
69
- test('clamps result to [71, 600]', () => {
70
- const { instance } = make();
71
- expect(instance.convertColorTemperatureFromTuyaToHomeKit(255)).toBeGreaterThanOrEqual(71);
72
- expect(instance.convertColorTemperatureFromTuyaToHomeKit(0)).toBeLessThanOrEqual(600);
69
+ test('clamps result to the configured mired window', () => {
70
+ const { instance } = make({}, { minWhiteColor: 154, maxWhiteColor: 370 });
71
+ for (const tuya of [-500, 0, 128, 255, 5000]) {
72
+ const hk = instance.convertColorTemperatureFromTuyaToHomeKit(tuya);
73
+ expect(hk).toBeGreaterThanOrEqual(154);
74
+ expect(hk).toBeLessThanOrEqual(370);
75
+ }
73
76
  });
74
77
 
75
78
  test('round-trips with convertColorTemperatureFromHomeKitToTuya at boundaries', () => {
@@ -79,6 +82,38 @@ describe('convertColorTemperatureFromTuyaToHomeKit', () => {
79
82
  expect(instance.convertColorTemperatureFromTuyaToHomeKit(tuya)).toBe(hk);
80
83
  }
81
84
  });
85
+
86
+ // A missing/unusable data point used to convert to NaN, which HomeKit
87
+ // rejects with a characteristic warning on every read (issue #34).
88
+ test('falls back to the coolest mired for an unreported data point', () => {
89
+ const { instance } = make();
90
+ for (const unusable of [undefined, null, '', 'abc', true, {}]) {
91
+ expect(instance.convertColorTemperatureFromTuyaToHomeKit(unusable)).toBe(140);
92
+ }
93
+ });
94
+
95
+ test('reads a data point reported as a string', () => {
96
+ const { instance } = make();
97
+ expect(instance.convertColorTemperatureFromTuyaToHomeKit('255')).toBe(140);
98
+ expect(instance.convertColorTemperatureFromTuyaToHomeKit('0')).toBe(400);
99
+ });
100
+
101
+ test('accepts a mired window configured as strings', () => {
102
+ const { instance } = make({}, { minWhiteColor: '154', maxWhiteColor: '370' });
103
+ expect(instance.convertColorTemperatureFromTuyaToHomeKit(255)).toBe(154);
104
+ expect(instance.convertColorTemperatureFromTuyaToHomeKit(0)).toBe(370);
105
+ });
106
+ });
107
+
108
+ describe('convertColorTemperatureFromHomeKitToTuya', () => {
109
+ test('never returns NaN for an unusable value', () => {
110
+ const { instance } = make();
111
+ for (const unusable of [undefined, null, '', 'abc', NaN]) {
112
+ const tuya = instance.convertColorTemperatureFromHomeKitToTuya(unusable);
113
+ expect(Number.isFinite(tuya)).toBe(true);
114
+ expect(tuya).toBe(instance.convertColorTemperatureFromHomeKitToTuya(140));
115
+ }
116
+ });
82
117
  });
83
118
 
84
119
  // ---------------------------------------------------------------------------
@@ -0,0 +1,50 @@
1
+ 'use strict';
2
+
3
+ const OilDiffuserAccessory = require('../lib/OilDiffuserAccessory');
4
+ const { makeInstance, HAP } = require('./support/mocks');
5
+
6
+ // The dp/command fields are normally established by _registerCharacteristics,
7
+ // which only runs once the device reports its first state.
8
+ function makeDiffuser(state = {}, context = {}) {
9
+ const result = makeInstance(OilDiffuserAccessory, state, { colorFunction: 'HEXHSB', ...context });
10
+ const { instance, device } = result;
11
+
12
+ instance.dpLight = '5';
13
+ instance.dpMode = '6';
14
+ instance.dpColor = '8';
15
+ instance.dpColorTemperature = instance._getCustomDP(device.context.dpColorTemperature);
16
+ instance.cmdWhite = 'white';
17
+ instance.cmdColor = 'colour';
18
+ instance.colorFunction = 'HEXHSB';
19
+
20
+ return result;
21
+ }
22
+
23
+ // ---------------------------------------------------------------------------
24
+ // getColorTemperature
25
+ // ---------------------------------------------------------------------------
26
+ describe('OilDiffuserAccessory.getColorTemperature', () => {
27
+ // These diffusers have no colour-temperature dp; reading the undefined one
28
+ // returned NaN, which HomeKit rejects with a characteristic warning.
29
+ test('returns the neutral value in white mode when no dp is configured', () => {
30
+ const { instance } = makeDiffuser({ '6': 'white' });
31
+ expect(instance.getColorTemperature()).toBe(0);
32
+ });
33
+
34
+ test('returns the neutral value in color mode', () => {
35
+ const { instance } = makeDiffuser({ '6': 'colour' });
36
+ expect(instance.getColorTemperature()).toBe(0);
37
+ });
38
+
39
+ test('reads the configured dp in white mode', () => {
40
+ const { instance } = makeDiffuser({ '6': 'white', '4': 255 }, { dpColorTemperature: 4 });
41
+ // convertColorTemperatureFromTuyaToHomeKit(255) == 140
42
+ expect(instance.getColorTemperature()).toBe(140);
43
+ });
44
+
45
+ test('rejects (No Response) when the device is not connected', () => {
46
+ const { instance, device } = makeDiffuser({ '6': 'white' });
47
+ device.connected = false;
48
+ expect(() => instance.getColorTemperature()).toThrow(HAP.HapStatusError);
49
+ });
50
+ });
@@ -82,6 +82,16 @@ describe('RGBTWLightAccessory.getColorTemperature', () => {
82
82
  expect(typeof result).toBe('number');
83
83
  expect(Number.isFinite(result)).toBe(true);
84
84
  });
85
+
86
+ // HomeKit warns about every non-finite read, so a white-mode light whose
87
+ // colour-temperature dp hasn't been reported yet must still answer in range.
88
+ test('returns an in-range value when the color temperature dp is missing', () => {
89
+ const { instance } = makeLight({ '2': 'white' });
90
+ const result = instance.getColorTemperature();
91
+ expect(Number.isFinite(result)).toBe(true);
92
+ expect(result).toBeGreaterThanOrEqual(instance.minWhiteColor);
93
+ expect(result).toBeLessThanOrEqual(instance.maxWhiteColor);
94
+ });
85
95
  });
86
96
 
87
97
  // ---------------------------------------------------------------------------
@@ -1214,6 +1214,11 @@ Aroma diffusers and humidifiers with mist on/off, mist intensity (speed) and an
1214
1214
  /* Data-point for light colour / brightness value. Default: "8" */
1215
1215
  "dpColor": 8,
1216
1216
 
1217
+ /* Data-point for a colour-temperature value, if your diffuser reports one.
1218
+ Unset by default: these diffusers have no such data-point, and HomeKit's
1219
+ temperature slider is served by writing the matching colour instead. */
1220
+ "dpColorTemperature": 4,
1221
+
1217
1222
  /* Data-point for the water-level status. Default: "9" */
1218
1223
  "dpWaterLevel": 9,
1219
1224