homebridge-http-webhooks-plus 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +543 -0
- package/LICENSE +674 -0
- package/README.md +505 -0
- package/config.schema.json +1821 -0
- package/docs/COMPATIBILITY.md +73 -0
- package/index.js +35 -0
- package/package.json +56 -0
- package/src/Constants.js +12 -0
- package/src/Server.js +192 -0
- package/src/Util.js +51 -0
- package/src/homekit/HttpWebHooksPlatform.js +136 -0
- package/src/homekit/accessories/HttpWebHookCarbonDioxideSensorAccessory.js +75 -0
- package/src/homekit/accessories/HttpWebHookDoorbellAccessory.js +139 -0
- package/src/homekit/accessories/HttpWebHookFanv2Accessory.js +320 -0
- package/src/homekit/accessories/HttpWebHookGarageDoorOpenerAccessory.js +142 -0
- package/src/homekit/accessories/HttpWebHookLightBulbAccessory.js +153 -0
- package/src/homekit/accessories/HttpWebHookLockMechanismAccessory.js +125 -0
- package/src/homekit/accessories/HttpWebHookOutletAccessory.js +125 -0
- package/src/homekit/accessories/HttpWebHookPushButtonAccessory.js +93 -0
- package/src/homekit/accessories/HttpWebHookSecurityAccessory.js +104 -0
- package/src/homekit/accessories/HttpWebHookSensorAccessory.js +169 -0
- package/src/homekit/accessories/HttpWebHookStatelessSwitchAccessory.js +101 -0
- package/src/homekit/accessories/HttpWebHookSwitchAccessory.js +95 -0
- package/src/homekit/accessories/HttpWebHookThermostatAccessory.js +164 -0
- package/src/homekit/accessories/HttpWebHookValveAccessory.js +151 -0
- package/src/homekit/accessories/HttpWebHookWindowCoveringAccessory.js +203 -0
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
const Constants = require('../../Constants');
|
|
2
|
+
|
|
3
|
+
function HttpWebHookSensorAccessory(ServiceParam, CharacteristicParam, platform, sensorConfig) {
|
|
4
|
+
Service = ServiceParam;
|
|
5
|
+
Characteristic = CharacteristicParam;
|
|
6
|
+
|
|
7
|
+
this.platform = platform;
|
|
8
|
+
this.log = platform.log;
|
|
9
|
+
this.storage = platform.storage;
|
|
10
|
+
|
|
11
|
+
this.id = sensorConfig["id"];
|
|
12
|
+
this.name = sensorConfig["name"];
|
|
13
|
+
this.type = sensorConfig["type"];
|
|
14
|
+
this.autoRelease = sensorConfig["autoRelease"];
|
|
15
|
+
this.autoReleaseTime = sensorConfig["autoReleaseTime"] || Constants.DEFAULT_SENSOR_TIMEOUT;
|
|
16
|
+
|
|
17
|
+
this.informationService = new Service.AccessoryInformation();
|
|
18
|
+
this.informationService.setCharacteristic(Characteristic.Manufacturer, "HttpWebHooksPlatform");
|
|
19
|
+
this.informationService.setCharacteristic(Characteristic.Model, "HttpWebHookSensorAccessory-" + this.name);
|
|
20
|
+
this.informationService.setCharacteristic(Characteristic.SerialNumber, "HttpWebHookSensorAccessory-" + this.id);
|
|
21
|
+
|
|
22
|
+
if (this.type === "contact") {
|
|
23
|
+
this.service = new Service.ContactSensor(this.name);
|
|
24
|
+
this.service.getCharacteristic(Characteristic.ContactSensorState).on('get', this.getState.bind(this));
|
|
25
|
+
}
|
|
26
|
+
else if (this.type === "motion") {
|
|
27
|
+
this.service = new Service.MotionSensor(this.name);
|
|
28
|
+
this.service.getCharacteristic(Characteristic.MotionDetected).on('get', this.getState.bind(this));
|
|
29
|
+
}
|
|
30
|
+
else if (this.type === "occupancy") {
|
|
31
|
+
this.service = new Service.OccupancySensor(this.name);
|
|
32
|
+
this.service.getCharacteristic(Characteristic.OccupancyDetected).on('get', this.getState.bind(this));
|
|
33
|
+
}
|
|
34
|
+
else if (this.type === "smoke") {
|
|
35
|
+
this.service = new Service.SmokeSensor(this.name);
|
|
36
|
+
this.service.getCharacteristic(Characteristic.SmokeDetected).on('get', this.getState.bind(this));
|
|
37
|
+
}
|
|
38
|
+
else if (this.type === "humidity") {
|
|
39
|
+
this.service = new Service.HumiditySensor(this.name);
|
|
40
|
+
this.service.getCharacteristic(Characteristic.CurrentRelativeHumidity).on('get', this.getState.bind(this));
|
|
41
|
+
}
|
|
42
|
+
else if (this.type === "temperature") {
|
|
43
|
+
this.service = new Service.TemperatureSensor(this.name);
|
|
44
|
+
this.service.getCharacteristic(Characteristic.CurrentTemperature).setProps({
|
|
45
|
+
minValue : -100,
|
|
46
|
+
maxValue : 140
|
|
47
|
+
}).on('get', this.getState.bind(this));
|
|
48
|
+
}
|
|
49
|
+
else if (this.type === "airquality") {
|
|
50
|
+
this.service = new Service.AirQualitySensor(this.name);
|
|
51
|
+
this.service.getCharacteristic(Characteristic.AirQuality).on('get', this.getState.bind(this));
|
|
52
|
+
}
|
|
53
|
+
else if (this.type === "light") {
|
|
54
|
+
this.service = new Service.LightSensor(this.name);
|
|
55
|
+
this.service.getCharacteristic(Characteristic.CurrentAmbientLightLevel).on('get', this.getState.bind(this));
|
|
56
|
+
}
|
|
57
|
+
else if (this.type === "leak") {
|
|
58
|
+
this.service = new Service.LeakSensor(this.name);
|
|
59
|
+
this.service.getCharacteristic(Characteristic.LeakDetected).on('get', this.getState.bind(this));
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
HttpWebHookSensorAccessory.prototype.changeFromServer = function(urlParams) {
|
|
64
|
+
var cached = this.storage.getItemSync("http-webhook-" + this.id);
|
|
65
|
+
var isNumberBased = this.type === "leak" || this.type === "humidity" || this.type === "temperature" || this.type === "airquality" || this.type === "light";
|
|
66
|
+
if (cached === undefined) {
|
|
67
|
+
cached = isNumberBased ? 0 : false;
|
|
68
|
+
}
|
|
69
|
+
var noUrlValue = isNumberBased ? urlParams.value === undefined : urlParams.state === undefined;
|
|
70
|
+
if (noUrlValue) {
|
|
71
|
+
this.log.debug(this.name + ": No urlValue");
|
|
72
|
+
return {
|
|
73
|
+
"success" : true,
|
|
74
|
+
"state" : cached
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
var urlValue = isNumberBased ? urlParams.value : urlParams.state === "true";
|
|
78
|
+
this.log.debug(this.name + ": urlValue: "+ urlValue);
|
|
79
|
+
this.storage.setItemSync("http-webhook-" + this.id, urlValue);
|
|
80
|
+
this.log.debug(this.name + ": cached: "+ cached);
|
|
81
|
+
this.log.debug(this.name + ": cached !== urlValue: "+ (cached !== urlValue));
|
|
82
|
+
if (cached !== urlValue) {
|
|
83
|
+
this.log(this.name + ": Change HomeKit value for " + this.type + " sensor to '%s'.", urlValue);
|
|
84
|
+
|
|
85
|
+
if (this.type === "contact") {
|
|
86
|
+
this.service.getCharacteristic(Characteristic.ContactSensorState).updateValue(urlValue ? Characteristic.ContactSensorState.CONTACT_DETECTED : Characteristic.ContactSensorState.CONTACT_NOT_DETECTED, undefined, Constants.CONTEXT_FROM_WEBHOOK);
|
|
87
|
+
if (this.autoRelease) {
|
|
88
|
+
setTimeout(function() {
|
|
89
|
+
this.storage.setItemSync("http-webhook-" + this.id, true);
|
|
90
|
+
this.service.getCharacteristic(Characteristic.ContactSensorState).updateValue(Characteristic.ContactSensorState.CONTACT_DETECTED, undefined, Constants.CONTEXT_FROM_TIMEOUTCALL);
|
|
91
|
+
}.bind(this), this.autoReleaseTime);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
else if (this.type === "motion") {
|
|
95
|
+
this.service.getCharacteristic(Characteristic.MotionDetected).updateValue(urlValue, undefined, Constants.CONTEXT_FROM_WEBHOOK);
|
|
96
|
+
if (this.autoRelease) {
|
|
97
|
+
setTimeout(function() {
|
|
98
|
+
this.storage.setItemSync("http-webhook-" + this.id, false);
|
|
99
|
+
this.service.getCharacteristic(Characteristic.MotionDetected).updateValue(false, undefined, Constants.CONTEXT_FROM_TIMEOUTCALL);
|
|
100
|
+
}.bind(this), this.autoReleaseTime);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
else if (this.type === "occupancy") {
|
|
104
|
+
this.service.getCharacteristic(Characteristic.OccupancyDetected).updateValue(urlValue ? Characteristic.OccupancyDetected.OCCUPANCY_DETECTED : Characteristic.OccupancyDetected.OCCUPANCY_NOT_DETECTED, undefined, Constants.CONTEXT_FROM_WEBHOOK);
|
|
105
|
+
if (this.autoRelease) {
|
|
106
|
+
setTimeout(function() {
|
|
107
|
+
this.storage.setItemSync("http-webhook-" + this.id, false);
|
|
108
|
+
this.service.getCharacteristic(Characteristic.OccupancyDetected).updateValue(Characteristic.OccupancyDetected.OCCUPANCY_NOT_DETECTED, undefined, Constants.CONTEXT_FROM_TIMEOUTCALL);
|
|
109
|
+
}.bind(this), this.autoReleaseTime);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
else if (this.type === "smoke") {
|
|
113
|
+
this.service.getCharacteristic(Characteristic.SmokeDetected).updateValue(urlValue ? Characteristic.SmokeDetected.SMOKE_DETECTED : Characteristic.SmokeDetected.SMOKE_NOT_DETECTED, undefined, Constants.CONTEXT_FROM_WEBHOOK);
|
|
114
|
+
}
|
|
115
|
+
else if (this.type === "humidity") {
|
|
116
|
+
this.service.getCharacteristic(Characteristic.CurrentRelativeHumidity).updateValue(urlValue, undefined, Constants.CONTEXT_FROM_WEBHOOK);
|
|
117
|
+
}
|
|
118
|
+
else if (this.type === "temperature") {
|
|
119
|
+
this.service.getCharacteristic(Characteristic.CurrentTemperature).updateValue(urlValue, undefined, Constants.CONTEXT_FROM_WEBHOOK);
|
|
120
|
+
}
|
|
121
|
+
else if (this.type === "airquality") {
|
|
122
|
+
this.service.getCharacteristic(Characteristic.AirQuality).updateValue(urlValue, undefined, Constants.CONTEXT_FROM_WEBHOOK);
|
|
123
|
+
}
|
|
124
|
+
else if (this.type === "light") {
|
|
125
|
+
urlValue = parseFloat(urlValue)
|
|
126
|
+
this.service.getCharacteristic(Characteristic.CurrentAmbientLightLevel).updateValue(urlValue, undefined, Constants.CONTEXT_FROM_WEBHOOK);
|
|
127
|
+
}
|
|
128
|
+
else if (this.type === "leak") {
|
|
129
|
+
this.service.getCharacteristic(Characteristic.LeakDetected).updateValue(urlValue, undefined, Constants.CONTEXT_FROM_WEBHOOK);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
}
|
|
133
|
+
return {
|
|
134
|
+
"success" : true
|
|
135
|
+
};
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
HttpWebHookSensorAccessory.prototype.getState = function(callback) {
|
|
139
|
+
this.log.debug(this.name + ": Getting current state for '%s'...", this.id);
|
|
140
|
+
var state = this.storage.getItemSync("http-webhook-" + this.id);
|
|
141
|
+
this.log.debug(this.name + ": State for '%s' is '%s'", this.id, state);
|
|
142
|
+
if (state === undefined) {
|
|
143
|
+
state = false; // force default value if undefined
|
|
144
|
+
}
|
|
145
|
+
if (this.type === "contact") {
|
|
146
|
+
callback(null, state ? Characteristic.ContactSensorState.CONTACT_DETECTED : Characteristic.ContactSensorState.CONTACT_NOT_DETECTED);
|
|
147
|
+
}
|
|
148
|
+
else if (this.type === "smoke") {
|
|
149
|
+
callback(null, state ? Characteristic.SmokeDetected.SMOKE_DETECTED : Characteristic.SmokeDetected.SMOKE_NOT_DETECTED);
|
|
150
|
+
}
|
|
151
|
+
else if (this.type === "occupancy") {
|
|
152
|
+
callback(null, state ? Characteristic.OccupancyDetected.OCCUPANCY_DETECTED : Characteristic.OccupancyDetected.OCCUPANCY_NOT_DETECTED);
|
|
153
|
+
}
|
|
154
|
+
else if (this.type === "light") {
|
|
155
|
+
if (state === false) {
|
|
156
|
+
state = 0.0001 // 0.0001 is minimum valid value for light
|
|
157
|
+
}
|
|
158
|
+
callback(null, parseFloat(state));
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
callback(null, state);
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
HttpWebHookSensorAccessory.prototype.getServices = function() {
|
|
166
|
+
return [ this.service, this.informationService ];
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
module.exports = HttpWebHookSensorAccessory;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
const Constants = require('../../Constants');
|
|
2
|
+
|
|
3
|
+
function HttpWebHookStatelessSwitchAccessory(ServiceParam, CharacteristicParam, platform, statelessSwitchConfig) {
|
|
4
|
+
Service = ServiceParam;
|
|
5
|
+
Characteristic = CharacteristicParam;
|
|
6
|
+
|
|
7
|
+
this.platform = platform;
|
|
8
|
+
this.log = platform.log;
|
|
9
|
+
this.storage = platform.storage;
|
|
10
|
+
|
|
11
|
+
this.id = statelessSwitchConfig["id"];
|
|
12
|
+
this.type = "statelessswitch";
|
|
13
|
+
this.name = statelessSwitchConfig["name"];
|
|
14
|
+
this.buttons = statelessSwitchConfig["buttons"] || [];
|
|
15
|
+
|
|
16
|
+
this.service = [];
|
|
17
|
+
for (var index = 0; index < this.buttons.length; index++) {
|
|
18
|
+
var single_press = this.buttons[index]["single_press"] === undefined ? true : this.buttons[index]["single_press"];
|
|
19
|
+
var double_press = this.buttons[index]["double_press"] === undefined ? true : this.buttons[index]["double_press"];
|
|
20
|
+
var long_press = this.buttons[index]["long_press"] === undefined ? true : this.buttons[index]["long_press"];
|
|
21
|
+
var button = new Service.StatelessProgrammableSwitch(this.buttons[index].name, '' + index);
|
|
22
|
+
button.getCharacteristic(Characteristic.ProgrammableSwitchEvent).setProps(GetStatelessSwitchProps(single_press, double_press, long_press));
|
|
23
|
+
button.getCharacteristic(Characteristic.ServiceLabelIndex).setValue(index + 1);
|
|
24
|
+
this.service.push(button);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
var informationService = new Service.AccessoryInformation();
|
|
28
|
+
informationService.setCharacteristic(Characteristic.Manufacturer, "HttpWebHooksPlatform");
|
|
29
|
+
informationService.setCharacteristic(Characteristic.Model, "HttpWebHookStatelessSwitchAccessory-" + this.name);
|
|
30
|
+
informationService.setCharacteristic(Characteristic.SerialNumber, "HttpWebHookStatelessSwitchAccessory-" + this.id);
|
|
31
|
+
this.service.push(informationService);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
HttpWebHookStatelessSwitchAccessory.prototype.changeFromServer = function(urlParams) {
|
|
35
|
+
if (urlParams.event && urlParams.event) {
|
|
36
|
+
for (var index = 0; index < this.service.length; index++) {
|
|
37
|
+
var serviceName = this.service[index].getCharacteristic(Characteristic.Name).value;
|
|
38
|
+
if (serviceName === urlParams.buttonName) {
|
|
39
|
+
this.log(this.name + ": Pressing '%s' with event '%i'", urlParams.buttonName, urlParams.event)
|
|
40
|
+
this.service[index].getCharacteristic(Characteristic.ProgrammableSwitchEvent).updateValue(urlParams.event, undefined, Constants.CONTEXT_FROM_WEBHOOK);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
"success" : true
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function GetStatelessSwitchProps(single_press, double_press, long_press) {
|
|
50
|
+
var props;
|
|
51
|
+
if (single_press && !double_press && !long_press) {
|
|
52
|
+
props = {
|
|
53
|
+
minValue : Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS,
|
|
54
|
+
maxValue : Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
if (single_press && double_press && !long_press) {
|
|
58
|
+
props = {
|
|
59
|
+
minValue : Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS,
|
|
60
|
+
maxValue : Characteristic.ProgrammableSwitchEvent.DOUBLE_PRESS
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
if (single_press && !double_press && long_press) {
|
|
64
|
+
props = {
|
|
65
|
+
minValue : Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS,
|
|
66
|
+
maxValue : Characteristic.ProgrammableSwitchEvent.LONG_PRESS,
|
|
67
|
+
validValues : [ Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS, Characteristic.ProgrammableSwitchEvent.LONG_PRESS ]
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
if (!single_press && double_press && !long_press) {
|
|
71
|
+
props = {
|
|
72
|
+
minValue : Characteristic.ProgrammableSwitchEvent.DOUBLE_PRESS,
|
|
73
|
+
maxValue : Characteristic.ProgrammableSwitchEvent.DOUBLE_PRESS
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
if (!single_press && double_press && long_press) {
|
|
77
|
+
props = {
|
|
78
|
+
minValue : Characteristic.ProgrammableSwitchEvent.DOUBLE_PRESS,
|
|
79
|
+
maxValue : Characteristic.ProgrammableSwitchEvent.LONG_PRESS
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
if (!single_press && !double_press && long_press) {
|
|
83
|
+
props = {
|
|
84
|
+
minValue : Characteristic.ProgrammableSwitchEvent.LONG_PRESS,
|
|
85
|
+
maxValue : Characteristic.ProgrammableSwitchEvent.LONG_PRESS
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
if (single_press && double_press && long_press) {
|
|
89
|
+
props = {
|
|
90
|
+
minValue : Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS,
|
|
91
|
+
maxValue : Characteristic.ProgrammableSwitchEvent.LONG_PRESS
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
return props;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
HttpWebHookStatelessSwitchAccessory.prototype.getServices = function() {
|
|
98
|
+
return this.service;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
module.exports = HttpWebHookStatelessSwitchAccessory;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
const Constants = require('../../Constants');
|
|
2
|
+
const Util = require('../../Util');
|
|
3
|
+
|
|
4
|
+
function HttpWebHookSwitchAccessory(ServiceParam, CharacteristicParam, platform, switchConfig) {
|
|
5
|
+
Service = ServiceParam;
|
|
6
|
+
Characteristic = CharacteristicParam;
|
|
7
|
+
|
|
8
|
+
this.platform = platform;
|
|
9
|
+
this.log = platform.log;
|
|
10
|
+
this.storage = platform.storage;
|
|
11
|
+
|
|
12
|
+
this.id = switchConfig["id"];
|
|
13
|
+
this.name = switchConfig["name"];
|
|
14
|
+
this.rejectUnauthorized = switchConfig["rejectUnauthorized"] === undefined ? true: switchConfig["rejectUnauthorized"] === true;
|
|
15
|
+
this.onURL = switchConfig["on_url"] || "";
|
|
16
|
+
this.onMethod = switchConfig["on_method"] || "GET";
|
|
17
|
+
this.onBody = switchConfig["on_body"] || "";
|
|
18
|
+
this.onForm = switchConfig["on_form"] || "";
|
|
19
|
+
this.onHeaders = switchConfig["on_headers"] || "{}";
|
|
20
|
+
this.offURL = switchConfig["off_url"] || "";
|
|
21
|
+
this.offMethod = switchConfig["off_method"] || "GET";
|
|
22
|
+
this.offBody = switchConfig["off_body"] || "";
|
|
23
|
+
this.offForm = switchConfig["off_form"] || "";
|
|
24
|
+
this.offHeaders = switchConfig["off_headers"] || "{}";
|
|
25
|
+
|
|
26
|
+
this.informationService = new Service.AccessoryInformation();
|
|
27
|
+
this.informationService.setCharacteristic(Characteristic.Manufacturer, "HttpWebHooksPlatform");
|
|
28
|
+
this.informationService.setCharacteristic(Characteristic.Model, "HttpWebHookSwitchAccessory-" + this.name);
|
|
29
|
+
this.informationService.setCharacteristic(Characteristic.SerialNumber, "HttpWebHookSwitchAccessory-" + this.id);
|
|
30
|
+
|
|
31
|
+
this.service = new Service.Switch(this.name);
|
|
32
|
+
this.service.getCharacteristic(Characteristic.On).on('get', this.getState.bind(this)).on('set', this.setState.bind(this));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
HttpWebHookSwitchAccessory.prototype.changeFromServer = function(urlParams) {
|
|
36
|
+
var cachedState = this.storage.getItemSync("http-webhook-" + this.id);
|
|
37
|
+
if (cachedState === undefined) {
|
|
38
|
+
cachedState = false;
|
|
39
|
+
}
|
|
40
|
+
if (!urlParams.state) {
|
|
41
|
+
return {
|
|
42
|
+
"success" : true,
|
|
43
|
+
"state" : cachedState
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
var state = urlParams.state;
|
|
48
|
+
var stateBool = state === "true";
|
|
49
|
+
this.storage.setItemSync("http-webhook-" + this.id, stateBool);
|
|
50
|
+
// this.log("[INFO Http WebHook Server] State change of '%s'
|
|
51
|
+
// to '%s'.",accessory.id,stateBool);
|
|
52
|
+
if (cachedState !== stateBool) {
|
|
53
|
+
this.log(this.name + ": Change HomeKit state for switch to '%s'.", stateBool);
|
|
54
|
+
this.service.getCharacteristic(Characteristic.On).updateValue(stateBool, undefined, Constants.CONTEXT_FROM_WEBHOOK);
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
"success" : true
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
HttpWebHookSwitchAccessory.prototype.getState = function(callback) {
|
|
63
|
+
this.log.debug(this.name + ": Getting current state for '%s'...", this.id);
|
|
64
|
+
var state = this.storage.getItemSync("http-webhook-" + this.id);
|
|
65
|
+
if (state === undefined) {
|
|
66
|
+
state = false;
|
|
67
|
+
}
|
|
68
|
+
callback(null, state);
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
HttpWebHookSwitchAccessory.prototype.setState = function(powerOn, callback, context) {
|
|
72
|
+
this.log(this.name + ": Switch state for '%s'...", this.id);
|
|
73
|
+
this.storage.setItemSync("http-webhook-" + this.id, powerOn);
|
|
74
|
+
var urlToCall = this.onURL;
|
|
75
|
+
var urlMethod = this.onMethod;
|
|
76
|
+
var urlBody = this.onBody;
|
|
77
|
+
var urlForm = this.onForm;
|
|
78
|
+
var urlHeaders = this.onHeaders;
|
|
79
|
+
|
|
80
|
+
if (!powerOn) {
|
|
81
|
+
urlToCall = this.offURL;
|
|
82
|
+
urlMethod = this.offMethod;
|
|
83
|
+
urlBody = this.offBody;
|
|
84
|
+
urlForm = this.offForm;
|
|
85
|
+
urlHeaders = this.offHeaders;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
Util.callHttpApi(this.log, urlToCall, urlMethod, urlBody, urlForm, urlHeaders, this.rejectUnauthorized, callback, context);
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
HttpWebHookSwitchAccessory.prototype.getServices = function() {
|
|
92
|
+
return [ this.service, this.informationService ];
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
module.exports = HttpWebHookSwitchAccessory;
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
const Constants = require('../../Constants');
|
|
2
|
+
const Util = require('../../Util');
|
|
3
|
+
|
|
4
|
+
function HttpWebHookThermostatAccessory(ServiceParam, CharacteristicParam, platform, thermostatConfig) {
|
|
5
|
+
Service = ServiceParam;
|
|
6
|
+
Characteristic = CharacteristicParam;
|
|
7
|
+
|
|
8
|
+
this.platform = platform;
|
|
9
|
+
this.log = platform.log;
|
|
10
|
+
this.storage = platform.storage;
|
|
11
|
+
|
|
12
|
+
this.id = thermostatConfig["id"];
|
|
13
|
+
this.name = thermostatConfig["name"];
|
|
14
|
+
this.type = "thermostat";
|
|
15
|
+
this.minValue = thermostatConfig["minTemp"] || 15;
|
|
16
|
+
this.maxValue = thermostatConfig["maxTemp"] || 30;
|
|
17
|
+
this.minStep = thermostatConfig["minStep"] || 0.5;
|
|
18
|
+
this.rejectUnauthorized = thermostatConfig["rejectUnauthorized"] === undefined ? true: thermostatConfig["rejectUnauthorized"] === true;
|
|
19
|
+
this.setTargetTemperatureURL = thermostatConfig["set_target_temperature_url"] || "";
|
|
20
|
+
this.setTargetTemperatureMethod = thermostatConfig["set_target_temperature_method"] || "GET";
|
|
21
|
+
this.setTargetTemperatureBody = thermostatConfig["set_target_temperature_body"] || "";
|
|
22
|
+
this.setTargetTemperatureForm = thermostatConfig["set_target_temperature_form"] || "";
|
|
23
|
+
this.setTargetTemperatureHeaders = thermostatConfig["set_target_temperature_headers"] || "{}";
|
|
24
|
+
this.setTargetHeatingCoolingStateURL = thermostatConfig["set_target_heating_cooling_state_url"] || "";
|
|
25
|
+
this.setTargetHeatingCoolingStateMethod = thermostatConfig["set_target_heating_cooling_state_method"] || "GET";
|
|
26
|
+
this.setTargetHeatingCoolingStateBody = thermostatConfig["set_target_heating_cooling_state_body"] || "";
|
|
27
|
+
this.setTargetHeatingCoolingStateForm = thermostatConfig["set_target_heating_cooling_state_form"] || "";
|
|
28
|
+
this.setTargetHeatingCoolingStateHeaders = thermostatConfig["set_target_heating_cooling_state_headers"] || "{}";
|
|
29
|
+
|
|
30
|
+
this.informationService = new Service.AccessoryInformation();
|
|
31
|
+
this.informationService.setCharacteristic(Characteristic.Manufacturer, "HttpWebHooksPlatform");
|
|
32
|
+
this.informationService.setCharacteristic(Characteristic.Model, "HttpWebHookThermostatAccessory-" + this.name);
|
|
33
|
+
this.informationService.setCharacteristic(Characteristic.SerialNumber, "HttpWebHookThermostatAccessory-" + this.id);
|
|
34
|
+
|
|
35
|
+
this.service = new Service.Thermostat(this.name);
|
|
36
|
+
this.service.getCharacteristic(Characteristic.TargetHeatingCoolingState).on('get', this.getTargetHeatingCoolingState.bind(this)).on('set', this.setTargetHeatingCoolingState.bind(this));
|
|
37
|
+
this.service.getCharacteristic(Characteristic.CurrentHeatingCoolingState).on('get', this.getCurrentHeatingCoolingState.bind(this));
|
|
38
|
+
this.service.getCharacteristic(Characteristic.TargetTemperature).on('get', this.getTargetTemperature.bind(this)).on('set', this.setTargetTemperature.bind(this)).setProps({
|
|
39
|
+
minValue: this.minValue,
|
|
40
|
+
maxValue: this.maxValue,
|
|
41
|
+
minStep: this.minStep
|
|
42
|
+
});
|
|
43
|
+
this.service.getCharacteristic(Characteristic.CurrentTemperature).on('get', this.getCurrentTemperature.bind(this));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
HttpWebHookThermostatAccessory.prototype.changeFromServer = function(urlParams) {
|
|
47
|
+
if (urlParams.currenttemperature != null) {
|
|
48
|
+
var cachedCurTemp = this.storage.getItemSync("http-webhook-current-temperature-" + this.id);
|
|
49
|
+
if (cachedCurTemp === undefined) {
|
|
50
|
+
cachedCurTemp = 0;
|
|
51
|
+
}
|
|
52
|
+
this.storage.setItemSync("http-webhook-current-temperature-" + this.id, urlParams.currenttemperature);
|
|
53
|
+
if (cachedCurTemp !== urlParams.currenttemperature) {
|
|
54
|
+
this.log(this.name + ": Change current Temperature for thermostat to '%d'.", urlParams.currenttemperature);
|
|
55
|
+
this.service.getCharacteristic(Characteristic.CurrentTemperature).updateValue(urlParams.currenttemperature, undefined, Constants.CONTEXT_FROM_WEBHOOK);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (urlParams.targettemperature != null) {
|
|
59
|
+
var cachedCurTemp = this.storage.getItemSync("http-webhook-target-temperature-" + this.id);
|
|
60
|
+
if (cachedCurTemp === undefined) {
|
|
61
|
+
cachedCurTemp = 10;
|
|
62
|
+
}
|
|
63
|
+
this.storage.setItemSync("http-webhook-target-temperature-" + this.id, urlParams.targettemperature);
|
|
64
|
+
if (cachedCurTemp !== urlParams.targettemperature) {
|
|
65
|
+
this.log(this.name + ": Change target Temperature for thermostat to '%d'.", urlParams.targettemperature);
|
|
66
|
+
this.service.getCharacteristic(Characteristic.TargetTemperature).updateValue(urlParams.targettemperature, undefined, Constants.CONTEXT_FROM_WEBHOOK);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (urlParams.currentstate != null) {
|
|
70
|
+
var cachedState = this.storage.getItemSync("http-webhook-current-heating-cooling-state-" + this.id);
|
|
71
|
+
if (cachedState === undefined) {
|
|
72
|
+
cachedState = Characteristic.CurrentHeatingCoolingState.OFF;
|
|
73
|
+
}
|
|
74
|
+
this.storage.setItemSync("http-webhook-current-heating-cooling-state-" + this.id, urlParams.currentstate);
|
|
75
|
+
if (cachedState !== urlParams.currentstate) {
|
|
76
|
+
if (urlParams.currentstate) {
|
|
77
|
+
this.log(this.name + ": Change Current Heating Cooling State for thermostat to '%s'.", urlParams.currentstate);
|
|
78
|
+
this.service.getCharacteristic(Characteristic.CurrentHeatingCoolingState).updateValue(urlParams.currentstate, undefined, Constants.CONTEXT_FROM_WEBHOOK);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (urlParams.targetstate != null) {
|
|
83
|
+
var cachedState = this.storage.getItemSync("http-webhook-target-heating-cooling-state-" + this.id);
|
|
84
|
+
if (cachedState === undefined) {
|
|
85
|
+
cachedState = Characteristic.TargetHeatingCoolingState.OFF;
|
|
86
|
+
}
|
|
87
|
+
this.storage.setItemSync("http-webhook-target-heating-cooling-state-" + this.id, urlParams.targetstate);
|
|
88
|
+
if (cachedState !== urlParams.targetstate) {
|
|
89
|
+
if (urlParams.targetstate) {
|
|
90
|
+
this.log(this.name + ": Change Target Heating Cooling State for thermostat to '%s'.", urlParams.targetstate);
|
|
91
|
+
this.service.getCharacteristic(Characteristic.TargetHeatingCoolingState).updateValue(urlParams.targetstate, undefined, Constants.CONTEXT_FROM_WEBHOOK);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return {
|
|
96
|
+
"success" : true
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
HttpWebHookThermostatAccessory.prototype.getTargetTemperature = function(callback) {
|
|
101
|
+
this.log.debug(this.name + ": Getting target temperature for '%s'...", this.id);
|
|
102
|
+
var temp = this.storage.getItemSync("http-webhook-target-temperature-" + this.id);
|
|
103
|
+
if (temp === undefined) {
|
|
104
|
+
temp = 20;
|
|
105
|
+
}
|
|
106
|
+
callback(null, temp);
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
HttpWebHookThermostatAccessory.prototype.setTargetTemperature = function(temp, callback, context) {
|
|
110
|
+
this.log(this.name + ": Target temperature for '%s'...", this.id);
|
|
111
|
+
this.storage.setItemSync("http-webhook-target-temperature-" + this.id, temp);
|
|
112
|
+
var urlToCall = this.setTargetTemperatureURL.replace("%f", temp);
|
|
113
|
+
var urlMethod = this.setTargetTemperatureMethod;
|
|
114
|
+
var urlBody = this.setTargetTemperatureBody;
|
|
115
|
+
var urlForm = this.setTargetTemperatureForm;
|
|
116
|
+
var urlHeaders = this.setTargetTemperatureHeaders;
|
|
117
|
+
|
|
118
|
+
Util.callHttpApi(this.log, urlToCall, urlMethod, urlBody, urlForm, urlHeaders, this.rejectUnauthorized, callback, context);
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
HttpWebHookThermostatAccessory.prototype.getCurrentTemperature = function(callback) {
|
|
122
|
+
this.log.debug(this.name + ": Getting current temperature for '%s'...", this.id);
|
|
123
|
+
var temp = this.storage.getItemSync("http-webhook-current-temperature-" + this.id);
|
|
124
|
+
if (temp === undefined) {
|
|
125
|
+
temp = 20;
|
|
126
|
+
}
|
|
127
|
+
callback(null, temp);
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
HttpWebHookThermostatAccessory.prototype.getTargetHeatingCoolingState = function(callback) {
|
|
131
|
+
this.log.debug(this.name + ": Getting current Target Heating Cooling state for '%s'...", this.id);
|
|
132
|
+
var state = this.storage.getItemSync("http-webhook-target-heating-cooling-state-" + this.id);
|
|
133
|
+
if (state === undefined) {
|
|
134
|
+
state = Characteristic.TargetHeatingCoolingState.OFF;
|
|
135
|
+
}
|
|
136
|
+
callback(null, state);
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
HttpWebHookThermostatAccessory.prototype.setTargetHeatingCoolingState = function(newState, callback, context) {
|
|
140
|
+
this.log(this.name + ": Target Heating Cooling state for '%s'...", this.id);
|
|
141
|
+
this.storage.setItemSync("http-webhook-target-heating-cooling-state-" + this.id, newState);
|
|
142
|
+
var urlToCall = this.setTargetHeatingCoolingStateURL.replace("%b", newState);
|
|
143
|
+
var urlMethod = this.setTargetHeatingCoolingStateMethod;
|
|
144
|
+
var urlBody = this.setTargetHeatingCoolingStateBody;
|
|
145
|
+
var urlForm = this.setTargetHeatingCoolingStateForm;
|
|
146
|
+
var urlHeaders = this.setTargetHeatingCoolingStateHeaders;
|
|
147
|
+
|
|
148
|
+
Util.callHttpApi(this.log, urlToCall, urlMethod, urlBody, urlForm, urlHeaders, this.rejectUnauthorized, callback, context);
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
HttpWebHookThermostatAccessory.prototype.getCurrentHeatingCoolingState = function(callback) {
|
|
152
|
+
this.log.debug(this.name + ": Getting current Target Heating Cooling state for '%s'...", this.id);
|
|
153
|
+
var state = this.storage.getItemSync("http-webhook-current-heating-cooling-state-" + this.id);
|
|
154
|
+
if (state === undefined) {
|
|
155
|
+
state = Characteristic.CurrentHeatingCoolingState.OFF;
|
|
156
|
+
}
|
|
157
|
+
callback(null, state);
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
HttpWebHookThermostatAccessory.prototype.getServices = function() {
|
|
161
|
+
return [ this.service, this.informationService ];
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
module.exports = HttpWebHookThermostatAccessory;
|