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.
@@ -0,0 +1,151 @@
1
+ const Constants = require('../../Constants');
2
+ const Util = require('../../Util');
3
+
4
+ function HttpWebHookValveAccessory(ServiceParam, CharacteristicParam, platform, valveConfig) {
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 = valveConfig["id"];
13
+ this.name = valveConfig["name"];
14
+ this.type = "valve";
15
+ this.rejectUnauthorized = valveConfig["rejectUnauthorized"] === undefined ? true: valveConfig["rejectUnauthorized"] === true;
16
+ this.onURL = valveConfig["on_url"] || "";
17
+ this.onMethod = valveConfig["on_method"] || "GET";
18
+ this.onBody = valveConfig["on_body"] || "";
19
+ this.onForm = valveConfig["on_form"] || "";
20
+ this.onHeaders = valveConfig["on_headers"] || "{}";
21
+ this.offURL = valveConfig["off_url"] || "";
22
+ this.offMethod = valveConfig["off_method"] || "GET";
23
+ this.offBody = valveConfig["off_body"] || "";
24
+ this.offForm = valveConfig["off_form"] || "";
25
+ this.offHeaders = valveConfig["off_headers"] || "{}";
26
+
27
+ this.informationService = new Service.AccessoryInformation();
28
+ this.informationService.setCharacteristic(Characteristic.Manufacturer, "HttpWebHooksPlatform");
29
+ this.informationService.setCharacteristic(Characteristic.Model, "HttpWebHookValveAccessory-" + this.name);
30
+ this.informationService.setCharacteristic(Characteristic.SerialNumber, "HttpWebHookValveAccessory-" + this.id);
31
+
32
+ var valveType = 0;
33
+ switch(valveConfig.type) {
34
+ default:
35
+ case "generic valve":
36
+ valveType = 0; // ValveType.GENERIC_VALVE
37
+ break;
38
+ case "irrigation":
39
+ valveType = 1; // ValveType.IRRIGATION
40
+ break;
41
+ case "shower head":
42
+ valveType = 2; // ValveType.SHOWER_HEAD
43
+ break;
44
+ case "water faucet":
45
+ valveType = 3; // ValveType.WATER_FAUCET
46
+ break;
47
+ }
48
+
49
+ this.service = new Service.Valve(this.name);
50
+ this.service.setCharacteristic(Characteristic.ValveType, valveType);
51
+ this.service.getCharacteristic(Characteristic.Active).on('get', this.getState.bind(this)).on('set', this.setState.bind(this));
52
+ this.service.getCharacteristic(Characteristic.InUse).on('get', this.getState.bind(this)).on('set', this.setState.bind(this));
53
+ this.service.getCharacteristic(Characteristic.StatusFault).on('get', this.getStatusFault.bind(this));
54
+
55
+ this.category = 30;
56
+ }
57
+
58
+ HttpWebHookValveAccessory.prototype.changeFromServer = function(urlParams) {
59
+ var cachedState = this.storage.getItemSync("http-webhook-" + this.id);
60
+ if (cachedState === undefined) {
61
+ cachedState = 0;
62
+ }
63
+ var cachedStatusFault = this.storage.getItemSync("http-webhook-" + this.id + "-statusFault");
64
+ if (cachedStatusFault === undefined) {
65
+ cachedStatusFault = 0;
66
+ }
67
+ if (!urlParams.state && !urlParams.statusFault) {
68
+ return {
69
+ "success" : true,
70
+ "state" : cachedState,
71
+ "statusFault" : cachedStatusFault
72
+ };
73
+ }
74
+ else {
75
+ if (urlParams.state) {
76
+ var state = urlParams.state;
77
+ var stateNumber = state === "true" ? 1 : 0;
78
+ this.storage.setItemSync("http-webhook-" + this.id, stateNumber);
79
+ this.log.info("Change HomeKit value for " + this.type + " state to '%s'.", stateNumber);
80
+
81
+ if (cachedState !== stateNumber) {
82
+ this.log(this.name + ": Change HomeKit state for valve to '%s'.", stateNumber);
83
+ this.service.getCharacteristic(Characteristic.Active).updateValue(stateNumber, undefined, Constants.CONTEXT_FROM_WEBHOOK);
84
+ this.service.getCharacteristic(Characteristic.InUse).updateValue(stateNumber, undefined, Constants.CONTEXT_FROM_WEBHOOK);
85
+ }
86
+ }
87
+ if (urlParams.statusFault) {
88
+ var statusFault = urlParams.statusFault;
89
+ var statusFaultNumber = statusFault === "true" ? 1 : 0;
90
+ this.storage.setItemSync("http-webhook-" + this.id + "-statusFault", statusFaultNumber);
91
+ this.log.info("Change HomeKit value for " + this.type + " statusFault to '%s'.", statusFaultNumber);
92
+
93
+ if (cachedStatusFault !== statusFaultNumber) {
94
+ this.log(this.name + ": Change HomeKit statusFault for valve to '%s'.", statusFault);
95
+ this.service.getCharacteristic(Characteristic.StatusFault).updateValue(statusFaultNumber, undefined, Constants.CONTEXT_FROM_WEBHOOK);
96
+ }
97
+ }
98
+ return {
99
+ "success" : true
100
+ };
101
+ }
102
+ };
103
+
104
+ HttpWebHookValveAccessory.prototype.getState = function(callback) {
105
+ this.log.debug(this.name + ": Getting current state for", this.id);
106
+ var state = this.storage.getItemSync("http-webhook-" + this.id);
107
+ if (state === undefined) {
108
+ state = 0;
109
+ }
110
+
111
+ callback(null, state);
112
+ };
113
+
114
+ HttpWebHookValveAccessory.prototype.getStatusFault = function(callback) {
115
+ this.log.debug(this.name + ": Getting status fault for", this.id);
116
+ var statusFault = this.storage.getItemSync("http-webhook-" + this.id + "-statusFault");
117
+ if (statusFault === undefined) {
118
+ statusFault = 0;
119
+ }
120
+
121
+ callback(null, statusFault);
122
+ };
123
+
124
+ HttpWebHookValveAccessory.prototype.setState = function(active, callback, context) {
125
+ this.log.info("Set valve state for", this.id, "to", active);
126
+ this.storage.setItemSync("http-webhook-" + this.id, active);
127
+
128
+ this.service.getCharacteristic(Characteristic.Active).updateValue(active, undefined, Constants.CONTEXT_FROM_WEBHOOK);
129
+ this.service.getCharacteristic(Characteristic.InUse).updateValue(active, undefined, Constants.CONTEXT_FROM_WEBHOOK);
130
+
131
+ var urlToCall = this.onURL;
132
+ var urlMethod = this.onMethod;
133
+ var urlBody = this.onBody;
134
+ var urlForm = this.onForm;
135
+ var urlHeaders = this.onHeaders;
136
+ if (!active) {
137
+ urlToCall = this.offURL;
138
+ urlMethod = this.offMethod;
139
+ urlBody = this.offBody;
140
+ urlForm = this.offForm;
141
+ urlHeaders = this.offHeaders;
142
+ }
143
+
144
+ Util.callHttpApi(this.log, urlToCall, urlMethod, urlBody, urlForm, urlHeaders, this.rejectUnauthorized, callback, context);
145
+ };
146
+
147
+ HttpWebHookValveAccessory.prototype.getServices = function() {
148
+ return [ this.service, this.informationService ];
149
+ };
150
+
151
+ module.exports = HttpWebHookValveAccessory;
@@ -0,0 +1,203 @@
1
+ const Constants = require('../../Constants');
2
+ const Util = require('../../Util');
3
+
4
+ function HttpWebHookWindowCoveringAccessory(ServiceParam, CharacteristicParam, platform, windowcoveringConfig) {
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 = windowcoveringConfig["id"];
13
+ this.name = windowcoveringConfig["name"];
14
+ this.type = "windowcovering";
15
+ this.rejectUnauthorized = windowcoveringConfig["rejectUnauthorized"] === undefined ? true: windowcoveringConfig["rejectUnauthorized"] === true;
16
+ this.setTargetPositionOpenURL = windowcoveringConfig["open_url"] || "";
17
+ this.setTargetPositionOpenMethod = windowcoveringConfig["open_method"] || "GET";
18
+ this.setTargetPositionOpenBody = windowcoveringConfig["open_body"] || "";
19
+ this.setTargetPositionOpenForm = windowcoveringConfig["open_form"] || "";
20
+ this.setTargetPositionOpenHeaders = windowcoveringConfig["open_headers"] || "{}";
21
+ this.setTargetPositionOpen20URL = windowcoveringConfig["open_20_url"] || "";
22
+ this.setTargetPositionOpen20Method = windowcoveringConfig["open_20_method"] || "GET";
23
+ this.setTargetPositionOpen20Body = windowcoveringConfig["open_20_body"] || "";
24
+ this.setTargetPositionOpen20Form = windowcoveringConfig["open_20_form"] || "";
25
+ this.setTargetPositionOpen20Headers = windowcoveringConfig["open_20_headers"] || "{}";
26
+ this.setTargetPositionOpen40URL = windowcoveringConfig["open_40_url"] || "";
27
+ this.setTargetPositionOpen40Method = windowcoveringConfig["open_40_method"] || "GET";
28
+ this.setTargetPositionOpen40Body = windowcoveringConfig["open_40_body"] || "";
29
+ this.setTargetPositionOpen40Form = windowcoveringConfig["open_40_form"] || "";
30
+ this.setTargetPositionOpen40Headers = windowcoveringConfig["open_40_headers"] || "{}";
31
+ this.setTargetPositionOpen60URL = windowcoveringConfig["open_60_url"] || "";
32
+ this.setTargetPositionOpen60Method = windowcoveringConfig["open_60_method"] || "GET";
33
+ this.setTargetPositionOpen60Body = windowcoveringConfig["open_60_body"] || "";
34
+ this.setTargetPositionOpen60Form = windowcoveringConfig["open_60_form"] || "";
35
+ this.setTargetPositionOpen60Headers = windowcoveringConfig["open_60_headers"] || "{}";
36
+ this.setTargetPositionOpen80URL = windowcoveringConfig["open_80_url"] || "";
37
+ this.setTargetPositionOpen80Method = windowcoveringConfig["open_80_method"] || "GET";
38
+ this.setTargetPositionOpen80Body = windowcoveringConfig["open_80_body"] || "";
39
+ this.setTargetPositionOpen80Form = windowcoveringConfig["open_80_form"] || "";
40
+ this.setTargetPositionOpen80Headers = windowcoveringConfig["open_80_headers"] || "{}";
41
+ this.setTargetPositionCloseURL = windowcoveringConfig["close_url"] || "";
42
+ this.setTargetPositionCloseMethod = windowcoveringConfig["close_method"] || "GET";
43
+ this.setTargetPositionCloseBody = windowcoveringConfig["close_body"] || "";
44
+ this.setTargetPositionCloseForm = windowcoveringConfig["close_form"] || "";
45
+ this.setTargetPositionCloseHeaders = windowcoveringConfig["close_headers"] || "{}";
46
+ this.autoSetCurrentPosition = windowcoveringConfig["auto_set_current_position"] || false;
47
+
48
+ this.informationService = new Service.AccessoryInformation();
49
+ this.informationService.setCharacteristic(Characteristic.Manufacturer, "HttpWebHooksPlatform");
50
+ this.informationService.setCharacteristic(Characteristic.Model, "HttpWebHookWindowCoveringAccessory-" + this.name);
51
+ this.informationService.setCharacteristic(Characteristic.SerialNumber, "HttpWebHookWindowCoveringAccessory-" + this.id);
52
+
53
+ this.service = new Service.WindowCovering(this.name);
54
+ this.service.getCharacteristic(Characteristic.TargetPosition).on('get', this.getTargetPosition.bind(this)).on('set', this.setTargetPosition.bind(this));
55
+ this.service.getCharacteristic(Characteristic.CurrentPosition).on('get', this.getCurrentPosition.bind(this));
56
+ this.service.getCharacteristic(Characteristic.PositionState).on('get', this.getPositionState.bind(this));
57
+ }
58
+
59
+ HttpWebHookWindowCoveringAccessory.prototype.changeFromServer = function(urlParams) {
60
+ if (urlParams.currentposition != null) {
61
+ var cachedCurrentPosition = this.storage.getItemSync("http-webhook-current-position-" + this.id);
62
+ if (cachedCurrentPosition === undefined) {
63
+ cachedCurrentPosition = 100;
64
+ }
65
+ this.storage.setItemSync("http-webhook-current-position-" + this.id, urlParams.currentposition);
66
+ if (cachedCurrentPosition !== urlParams.currentposition) {
67
+ this.log(this.name + ": Change Current Window Covering for covers to '%s'.", urlParams.currentposition);
68
+ this.service.getCharacteristic(Characteristic.CurrentPosition).updateValue(urlParams.currentposition, undefined, Constants.CONTEXT_FROM_WEBHOOK);
69
+ }
70
+ }
71
+
72
+ if (urlParams.targetposition != null) {
73
+ var cachedTargetPosition = this.storage.getItemSync("http-webhook-target-position-" + this.id);
74
+ if (cachedTargetPosition === undefined) {
75
+ cachedTargetPosition = 100;
76
+ }
77
+ this.storage.setItemSync("http-webhook-target-position-" + this.id, urlParams.targetposition);
78
+ if (cachedTargetPosition !== urlParams.targetposition) {
79
+ if (urlParams.targetposition) {
80
+ this.log(this.name + ": Change Target Position for covers to '%s'.", urlParams.targetposition);
81
+ this.service.getCharacteristic(Characteristic.TargetPosition).updateValue(urlParams.targetposition, undefined, Constants.CONTEXT_FROM_WEBHOOK);
82
+ }
83
+ }
84
+ }
85
+ if (urlParams.positionstate != null) {
86
+ var cachedPositionState = this.storage.getItemSync("http-webhook-position-state-" + this.id);
87
+ if (cachedPositionState === undefined) {
88
+ cachedPositionState = false;
89
+ }
90
+ this.storage.setItemSync("http-webhook-position-state-" + this.id, urlParams.positionstate);
91
+ if (cachedPositionState !== urlParams.positionstate) {
92
+ if (urlParams.positionstate) {
93
+ this.log(this.name + ": Change Position State for covers to '%s'.", urlParams.positionstate);
94
+ this.service.getCharacteristic(Characteristic.PositionState).updateValue(urlParams.positionstate, undefined, Constants.CONTEXT_FROM_WEBHOOK);
95
+ }
96
+ }
97
+ }
98
+ return {
99
+ "success" : true,
100
+ "CurrentPosition" : cachedCurrentPosition,
101
+ "TargetPosition" : cachedTargetPosition,
102
+ "PositionState" : cachedPositionState
103
+ };
104
+ }
105
+
106
+ HttpWebHookWindowCoveringAccessory.prototype.getTargetPosition = function(callback) {
107
+ this.log.debug(this.name + ": Getting current Target Position for '%s'...", this.id);
108
+ var state = this.storage.getItemSync("http-webhook-target-position-" + this.id);
109
+ if (state === undefined) {
110
+ state = 100;
111
+ }
112
+ callback(null, state);
113
+ };
114
+
115
+ HttpWebHookWindowCoveringAccessory.prototype.setTargetPosition = function(newState, callback, context) {
116
+ this.log(this.name + ": Target Position State for '%s'...", this.id);
117
+ this.log(this.name + ": New target state is: " + newState);
118
+ this.storage.setItemSync("http-webhook-target-position-" + this.id, newState);
119
+ if(this.autoSetCurrentPosition) {
120
+ this.storage.setItemSync("http-webhook-current-position-" + this.id, newState);
121
+ }
122
+ var urlToCall = this.setTargetPositionCloseURL;
123
+ var urlMethod = this.setTargetPositionCloseMethod;
124
+ var urlBody = this.setTargetPositionCloseBody;
125
+ var urlForm = this.setTargetPositionCloseForm;
126
+ var urlHeaders = this.setTargetPositionCloseHeaders;
127
+ if (newState === 0) {
128
+ urlToCall = this.setTargetPositionOpenURL;
129
+ urlMethod = this.setTargetPositionOpenMethod;
130
+ urlBody = this.setTargetPositionOpenBody;
131
+ urlForm = this.setTargetPositionOpenForm;
132
+ urlHeaders = this.setTargetPositionOpenHeaders;
133
+ }
134
+ if (newState >= 1 && newState <= 25) {
135
+ urlToCall = this.setTargetPositionOpen20URL;
136
+ urlMethod = this.setTargetPositionOpen20Method;
137
+ urlBody = this.setTargetPositionOpen20Body;
138
+ urlForm = this.setTargetPositionOpen20Form;
139
+ urlHeaders = this.setTargetPositionOpen20Headers;
140
+ }
141
+ if (newState >= 26 && newState <= 45) {
142
+ urlToCall = this.setTargetPositionOpen40URL;
143
+ urlMethod = this.setTargetPositionOpen40Method;
144
+ urlBody = this.setTargetPositionOpen40Body;
145
+ urlForm = this.setTargetPositionOpen40Form;
146
+ urlHeaders = this.setTargetPositionOpen40Headers;
147
+ }
148
+ if (newState >= 46 && newState <= 65) {
149
+ urlToCall = this.setTargetPositionOpen60URL;
150
+ urlMethod = this.setTargetPositionOpen60Method;
151
+ urlBody = this.setTargetPositionOpen60Body;
152
+ urlForm = this.setTargetPositionOpen60Form;
153
+ urlHeaders = this.setTargetPositionOpen60Headers;
154
+ }
155
+ if (newState >= 66 && newState <= 94) {
156
+ urlToCall = this.setTargetPositionOpen80URL;
157
+ urlMethod = this.setTargetPositionOpen80Method;
158
+ urlBody = this.setTargetPositionOpen80Body;
159
+ urlForm = this.setTargetPositionOpen80Form;
160
+ urlHeaders = this.setTargetPositionOpen80Headers;
161
+ }
162
+ if (newState >= 95) {
163
+ urlToCall = this.setTargetPositionCloseURL;
164
+ urlMethod = this.setTargetPositionCloseMethod;
165
+ urlBody = this.setTargetPositionCloseBody;
166
+ urlForm = this.setTargetPositionCloseForm;
167
+ urlHeaders = this.setTargetPositionCloseHeaders;
168
+ }
169
+
170
+ Util.callHttpApi(this.log, urlToCall, urlMethod, urlBody, urlForm, urlHeaders, this.rejectUnauthorized, callback, context, (function() {
171
+ this.service.getCharacteristic(Characteristic.TargetPosition).updateValue(newState, undefined, null);
172
+ if(this.autoSetCurrentPosition) {
173
+ this.log(this.name + ": New current state is: " + newState);
174
+ setTimeout(function() {
175
+ this.service.getCharacteristic(Characteristic.CurrentPosition).updateValue(newState, undefined, null);
176
+ }.bind(this), 1000);
177
+ }
178
+ }).bind(this), null, Constants.COVERS_REQUEST_TIMEOUT);
179
+ };
180
+
181
+ HttpWebHookWindowCoveringAccessory.prototype.getCurrentPosition = function(callback) {
182
+ this.log.debug(this.name + ": Getting Current Position for '%s'...", this.id);
183
+ var state = this.storage.getItemSync("http-webhook-current-position-" + this.id);
184
+ if (state === undefined) {
185
+ state = 100;
186
+ }
187
+ callback(null, state);
188
+ };
189
+
190
+ HttpWebHookWindowCoveringAccessory.prototype.getPositionState = function(callback) {
191
+ this.log.debug(this.name + ": Getting position state for '%s'...", this.id);
192
+ var state = this.storage.getItemSync("http-webhook-position-state-" + this.id);
193
+ if (state === undefined) {
194
+ state = Characteristic.PositionState.STOPPED;
195
+ }
196
+ callback(null, state);
197
+ };
198
+
199
+ HttpWebHookWindowCoveringAccessory.prototype.getServices = function() {
200
+ return [ this.service, this.informationService ];
201
+ };
202
+
203
+ module.exports = HttpWebHookWindowCoveringAccessory;