forked from kasteleman/com.innr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
64 lines (53 loc) · 2.09 KB
/
app.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
'use strict';
const Homey = require('homey');
class INNRZigbeeApp extends Homey.App {
onInit() {
this.log('init');
this.actionStartDimLevelChange = new Homey.FlowCardAction('action_DIM_startLevelChange')
.register()
.registerRunListener(this._actionStartDimLevelChangeRunListener.bind(this));
this.actionStopDimLevelChange = new Homey.FlowCardAction('action_DIM_stopLevelChange')
.register()
.registerRunListener(this._actionStopDimLevelChangeRunListener.bind(this));
}
async _actionStartDimLevelChangeRunListener(args, state) {
if (!args.hasOwnProperty('direction')) return Promise.reject('direction_property_missing');
args.device.log('FlowCardAction triggered to start dim level change in direction', args.direction, 'with rate', 100 * args.rate);
let moveObj = {
movemode: args.direction,
rate: 100 * args.rate,
};
if (args.device.node.endpoints[0].clusters['genLevelCtrl']) {
return await args.device.node.endpoints[0].clusters['genLevelCtrl'].do("moveWithOnOff", moveObj)
}
return Promise.reject('unknown_error');
}
async _actionStopDimLevelChangeRunListener(args, state) {
args.device.log("FlowCardAction triggered to stop dim level change");
if (args.device.node.endpoints[0].clusters["genLevelCtrl"]) {
try {
// 1. trigger stop command
await args.device.node.endpoints[0].clusters["genLevelCtrl"].do("stop", {});
// 2. when stop command is successfull, trigger currentLevel read command
const result = await args.device.node.endpoints[0].clusters["genLevelCtrl"].read("currentLevel");
args.device.log('Updating capabilities after stopping, onoff:', result > 1, 'dim:', result / 254);
// 3. update onoff capability
if (args.device.hasCapability("onoff")) {
args.device.setCapabilityValue("onoff", result > 1);
}
// 4. update dim capability
if (args.device.hasCapability("dim")) {
args.device.setCapabilityValue("dim", result / 254);
}
}
catch (err) {
args.device.log(err);
return Promise.reject(err);;
}
}
else {
return Promise.reject('unknown_error');
}
}
}
module.exports = INNRZigbeeApp;