homebridge-tuya-plus 3.14.0-dev.19 → 3.14.0-dev.21
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 +4 -3
- package/Readme.MD +4 -6
- package/config.schema.json +4 -13
- package/index.js +349 -359
- package/lib/IrrigationSystemAccessory.js +12 -23
- package/lib/SimpleGarageDoorAccessory.js +49 -8
- package/lib/TuyaCloudApi.js +21 -0
- package/lib/TuyaCloudDevice.js +86 -15
- package/lib/TuyaDevice.js +266 -0
- package/package.json +2 -2
- package/test/IrrigationSystemAccessory.test.js +44 -49
- package/test/SimpleGarageDoorAccessory.test.js +59 -7
- package/test/TuyaCloudApi.test.js +17 -0
- package/test/TuyaCloudDevice.test.js +49 -0
- package/test/TuyaDevice.test.js +252 -0
- package/test/index.test.js +141 -0
- package/wiki/Supported-Device-Types.md +5 -4
- package/wiki/Tuya-Cloud-Setup.md +15 -29
package/index.js
CHANGED
|
@@ -1,359 +1,349 @@
|
|
|
1
|
-
const TuyaAccessory = require('./lib/TuyaAccessory');
|
|
2
|
-
const
|
|
3
|
-
const
|
|
4
|
-
const
|
|
5
|
-
const TuyaCloudMessaging = require('./lib/TuyaCloudMessaging');
|
|
6
|
-
|
|
7
|
-
const OutletAccessory = require('./lib/OutletAccessory');
|
|
8
|
-
const SimpleLightAccessory = require('./lib/SimpleLightAccessory');
|
|
9
|
-
const MultiOutletAccessory = require('./lib/MultiOutletAccessory');
|
|
10
|
-
const CustomMultiOutletAccessory = require('./lib/CustomMultiOutletAccessory');
|
|
11
|
-
const RGBTWLightAccessory = require('./lib/RGBTWLightAccessory');
|
|
12
|
-
const RGBTWOutletAccessory = require('./lib/RGBTWOutletAccessory');
|
|
13
|
-
const TWLightAccessory = require('./lib/TWLightAccessory');
|
|
14
|
-
const AirConditionerAccessory = require('./lib/AirConditionerAccessory');
|
|
15
|
-
const AirPurifierAccessory = require('./lib/AirPurifierAccessory');
|
|
16
|
-
const DehumidifierAccessory = require('./lib/DehumidifierAccessory');
|
|
17
|
-
const ConvectorAccessory = require('./lib/ConvectorAccessory');
|
|
18
|
-
const GarageDoorAccessory = require('./lib/GarageDoorAccessory');
|
|
19
|
-
const SimpleGarageDoorAccessory = require('./lib/SimpleGarageDoorAccessory');
|
|
20
|
-
const WledDimmerAccessory = require('./lib/WledDimmerAccessory');
|
|
21
|
-
const SimpleDimmerAccessory = require('./lib/SimpleDimmerAccessory');
|
|
22
|
-
const SimpleDimmer2Accessory = require('./lib/SimpleDimmer2Accessory');
|
|
23
|
-
const SimpleBlindsAccessory = require('./lib/SimpleBlindsAccessory');
|
|
24
|
-
const SimpleHeaterAccessory = require('./lib/SimpleHeaterAccessory');
|
|
25
|
-
const SimpleFanAccessory = require('./lib/SimpleFanAccessory');
|
|
26
|
-
const SimpleFanLightAccessory = require('./lib/SimpleFanLightAccessory');
|
|
27
|
-
const SwitchAccessory = require('./lib/SwitchAccessory');
|
|
28
|
-
const ValveAccessory = require('./lib/ValveAccessory');
|
|
29
|
-
const IrrigationSystemAccessory = require('./lib/IrrigationSystemAccessory');
|
|
30
|
-
const OilDiffuserAccessory = require('./lib/OilDiffuserAccessory');
|
|
31
|
-
const DoorbellAccessory = require('./lib/DoorbellAccessory');
|
|
32
|
-
const VerticalBlindsWithTilt = require('./lib/VerticalBlindsWithTilt');
|
|
33
|
-
const PercentBlindsAccessory = require('./lib/PercentBlindsAccessory');
|
|
34
|
-
|
|
35
|
-
const PLUGIN_NAME = 'homebridge-tuya-plus';
|
|
36
|
-
const PLATFORM_NAME = 'TuyaLan';
|
|
37
|
-
// Seed used to derive accessory UUIDs. Must remain 'homebridge-tuya' so that
|
|
38
|
-
// devices already paired with HomeKit keep their existing identity (names,
|
|
39
|
-
// rooms, automations).
|
|
40
|
-
const UUID_SEED = 'homebridge-tuya';
|
|
41
|
-
const DEFAULT_DISCOVER_TIMEOUT = 60000;
|
|
42
|
-
|
|
43
|
-
// Lenient boolean coercion (matches BaseAccessory._coerceBoolean) so config
|
|
44
|
-
// values like true / "true" / 1 all read as true.
|
|
45
|
-
const coerceBoolean = (b, df = false) =>
|
|
46
|
-
typeof b === 'boolean' ? b :
|
|
47
|
-
typeof b === 'string' ? b.toLowerCase().trim() === 'true' :
|
|
48
|
-
typeof b === 'number' ? b !== 0 : df;
|
|
49
|
-
|
|
50
|
-
const CLASS_DEF = {
|
|
51
|
-
outlet: OutletAccessory,
|
|
52
|
-
simplelight: SimpleLightAccessory,
|
|
53
|
-
rgbtwlight: RGBTWLightAccessory,
|
|
54
|
-
rgbtwoutlet: RGBTWOutletAccessory,
|
|
55
|
-
twlight: TWLightAccessory,
|
|
56
|
-
multioutlet: MultiOutletAccessory,
|
|
57
|
-
custommultioutlet: CustomMultiOutletAccessory,
|
|
58
|
-
airconditioner: AirConditionerAccessory,
|
|
59
|
-
airpurifier: AirPurifierAccessory,
|
|
60
|
-
dehumidifier: DehumidifierAccessory,
|
|
61
|
-
convector: ConvectorAccessory,
|
|
62
|
-
garagedoor: GarageDoorAccessory,
|
|
63
|
-
simplegaragedoor: SimpleGarageDoorAccessory,
|
|
64
|
-
simpledimmer: SimpleDimmerAccessory,
|
|
65
|
-
wleddimmer: WledDimmerAccessory,
|
|
66
|
-
simpledimmer2: SimpleDimmer2Accessory,
|
|
67
|
-
simpleblinds: SimpleBlindsAccessory,
|
|
68
|
-
simpleheater: SimpleHeaterAccessory,
|
|
69
|
-
switch: SwitchAccessory,
|
|
70
|
-
fan: SimpleFanAccessory,
|
|
71
|
-
fanlight: SimpleFanLightAccessory,
|
|
72
|
-
watervalve: ValveAccessory,
|
|
73
|
-
irrigationsystem: IrrigationSystemAccessory,
|
|
74
|
-
oildiffuser: OilDiffuserAccessory,
|
|
75
|
-
doorbell: DoorbellAccessory,
|
|
76
|
-
verticalblindswithtilt: VerticalBlindsWithTilt,
|
|
77
|
-
percentblinds: PercentBlindsAccessory
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
let Characteristic, Formats, Perms, Categories, PlatformAccessory, Service, AdaptiveLightingController, UUID;
|
|
81
|
-
|
|
82
|
-
module.exports = function(homebridge) {
|
|
83
|
-
({
|
|
84
|
-
platformAccessory: PlatformAccessory,
|
|
85
|
-
hap: {Characteristic, Formats, Perms, Categories, Service, AdaptiveLightingController, uuid: UUID}
|
|
86
|
-
} = homebridge);
|
|
87
|
-
|
|
88
|
-
homebridge.registerPlatform(PLUGIN_NAME, PLATFORM_NAME, TuyaLan, true);
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
class TuyaLan {
|
|
92
|
-
constructor(...props) {
|
|
93
|
-
[this.log, this.config, this.api] = [...props];
|
|
94
|
-
|
|
95
|
-
this.cachedAccessories = new Map();
|
|
96
|
-
//
|
|
97
|
-
//
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
.
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
*
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
this.
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
if (
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
.
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
this.log.warn('Unregistering', homebridgeAccessory.displayName);
|
|
351
|
-
|
|
352
|
-
delete this.cachedAccessories[homebridgeAccessory.UUID];
|
|
353
|
-
this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [homebridgeAccessory]);
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
removeAccessoryByUUID(uuid) {
|
|
357
|
-
if (uuid) this.removeAccessory(this.cachedAccessories.get(uuid));
|
|
358
|
-
}
|
|
359
|
-
}
|
|
1
|
+
const TuyaAccessory = require('./lib/TuyaAccessory');
|
|
2
|
+
const TuyaDevice = require('./lib/TuyaDevice');
|
|
3
|
+
const TuyaDiscovery = require('./lib/TuyaDiscovery');
|
|
4
|
+
const TuyaCloudApi = require('./lib/TuyaCloudApi');
|
|
5
|
+
const TuyaCloudMessaging = require('./lib/TuyaCloudMessaging');
|
|
6
|
+
|
|
7
|
+
const OutletAccessory = require('./lib/OutletAccessory');
|
|
8
|
+
const SimpleLightAccessory = require('./lib/SimpleLightAccessory');
|
|
9
|
+
const MultiOutletAccessory = require('./lib/MultiOutletAccessory');
|
|
10
|
+
const CustomMultiOutletAccessory = require('./lib/CustomMultiOutletAccessory');
|
|
11
|
+
const RGBTWLightAccessory = require('./lib/RGBTWLightAccessory');
|
|
12
|
+
const RGBTWOutletAccessory = require('./lib/RGBTWOutletAccessory');
|
|
13
|
+
const TWLightAccessory = require('./lib/TWLightAccessory');
|
|
14
|
+
const AirConditionerAccessory = require('./lib/AirConditionerAccessory');
|
|
15
|
+
const AirPurifierAccessory = require('./lib/AirPurifierAccessory');
|
|
16
|
+
const DehumidifierAccessory = require('./lib/DehumidifierAccessory');
|
|
17
|
+
const ConvectorAccessory = require('./lib/ConvectorAccessory');
|
|
18
|
+
const GarageDoorAccessory = require('./lib/GarageDoorAccessory');
|
|
19
|
+
const SimpleGarageDoorAccessory = require('./lib/SimpleGarageDoorAccessory');
|
|
20
|
+
const WledDimmerAccessory = require('./lib/WledDimmerAccessory');
|
|
21
|
+
const SimpleDimmerAccessory = require('./lib/SimpleDimmerAccessory');
|
|
22
|
+
const SimpleDimmer2Accessory = require('./lib/SimpleDimmer2Accessory');
|
|
23
|
+
const SimpleBlindsAccessory = require('./lib/SimpleBlindsAccessory');
|
|
24
|
+
const SimpleHeaterAccessory = require('./lib/SimpleHeaterAccessory');
|
|
25
|
+
const SimpleFanAccessory = require('./lib/SimpleFanAccessory');
|
|
26
|
+
const SimpleFanLightAccessory = require('./lib/SimpleFanLightAccessory');
|
|
27
|
+
const SwitchAccessory = require('./lib/SwitchAccessory');
|
|
28
|
+
const ValveAccessory = require('./lib/ValveAccessory');
|
|
29
|
+
const IrrigationSystemAccessory = require('./lib/IrrigationSystemAccessory');
|
|
30
|
+
const OilDiffuserAccessory = require('./lib/OilDiffuserAccessory');
|
|
31
|
+
const DoorbellAccessory = require('./lib/DoorbellAccessory');
|
|
32
|
+
const VerticalBlindsWithTilt = require('./lib/VerticalBlindsWithTilt');
|
|
33
|
+
const PercentBlindsAccessory = require('./lib/PercentBlindsAccessory');
|
|
34
|
+
|
|
35
|
+
const PLUGIN_NAME = 'homebridge-tuya-plus';
|
|
36
|
+
const PLATFORM_NAME = 'TuyaLan';
|
|
37
|
+
// Seed used to derive accessory UUIDs. Must remain 'homebridge-tuya' so that
|
|
38
|
+
// devices already paired with HomeKit keep their existing identity (names,
|
|
39
|
+
// rooms, automations).
|
|
40
|
+
const UUID_SEED = 'homebridge-tuya';
|
|
41
|
+
const DEFAULT_DISCOVER_TIMEOUT = 60000;
|
|
42
|
+
|
|
43
|
+
// Lenient boolean coercion (matches BaseAccessory._coerceBoolean) so config
|
|
44
|
+
// values like true / "true" / 1 all read as true.
|
|
45
|
+
const coerceBoolean = (b, df = false) =>
|
|
46
|
+
typeof b === 'boolean' ? b :
|
|
47
|
+
typeof b === 'string' ? b.toLowerCase().trim() === 'true' :
|
|
48
|
+
typeof b === 'number' ? b !== 0 : df;
|
|
49
|
+
|
|
50
|
+
const CLASS_DEF = {
|
|
51
|
+
outlet: OutletAccessory,
|
|
52
|
+
simplelight: SimpleLightAccessory,
|
|
53
|
+
rgbtwlight: RGBTWLightAccessory,
|
|
54
|
+
rgbtwoutlet: RGBTWOutletAccessory,
|
|
55
|
+
twlight: TWLightAccessory,
|
|
56
|
+
multioutlet: MultiOutletAccessory,
|
|
57
|
+
custommultioutlet: CustomMultiOutletAccessory,
|
|
58
|
+
airconditioner: AirConditionerAccessory,
|
|
59
|
+
airpurifier: AirPurifierAccessory,
|
|
60
|
+
dehumidifier: DehumidifierAccessory,
|
|
61
|
+
convector: ConvectorAccessory,
|
|
62
|
+
garagedoor: GarageDoorAccessory,
|
|
63
|
+
simplegaragedoor: SimpleGarageDoorAccessory,
|
|
64
|
+
simpledimmer: SimpleDimmerAccessory,
|
|
65
|
+
wleddimmer: WledDimmerAccessory,
|
|
66
|
+
simpledimmer2: SimpleDimmer2Accessory,
|
|
67
|
+
simpleblinds: SimpleBlindsAccessory,
|
|
68
|
+
simpleheater: SimpleHeaterAccessory,
|
|
69
|
+
switch: SwitchAccessory,
|
|
70
|
+
fan: SimpleFanAccessory,
|
|
71
|
+
fanlight: SimpleFanLightAccessory,
|
|
72
|
+
watervalve: ValveAccessory,
|
|
73
|
+
irrigationsystem: IrrigationSystemAccessory,
|
|
74
|
+
oildiffuser: OilDiffuserAccessory,
|
|
75
|
+
doorbell: DoorbellAccessory,
|
|
76
|
+
verticalblindswithtilt: VerticalBlindsWithTilt,
|
|
77
|
+
percentblinds: PercentBlindsAccessory
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
let Characteristic, Formats, Perms, Categories, PlatformAccessory, Service, AdaptiveLightingController, UUID;
|
|
81
|
+
|
|
82
|
+
module.exports = function(homebridge) {
|
|
83
|
+
({
|
|
84
|
+
platformAccessory: PlatformAccessory,
|
|
85
|
+
hap: {Characteristic, Formats, Perms, Categories, Service, AdaptiveLightingController, uuid: UUID}
|
|
86
|
+
} = homebridge);
|
|
87
|
+
|
|
88
|
+
homebridge.registerPlatform(PLUGIN_NAME, PLATFORM_NAME, TuyaLan, true);
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
class TuyaLan {
|
|
92
|
+
constructor(...props) {
|
|
93
|
+
[this.log, this.config, this.api] = [...props];
|
|
94
|
+
|
|
95
|
+
this.cachedAccessories = new Map();
|
|
96
|
+
// One TuyaDevice per configured device, keyed by Tuya id, so discovery can
|
|
97
|
+
// hand each its LAN target once it's found on the network.
|
|
98
|
+
this.tuyaDevices = new Map();
|
|
99
|
+
// A SINGLE shared Tuya Cloud session (OpenAPI token + realtime MQTT) for the
|
|
100
|
+
// whole platform — the global fallback every device can lean on. Stays null
|
|
101
|
+
// unless cloud credentials are configured.
|
|
102
|
+
this.cloudApi = null;
|
|
103
|
+
this.cloudMessaging = null;
|
|
104
|
+
this.api.hap.EnergyCharacteristics = require('./lib/EnergyCharacteristics')(this.api.hap);
|
|
105
|
+
|
|
106
|
+
if(!this.config || !this.config.devices) {
|
|
107
|
+
this.log("No devices found. Check that you have specified them in your config.json file.");
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
this._expectedUUIDs = this.config.devices.map(device => UUID.generate(UUID_SEED +(device.fake ? ':fake:' : ':') + device.id));
|
|
112
|
+
|
|
113
|
+
this.api.on('didFinishLaunching', () => {
|
|
114
|
+
this.discoverDevices();
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
discoverDevices() {
|
|
119
|
+
// Bring up the single shared cloud session first, so every device that opts
|
|
120
|
+
// into (or falls back to) the cloud shares one token and one MQTT stream.
|
|
121
|
+
this._setupCloudSession();
|
|
122
|
+
|
|
123
|
+
const lanDeviceIds = []; // ids we still want to find on the LAN
|
|
124
|
+
const connectedDevices = []; // ids discovered on the LAN
|
|
125
|
+
const fakeDevices = [];
|
|
126
|
+
|
|
127
|
+
this.config.devices.forEach(device => {
|
|
128
|
+
try {
|
|
129
|
+
device.id = ('' + device.id).trim();
|
|
130
|
+
// Cloud-only devices don't need a local key; only trim when present.
|
|
131
|
+
if (device.key != null) device.key = ('' + device.key).trim();
|
|
132
|
+
device.type = ('' + device.type).trim();
|
|
133
|
+
|
|
134
|
+
device.ip = ('' + (device.ip || '')).trim();
|
|
135
|
+
} catch(ex) {}
|
|
136
|
+
|
|
137
|
+
if (!device.type) return this.log.error('%s (%s) doesn\'t have a type defined.', device.name || 'Unnamed device', device.id);
|
|
138
|
+
if (!CLASS_DEF[device.type.toLowerCase()]) return this.log.error('%s (%s) doesn\'t have a valid type defined.', device.name || 'Unnamed device', device.id);
|
|
139
|
+
|
|
140
|
+
if (device.fake) {
|
|
141
|
+
fakeDevices.push({name: device.id.slice(8), ...device});
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const tuyaDevice = this._createDevice({name: device.id.slice(8), ...device});
|
|
146
|
+
this.tuyaDevices.set(device.id, tuyaDevice);
|
|
147
|
+
this.addAccessory(tuyaDevice);
|
|
148
|
+
|
|
149
|
+
// A device with a local key can be reached on the LAN, so it wants
|
|
150
|
+
// discovery; the cloud, when configured, is its fallback. A device with
|
|
151
|
+
// no key is cloud-only (e.g. a battery-powered "sleepy" unit) and relies
|
|
152
|
+
// on the cloud session to be reachable at all.
|
|
153
|
+
if (device.key) lanDeviceIds.push(device.id);
|
|
154
|
+
else if (!this.cloudApi) this.log.error('%s (%s) has no local key and no Tuya Cloud session is configured, so it can\'t be reached. Add a key for LAN control, or a top-level "cloud" block.', tuyaDevice.context.name, device.id);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
fakeDevices.forEach(config => {
|
|
158
|
+
this.log.info('Adding fake device: %s', config.name);
|
|
159
|
+
this.addAccessory(new TuyaAccessory({
|
|
160
|
+
...config,
|
|
161
|
+
log: this.log,
|
|
162
|
+
UUID: UUID.generate(UUID_SEED + ':fake:' + config.id),
|
|
163
|
+
connect: false
|
|
164
|
+
}));
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
if (lanDeviceIds.length === 0) {
|
|
168
|
+
if (this.tuyaDevices.size === 0 && fakeDevices.length === 0) this.log.error('No valid configured devices found.');
|
|
169
|
+
return; // cloud-only (or empty) configuration: nothing to discover over the LAN
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
this.log.info('Starting discovery...');
|
|
173
|
+
|
|
174
|
+
TuyaDiscovery.start({ids: lanDeviceIds, log: this.log})
|
|
175
|
+
.on('discover', config => {
|
|
176
|
+
if (!config || !config.id) return;
|
|
177
|
+
const tuyaDevice = this.tuyaDevices.get(config.id);
|
|
178
|
+
if (!tuyaDevice) return this.log.warn('Discovered a device that has not been configured yet (%s@%s).', config.id, config.ip);
|
|
179
|
+
if (connectedDevices.includes(config.id)) return;
|
|
180
|
+
|
|
181
|
+
connectedDevices.push(config.id);
|
|
182
|
+
|
|
183
|
+
this.log.info('Discovered %s (%s) identified as %s (%s)', tuyaDevice.context.name, config.id, tuyaDevice.context.type, config.version);
|
|
184
|
+
|
|
185
|
+
// The version broadcast by the device wins over a configured `version`,
|
|
186
|
+
// but `forceVersion` overrides everything; attachLan applies that order.
|
|
187
|
+
tuyaDevice.attachLan({ip: config.ip, version: config.version});
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
setTimeout(() => {
|
|
191
|
+
lanDeviceIds.forEach(deviceId => {
|
|
192
|
+
if (connectedDevices.includes(deviceId)) return;
|
|
193
|
+
|
|
194
|
+
const tuyaDevice = this.tuyaDevices.get(deviceId);
|
|
195
|
+
if (!tuyaDevice) return;
|
|
196
|
+
|
|
197
|
+
if (tuyaDevice.context.ip) {
|
|
198
|
+
this.log.info('Failed to discover %s (%s) in time but will connect via %s.', tuyaDevice.context.name, deviceId, tuyaDevice.context.ip);
|
|
199
|
+
tuyaDevice.attachLan({ip: tuyaDevice.context.ip});
|
|
200
|
+
} else if (tuyaDevice.cloud) {
|
|
201
|
+
this.log.info('Failed to discover %s (%s) on the LAN; it will run over the Tuya Cloud fallback.', tuyaDevice.context.name, deviceId);
|
|
202
|
+
} else {
|
|
203
|
+
this.log.warn('Failed to discover %s (%s) in time but will keep looking.', tuyaDevice.context.name, deviceId);
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
}, this.config.discoverTimeout ?? DEFAULT_DISCOVER_TIMEOUT);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/* ------------------------------------------------------------------ *
|
|
210
|
+
* Tuya Cloud — a single, global fallback session.
|
|
211
|
+
*
|
|
212
|
+
* This plugin stays LAN-first: every device is controlled locally when it
|
|
213
|
+
* can be. When a top-level `cloud` block is configured, the plugin keeps one
|
|
214
|
+
* shared Tuya Cloud session alive in the background, and every device gains a
|
|
215
|
+
* transparent cloud fallback for the moments the LAN can't be reached (a
|
|
216
|
+
* flaky connection, or a battery-powered "sleepy" device that never appears
|
|
217
|
+
* on the LAN at all). It's all opt-in — without `cloud`, nothing here runs.
|
|
218
|
+
* ------------------------------------------------------------------ */
|
|
219
|
+
|
|
220
|
+
_setupCloudSession() {
|
|
221
|
+
const cloudCfg = (this.config.cloud && typeof this.config.cloud === 'object') ? this.config.cloud : null;
|
|
222
|
+
if (!cloudCfg) return; // no cloud block: the plugin runs purely on the LAN
|
|
223
|
+
|
|
224
|
+
if (!cloudCfg.accessId || !cloudCfg.accessKey) {
|
|
225
|
+
return this.log.error('A "cloud" block is present but is missing accessId/accessKey, so the Tuya Cloud fallback is disabled. See the wiki: Tuya Cloud Setup.');
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
this.cloudApi = new TuyaCloudApi({...cloudCfg, log: this.log});
|
|
229
|
+
|
|
230
|
+
const realtime = cloudCfg.realtime === undefined ? true : coerceBoolean(cloudCfg.realtime, true);
|
|
231
|
+
this.cloudMessaging = realtime ? new TuyaCloudMessaging({api: this.cloudApi, log: this.log}) : null;
|
|
232
|
+
|
|
233
|
+
this.log.info('Tuya Cloud fallback enabled via %s%s.', this.cloudApi.endpoint, this.cloudMessaging ? ' (with realtime updates)' : ' (realtime updates disabled)');
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
_createDevice(device) {
|
|
237
|
+
// The one shared cloud session, when configured, backs every device as its
|
|
238
|
+
// fallback. There is no per-device cloud configuration.
|
|
239
|
+
const usesCloud = !!this.cloudApi;
|
|
240
|
+
return new TuyaDevice({
|
|
241
|
+
...device,
|
|
242
|
+
cloudApi: usesCloud ? this.cloudApi : undefined,
|
|
243
|
+
messaging: usesCloud ? this.cloudMessaging : undefined,
|
|
244
|
+
cloudStartDelay: usesCloud ? this._nextCloudStartDelay() : 0,
|
|
245
|
+
log: this.log,
|
|
246
|
+
UUID: UUID.generate(UUID_SEED + ':' + device.id),
|
|
247
|
+
connect: false
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// Spread the cloud devices' first reads over a few seconds so a large install
|
|
252
|
+
// doesn't fire dozens of OpenAPI calls at once and trip Tuya's per-second rate
|
|
253
|
+
// limit at startup.
|
|
254
|
+
_nextCloudStartDelay() {
|
|
255
|
+
this._cloudStartCount = (this._cloudStartCount || 0) + 1;
|
|
256
|
+
return Math.min(15000, (this._cloudStartCount - 1) * 300);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
registerPlatformAccessories(platformAccessories) {
|
|
260
|
+
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, Array.isArray(platformAccessories) ? platformAccessories : [platformAccessories]);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
configureAccessory(accessory) {
|
|
264
|
+
// also checks null objects or empty config - this._expectedUUIDs
|
|
265
|
+
if (accessory instanceof PlatformAccessory && this._expectedUUIDs && this._expectedUUIDs.includes(accessory.UUID)) {
|
|
266
|
+
this.cachedAccessories.set(accessory.UUID, accessory);
|
|
267
|
+
accessory.services.forEach(service => {
|
|
268
|
+
if (service.UUID === Service.AccessoryInformation.UUID) return;
|
|
269
|
+
service.characteristics.some(characteristic => {
|
|
270
|
+
if (!characteristic.props ||
|
|
271
|
+
!Array.isArray(characteristic.props.perms) ||
|
|
272
|
+
characteristic.props.perms.length !== 3 ||
|
|
273
|
+
!(characteristic.props.perms.includes(Perms.WRITE) && characteristic.props.perms.includes(Perms.NOTIFY))
|
|
274
|
+
) return;
|
|
275
|
+
|
|
276
|
+
this.log.info('Marked %s unreachable by faulting Service.%s.%s', accessory.displayName, service.displayName, characteristic.displayName);
|
|
277
|
+
|
|
278
|
+
characteristic.updateValue(new Error('Unreachable'));
|
|
279
|
+
return true;
|
|
280
|
+
});
|
|
281
|
+
});
|
|
282
|
+
} else {
|
|
283
|
+
/*
|
|
284
|
+
* Irrespective of this unregistering, Homebridge continues
|
|
285
|
+
* to "_prepareAssociatedHAPAccessory" and "addBridgedAccessory".
|
|
286
|
+
* This timeout will hopefully remove the accessory after that has happened.
|
|
287
|
+
*/
|
|
288
|
+
setTimeout(() => {
|
|
289
|
+
this.removeAccessory(accessory);
|
|
290
|
+
}, 1000);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
addAccessory(device) {
|
|
295
|
+
const deviceConfig = device.context;
|
|
296
|
+
const type = (deviceConfig.type || '').toLowerCase();
|
|
297
|
+
|
|
298
|
+
const Accessory = CLASS_DEF[type];
|
|
299
|
+
|
|
300
|
+
let accessory = this.cachedAccessories.get(deviceConfig.UUID),
|
|
301
|
+
isCached = true;
|
|
302
|
+
|
|
303
|
+
const expectedCategory = Accessory.getCategory(Categories);
|
|
304
|
+
|
|
305
|
+
// Only treat a cached accessory as a "different type" when we actually
|
|
306
|
+
// have a category to compare against. If getCategory() resolves to
|
|
307
|
+
// undefined (e.g. an unknown HAP category constant), HomeKit stores the
|
|
308
|
+
// accessory as Categories.OTHER, so an undefined expectation would never
|
|
309
|
+
// match and we would needlessly unregister & recreate the accessory on
|
|
310
|
+
// every restart — wiping its HomeKit identity (name, room, automations).
|
|
311
|
+
if (accessory && expectedCategory !== undefined && accessory.category !== expectedCategory) {
|
|
312
|
+
this.log.info("%s has a different type (%s vs %s)", accessory.displayName, accessory.category, expectedCategory);
|
|
313
|
+
this.removeAccessory(accessory);
|
|
314
|
+
accessory = null;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
if (!accessory) {
|
|
318
|
+
accessory = new PlatformAccessory(deviceConfig.name, deviceConfig.UUID, expectedCategory);
|
|
319
|
+
accessory.getService(Service.AccessoryInformation)
|
|
320
|
+
.setCharacteristic(Characteristic.Manufacturer, deviceConfig.manufacturer || "Unknown")
|
|
321
|
+
.setCharacteristic(Characteristic.Model, deviceConfig.model || "Unknown")
|
|
322
|
+
.setCharacteristic(Characteristic.SerialNumber, deviceConfig.id.slice(8));
|
|
323
|
+
|
|
324
|
+
isCached = false;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
if (accessory && accessory.displayName !== deviceConfig.name) {
|
|
328
|
+
this.log.info(
|
|
329
|
+
"Configuration name %s differs from cached displayName %s. Updating cached displayName to %s ",
|
|
330
|
+
deviceConfig.name, accessory.displayName, deviceConfig.name);
|
|
331
|
+
accessory.displayName = deviceConfig.name;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
this.cachedAccessories.set(deviceConfig.UUID, new Accessory(this, accessory, device, !isCached));
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
removeAccessory(homebridgeAccessory) {
|
|
338
|
+
if (!homebridgeAccessory) return;
|
|
339
|
+
|
|
340
|
+
this.log.warn('Unregistering', homebridgeAccessory.displayName);
|
|
341
|
+
|
|
342
|
+
delete this.cachedAccessories[homebridgeAccessory.UUID];
|
|
343
|
+
this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [homebridgeAccessory]);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
removeAccessoryByUUID(uuid) {
|
|
347
|
+
if (uuid) this.removeAccessory(this.cachedAccessories.get(uuid));
|
|
348
|
+
}
|
|
349
|
+
}
|