-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
index.ts
376 lines (332 loc) · 16.3 KB
/
index.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
import * as configureKey from './lib/configureKey';
import * as exposes from './lib/exposes';
import type {Feature, Numeric, Enum, Binary, Text, Composite, List, Light, Climate, Switch, Lock, Cover, Fan} from './lib/exposes';
import toZigbee from './converters/toZigbee';
import fromZigbee from './converters/fromZigbee';
import assert from 'assert';
import * as ota from './lib/ota';
import allDefinitions from './devices';
import * as utils from './lib/utils';
import { Definition, Fingerprint, Zh, OnEventData, OnEventType, Configure, Expose, Tz, OtaUpdateAvailableResult, KeyValue, Logger } from './lib/types';
import {generateDefinition} from './lib/generateDefinition';
import {Zcl} from 'zigbee-herdsman';
import * as logger from './lib/logger';
export {
Definition as Definition,
OnEventType as OnEventType,
Feature as Feature,
Expose as Expose,
Numeric as Numeric,
Binary as Binary,
Enum as Enum,
Text as Text,
Composite as Composite,
List as List,
Light as Light,
Climate as Climate,
Switch as Switch,
Lock as Lock,
Cover as Cover,
Fan as Fan,
toZigbee as toZigbee,
fromZigbee as fromZigbee,
Tz as Tz,
OtaUpdateAvailableResult as OtaUpdateAvailableResult,
ota as ota,
};
export const getConfigureKey = configureKey.getConfigureKey;
// key: zigbeeModel, value: array of definitions (most of the times 1)
const lookup = new Map();
export const definitions: Definition[] = [];
function arrayEquals<T>(as: T[], bs: T[]) {
if (as.length !== bs.length) return false;
for (const a of as) if (!bs.includes(a)) return false;
return true;
}
function addToLookup(zigbeeModel: string, definition: Definition) {
zigbeeModel = zigbeeModel ? zigbeeModel.toLowerCase() : null;
if (!lookup.has(zigbeeModel)) {
lookup.set(zigbeeModel, []);
}
if (!lookup.get(zigbeeModel).includes(definition)) {
lookup.get(zigbeeModel).splice(0, 0, definition);
}
}
function getFromLookup(zigbeeModel: string) {
zigbeeModel = zigbeeModel ? zigbeeModel.toLowerCase() : null;
if (lookup.has(zigbeeModel)) {
return lookup.get(zigbeeModel);
}
zigbeeModel = zigbeeModel ? zigbeeModel.replace(/\0(.|\n)*$/g, '').trim() : null;
return lookup.get(zigbeeModel);
}
const converterRequiredFields = {
model: 'String',
vendor: 'String',
description: 'String',
fromZigbee: 'Array',
toZigbee: 'Array',
};
function validateDefinition(definition: Definition) {
for (const [field, expectedType] of Object.entries(converterRequiredFields)) {
// @ts-expect-error
assert.notStrictEqual(null, definition[field], `Converter field ${field} is null`);
// @ts-expect-error
assert.notStrictEqual(undefined, definition[field], `Converter field ${field} is undefined`);
// @ts-expect-error
const msg = `Converter field ${field} expected type doenst match to ${definition[field]}`;
// @ts-expect-error
assert.strictEqual(definition[field].constructor.name, expectedType, msg);
}
assert.ok(Array.isArray(definition.exposes) || typeof definition.exposes === 'function', 'Exposes incorrect');
}
function processExtensions(definition: Definition): Definition {
if ('extend' in definition) {
if (!Array.isArray(definition.extend)) {
assert.fail(`'${definition.model}' has legacy extend which is not supported anymore`);
}
// Modern extend, merges properties, e.g. when both extend and definition has toZigbee, toZigbee will be combined
let {extend, toZigbee, fromZigbee, exposes, meta, endpoint, configure: definitionConfigure, onEvent, ota, ...definitionWithoutExtend} = definition;
if (typeof exposes === 'function') {
assert.fail(`'${definition.model}' has function exposes which is not allowed`);
}
toZigbee = [...toZigbee ?? []];
fromZigbee = [...fromZigbee ?? []];
exposes = [...exposes ?? []];
const configures: Configure[] = definitionConfigure ? [definitionConfigure] : [];
for (const ext of extend) {
if (!ext.isModernExtend) {
assert.fail(`'${definition.model}' has legacy extend in modern extend`);
}
if (ext.toZigbee) toZigbee.push(...ext.toZigbee);
if (ext.fromZigbee) fromZigbee.push(...ext.fromZigbee);
if (ext.exposes) exposes.push(...ext.exposes);
if (ext.meta) meta = {...ext.meta, ...meta};
if (ext.configure) configures.push(ext.configure);
if (ext.ota) {
if (ota) {
assert.fail(`'${definition.model}' has multiple 'ota', this is not allowed`);
}
ota = ext.ota;
}
if (ext.endpoint) {
if (endpoint) {
assert.fail(`'${definition.model}' has multiple 'endpoint', this is not allowed`);
}
endpoint = ext.endpoint;
}
if (ext.onEvent) {
if (onEvent) {
assert.fail(`'${definition.model}' has multiple 'onEvent', this is not allowed`);
}
onEvent = ext.onEvent;
}
}
let configure: Configure = null;
if (configures.length !== 0) {
configure = async (device, coordinatorEndpoint, logger) => {
for (const func of configures) {
await func(device, coordinatorEndpoint, logger);
}
}
}
definition = {toZigbee, fromZigbee, exposes, meta, configure, endpoint, onEvent, ota, ...definitionWithoutExtend};
}
return definition
}
function prepareDefinition(definition: Definition): Definition {
definition = processExtensions(definition);
definition.toZigbee.push(
toZigbee.scene_store, toZigbee.scene_recall, toZigbee.scene_add, toZigbee.scene_remove, toZigbee.scene_remove_all,
toZigbee.scene_rename, toZigbee.read, toZigbee.write,
toZigbee.command, toZigbee.factory_reset);
if (definition.exposes && Array.isArray(definition.exposes) && !definition.exposes.find((e) => e.name === 'linkquality')) {
definition.exposes = definition.exposes.concat([exposes.presets.linkquality()]);
}
validateDefinition(definition);
// Add all the options
if (!definition.options) definition.options = [];
const optionKeys = definition.options.map((o) => o.name);
// Add calibration/precision options based on expose
for (const expose of Array.isArray(definition.exposes) ? definition.exposes : definition.exposes(null, null)) {
if (!optionKeys.includes(expose.name) && utils.isNumericExposeFeature(expose) && expose.name in utils.calibrateAndPrecisionRoundOptionsDefaultPrecision) {
// Battery voltage is not calibratable
if (expose.name === 'voltage' && expose.unit === 'mV') continue;
const type = utils.calibrateAndPrecisionRoundOptionsIsPercentual(expose.name) ? 'percentual' : 'absolute';
definition.options.push(exposes.options.calibration(expose.name, type));
if (utils.calibrateAndPrecisionRoundOptionsDefaultPrecision[expose.name] !== 0) {
definition.options.push(exposes.options.precision(expose.name));
}
optionKeys.push(expose.name);
}
}
for (const converter of [...definition.toZigbee, ...definition.fromZigbee]) {
if (converter.options) {
const options = typeof converter.options === 'function' ? converter.options(definition) : converter.options;
for (const option of options) {
if (!optionKeys.includes(option.name)) {
definition.options.push(option);
optionKeys.push(option.name);
}
}
}
}
return definition
}
export function postProcessConvertedFromZigbeeMessage(definition: Definition, payload: KeyValue, options: KeyValue, logger: Logger) {
// Apply calibration/precision options
for (const [key, value] of Object.entries(payload)) {
const definitionExposes = Array.isArray(definition.exposes) ? definition.exposes : definition.exposes(null, null);
const expose = definitionExposes.find((e) => e.property === key);
if (expose?.name in utils.calibrateAndPrecisionRoundOptionsDefaultPrecision && utils.isNumber(value)) {
try {
payload[key] = utils.calibrateAndPrecisionRoundOptions(value, options, expose.name);
} catch (error) {
logger.error(`Failed to apply calibration to '${expose.name}': ${error.message}`);
}
}
}
}
export function addDefinition(definition: Definition) {
definition = prepareDefinition(definition)
definitions.splice(0, 0, definition);
if ('fingerprint' in definition) {
for (const fingerprint of definition.fingerprint) {
addToLookup(fingerprint.modelID, definition);
}
}
if ('zigbeeModel' in definition) {
for (const zigbeeModel of definition.zigbeeModel) {
addToLookup(zigbeeModel, definition);
}
}
}
for (const definition of allDefinitions) {
addDefinition(definition);
}
export async function findByDevice(device: Zh.Device, generateForUnknown: boolean = false) {
let definition = await findDefinition(device, generateForUnknown);
if (definition && definition.whiteLabel) {
const match = definition.whiteLabel.find((w) => 'fingerprint' in w && w.fingerprint.find((f) => isFingerprintMatch(f, device)));
if (match) {
definition = {
...definition,
model: match.model,
vendor: match.vendor,
description: match.description || definition.description,
};
}
}
return definition;
}
export async function findDefinition(device: Zh.Device, generateForUnknown: boolean = false): Promise<Definition> {
if (!device) {
return null;
}
const candidates = getFromLookup(device.modelID);
if (!candidates) {
if (!generateForUnknown || device.type === 'Coordinator') {
return null;
}
// Do not add this definition to cache,
// as device configuration might change.
return prepareDefinition((await generateDefinition(device)).definition);
} else if (candidates.length === 1 && candidates[0].hasOwnProperty('zigbeeModel')) {
return candidates[0];
} else {
// First try to match based on fingerprint, return the first matching one.
const fingerprintMatch: {priority: number, definition: Definition} = {priority: null, definition: null};
for (const candidate of candidates) {
if (candidate.hasOwnProperty('fingerprint')) {
for (const fingerprint of candidate.fingerprint) {
const priority = fingerprint.hasOwnProperty('priority') ? fingerprint.priority : 0;
if (isFingerprintMatch(fingerprint, device) && (!fingerprintMatch.definition || priority > fingerprintMatch.priority)) {
fingerprintMatch.definition = candidate;
fingerprintMatch.priority = priority;
}
}
}
}
if (fingerprintMatch.definition) {
return fingerprintMatch.definition;
}
// Match based on fingerprint failed, return first matching definition based on zigbeeModel
for (const candidate of candidates) {
if (candidate.hasOwnProperty('zigbeeModel') && candidate.zigbeeModel.includes(device.modelID)) {
return candidate;
}
}
}
return null;
}
export async function generateExternalDefinitionSource(device: Zh.Device): Promise<string> {
return (await generateDefinition(device)).externalDefinitionSource;
}
function isFingerprintMatch(fingerprint: Fingerprint, device: Zh.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.ieeeAddr || device.ieeeAddr.match(fingerprint.ieeeAddr)) &&
(!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;
}
export function findByModel(model: string){
/*
Search device description by definition model name.
Useful when redefining, expanding device descriptions in external converters.
*/
model = model.toLowerCase();
return definitions.find((definition) => {
const whiteLabelMatch = definition.whiteLabel && definition.whiteLabel.find((dd) => dd.model.toLowerCase() === model);
return definition.model.toLowerCase() == model || whiteLabelMatch;
});
}
// 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
export async function onEvent(type: OnEventType, data: OnEventData, device: Zh.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 === Zcl.ManufacturerCode.LEGRAND_GROUP && type === 'message' && data.type === 'read' &&
data.cluster === 'genBasic' && data.data && data.data.includes(61440)) {
const endpoint = device.getEndpoint(1);
const options = {manufacturerCode: Zcl.ManufacturerCode.LEGRAND_GROUP, disableDefaultResponse: true};
const payload = {0xf00: {value: 23, type: 35}};
await endpoint.readResponse('genBasic', data.meta.zclTransactionSequenceNumber, payload, options);
}
// Aqara feeder C1 polls the time during the interview, need to send back the local time instead of the UTC.
// The device.definition has not yet been set - therefore the device.definition.onEvent method does not work.
if (type === 'message' && data.type === 'read' && data.cluster === 'genTime' &&
device.modelID === 'aqara.feeder.acn001') {
device.skipTimeResponse = true;
const oneJanuary2000 = new Date('January 01, 2000 00:00:00 UTC+00:00').getTime();
const secondsUTC = Math.round(((new Date()).getTime() - oneJanuary2000) / 1000);
const secondsLocal = secondsUTC - (new Date()).getTimezoneOffset() * 60;
await device.getEndpoint(1).readResponse('genTime', data.meta.zclTransactionSequenceNumber, {time: secondsLocal});
}
}
export const setLogger = logger.setLogger;