-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathkmpcil.ts
204 lines (190 loc) · 8.8 KB
/
kmpcil.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
import {Zcl} from 'zigbee-herdsman';
import fz from '../converters/fromZigbee';
import tz from '../converters/toZigbee';
import * as constants from '../lib/constants';
import * as exposes from '../lib/exposes';
import * as reporting from '../lib/reporting';
import * as globalStore from '../lib/store';
import {DefinitionWithExtend, Fz, KeyValue, Publish} from '../lib/types';
import * as utils from '../lib/utils';
const e = exposes.presets;
const ea = exposes.access;
const kmpcilOptions = {
presence_timeout_dc: () => {
return e
.numeric('presence_timeout_dc', ea.STATE)
.withValueMin(60)
.withDescription('Time in seconds after which presence is cleared after detecting it (default 60 seconds) while in DC.');
},
presence_timeout_battery: () => {
return e
.numeric('presence_timeout_battery', ea.STATE)
.withValueMin(120)
.withDescription('Time in seconds after which presence is cleared after detecting it (default 420 seconds) while in Battery.');
},
};
function handleKmpcilPresence(model: DefinitionWithExtend, msg: Fz.Message, publish: Publish, options: KeyValue, meta: Fz.Meta): KeyValue {
const useOptionsTimeoutBattery = options && options.hasOwnProperty('presence_timeout_battery');
const timeoutBattery = useOptionsTimeoutBattery ? options.presence_timeout_battery : 420; // 100 seconds by default
const useOptionsTimeoutDc = options && options.hasOwnProperty('presence_timeout_dc');
const timeoutDc = useOptionsTimeoutDc ? options.presence_timeout_dc : 60;
const mode = meta.state ? meta.state['power_state'] : false;
const timeout = Number(mode ? timeoutDc : timeoutBattery);
// Stop existing timer because motion is detected and set a new one.
clearTimeout(globalStore.getValue(msg.endpoint, 'timer'));
const timer = setTimeout(() => publish({presence: false}), timeout * 1000);
globalStore.putValue(msg.endpoint, 'timer', timer);
return {presence: true};
}
const kmpcilConverters = {
presence_binary_input: {
cluster: 'genBinaryInput',
type: ['attributeReport', 'readResponse'],
convert: (model, msg, publish, options, meta) => {
const payload = handleKmpcilPresence(model, msg, publish, options, meta);
if (msg.data.hasOwnProperty('presentValue')) {
const presentValue = msg.data['presentValue'];
payload.power_state = (presentValue & 0x01) > 0;
payload.occupancy = (presentValue & 0x04) > 0;
payload.vibration = (presentValue & 0x02) > 0;
}
return payload;
},
} satisfies Fz.Converter,
presence_power: {
cluster: 'genPowerCfg',
type: ['attributeReport', 'readResponse'],
options: [kmpcilOptions.presence_timeout_dc(), kmpcilOptions.presence_timeout_battery()],
convert: (model, msg, publish, options, meta) => {
const payload = handleKmpcilPresence(model, msg, publish, options, meta);
if (msg.data.hasOwnProperty('batteryVoltage')) {
payload.voltage = msg.data['batteryVoltage'] * 100;
if (model.meta && model.meta.battery && model.meta.battery.voltageToPercentage) {
// @ts-expect-error ignore
payload.battery = utils.batteryVoltageToPercentage(payload.voltage, model.meta.battery.voltageToPercentage);
}
}
return payload;
},
} satisfies Fz.Converter,
};
const definitions: DefinitionWithExtend[] = [
{
zigbeeModel: ['RES005'],
model: 'KMPCIL_RES005',
vendor: 'KMPCIL',
description: 'Environment sensor',
exposes: [
e.battery(),
e.temperature(),
e.humidity(),
e.pressure(),
e.illuminance().withAccess(ea.STATE_GET),
e.illuminance_lux().withAccess(ea.STATE_GET),
e.occupancy(),
e.switch(),
],
fromZigbee: [fz.battery, fz.temperature, fz.humidity, fz.pressure, fz.illuminance, fz.kmpcil_res005_occupancy, fz.kmpcil_res005_on_off],
toZigbee: [tz.kmpcil_res005_on_off, tz.illuminance],
configure: async (device, coordinatorEndpoint) => {
const endpoint = device.getEndpoint(8);
const binds = [
'genPowerCfg',
'msTemperatureMeasurement',
'msRelativeHumidity',
'msPressureMeasurement',
'msIlluminanceMeasurement',
'genBinaryInput',
'genBinaryOutput',
];
await reporting.bind(endpoint, coordinatorEndpoint, binds);
await reporting.temperature(endpoint);
await reporting.humidity(endpoint);
const payloadBattery = [
{
attribute: 'batteryPercentageRemaining',
minimumReportInterval: 1,
maximumReportInterval: 120,
reportableChange: 1,
},
];
await endpoint.configureReporting('genPowerCfg', payloadBattery);
const payload = [
{attribute: 'measuredValue', minimumReportInterval: 5, maximumReportInterval: constants.repInterval.HOUR, reportableChange: 200},
];
await endpoint.configureReporting('msIlluminanceMeasurement', payload);
const payloadPressure = [
{
// 0 = measuredValue, override dataType from int16 to uint16
// https://github.com/Koenkk/zigbee-herdsman/pull/191/files?file-filters%5B%5D=.ts#r456569398
attribute: {ID: 0, type: Zcl.DataType.UINT16},
minimumReportInterval: 2,
maximumReportInterval: constants.repInterval.HOUR,
reportableChange: 3,
},
];
await endpoint.configureReporting('msPressureMeasurement', payloadPressure);
const options = {disableDefaultResponse: true};
await endpoint.write('genBinaryInput', {0x0051: {value: 0x01, type: 0x10}}, options);
await endpoint.write('genBinaryInput', {0x0101: {value: 25, type: 0x23}}, options);
const payloadBinaryInput = [
{
attribute: 'presentValue',
minimumReportInterval: 0,
maximumReportInterval: 30,
reportableChange: 1,
},
];
await endpoint.configureReporting('genBinaryInput', payloadBinaryInput);
await endpoint.write('genBinaryOutput', {0x0051: {value: 0x01, type: 0x10}}, options);
const payloadBinaryOutput = [
{
attribute: 'presentValue',
minimumReportInterval: 0,
maximumReportInterval: 30,
reportableChange: 1,
},
];
await endpoint.configureReporting('genBinaryOutput', payloadBinaryOutput);
},
},
{
zigbeeModel: ['tagv1'],
model: 'KMPCIL-tag-001',
vendor: 'KMPCIL',
description: 'Arrival sensor',
fromZigbee: [kmpcilConverters.presence_binary_input, kmpcilConverters.presence_power, fz.temperature],
exposes: [
e.battery(),
e.presence(),
e.binary('power_state', exposes.access.STATE, true, false),
e.occupancy(),
e.vibration(),
e.temperature(),
],
toZigbee: [],
meta: {battery: {voltageToPercentage: '3V_1500_2800'}},
configure: async (device, coordinatorEndpoint) => {
const endpoint = device.getEndpoint(1);
for (const cluster of ['msTemperatureMeasurement', 'genPowerCfg', 'genBinaryInput']) {
// This sleep here(and the sleep) after is to allow the command to be
// fully sent to coordinator. In case repeater involved and the repeater
// is litted in resources, we may want to give some time so that the sequence of
// commands does not overwhelm the repeater.
await utils.sleep(2000);
await endpoint.bind(cluster, coordinatorEndpoint);
}
await utils.sleep(1000);
const p = reporting.payload('batteryVoltage', 0, 10, 1);
await endpoint.configureReporting('genPowerCfg', p);
await utils.sleep(1000);
const p2 = reporting.payload('presentValue', 0, 300, 1);
await endpoint.configureReporting('genBinaryInput', p2);
await utils.sleep(1000);
await reporting.temperature(endpoint);
await endpoint.read('genBinaryInput', ['presentValue']);
},
},
];
export default definitions;
module.exports = definitions;