-
Notifications
You must be signed in to change notification settings - Fork 279
/
device.ts
executable file
·718 lines (637 loc) · 32.6 KB
/
device.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
import {KeyValue, DatabaseEntry, DeviceType} from '../tstype';
import {Events as AdapterEvents} from '../../adapter';
import Endpoint from './endpoint';
import Entity from './entity';
import {Wait} from '../../utils';
import Debug from "debug";
import * as Zcl from '../../zcl';
import assert from 'assert';
/**
* @ignore
*/
const OneJanuary2000 = new Date('January 01, 2000 00:00:00 UTC+00:00').getTime();
const debug = {
error: Debug('zigbee-herdsman:controller:device:error'),
log: Debug('zigbee-herdsman:controller:device:log'),
};
interface LQI {
neighbors: {
ieeeAddr: string; networkAddress: number; linkquality: number;
relationship: number; depth: number;
}[];
}
interface RoutingTable {
table: {destinationAddress: number; status: string; nextHop: number}[];
}
class Device extends Entity {
private readonly ID: number;
private _applicationVersion?: number;
private _dateCode?: string;
private _endpoints: Endpoint[];
private _hardwareVersion?: number;
private _ieeeAddr: string;
private _interviewCompleted: boolean;
private _interviewing: boolean;
private _lastSeen: number;
private _manufacturerID?: number;
private _manufacturerName?: string;
private _modelID?: string;
private _networkAddress: number;
private _powerSource?: string;
private _softwareBuildID?: string;
private _stackVersion?: number;
private _type?: DeviceType;
private _zclVersion?: number;
private _linkquality?: number;
private _skipDefaultResponse: boolean;
private _skipTimeResponse: boolean;
private _deleted: boolean;
// Getters/setters
get ieeeAddr(): string {return this._ieeeAddr;}
set ieeeAddr(ieeeAddr: string) {this._ieeeAddr = ieeeAddr;}
get applicationVersion(): number {return this._applicationVersion;}
set applicationVersion(applicationVersion: number) {this._applicationVersion = applicationVersion;}
get endpoints(): Endpoint[] {return this._endpoints;}
get interviewCompleted(): boolean {return this._interviewCompleted;}
get interviewing(): boolean {return this._interviewing;}
get lastSeen(): number {return this._lastSeen;}
get manufacturerID(): number {return this._manufacturerID;}
get isDeleted(): boolean {return this._deleted;}
set type(type: DeviceType) {this._type = type;}
get type(): DeviceType {return this._type;}
get dateCode(): string {return this._dateCode;}
set dateCode(dateCode: string) {this._dateCode = dateCode;}
set hardwareVersion(hardwareVersion: number) {this._hardwareVersion = hardwareVersion;}
get hardwareVersion(): number {return this._hardwareVersion;}
get manufacturerName(): string {return this._manufacturerName;}
set manufacturerName(manufacturerName: string) {this._manufacturerName = manufacturerName;}
set modelID(modelID: string) {this._modelID = modelID;}
get modelID(): string {return this._modelID;}
get networkAddress(): number {return this._networkAddress;}
set networkAddress(networkAddress: number) {
this._networkAddress = networkAddress;
for (const endpoint of this._endpoints) {
endpoint.deviceNetworkAddress = networkAddress;
}
}
get powerSource(): string {return this._powerSource;}
set powerSource(powerSource: string) {
this._powerSource = typeof powerSource === 'number' ? Zcl.PowerSource[powerSource] : powerSource;
}
get softwareBuildID(): string {return this._softwareBuildID;}
set softwareBuildID(softwareBuildID: string) {this._softwareBuildID = softwareBuildID;}
get stackVersion(): number {return this._stackVersion;}
set stackVersion(stackVersion: number) {this._stackVersion = stackVersion;}
get zclVersion(): number {return this._zclVersion;}
set zclVersion(zclVersion: number) {this._zclVersion = zclVersion;}
get linkquality(): number {return this._linkquality;}
set linkquality(linkquality: number) {this._linkquality = linkquality;}
get skipDefaultResponse(): boolean {return this._skipDefaultResponse;}
set skipDefaultResponse(skipDefaultResponse: boolean) {this._skipDefaultResponse = skipDefaultResponse;}
get skipTimeResponse(): boolean {return this._skipTimeResponse;}
set skipTimeResponse(skipTimeResponse: boolean) {this._skipTimeResponse = skipTimeResponse;}
public meta: KeyValue;
private useImplicitCheckin: boolean;
// This lookup contains all devices that are queried from the database, this is to ensure that always
// the same instance is returned.
private static devices: {[ieeeAddr: string]: Device} = null;
public static readonly ReportablePropertiesMapping: {[s: string]: {
set: (value: string | number, device: Device) => void;
key: 'modelID' | 'manufacturerName' | 'applicationVersion' | 'zclVersion' | 'powerSource' | 'stackVersion' |
'dateCode' | 'softwareBuildID' | 'hardwareVersion';
};} = {
modelId: {key: 'modelID', set: (v: string, d: Device): void => {d.modelID = v;}},
manufacturerName: {key: 'manufacturerName', set: (v: string, d: Device): void => {d.manufacturerName = v;}},
powerSource: {key: 'powerSource', set: (v: string, d: Device): void => {d.powerSource = v;}},
zclVersion: {key: 'zclVersion', set: (v: number, d: Device): void => {d.zclVersion = v;}},
appVersion: {key: 'applicationVersion', set: (v: number, d: Device): void => {d.applicationVersion = v;}},
stackVersion: {key: 'stackVersion', set: (v: number, d: Device): void => {d.stackVersion = v;}},
hwVersion: {key: 'hardwareVersion', set: (v: number, d: Device): void => {d.hardwareVersion = v;}},
dateCode: {key: 'dateCode', set: (v: string, d: Device): void => {d.dateCode = v;}},
swBuildId: {key: 'softwareBuildID', set: (v: string, d: Device): void => {d.softwareBuildID = v;}},
};
private constructor(
ID: number, type: DeviceType, ieeeAddr: string, networkAddress: number,
manufacturerID: number, endpoints: Endpoint[], manufacturerName: string,
powerSource: string, modelID: string, applicationVersion: number, stackVersion: number, zclVersion: number,
hardwareVersion: number, dateCode: string, softwareBuildID: string, interviewCompleted: boolean, meta: KeyValue,
lastSeen: number, useImplicitCheckin: boolean,
) {
super();
this.ID = ID;
this._type = type;
this.ieeeAddr = ieeeAddr;
this._networkAddress = networkAddress;
this._manufacturerID = manufacturerID;
this._endpoints = endpoints;
this._manufacturerName = manufacturerName;
this._powerSource = powerSource;
this._modelID = modelID;
this._applicationVersion = applicationVersion;
this._stackVersion = stackVersion;
this._zclVersion = zclVersion;
this.hardwareVersion = hardwareVersion;
this._dateCode = dateCode;
this._softwareBuildID = softwareBuildID;
this._interviewCompleted = interviewCompleted;
this._interviewing = false;
this._skipDefaultResponse = false;
this._skipTimeResponse = false;
this.meta = meta;
this._lastSeen = lastSeen;
this.useImplicitCheckin = useImplicitCheckin;
}
public createEndpoint(ID: number): Endpoint {
if (this.getEndpoint(ID)) {
throw new Error(`Device '${this.ieeeAddr}' already has an endpoint '${ID}'`);
}
const endpoint = Endpoint.create(ID, undefined, undefined, [], [], this.networkAddress, this.ieeeAddr);
this.endpoints.push(endpoint);
this.save();
return endpoint;
}
public changeIeeeAddress(ieeeAddr: string): void {
delete Device.devices[this.ieeeAddr];
this.ieeeAddr = ieeeAddr;
Device.devices[this.ieeeAddr] = this;
this.save();
}
public getEndpoint(ID: number): Endpoint {
return this.endpoints.find((e): boolean => e.ID === ID);
}
// There might be multiple endpoints with same DeviceId but it is not supported and first endpoint is returned
public getEndpointByDeviceType(deviceType: string): Endpoint {
const deviceID = Zcl.EndpointDeviceType[deviceType];
return this.endpoints.find((d): boolean => d.deviceID === deviceID);
}
public implicitCheckin(): void {
if (!this.useImplicitCheckin) {
return;
}
this.endpoints.forEach(async e => e.sendPendingRequests());
}
public updateLastSeen(): void {
this._lastSeen = Date.now();
}
private hasPendingRequests(): boolean {
return this.endpoints.find(e => e.hasPendingRequests()) !== undefined;
}
public async configurePollControl(coordinatorEndpoint: Endpoint, enable: boolean): Promise<void> {
const endpoint = this.getEndpoint(1);
if (enable) {
await endpoint.bind('genPollCtrl', coordinatorEndpoint);
} else {
await endpoint.unbind('genPollCtrl', coordinatorEndpoint);
}
this.useImplicitCheckin = !enable;
this.save();
}
public async onZclData(dataPayload: AdapterEvents.ZclDataPayload, endpoint: Endpoint): Promise<void> {
const frame = dataPayload.frame;
// Respond to enroll requests
if (frame.isSpecific() && frame.isCluster('ssIasZone') && frame.isCommand('enrollReq')) {
debug.log(`IAS - '${this.ieeeAddr}' responding to enroll response`);
const payload = {enrollrspcode: 0, zoneid: 23};
await endpoint.command('ssIasZone', 'enrollRsp', payload, {disableDefaultResponse: true});
}
// Reponse to read requests
if (frame.isGlobal() && frame.isCommand('read')) {
const time = Math.round(((new Date()).getTime() - OneJanuary2000) / 1000);
const attributes: {[s: string]: KeyValue} = {
...endpoint.clusters,
genTime: {attributes: {
timeStatus: 3, // Time-master + synchronised
time: time,
timeZone: ((new Date()).getTimezoneOffset() * -1) * 60,
localTime: time - (new Date()).getTimezoneOffset() * 60,
}},
};
if (frame.Cluster.name in attributes && (frame.Cluster.name !== 'genTime' || !this._skipTimeResponse)) {
const response: KeyValue = {};
for (const entry of frame.Payload) {
const name = frame.Cluster.getAttribute(entry.attrId).name;
if (name in attributes[frame.Cluster.name].attributes) {
response[name] = attributes[frame.Cluster.name].attributes[name];
}
}
try {
await endpoint.readResponse(frame.Cluster.ID, frame.Header.transactionSequenceNumber, response,
{srcEndpoint: dataPayload.destinationEndpoint});
} catch (error) {
debug.error(`Read response to ${this.ieeeAddr} failed`);
}
}
}
// Handle check-in from sleeping end devices
if (frame.isSpecific() && frame.isCluster("genPollCtrl") && frame.isCommand("checkin")) {
try {
if (this.hasPendingRequests()) {
const payload = {
startFastPolling: true,
fastPollTimeout: 0,
};
debug.log(`check-in from ${this.ieeeAddr}: accepting fast-poll`);
await endpoint.command(frame.Cluster.ID, 'checkinRsp', payload, {sendWhenActive: false});
await Promise.all(this.endpoints.map(async e => e.sendPendingRequests()));
// We *must* end fast-poll when we're done sending things. Otherwise
// we cause undue power-drain.
debug.log(`check-in from ${this.ieeeAddr}: stopping fast-poll`);
await endpoint.command(frame.Cluster.ID, 'fastPollStop', {}, {sendWhenActive: false});
} else {
const payload = {
startFastPolling: false,
fastPollTimeout: 0,
};
debug.log(`check-in from ${this.ieeeAddr}: declining fast-poll`);
await endpoint.command(frame.Cluster.ID, 'checkinRsp', payload, {sendWhenActive: false});
}
} catch (error) {
/* istanbul ignore next */
debug.error(`Handling of poll check-in form ${this.ieeeAddr} failed`);
}
}
// Send a default response if necessary.
const isDefaultResponse = frame.isGlobal() && frame.getCommand().name === 'defaultRsp';
const commandHasResponse = frame.getCommand().hasOwnProperty('response');
const disableDefaultResponse = frame.Header.frameControl.disableDefaultResponse;
if (!dataPayload.wasBroadcast && !disableDefaultResponse && !isDefaultResponse && !commandHasResponse &&
!this._skipDefaultResponse) {
try {
await endpoint.defaultResponse(
frame.getCommand().ID, 0, frame.Cluster.ID, frame.Header.transactionSequenceNumber,
);
} catch (error) {
debug.error(`Default response to ${this.ieeeAddr} failed`);
}
}
}
/*
* CRUD
*/
private static fromDatabaseEntry(entry: DatabaseEntry): Device {
const networkAddress = entry.nwkAddr;
const ieeeAddr = entry.ieeeAddr;
const endpoints = Object.values(entry.endpoints).map((e): Endpoint => {
return Endpoint.fromDatabaseRecord(e, networkAddress, ieeeAddr);
});
const meta = entry.meta ? entry.meta : {};
if (entry.type === 'Group') {
throw new Error('Cannot load device from group');
}
return new Device(
entry.id, entry.type, ieeeAddr, networkAddress, entry.manufId, endpoints,
entry.manufName, entry.powerSource, entry.modelId, entry.appVersion,
entry.stackVersion, entry.zclVersion, entry.hwVersion, entry.dateCode, entry.swBuildId,
entry.interviewCompleted, meta, entry.lastSeen || null, entry.useImplicitCheckin,
);
}
private toDatabaseEntry(): DatabaseEntry {
const epList = this.endpoints.map((e): number => e.ID);
const endpoints: KeyValue = {};
for (const endpoint of this.endpoints) {
endpoints[endpoint.ID] = endpoint.toDatabaseRecord();
}
return {
id: this.ID, type: this.type, ieeeAddr: this.ieeeAddr, nwkAddr: this.networkAddress,
manufId: this.manufacturerID, manufName: this.manufacturerName, powerSource: this.powerSource,
modelId: this.modelID, epList, endpoints, appVersion: this.applicationVersion,
stackVersion: this.stackVersion, hwVersion: this.hardwareVersion, dateCode: this.dateCode,
swBuildId: this.softwareBuildID, zclVersion: this.zclVersion, interviewCompleted: this.interviewCompleted,
meta: this.meta, lastSeen: this.lastSeen, useImplicitCheckin: this.useImplicitCheckin,
};
}
public save(writeDatabase=true): void {
Entity.database.update(this.toDatabaseEntry(), writeDatabase);
}
private static loadFromDatabaseIfNecessary(): void {
if (!Device.devices) {
Device.devices = {};
const entries = Entity.database.getEntries(['Coordinator', 'EndDevice', 'Router', 'GreenPower', 'Unknown']);
for (const entry of entries) {
const device = Device.fromDatabaseEntry(entry);
Device.devices[device.ieeeAddr] = device;
}
}
}
public static byIeeeAddr(ieeeAddr: string, includeDeleted=false): Device {
Device.loadFromDatabaseIfNecessary();
const device = Device.devices[ieeeAddr];
return device?._deleted && !includeDeleted ? undefined : device;
}
public static byNetworkAddress(networkAddress: number): Device {
return Device.all().find(d => d.networkAddress === networkAddress);
}
public static byType(type: DeviceType): Device[] {
return Device.all().filter(d => d.type === type);
}
public static all(): Device[] {
Device.loadFromDatabaseIfNecessary();
return Object.values(Device.devices).filter(d => !d._deleted);
}
public undelete(): void {
assert(this._deleted, `Device '${this.ieeeAddr}' is not deleted`);
this._deleted = false;
Entity.database.insert(this.toDatabaseEntry());
}
public static create(
type: DeviceType, ieeeAddr: string, networkAddress: number,
manufacturerID: number, manufacturerName: string,
powerSource: string, modelID: string, interviewCompleted: boolean,
endpoints: {
ID: number; profileID: number; deviceID: number; inputClusters: number[]; outputClusters: number[];
}[],
): Device {
Device.loadFromDatabaseIfNecessary();
if (Device.devices[ieeeAddr] && !Device.devices[ieeeAddr]._deleted) {
throw new Error(`Device with ieeeAddr '${ieeeAddr}' already exists`);
}
const endpointsMapped = endpoints.map((e): Endpoint => {
return Endpoint.create(
e.ID, e.profileID, e.deviceID, e.inputClusters, e.outputClusters, networkAddress, ieeeAddr
);
});
const ID = Entity.database.newID();
const device = new Device(
ID, type, ieeeAddr, networkAddress, manufacturerID, endpointsMapped, manufacturerName,
powerSource, modelID, undefined, undefined, undefined, undefined, undefined, undefined,
interviewCompleted, {}, null, true
);
Entity.database.insert(device.toDatabaseEntry());
Device.devices[device.ieeeAddr] = device;
return device;
}
/*
* Zigbee functions
*/
public async interview(): Promise<void> {
if (this.interviewing) {
const message = `Interview - interview already in progress for '${this.ieeeAddr}'`;
debug.log(message);
throw new Error(message);
}
let error;
this._interviewing = true;
debug.log(`Interview - start device '${this.ieeeAddr}'`);
try {
await this.interviewInternal();
debug.log(`Interview - completed for device '${this.ieeeAddr}'`);
this._interviewCompleted = true;
} catch (e) {
if (this.interviewQuirks()) {
debug.log(`Interview - completed for device '${this.ieeeAddr}' because of quirks ('${e}')`);
} else {
debug.log(`Interview - failed for device '${this.ieeeAddr}' with error '${e.stack}'`);
error = e;
}
} finally {
this._interviewing = false;
this.save();
}
if (error) {
throw error;
}
}
private interviewQuirks(): boolean {
debug.log(`Interview - quirks check for '${this.modelID}'-'${this.manufacturerName}'-'${this.type}'`);
// TuYa devices are typically hard to interview. They also don't require a full interview to work correctly
// e.g. no ias enrolling is required for the devices to work.
// Assume that in case we got both the manufacturerName and modelID the device works correctly.
// https://github.com/Koenkk/zigbee2mqtt/issues/7564:
// Fails during ias enroll due to UNSUPPORTED_ATTRIBUTE
// https://github.com/Koenkk/zigbee2mqtt/issues/4655
// Device does not change zoneState after enroll (event with original gateway)
// modelID is mostly in the form of e.g. TS0202 and manufacturerName like e.g. _TYZB01_xph99wvr
if (this.modelID?.match('^TS\\d*$') &&
(this.manufacturerName?.match('^_TZ.*_.*$') || this.manufacturerName?.match('^_TYZB01_.*$'))) {
this._powerSource = this._powerSource || 'Battery';
this._interviewing = false;
this._interviewCompleted = true;
this.save();
debug.log(`Interview - quirks matched for TuYa end device`);
return true;
}
// Some devices, e.g. Xiaomi end devices have a different interview procedure, after pairing they
// report it's modelID trough a readResponse. The readResponse is received by the controller and set
// on the device.
const lookup: {[s: string]: {
type?: DeviceType; manufacturerID?: number; manufacturerName?: string; powerSource?: string;
};} = {
'^3R.*?Z': {
type: 'EndDevice', powerSource: 'Battery'
},
'lumi\..*': {
type: 'EndDevice', manufacturerID: 4151, manufacturerName: 'LUMI', powerSource: 'Battery'
},
'TERNCY-PP01': {
type: 'EndDevice', manufacturerID: 4648, manufacturerName: 'TERNCY', powerSource: 'Battery'
},
// https://github.com/Koenkk/zigbee-herdsman-converters/pull/2710
'3RWS18BZ': {},
'MULTI-MECI--EA01': {},
};
const match = Object.keys(lookup).find((key) => this.modelID && this.modelID.match(key));
if (match) {
const info = lookup[match];
debug.log(`Interview procedure failed but got modelID matching '${match}', assuming interview succeeded`);
this._type = this._type === 'Unknown' ? info.type : this._type;
this._manufacturerID = this._manufacturerID || info.manufacturerID;
this._manufacturerName = this._manufacturerName || info.manufacturerName;
this._powerSource = this._powerSource || info.powerSource;
this._interviewing = false;
this._interviewCompleted = true;
this.save();
debug.log(`Interview - quirks matched on '${match}'`);
return true;
} else {
debug.log('Interview - quirks did not match');
return false;
}
}
private async interviewInternal(): Promise<void> {
const nodeDescriptorQuery = async (): Promise<void> => {
const nodeDescriptor = await Entity.adapter.nodeDescriptor(this.networkAddress);
this._manufacturerID = nodeDescriptor.manufacturerCode;
this._type = nodeDescriptor.type;
this.save();
debug.log(`Interview - got node descriptor for device '${this.ieeeAddr}'`);
};
const hasNodeDescriptor = (): boolean => this._manufacturerID != null && this._type != null;
if (!hasNodeDescriptor()) {
for (let attempt = 0; attempt < 6; attempt++) {
try {
await nodeDescriptorQuery();
break;
} catch (error) {
if (this.interviewQuirks()) {
debug.log(`Interview - completed for device '${this.ieeeAddr}' because of quirks ('${error}')`);
return;
} else {
// Most of the times the first node descriptor query fails and the seconds one succeeds.
debug.log(
`Interview - node descriptor request failed for '${this.ieeeAddr}', attempt ${attempt + 1}`
);
}
}
}
} else {
debug.log(`Interview - skip node descriptor request for '${this.ieeeAddr}', already got it`);
}
if (!hasNodeDescriptor()) {
throw new Error(`Interview failed because can not get node descriptor ('${this.ieeeAddr}')`);
}
if (this.manufacturerID === 4619 && this._type === 'EndDevice') {
// Give TuYa end device some time to pair. Otherwise they leave immediately.
// https://github.com/Koenkk/zigbee2mqtt/issues/5814
debug.log("Interview - Detected TuYa end device, waiting 10 seconds...");
await Wait(10000);
} else if ([0, 4098].includes(this.manufacturerID)) {
// Potentially a TuYa device, some sleep fast so make sure to read the modelId and manufacturerName quickly.
// In case the device responds, the endoint and modelID/manufacturerName are set
// in controller.onZclOrRawData()
// https://github.com/Koenkk/zigbee2mqtt/issues/7553
debug.log("Interview - Detected potential TuYa end device, reading modelID and manufacturerName...");
try {
const endpoint = Endpoint.create(1, undefined, undefined, [], [], this.networkAddress, this.ieeeAddr);
const result = await endpoint.read('genBasic', ['modelId', 'manufacturerName']);
Object.entries(result)
.forEach((entry) => Device.ReportablePropertiesMapping[entry[0]].set(entry[1], this));
} catch (error) {
/* istanbul ignore next */
debug.log(`Interview - TuYa read modelID and manufacturerName failed (${error})`);
}
}
// e.g. Xiaomi Aqara Opple devices fail to respond to the first active endpoints request, therefore try 2 times
// https://github.com/Koenkk/zigbee-herdsman/pull/103
let activeEndpoints;
for (let attempt = 0; attempt < 2; attempt++) {
try {
activeEndpoints = await Entity.adapter.activeEndpoints(this.networkAddress);
break;
} catch (error) {
debug.log(`Interview - active endpoints request failed for '${this.ieeeAddr}', attempt ${attempt + 1}`);
}
}
if (!activeEndpoints) {
throw new Error(`Interview failed because can not get active endpoints ('${this.ieeeAddr}')`);
}
// Make sure that the endpoint are sorted.
activeEndpoints.endpoints.sort();
// Some devices, e.g. TERNCY return endpoint 0 in the active endpoints request.
// This is not a valid endpoint number according to the ZCL, requesting a simple descriptor will result
// into an error. Therefore we filter it, more info: https://github.com/Koenkk/zigbee-herdsman/issues/82
activeEndpoints.endpoints.filter((e) => e !== 0 && !this.getEndpoint(e)).forEach((e) =>
this._endpoints.push(Endpoint.create(e, undefined, undefined, [], [], this.networkAddress, this.ieeeAddr)));
this.save();
debug.log(`Interview - got active endpoints for device '${this.ieeeAddr}'`);
for (const endpoint of this.endpoints) {
const simpleDescriptor = await Entity.adapter.simpleDescriptor(this.networkAddress, endpoint.ID);
endpoint.profileID = simpleDescriptor.profileID;
endpoint.deviceID = simpleDescriptor.deviceID;
endpoint.inputClusters = simpleDescriptor.inputClusters;
endpoint.outputClusters = simpleDescriptor.outputClusters;
debug.log(`Interview - got simple descriptor for endpoint '${endpoint.ID}' device '${this.ieeeAddr}'`);
this.save();
// Read attributes, nice to have but not required for succesfull pairing as most of the attributes
// are not mandatory in ZCL specification.
if (endpoint.supportsInputCluster('genBasic')) {
for (const [key, item] of Object.entries(Device.ReportablePropertiesMapping)) {
if (!this[item.key]) {
try {
let result: KeyValue;
try {
result = await endpoint.read('genBasic', [key]);
} catch (error) {
// Reading attributes can fail for many reason, e.g. it could be that device rejoins
// while joining like in:
// https://github.com/Koenkk/zigbee-herdsman-converters/issues/2485.
// The modelID and manufacturerName are crucial for device identification, so retry.
if (item.key === 'modelID' || item.key === 'manufacturerName') {
debug.log(`Interview - first ${item.key} retrieval attempt failed, ` +
`retrying after 10 seconds...`);
await Wait(10000);
result = await endpoint.read('genBasic', [key]);
}
}
item.set(result[key], this);
debug.log(`Interview - got '${item.key}' for device '${this.ieeeAddr}'`);
} catch (error) {
debug.log(
`Interview - failed to read attribute '${item.key}' from ` +
`endpoint '${endpoint.ID}' (${error})`
);
}
}
}
}
}
// Enroll IAS device
for (const endpoint of this.endpoints.filter((e): boolean => e.supportsInputCluster('ssIasZone'))) {
debug.log(`Interview - IAS - enrolling '${this.ieeeAddr}' endpoint '${endpoint.ID}'`);
const stateBefore = await endpoint.read('ssIasZone', ['iasCieAddr', 'zoneState']);
debug.log(`Interview - IAS - before enrolling state: '${JSON.stringify(stateBefore)}'`);
// Do not enroll when device has already been enrolled
const coordinator = Device.byType('Coordinator')[0];
if (stateBefore.zoneState !== 1 || stateBefore.iasCieAddr !== coordinator.ieeeAddr) {
debug.log(`Interview - IAS - not enrolled, enrolling`);
await endpoint.write('ssIasZone', {'iasCieAddr': coordinator.ieeeAddr});
debug.log(`Interview - IAS - wrote iasCieAddr`);
// There are 2 enrollment procedures:
// - Auto enroll: coordinator has to send enrollResponse without receiving an enroll request
// this case is handled below.
// - Manual enroll: coordinator replies to enroll request with an enroll response.
// this case in hanled in onZclData().
// https://github.com/Koenkk/zigbee2mqtt/issues/4569#issuecomment-706075676
await Wait(500);
debug.log(`IAS - '${this.ieeeAddr}' sending enroll response (auto enroll)`);
const payload = {enrollrspcode: 0, zoneid: 23};
await endpoint.command('ssIasZone', 'enrollRsp', payload, {disableDefaultResponse: true});
let enrolled = false;
for (let attempt = 0; attempt < 20; attempt++) {
await Wait(500);
const stateAfter = await endpoint.read('ssIasZone', ['iasCieAddr', 'zoneState']);
debug.log(`Interview - IAS - after enrolling state (${attempt}): '${JSON.stringify(stateAfter)}'`);
if (stateAfter.zoneState === 1) {
enrolled = true;
break;
}
}
if (enrolled) {
debug.log(`Interview - IAS successfully enrolled '${this.ieeeAddr}' endpoint '${endpoint.ID}'`);
} else {
throw new Error(
`Interview failed because of failed IAS enroll (zoneState didn't change ('${this.ieeeAddr}')`
);
}
} else {
debug.log(`Interview - IAS - already enrolled, skipping enroll`);
}
}
}
public async removeFromNetwork(): Promise<void> {
await Entity.adapter.removeDevice(this.networkAddress, this.ieeeAddr);
await this.removeFromDatabase();
}
public async removeFromDatabase(): Promise<void> {
Device.loadFromDatabaseIfNecessary();
for (const endpoint of this.endpoints) {
endpoint.removeFromAllGroupsDatabase();
}
if (Entity.database.has(this.ID)) {
Entity.database.remove(this.ID);
}
this._deleted = true;
}
public async lqi(): Promise<LQI> {
return Entity.adapter.lqi(this.networkAddress);
}
public async routingTable(): Promise<RoutingTable> {
return Entity.adapter.routingTable(this.networkAddress);
}
public async ping(disableRecovery = true): Promise<void> {
// Zigbee does not have an official pining mechamism. Use a read request
// of a mandatory basic cluster attribute to keep it as lightweight as
// possible.
await this.endpoints[0].read('genBasic', ['zclVersion'], {disableRecovery});
}
}
export default Device;