forked from phenotypic/homebridge-web-thermostat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
286 lines (250 loc) · 11.8 KB
/
index.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
var Service, Characteristic
const packageJson = require('./package.json')
const request = require('request')
const ip = require('ip')
const http = require('http')
module.exports = function (homebridge) {
Service = homebridge.hap.Service
Characteristic = homebridge.hap.Characteristic
homebridge.registerAccessory('homebridge-web-thermostat', 'Thermostat', Thermostat)
}
function Thermostat(log, config) {
this.log = log
this.name = config.name
this.apiroute = config.apiroute
this.pollInterval = config.pollInterval || 300
this.listener = config.listener || false
this.port = config.port || 2000
this.requestArray = ['targetHeatingCoolingState', 'targetTemperature', 'coolingThresholdTemperature', 'heatingThresholdTemperature']
this.manufacturer = config.manufacturer || packageJson.author.name
this.serial = config.serial || this.apiroute
this.model = config.model || packageJson.name
this.firmware = config.firmware || packageJson.version
this.username = config.username || null
this.password = config.password || null
this.timeout = config.timeout || 3000
this.http_method = config.http_method || 'GET'
this.temperatureThresholds = config.temperatureThresholds || false
this.heatOnly = config.heatOnly || false
this.currentRelativeHumidity = config.currentRelativeHumidity || false
this.temperatureDisplayUnits = config.temperatureDisplayUnits || 0
this.maxTemp = config.maxTemp || 30
this.minTemp = config.minTemp || 15
this.minStep = config.minStep || 0.5
if (this.username != null && this.password != null) {
this.auth = {
user: this.username,
pass: this.password
}
}
if (this.listener) {
this.server = http.createServer(function (request, response) {
var baseURL = 'http://' + request.headers.host + '/'
var url = new URL(request.url, baseURL)
if (this.requestArray.includes(url.pathname.substr(1))) {
this.log.debug('Handling request')
response.end('Handling request')
this._httpHandler(url.pathname.substr(1), url.searchParams.get('value'))
} else {
this.log.warn('Invalid request: %s', request.url)
response.end('Invalid request')
}
}.bind(this))
this.server.listen(this.port, function () {
this.log('Listen server: http://%s:%s', ip.address(), this.port)
}.bind(this))
}
this.service = new Service.Thermostat(this.name)
}
Thermostat.prototype = {
identify: function (callback) {
this.log('Identify requested!')
callback()
},
_httpRequest: function (url, body, method, callback) {
request({
url: url,
body: body,
method: this.http_method,
timeout: this.timeout,
rejectUnauthorized: false,
auth: this.auth
},
function (error, response, body) {
callback(error, response, body)
})
},
_getStatus: function (callback) {
var url = this.apiroute + '/status';
this.log.debug('Getting status: %s', url)
return request.get({
url: url
}, (function (error, response, body) {
var json;
if (!error && response.statusCode === 200) {
json = JSON.parse(body);
this.log.debug('Device response: %s', body)
this.service.getCharacteristic(Characteristic.TargetTemperature).updateValue(json.targetTemperature)
this.log.debug('Updated TargetTemperature to: %s', json.targetTemperature)
this.service.getCharacteristic(Characteristic.CurrentTemperature).updateValue(json.currentTemperature)
this.log.debug('Updated CurrentTemperature to: %s', json.currentTemperature)
this.service.getCharacteristic(Characteristic.TargetHeatingCoolingState).updateValue(json.targetHeatingCoolingState)
this.log.debug('Updated TargetHeatingCoolingState to: %s', json.targetHeatingCoolingState)
this.service.getCharacteristic(Characteristic.CurrentHeatingCoolingState).updateValue(json.currentHeatingCoolingState)
this.log.debug('Updated CurrentHeatingCoolingState to: %s', json.currentHeatingCoolingState)
if (this.temperatureThresholds) {
this.service.getCharacteristic(Characteristic.CoolingThresholdTemperature).updateValue(json.coolingThresholdTemperature)
this.log.debug('Updated CoolingThresholdTemperature to: %s', json.coolingThresholdTemperature)
this.service.getCharacteristic(Characteristic.HeatingThresholdTemperature).updateValue(json.heatingThresholdTemperature)
this.log.debug('Updated HeatingThresholdTemperature to: %s', json.heatingThresholdTemperature)
}
if (this.currentRelativeHumidity) {
this.service.getCharacteristic(Characteristic.CurrentRelativeHumidity).updateValue(json.currentRelativeHumidity)
this.log.debug('Updated CurrentRelativeHumidity to: %s', json.currentRelativeHumidity)
}
callback()
} else {
this.log.warn('Error getting status: %s', error.message)
this.service.getCharacteristic(Characteristic.CurrentHeatingCoolingState).updateValue(new Error('Polling failed'))
callback(error)
}
}).bind(this));
},
_httpHandler: function (characteristic, value) {
switch (characteristic) {
case 'targetHeatingCoolingState':
this.service.getCharacteristic(Characteristic.TargetHeatingCoolingState).updateValue(value)
this.log('Updated %s to: %s', characteristic, value)
break
case 'targetTemperature':
this.service.getCharacteristic(Characteristic.TargetTemperature).updateValue(value)
this.log('Updated %s to: %s', characteristic, value)
break
case 'coolingThresholdTemperature':
this.service.getCharacteristic(Characteristic.CoolingThresholdTemperature).updateValue(value)
this.log('Updated %s to: %s', characteristic, value)
break
case 'heatingThresholdTemperature':
this.service.getCharacteristic(Characteristic.HeatingThresholdTemperature).updateValue(value)
this.log('Updated %s to: %s', characteristic, value)
break
default:
this.log.warn('Unknown characteristic "%s" with value "%s"', characteristic, value)
}
},
setTargetHeatingCoolingState: function (value, callback) {
var url = this.apiroute + '/status/?changestatus=true&targetHeatingCoolingState=' + value
this.log.debug('Setting targetHeatingCoolingState: %s', url)
return request.get({
url: url
}, (function (error, response, body) {
var json;
if (!error && response.statusCode === 200) {
json = JSON.parse(body);
this.log('Set targetHeatingCoolingState to: %s', value)
this.service.getCharacteristic(Characteristic.TargetHeatingCoolingState).updateValue(value)
callback()
} else {
this.log.warn('Error setting targetHeatingCoolingState: %s', error.message)
callback(error)
}
}).bind(this))
},
setTargetTemperature: function (value, callback) {
value = value.toFixed(1)
var url = this.apiroute + '/status/?changestatus=true&targetTemperature=' + value
this.log.debug('Setting targetTemperature: %s', url)
return request.get({
url: url
}, (function (error, response, body) {
var json;
if (!error && response.statusCode === 200) {
json = JSON.parse(body);
this.log('Set targetTemperature to: %s', value)
callback()
} else {
this.log.warn('Error setting targetTemperature: %s', error.message)
callback(error)
}
}).bind(this))
},
setCoolingThresholdTemperature: function (value, callback) {
value = value.toFixed(1)
var url = this.apiroute + '/status/?changestatus=true&coolingThresholdTemperature?value=' + value
this.log.debug('Setting coolingThresholdTemperature: %s', url)
this._httpRequest(url, '', this.http_method, function (error, response, responseBody) {
if (error) {
this.log.warn('Error setting coolingThresholdTemperature: %s', error.message)
callback(error)
} else {
this.log('Set coolingThresholdTemperature to: %s', value)
callback()
}
}.bind(this))
},
setHeatingThresholdTemperature: function (value, callback) {
value = value.toFixed(1)
var url = this.apiroute + '/status/?changestatus=true&heatingThresholdTemperature?value=' + value
this.log.debug('Setting heatingThresholdTemperature: %s', url)
this._httpRequest(url, '', this.http_method, function (error, response, responseBody) {
if (error) {
this.log.warn('Error setting heatingThresholdTemperature: %s', error.message)
callback(error)
} else {
this.log('Set heatingThresholdTemperature to: %s', value)
callback()
}
}.bind(this))
},
getServices: function () {
this.informationService = new Service.AccessoryInformation()
this.informationService
.setCharacteristic(Characteristic.Manufacturer, this.manufacturer)
.setCharacteristic(Characteristic.Model, this.model)
.setCharacteristic(Characteristic.SerialNumber, this.serial)
.setCharacteristic(Characteristic.FirmwareRevision, this.firmware)
this.service.getCharacteristic(Characteristic.TemperatureDisplayUnits).updateValue(this.temperatureDisplayUnits)
this.service
.getCharacteristic(Characteristic.TargetHeatingCoolingState)
.on('set', this.setTargetHeatingCoolingState.bind(this))
if (this.heatOnly) {
this.service.getCharacteristic(Characteristic.TargetHeatingCoolingState)
.setProps({
maxValue: Characteristic.TargetHeatingCoolingState.HEAT
})
}
this.service
.getCharacteristic(Characteristic.TargetTemperature)
.on('set', this.setTargetTemperature.bind(this))
.setProps({
minValue: this.minTemp,
maxValue: this.maxTemp,
minStep: this.minStep
})
if (this.temperatureThresholds) {
this.service
.getCharacteristic(Characteristic.CoolingThresholdTemperature)
.on('set', this.setCoolingThresholdTemperature.bind(this))
.setProps({
minValue: this.minTemp,
maxValue: this.maxTemp,
minStep: this.minStep
})
this.service
.getCharacteristic(Characteristic.HeatingThresholdTemperature)
.on('set', this.setHeatingThresholdTemperature.bind(this))
.setProps({
minValue: this.minTemp,
maxValue: this.maxTemp,
minStep: this.minStep
})
}
this._getStatus(function () {
})
setInterval(function () {
this._getStatus(function () {
})
}.bind(this), this.pollInterval * 1000)
return [this.informationService, this.service]
}
}