-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
150 lines (127 loc) · 5.61 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
'use strict'
const request = require('request')
const url = require('url')
let Service, Characteristic
module.exports = (homebridge) => {
Service = homebridge.hap.Service
Characteristic = homebridge.hap.Characteristic
homebridge.registerAccessory("homebridge-kettle", "MyKettle", StaggEKGAccessory)
}
class StaggEKGAccessory {
constructor (log, config) {
this.log = log;
this.config = config;
this.service = new Service.Thermostat(this.config.name);
this.url = this.config.url;
this.tempDisplayUnits = 1;
this.maxTemp = 100;
this.minTemp = 40;
}
getServices () {
const informationService = new Service.AccessoryInformation()
informationService
.setCharacteristic(Characteristic.Manufacturer, "Fellow")
.setCharacteristic(Characteristic.Model, "Stagg EKG+")
.setCharacteristic(Characteristic.SerialNumber, "123-456-789")
this.service.getCharacteristic(Characteristic.TargetHeatingCoolingState)
.on('get', this.getTargetHeatingCoolingStateCharacteristicHandler.bind(this))
.on('set', this.setTargetHeatingCoolingStateCharacteristicHandler.bind(this))
this.service.getCharacteristic(Characteristic.TargetTemperature)
.on('get', this.getTargetTemperatureHandler.bind(this))
.on('set', this.setTargetTemperatureHandler.bind(this))
this.service.getCharacteristic(Characteristic.TargetTemperature)
.setProps({
maxValue: 100,
minValue: 40,
unit: 1
})
this.service.getCharacteristic(Characteristic.CurrentTemperature)
.setProps({
maxValue: 100,
minValue: 0,
unit: 1
})
this.service.getCharacteristic(Characteristic.TemperatureDisplayUnits)
.setProps({value: 1})
this.service.getCharacteristic(Characteristic.CurrentTemperature)
.on('get', this.getCurrentTemperatureHandler.bind(this))
this.service.getCharacteristic(Characteristic.TemperatureDisplayUnits)
.on('get', this.getTemperatureDisplayUnitsHandler.bind(this))
.on('set', this.setTemperatureDisplayUnitsHandler.bind(this))
this.service.getCharacteristic(Characteristic.TargetHeatingCoolingState)
.setProps({validValues: [0, 1]})
this.service.getCharacteristic(Characteristic.CurrentHeatingCoolingState)
.setProps({validValues: [0, 1]})
return [informationService, this.service]
}
getTargetHeatingCoolingStateCharacteristicHandler (callback) {
this.log(`calling getTargetHeatingCoolingStateCharacteristicHandler`)
var self = this;
request({
url: self.url + "/state",
method: "GET"
}, function (error, response, body) {
self.log(`getTargetHeatingCoolingState result:`, body)
self.service.updateCharacteristic(Characteristic.TargetHeatingCoolingState, body)
});
callback(null, this.service.getCharacteristic(Characteristic.TargetHeatingCoolingState).value)
}
setTargetHeatingCoolingStateCharacteristicHandler (value, callback) {
this.service.updateCharacteristic(Characteristic.TargetHeatingCoolingState, value)
this.log(`calling setTargetHeatingCoolingStateCharacteristicHandler`, value)
var self = this;
request({
url: self.url + "/state",
method: "POST",
json: false,
body: "value=" + value,
headers: {"Content-Length": 7}
}, function (error, response, body){});
callback(null, value)
}
getTargetTemperatureHandler (callback) {
this.log(`calling getTargetTemperatureHandler`)
var self = this;
request({
url: self.url + "/target_temp",
method: "GET"
}, function (error, response, body) {
self.log(`getTargetTemperatureHandler result:`, body)
self.service.updateCharacteristic(Characteristic.TargetTemperature, (body - 32)/1.8000)
});
callback(null, this.service.getCharacteristic(Characteristic.TargetTemperature).value)
}
setTargetTemperatureHandler (value, callback) {
this.service.updateCharacteristic(Characteristic.TargetTemperature, value)
this.log(`calling setTargetTemperatureHandler`, value)
var self = this;
request({
url: self.url + "/target_temp",
method: "POST",
json: false,
body: "value=" + value.toString(),
headers: {"Content-Length": 6 + value.toString().length}
}, function (error, response, body){});
callback(null, value)
}
getCurrentTemperatureHandler (callback) {
this.log(`calling getCurrentTemperatureHandler`)
var self = this;
request({
url: self.url + "/current_temp",
method: "GET"
}, function (error, response, body) {
self.log(`getCurrentTemperatureHandler result:`, body)
self.service.updateCharacteristic(Characteristic.CurrentTemperature, (body - 32)/1.8000)
});
callback(null, this.service.getCharacteristic(Characteristic.CurrentTemperature).value)
}
getTemperatureDisplayUnitsHandler (callback) {
this.log(`calling getTemperatureDisplayUnitsHandler`, this.tempDisplayUnits)
callback(null, this.tempDisplayUnits)
}
setTemperatureDisplayUnitsHandler (value, callback) {
this.log(`calling setTemperatureDisplayUnitsHandler`, value)
callback(null, this.tempDisplayUnits)
}
}