forked from anthonywebb/homebridge-cbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trigger-accessory.js
64 lines (52 loc) · 1.66 KB
/
trigger-accessory.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';
let Service;
let Characteristic;
let CBusAccessory;
let uuid;
const chalk = require('chalk');
const ms = require('ms');
const cbusUtils = require('../lib/cbus-utils.js');
const FILE_ID = cbusUtils.extractIdentifierFromFileName(__filename);
module.exports = function (_service, _characteristic, _accessory, _uuid) {
Service = _service;
Characteristic = _characteristic;
CBusAccessory = _accessory;
uuid = _uuid;
return CBusTriggerAccessory;
};
function CBusTriggerAccessory(platform, accessoryData) {
// initialize the parent
CBusAccessory.call(this, platform, accessoryData);
/*
try {
this.action = cbusUtils.integerise(accessoryData.action);
} catch (err) {
throw new Error(`action value '${accessoryData.action}' for accessory '${this.name} is not an integer`);
}
*/
// register the on-off service
this.service = this.addService(new Service.Switch(this.name));
this.service.getCharacteristic(Characteristic.On)
.on('set', this.setTrigger.bind(this));
}
CBusTriggerAccessory.prototype.setTrigger = function (trigger, callback, context) {
if (context === `event`) {
// context helps us avoid a never-ending loop
callback();
} else {
console.assert((trigger === 1) || (trigger === 0) || (trigger === true) || (trigger === false));
if (trigger) {
this.client.triggerAction(this.netId, this.action, () => {
this.timeout = setTimeout(() => {
this.service.getCharacteristic(Characteristic.On).setValue(0);
}, 500);
});
}
callback();
}
};
CBusTriggerAccessory.prototype.processClientData = function (err, message) {
if (!err) {
console.assert(typeof message.level !== `undefined`, `message.level must be defined`);
}
};