-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathOneWaySwitch.js
85 lines (75 loc) · 2.32 KB
/
OneWaySwitch.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
/* Sample module - Just for logging
*/
/* jshint esversion: 6, strict: true, node: true */
'use strict';
/**
* @type {HandlerPattern}
*/
var HandlerPattern = require('./handlerpattern.js');
var log = require('debug')('OneWaySwitch');
/**
* @class A custom handler that switches back a homekit switch to off after one second
* @extends HandlerPattern
*/
class OneWaySwitch extends HandlerPattern {
constructor(knxAPI) {
super(knxAPI); // call the super constructor first. Always.
this.timer = undefined;
}
/****
* onKNXValueChange is invoked if a Bus value for one of the bound addresses is received
*
*/
onKNXValueChange(field, oldValue, knxValue) {
// value for HomeKit
var newValue;
log('INFO: onKNXValueChange(' + field + ", "+ oldValue + ", "+ knxValue+ ") -- ignored.");
} // onBusValueChange
/****
* onHKValueChange is invoked if HomeKit is changing characteristic values
*
*/
onHKValueChange(field, oldValue, newValue) {
//
log('INFO: onHKValueChange(' + field + ", "+ oldValue + ", "+ newValue + ")");
if (field === 'On') {
if (this.timer) {
log("Aborting old timer!");
clearTimeout(this.timer);
} else if (newValue==1){
log("Sending value "+ this.myAPI.getLocalConstant('SwitchSends') || 0 + " to the bus");
this.myAPI.knxWrite('On', this.myAPI.getLocalConstant('SwitchSends') || 0);
this.timer = setTimeout(function(field) {
// in 1000ms set the value back to off:
log('Timer reached!');
this.myAPI.setValue(field, 0);
this.timer = undefined;
}.bind(this), 300, field); // 300ms to give HomeKit a chance to switch forth and back
}
}
} // onHKValueChange
} // class
module.exports= OneWaySwitch;
/* **********************************************************************************************************************
* The config for that should look like this
* Reverse keyword is not allowed for custom handlers
*
* SwitchSends can be set to 1 or 0, so it only sends a 1 or a zero to the bus when the switch is triggered.
* The switch in HomeKit resets itself to off after 0,3 seconds
*
"Services": [{
"ServiceType": "Lightbulb",
"Handler": "OneWaySwitch",
"ServiceName": "Centrally off",
"Characteristics": [{
"Type": "On",
"Set": "1/2/1"
}],
"LocalConstants": {
"SwitchSends": 0
}
}]
*
*
*
*/