forked from Koenkk/zigbee-herdsman-converters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
182 lines (158 loc) · 7.16 KB
/
index.js
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
'use strict';
const devices = require('./devices');
const exposes = require('./lib/exposes');
const toZigbee = require('./converters/toZigbee');
const fromZigbee = require('./converters/fromZigbee');
const assert = require('assert');
// key: zigbeeModel, value: array of definitions (most of the times 1)
const lookup = new Map();
const definitions = [];
function arrayEquals(as, bs) {
if (as.length !== bs.length) return false;
for (const a of as) if (!bs.includes(a)) return false;
return true;
}
function addToLookup(zigbeeModel, definition) {
zigbeeModel = zigbeeModel ? zigbeeModel.toLowerCase() : null;
if (!lookup.has(zigbeeModel)) {
lookup.set(zigbeeModel, []);
}
if (!lookup.get(zigbeeModel).includes(definition)) {
lookup.get(zigbeeModel).push(definition);
}
}
function getFromLookup(zigbeeModel) {
zigbeeModel = zigbeeModel ? zigbeeModel.toLowerCase() : null;
if (lookup.has(zigbeeModel)) {
return lookup.get(zigbeeModel);
}
zigbeeModel = zigbeeModel ? zigbeeModel.replace(/\0.*$/g, '').trim() : null;
return lookup.get(zigbeeModel);
}
const converterRequiredFields = {
model: 'String',
vendor: 'String',
description: 'String',
fromZigbee: 'Array',
toZigbee: 'Array',
exposes: 'Array',
};
function validateDefinition(definition) {
for (const [field, expectedType] of Object.entries(converterRequiredFields)) {
assert.notStrictEqual(null, definition[field], `Converter field ${field} is null`);
assert.notStrictEqual(undefined, definition[field], `Converter field ${field} is undefined`);
const msg = `Converter field ${field} expected type doenst match to ${definition[field]}`;
assert.strictEqual(definition[field].constructor.name, expectedType, msg);
}
}
function addDefinition(definition) {
validateDefinition(definition);
definitions.push(definition);
if (definition.hasOwnProperty('fingerprint')) {
for (const fingerprint of definition.fingerprint) {
addToLookup(fingerprint.modelID, definition);
}
}
if (definition.hasOwnProperty('zigbeeModel')) {
for (const zigbeeModel of definition.zigbeeModel) {
addToLookup(zigbeeModel, definition);
}
}
}
for (const definition of devices) {
addDefinition(definition);
}
function findByZigbeeModel(zigbeeModel) {
if (!zigbeeModel) {
return null;
}
const candidates = getFromLookup(zigbeeModel);
// Multiple candidates possible, to use external converters in priority, use last one.
return candidates ? candidates[candidates.length-1] : null;
}
function findByDevice(device) {
if (!device) {
return null;
}
const candidates = getFromLookup(device.modelID);
if (!candidates) {
return null;
} else if (candidates.length === 1 && candidates[0].hasOwnProperty('zigbeeModel')) {
return candidates[0];
} else {
// Multiple candidates possible, to use external converters in priority, reverse the order of candidates before searching.
const reversedCandidates = candidates.reverse();
// First try to match based on fingerprint, return the first matching one.
for (const candidate of reversedCandidates) {
if (candidate.hasOwnProperty('fingerprint')) {
for (const fingerprint of candidate.fingerprint) {
if (fingerprintMatch(fingerprint, device)) {
return candidate;
}
}
}
}
// Match based on fingerprint failed, return first matching definition based on zigbeeModel
for (const candidate of reversedCandidates) {
if (candidate.hasOwnProperty('zigbeeModel') && candidate.zigbeeModel.includes(device.modelID)) {
return candidate;
}
}
}
return null;
}
function fingerprintMatch(fingerprint, device) {
let match =
(!fingerprint.applicationVersion || device.applicationVersion === fingerprint.applicationVersion) &&
(!fingerprint.manufacturerID || device.manufacturerID === fingerprint.manufacturerID) &&
(!fingerprint.type || device.type === fingerprint.type) &&
(!fingerprint.dateCode || device.dateCode === fingerprint.dateCode) &&
(!fingerprint.hardwareVersion || device.hardwareVersion === fingerprint.hardwareVersion) &&
(!fingerprint.manufacturerName || device.manufacturerName === fingerprint.manufacturerName) &&
(!fingerprint.modelID || device.modelID === fingerprint.modelID) &&
(!fingerprint.powerSource || device.powerSource === fingerprint.powerSource) &&
(!fingerprint.softwareBuildID || device.softwareBuildID === fingerprint.softwareBuildID) &&
(!fingerprint.stackVersion || device.stackVersion === fingerprint.stackVersion) &&
(!fingerprint.zclVersion || device.zclVersion === fingerprint.zclVersion) &&
(!fingerprint.endpoints ||
arrayEquals(device.endpoints.map((e) => e.ID), fingerprint.endpoints.map((e) => e.ID)));
if (match && fingerprint.endpoints) {
for (const fingerprintEndpoint of fingerprint.endpoints) {
const deviceEndpoint = device.getEndpoint(fingerprintEndpoint.ID);
match = match &&
(!fingerprintEndpoint.deviceID || deviceEndpoint.deviceID === fingerprintEndpoint.deviceID) &&
(!fingerprintEndpoint.profileID || deviceEndpoint.profileID === fingerprintEndpoint.profileID) &&
(!fingerprintEndpoint.inputClusters ||
arrayEquals(deviceEndpoint.inputClusters, fingerprintEndpoint.inputClusters)) &&
(!fingerprintEndpoint.outputClusters ||
arrayEquals(deviceEndpoint.outputClusters, fingerprintEndpoint.outputClusters));
}
}
return match;
}
module.exports = {
devices: definitions,
exposes,
definitions,
findByZigbeeModel, // Legacy method, use findByDevice instead.
findByDevice,
toZigbeeConverters: toZigbee,
fromZigbeeConverters: fromZigbee,
addDeviceDefinition: addDefinition,
// Can be used to handle events for devices which are not fully paired yet (no modelID).
// Example usecase: https://github.com/Koenkk/zigbee2mqtt/issues/2399#issuecomment-570583325
onEvent: async (type, data, device) => {
// support Legrand security protocol
// when pairing, a powered device will send a read frame to every device on the network
// it expects at least one answer. The payload contains the number of seconds
// since when the device is powered. If the value is too high, it will leave & not pair
// 23 works, 200 doesn't
if (data.meta && data.meta.manufacturerCode === 0x1021 && type === 'message' && data.type === 'read' &&
data.cluster === 'genBasic' && data.data && data.data.includes(61440)) {
const endpoint = device.getEndpoint(1);
const options = {manufacturerCode: 0x1021, disableDefaultResponse: true};
const payload = {0xf00: {value: 23, type: 35}};
await endpoint.readResponse('genBasic', data.meta.zclTransactionSequenceNumber, payload, options);
}
},
};