forked from WebThingsIO/zwave-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzwave-property.js
235 lines (207 loc) · 6.05 KB
/
zwave-property.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
/**
* ZWave Property.
*
* Object which decscribes a property, and its value.
*
* 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';
let Deferred, Property;
try {
Deferred = require('../deferred');
Property = require('../property');
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
throw e;
}
const gwa = require('gateway-addon');
Deferred = gwa.Deferred;
Property = gwa.Property;
}
// Refer to ZWave document SDS13781 "Z-Wave Application Command Class
// Specification". In the Notification Type and Event fields. These
// constants come from the "Event" column for the "Home Security (V2)
// section".
const ALARM_EVENT_HOME_SECURITY_CLEAR = 0;
const ALARM_EVENT_HOME_SECURITY_TAMPER = 3;
const ALARM_EVENT_HOME_SECURITY_MOTION = 8;
class ZWaveProperty extends Property {
constructor(device, name, propertyDescr, valueId,
setZwValueFromValue, parseValueFromZwValue) {
super(device, name, propertyDescr);
this.valueId = valueId;
if (!setZwValueFromValue) {
setZwValueFromValue = 'setIdentityValue';
}
this.setZwValueFromValue = Object.getPrototypeOf(this)[setZwValueFromValue];
if (!this.setZwValueFromValue) {
const err = `Unknown function: ${setZwValueFromValue}`;
console.error(err);
throw err;
}
if (!parseValueFromZwValue) {
parseValueFromZwValue = 'parseIdentityValue';
}
this.parseValueFromZwValue =
Object.getPrototypeOf(this)[parseValueFromZwValue];
if (!this.parseValueFromZwValue) {
const err = `Unknown function: ${parseValueFromZwValue}`;
console.error(err);
throw err;
}
const zwValue = device.zwValues[valueId];
if (zwValue) {
const [value, _logValue] = this.parseValueFromZwValue(zwValue.value);
this.value = value;
}
}
asDict() {
const dict = super.asDict();
dict.valueId = this.valueId;
dict.value = this.value;
return dict;
}
parseAlarmMotionZwValue(zwData) {
let motion = this.value;
switch (zwData) {
case ALARM_EVENT_HOME_SECURITY_CLEAR:
motion = false;
break;
case ALARM_EVENT_HOME_SECURITY_MOTION:
motion = true;
break;
}
if (typeof motion === 'undefined') {
motion = false;
}
return [motion, motion.toString()];
}
parseAlarmTamperZwValue(zwData) {
let tamper = this.value;
switch (zwData) {
case ALARM_EVENT_HOME_SECURITY_CLEAR:
tamper = false;
break;
case ALARM_EVENT_HOME_SECURITY_TAMPER:
tamper = true;
break;
}
if (typeof tamper === 'undefined') {
tamper = false;
}
return [tamper, tamper.toString()];
}
parseIdentityValue(zwData) {
const propertyValue = zwData;
return [propertyValue, propertyValue.toString()];
}
parseOnOffLevelZwValue(zwData) {
// For devices (like the Aeotec ZW099) which support level but don't
// support on/off we fake on/off
const ret = this.parseLevelZwValue(zwData);
if (this.name === 'on') {
const value = this.level > 0;
return [value, `${value}`];
}
return ret;
}
parseLevelZwValue(zwData) {
this.level = Math.max(zwData, 0);
let percent = this.level;
if (zwData >= 99) {
percent = 100;
}
return [
percent,
`${percent.toFixed(1)}% (zw: ${this.level})`,
];
}
parseZwValue(zwData) {
return this.parseValueFromZwValue(zwData);
}
setIdentityValue(propertyValue) {
const zwData = propertyValue;
return [zwData, zwData.toString()];
}
/**
* @method setOnOffLevelValue
*
* Special function used when a device only supports level and doesn't
* support on/off.
*/
setOnOffLevelValue(value) {
let percent;
if (this.name === 'on') {
percent = value ? 100 : 0;
} else {
percent = value;
}
return this.setLevelValue(percent);
}
/**
* @method setLevelValue
*
* The ZWave spec for COMMAND_CLASS_SWITCH_MULTILEVEL maps the values
* 0-99 onto 0%-100%
*
* For simplicity we treat it as an identity mapping but treat 99%
* and 100% as the same.
*/
setLevelValue(percent) {
if (typeof percent !== 'number') {
console.error('setLevelValue passed a non-numeric percentage:',
percent, '- ignoring');
return;
}
if (this.hasOwnProperty('min')) {
percent = Math.max(percent, this.min);
}
if (this.hasOwnProperty('max')) {
percent = Math.min(percent, this.max);
}
this.level = Math.round(Math.min(Math.max(percent, 0), 99));
return [
this.level,
`zw: ${this.level} (${percent.toFixed(1)}%)`,
];
}
/**
* @returns a promise which resolves to the updated value.
*
* @note it is possible that the updated value doesn't match
* the value passed in.
*/
setValue(propertyValue) {
let deferredSet = this.deferredSet;
if (!deferredSet) {
deferredSet = new Deferred();
this.deferredSet = deferredSet;
}
if (!this.valueId) {
deferredSet.reject(
`setProperty property ${this.name} for node ${this.device.id
} doesn't have a valueId`);
return deferredSet.promise;
}
const zwValue = this.device.zwValues[this.valueId];
if (zwValue.read_only) {
deferredSet.reject(
`setProperty property ${this.name} for node ${this.device.id
} is read-only`);
return deferredSet.promise;
}
this.setCachedValue(propertyValue);
const [zwValueData, logData] = this.setZwValueFromValue(propertyValue);
console.log('setProperty property:', this.name,
'for:', this.device.name,
'valueId:', this.valueId,
'value:', logData);
this.device.adapter.zwave.setValue(zwValue.node_id, zwValue.class_id,
zwValue.instance, zwValue.index,
zwValueData);
return deferredSet.promise;
}
}
module.exports = ZWaveProperty;