forked from WebThingsIO/zwave-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzwave-classifier.js
580 lines (530 loc) · 18.2 KB
/
zwave-classifier.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
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
/**
*
* ZWaveClassifier - Determines properties from command classes.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.*
*/
'use strict';
const ZWaveProperty = require('./zwave-property');
let Constants;
try {
Constants = require('../addon-constants');
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
throw e;
}
Constants = require('gateway-addon').Constants;
}
// See; http://wiki.micasaverde.com/index.php/ZWave_Command_Classes for a
// complete list of command classes.
const COMMAND_CLASS_SWITCH_BINARY = 37; // 0x25
const COMMAND_CLASS_SWITCH_MULTILEVEL = 38; // 0x26
const COMMAND_CLASS_SENSOR_BINARY = 48; // 0x30
const COMMAND_CLASS_SENSOR_MULTILEVEL = 49; // 0x31
const COMMAND_CLASS_METER = 50; // 0x32
// const COMMAND_CLASS_SWITCH_ALL = 39; // 0x27
const COMMAND_CLASS_CONFIGURATION = 112; // 0x70
const COMMAND_CLASS_ALARM = 113; // 0x71
const COMMAND_CLASS_BATTERY = 128; // 0x80
const AEOTEC_MANUFACTURER_ID = '0x0086';
const AEOTEC_ZW096_PRODUCT_ID = '0x0060'; // SmartPlug
const AEOTEC_ZW100_PRODUCT_ID = '0x0064'; // Multisensor 6
// From cpp/src/command_classes/SwitchMultilevel.cpp
// The code uses "_data[5]+3" for the index.
//
// Refer to ZWave document SDS13781 "Z-Wave Application Command Class
// Specification". In the Notification Type and Event fields. The
// notification type of "Home Security" has a Notification Type of 7,
// which means it will be reported as an index of 10 (due to the +3
// mentioned above).
const ALARM_INDEX_HOME_SECURITY = 10;
// This would be from Battery.cpp, but it only has a single index.
const BATTERY_INDEX_LEVEL = 0;
// Refer to ZWave document SDS13781 "Z-Wave Application Command Class
// Specification", Table 67 - Meter Table Capability Report.
// These constants are the bit number times 4.
const METER_INDEX_ELECTRIC_INSTANT_POWER = 8; // Bit 2
const METER_INDEX_ELECTRIC_INSTANT_VOLTAGE = 16; // Bit 3
const METER_INDEX_ELECTRIC_INSTANT_CURRENT = 20; // Bit 5
// This would be from SensorBinary.cpp, but it only has a single index.
const SENSOR_BINARY_INDEX_SENSOR = 0;
// From the SensorType enum in OpenZWave:
// cpp/src/command_classes/SensorMultilevel.cpp#L50
//
// Note: These constants are specific to the OpenZWave library and not
// part of the ZWave specification.
const SENSOR_MULTILEVEL_INDEX_TEMPERATURE = 1;
const SENSOR_MULTILEVEL_INDEX_LUMINANCE = 3;
const SENSOR_MULTILEVEL_INDEX_RELATIVE_HUMIDITY = 5;
const SENSOR_MULTILEVEL_INDEX_ULTRAVIOLET = 27;
// This would be from SwitchBinary.cpp, but it only has a single index.
const SWITCH_BINARY_INDEX_SWITCH = 0;
// From the SwitchMultilevelIndex enum in OpenZWave:
// cpp/src/command_classes/SwitchMultilevel.cpp
//
// Note: These constants are specific to the OpenZWave library and not
// part of the ZWave specification.
const SWITCH_MULTILEVEL_INDEX_LEVEL = 0;
const QUIRKS = [
{
// The Aeotec devices don't seem to notify on current changes, only on
// instantaneous power changes. So we exclude this for now. We might be
// able to support this by adding a read of current each time we get a
// power change.
zwInfo: {
manufacturerId: AEOTEC_MANUFACTURER_ID,
},
excludeProperties: ['current'],
},
{
// The Aeotec ZW096 says it supports the MULTILEVEL command class, but
// setting it acts like a no-op. We remove the 'level' property so that
// the UI doesn't see it.
zwInfo: {
manufacturerId: AEOTEC_MANUFACTURER_ID,
productId: AEOTEC_ZW096_PRODUCT_ID,
},
excludeProperties: ['level'],
},
{
// The Aeotec ZW100 says it supports the SENSOR_BINARY command class,
// but this is only true for some configurations. We use the alarm
// command class instead.
// We remove the 'on' property so that the UI doesn't see it.
zwInfo: {
manufacturerId: AEOTEC_MANUFACTURER_ID,
productId: AEOTEC_ZW100_PRODUCT_ID,
},
excludeProperties: ['on'],
setConfigs: [
// Configure motion sensor to send 'Basic Set', rather than
// 'Binary Sensor report'.
{instance: 1, index: 5, value: 1},
],
},
];
function quirkMatches(quirk, node) {
let match = true;
for (const id in quirk.zwInfo) {
if (node.zwInfo[id] !== quirk.zwInfo[id]) {
match = false;
break;
}
}
return match;
}
class ZWaveClassifier {
classify(node) {
this.classifyInternal(node);
// Any type of device can be battery powered, so we do this check for
// all devices.
const batteryValueId =
node.findValueId(COMMAND_CLASS_BATTERY,
1,
BATTERY_INDEX_LEVEL);
if (batteryValueId) {
this.addBatteryProperty(node, batteryValueId);
}
}
classifyInternal(node) {
// Search through the known quirks and see if we need to apply any
// configurations
for (const quirk of QUIRKS) {
if (!quirk.hasOwnProperty('setConfigs')) {
continue;
}
if (quirkMatches(quirk, node)) {
for (const setConfig of quirk.setConfigs) {
console.log(`Setting device ${node.id} config ` +
`instance: ${setConfig.instance}` +
`index: ${setConfig.index}` +
`to value: ${setConfig.value}`);
node.adapter.zwave.setValue(node.zwInfo.nodeId, // nodeId
COMMAND_CLASS_CONFIGURATION, // classId
setConfig.instance, // instance
setConfig.index, // index
setConfig.value); // value
}
}
}
const binarySwitchValueId =
node.findValueId(COMMAND_CLASS_SWITCH_BINARY,
1,
SWITCH_BINARY_INDEX_SWITCH);
const levelValueId =
node.findValueId(COMMAND_CLASS_SWITCH_MULTILEVEL,
1,
SWITCH_MULTILEVEL_INDEX_LEVEL);
if (binarySwitchValueId || levelValueId) {
// Some devices (like the ZW099 Smart Dimmer 6) advertise instance
// 1 and 2, and don't seem to work on instance 2. So we always
// use instance 1 for the first outlet, and then 3 and beyond for the
// second and beyond outlets.
this.initSwitch(node, binarySwitchValueId, levelValueId, '');
// Check to see if this is a switch with multiple outlets.
let inst = 3;
let switchCount = 1;
// eslint-disable-next-line no-constant-condition
while (true) {
const bsValueId =
node.findValueId(COMMAND_CLASS_SWITCH_BINARY,
inst,
SWITCH_BINARY_INDEX_SWITCH);
const lvlValueId =
node.findValueId(COMMAND_CLASS_SWITCH_MULTILEVEL,
inst,
SWITCH_MULTILEVEL_INDEX_LEVEL);
if (bsValueId || lvlValueId) {
switchCount += 1;
this.initSwitch(node, bsValueId, lvlValueId, switchCount.toString());
inst += 1;
} else {
break;
}
}
return;
}
node.type = 'thing'; // Just in case it doesn't classify as anything else
const alarmValueId =
node.findValueId(COMMAND_CLASS_ALARM,
1,
ALARM_INDEX_HOME_SECURITY);
if (alarmValueId) {
this.addAlarmProperty(node, alarmValueId);
}
const temperatureValueId =
node.findValueId(COMMAND_CLASS_SENSOR_MULTILEVEL,
1,
SENSOR_MULTILEVEL_INDEX_TEMPERATURE);
if (temperatureValueId) {
this.addTemperatureProperty(node, temperatureValueId);
}
const luminanceValueId =
node.findValueId(COMMAND_CLASS_SENSOR_MULTILEVEL,
1,
SENSOR_MULTILEVEL_INDEX_LUMINANCE);
if (luminanceValueId) {
this.addLuminanceProperty(node, luminanceValueId);
}
const humidityValueId =
node.findValueId(COMMAND_CLASS_SENSOR_MULTILEVEL,
1,
SENSOR_MULTILEVEL_INDEX_RELATIVE_HUMIDITY);
if (humidityValueId) {
this.addHumidityProperty(node, humidityValueId);
}
const uvValueId =
node.findValueId(COMMAND_CLASS_SENSOR_MULTILEVEL,
1,
SENSOR_MULTILEVEL_INDEX_ULTRAVIOLET);
if (uvValueId) {
this.addUltravioletProperty(node, uvValueId);
}
const binarySensorValueId =
node.findValueId(COMMAND_CLASS_SENSOR_BINARY,
1,
SENSOR_BINARY_INDEX_SENSOR);
if (binarySensorValueId) {
this.initBinarySensor(node, binarySensorValueId);
}
}
addProperty(node, name, descr, valueId,
setZwValueFromValue, parseValueFromZwValue) {
// Search through the known quirks and see if we need to apply any.
for (const quirk of QUIRKS) {
if (!quirk.hasOwnProperty('excludeProperties')) {
continue;
}
if (quirkMatches(quirk, node) && quirk.excludeProperties.includes(name)) {
console.log(
`Not adding property ${name} to device ${node.id} due to quirk.`);
return;
}
}
const property = new ZWaveProperty(node, name, descr, valueId,
setZwValueFromValue,
parseValueFromZwValue);
node.properties.set(name, property);
}
addAlarmProperty(node, alarmValueId) {
this.addProperty(
node,
'motion',
{
'@type': 'BooleanProperty',
label: 'Motion',
type: 'boolean',
},
alarmValueId,
'',
'parseAlarmMotionZwValue'
);
this.addProperty(
node,
'tamper',
{
'@type': 'BooleanProperty',
label: 'Tamper',
type: 'boolean',
},
alarmValueId,
'',
'parseAlarmTamperZwValue'
);
}
addBatteryProperty(node, batteryValueId) {
this.addProperty(
node,
'batteryLevel',
{
'@type': 'LevelProperty',
label: 'Battery',
type: 'number',
minimum: 0,
maximum: 100,
unit: 'percent',
},
batteryValueId
);
}
addHumidityProperty(node, humidityValueId) {
this.addProperty(
node,
'humidity',
{
'@type': 'LevelProperty',
label: 'Humidity',
type: 'number',
minimum: 0,
maximum: 100,
unit: 'percent',
},
humidityValueId
);
}
addLuminanceProperty(node, luminanceValueId) {
this.addProperty(
node,
'luminance',
{
// TODO: add proper @type
label: 'Luminance',
type: 'number',
unit: 'lux',
},
luminanceValueId
);
}
addTemperatureProperty(node, temperatureValueId) {
const descr = {
// TODO: add proper @type
label: 'Temperature',
type: 'number',
};
const zwValue = node.zwValues[temperatureValueId];
if (zwValue.units === 'F') {
descr.unit = 'farenheit';
} else if (zwValue.units === 'C') {
descr.unit = 'celsius';
}
this.addProperty(
node,
'temperature',
descr,
temperatureValueId
);
}
addUltravioletProperty(node, uvValueId) {
this.addProperty(
node,
'uvIndex',
{
// TODO: add proper @type
label: 'UV Index',
type: 'number',
},
uvValueId
);
}
initSwitch(node, binarySwitchValueId, levelValueId, suffix) {
node['@type'] = ['OnOffSwitch'];
if (binarySwitchValueId) {
if (suffix) {
// Until we have the capabilities system, in order for the UI
// to display the second switch, we need to call it a thing.
node.type = 'thing';
} else {
node.type = Constants.THING_TYPE_ON_OFF_SWITCH;
}
this.addProperty(
node, // node
`on${suffix}`, // name
{ // property decscription
'@type': suffix ? 'BooleanProperty' : 'OnOffProperty',
label: suffix ? `On/Off (${suffix})` : 'On/Off',
type: 'boolean',
},
binarySwitchValueId // valueId
);
if (levelValueId) {
if (!suffix) {
node.type = Constants.THING_TYPE_MULTI_LEVEL_SWITCH;
node['@type'].push('MultiLevelSwitch');
}
this.addProperty(
node, // node
`level${suffix}`, // name
{ // property decscription
'@type': suffix ? '' : 'LevelProperty',
label: suffix ? `Level (${suffix})` : 'Level',
type: 'number',
unit: 'percent',
minimum: 0,
maximum: 100,
},
levelValueId, // valueId
'setLevelValue', // setZwValueFromValue
'parseLevelZwValue' // parseValueFromZwValue
);
}
} else {
// For switches which don't support the on/off we fake it using level
if (!suffix) {
node.type = Constants.THING_TYPE_MULTI_LEVEL_SWITCH;
node['@type'].push('MultiLevelSwitch');
}
this.addProperty(
node, // node
`on${suffix}`, // name
{ // property decscription
'@type': suffix ? 'BooleanProperty' : 'OnOffProperty',
label: suffix ? `On/Off (${suffix})` : 'On/Off',
type: 'boolean',
},
levelValueId, // valueId
'setOnOffLevelValue', // setZwValueFromValue
'parseOnOffLevelZwValue' // parseValueFromZwValue
);
this.addProperty(
node, // node
`level${suffix}`, // name
{ // property decscription
'@type': suffix ? '' : 'LevelProperty',
label: suffix ? `Level (${suffix})` : 'Level',
type: 'number',
unit: 'percent',
minimum: 0,
maximum: 100,
},
levelValueId, // valueId
'setOnOffLevelValue', // setZwValueFromValue
'parseOnOffLevelZwValue'// parseValueFromZwValue
);
}
const powerValueId =
node.findValueId(COMMAND_CLASS_METER,
1,
METER_INDEX_ELECTRIC_INSTANT_POWER);
if (powerValueId) {
if (!suffix) {
node.type = Constants.THING_TYPE_SMART_PLUG;
node['@type'].push('SmartPlug', 'EnergyMonitor');
}
this.addProperty(
node, // node
`instantaneousPower${suffix}`, // name
{ // property decscription
'@type': suffix ? '' : 'InstantaneousPowerProperty',
label: suffix ? `Power (${suffix})` : 'Power',
type: 'number',
unit: 'watt',
},
powerValueId // valueId
);
}
const voltageValueId =
node.findValueId(COMMAND_CLASS_METER,
1,
METER_INDEX_ELECTRIC_INSTANT_VOLTAGE);
if (voltageValueId) {
if (!suffix) {
node.type = Constants.THING_TYPE_SMART_PLUG;
}
this.addProperty(
node, // node
`voltage${suffix}`, // name
{ // property decscription
'@type': suffix ? '' : 'VoltageProperty',
label: suffix ? `Voltage (${suffix})` : 'Voltage',
type: 'number',
unit: 'volt',
},
voltageValueId // valueId
);
}
const currentValueId =
node.findValueId(COMMAND_CLASS_METER,
1,
METER_INDEX_ELECTRIC_INSTANT_CURRENT);
if (currentValueId) {
if (!suffix) {
node.type = Constants.THING_TYPE_SMART_PLUG;
}
this.addProperty(
node, // node
`current${suffix}`, // name
{ // property decscription
'@type': suffix ? '' : 'CurrentProperty',
label: suffix ? `Current (${suffix})` : 'Current',
type: 'number',
unit: 'ampere',
},
currentValueId // valueId
);
}
// TODO: add this data into the quirks
if (node.zwInfo.manufacturer === 'Aeotec') {
// When the user presses the button, tell us about it
node.adapter.zwave.setValue(node.zwInfo.nodeId, // nodeId
COMMAND_CLASS_CONFIGURATION, // classId
1, // instance
80, // index
'Basic'); // value
if (node.type === Constants.THING_TYPE_SMART_PLUG) {
// Enable METER reporting
node.adapter.zwave.setValue(node.zwInfo.nodeId, // nodeId
COMMAND_CLASS_CONFIGURATION, // classId
1, // instance
90, // index
1); // value
// Report changes of 1 watt
node.adapter.zwave.setValue(node.zwInfo.nodeId, // nodeId
COMMAND_CLASS_CONFIGURATION, // classId
1, // instance
91, // index
1); // value
}
}
}
initBinarySensor(node, binarySensorValueId) {
if (node.properties.size == 0) {
node.type = Constants.THING_TYPE_BINARY_SENSOR;
node['@type'] = ['BinarySensor'];
}
this.addProperty(
node, // node
'on', // name
{ // property decscription
'@type': 'BooleanProperty',
type: 'boolean',
},
binarySensorValueId // valueId
);
if (node.type === 'thing' && node.name == node.defaultName) {
node.name = `${node.id}-thing`;
}
}
}
module.exports = new ZWaveClassifier();