-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDIYRuZ_SensBoard.js
executable file
·168 lines (151 loc) · 5.8 KB
/
DIYRuZ_SensBoard.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
const {
fromZigbeeConverters,
exposes
} = require('zigbee-herdsman-converters');
const {
postfixWithEndpointName,
getKey,
precisionRound
} = require('zigbee-herdsman-converters/lib/utils');
const {
bind
} = require('zigbee-herdsman-converters/lib/reporting');
const CHANNELS_STARTING_ENDPOINT = 2;
const CHANNELS_COUNT = 3;
const ADC_ROUNDING_PRECISION = 3;
const MINUTE = 60;
const defaultReportingConfig = {
minimumReportInterval: 0,
maximumReportInterval: 6 * MINUTE,
reportableChange: 0,
};
const endpointNames = {
l1: 2,
l2: 3,
l3: 4
};
const adcChannels = ['l1', 'l2'];
const deviceExposes = [
exposes.presets.device_temperature(),
exposes.numeric('voltage', exposes.access.STATE).withDescription('Battery voltage').withUnit('mV'),
exposes.presets.battery(),
...Object.keys(endpointNames).map(epName => exposes.numeric('present_value', exposes.access.STATE).withEndpoint(epName).withDescription('Current data in device memory')),
...Object.keys(endpointNames).map(epName => exposes.numeric('delta_value', exposes.access.STATE).withEndpoint(epName).withDescription('Last reported value')),
...Object.keys(endpointNames).map(epName => exposes.numeric('total_value', exposes.access.STATE).withEndpoint(epName).withDescription('Total value processed by device')),
...Object.keys(endpointNames).map(epName => exposes.enum('action', exposes.access.SET, ['clear_counter_stats']).withEndpoint(epName).withDescription("Actions")),
...adcChannels.map(epName => exposes.numeric('adc_value', exposes.access.STATE).withEndpoint(epName).withDescription('ADC voltage').withUnit('V')),
...Object.keys(endpointNames).map(epName => exposes.presets.contact().withEndpoint(epName)),
exposes.presets.action(Object.keys(endpointNames).map(epName => `toggle_${epName}`))
];
const adcValueConverter = {
cluster: 'genAnalogInput',
type: ['attributeReport', 'readResponse'],
convert: (model, msg, publish, options, meta) => {
const adcPropName = postfixWithEndpointName('adc_value', msg, model);
const {
presentValue
} = msg.data;
return {
[adcPropName]: precisionRound(presentValue, ADC_ROUNDING_PRECISION)
}
},
};
const pulseCounterConverter = {
cluster: 'genMultistateInput',
type: ['attributeReport', 'readResponse'],
convert: (model, msg, publish, options, meta) => {
const totalPropName = postfixWithEndpointName('total_value', msg, model);
const presentPropName = postfixWithEndpointName('present_value', msg, model);
const deltaPropName = postfixWithEndpointName('delta_value', msg, model);
const {
state = {}
} = meta;
const totalValue = state[totalPropName] || 0;
const prevValue = state[presentPropName] || 0;
const {
presentValue
} = msg.data;
const diff = prevValue <= presentValue ? presentValue - prevValue : presentValue;
return {
[presentPropName]: presentValue,
[totalPropName]: totalValue + diff,
[deltaPropName]: diff
}
}
};
const clearPulseCounterConverter = {
key: ['action'],
convertSet: async (entity, key, value, meta) => {
const result = {};
switch (value) {
case 'clear_counter_stats':
await entity.write('genMultistateInput', {
presentValue: 0
});
const names = ['total_value', 'present_value', 'delta_value']
const endpointName = getKey(endpointNames, entity.ID);
result.state = {};
names.forEach(name => result.state[`${name}_${endpointName}`] = 0);
break;
default:
meta.logger.error('Not implemented' + value);
break;
}
return result;
},
};
const device = {
zigbeeModel: ['DIYRuZ_SensBoard'],
model: 'DIYRuZ_SensBoard',
vendor: 'DIYRuZ',
description: '[DIYRuZ_SensBoard](http://xxx.ru)',
fromZigbee: [
adcValueConverter,
pulseCounterConverter,
fromZigbeeConverters.device_temperature,
fromZigbeeConverters.battery,
fromZigbeeConverters.ias_contact_alarm_1,
fromZigbeeConverters.command_toggle
],
toZigbee: [
clearPulseCounterConverter
],
meta: {
configureKey: 15,
multiEndpoint: true,
battery: {
voltageToPercentage: '3V_2500_3200' // FYI: https://github.com/Koenkk/zigbee-herdsman-converters/blob/239822497aae9ab51d22c80206b9a42a4265a94a/lib/utils.js#L118
}
},
configure: async (device, coordinatorEndpoint) => {
const firstEp = device.getEndpoint(1);
await bind(firstEp, coordinatorEndpoint, [
'genDeviceTempCfg', 'genPowerCfg'
]);
await firstEp.configureReporting('genDeviceTempCfg', [{
...defaultReportingConfig,
attribute: 'currentTemperature'
}]);
await firstEp.configureReporting('genPowerCfg', [{
...defaultReportingConfig,
attribute: 'batteryVoltage',
}]);
for (let i = 0; i < CHANNELS_COUNT; i++) {
const ep = device.getEndpoint(CHANNELS_STARTING_ENDPOINT + i);
await bind(ep, coordinatorEndpoint, [
'genMultistateInput', 'genAnalogInput', 'ssIasZone', 'genOnOff'
]);
await ep.configureReporting('genMultistateInput', [{
...defaultReportingConfig,
attribute: 'presentValue',
}]);
await ep.configureReporting('genAnalogInput', [{
...defaultReportingConfig,
attribute: 'presentValue',
}]);
}
},
exposes: deviceExposes,
endpoint: (device) => endpointNames
};
module.exports = device;