-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
228 lines (190 loc) · 7.93 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
const utils = require('./utils')
const util = require('util')
const os = require('os')
let Kelvin, Accessory, Service, Characteristic, UUIDGen
const UUID_KELVIN = 'C4E24248-04AC-44AF-ACFF-40164E829DBA'
module.exports = function(homebridge) {
Accessory = homebridge.platformAccessory
Service = homebridge.hap.Service
Characteristic = homebridge.hap.Characteristic
UUIDGen = homebridge.hap.uuid // @TODO: Should be using this
Characteristic.Kelvin = function() {
Characteristic.call(this, 'Kelvin', UUID_KELVIN)
this.setProps({
format: Characteristic.Formats.INT,
unit: 'Kelvin',
maxValue: 4000,
minValue: 2200,
minStep: 1,
perms: [Characteristic.Perms.READ, Characteristic.Perms.WRITE]
});
this.value = this.getDefaultValue();
}
util.inherits(Characteristic.Kelvin, Characteristic);
Characteristic.Kelvin.UUID = UUID_KELVIN
homebridge.registerPlatform("homebridge-ikea", "Ikea", IkeaPlatform)
}
function IkeaPlatform(log, config) {
this.log = log
this.config = config
this.config.log = this.log
this.devices = []
if (!this.config.coapClient && (os.platform() !== "darwin" && os.platform() !== "linux")) {
throw Error("No coap-client found, please specify the path to it using coapClient")
}
this.config.coapClient = this.config.coapClient || `${__dirname}/bin/coap-client-${os.platform()}`
}
IkeaPlatform.prototype = {
accessories: async function(callback) {
const self = this
const foundAccessories = []
const devices = await utils.getDevices(self.config)
await Promise.all(devices.map(async deviceId => {
const device = await utils.getDevice(self.config, deviceId)
if (device.type === 2) {
foundAccessories.push(new IkeaAccessory(self.log, self.config, device))
}
}))
callback(foundAccessories)
}
}
function IkeaAccessory(log, config, device) {
this.name = device.name
this.config = config
this.config.log = string => log("[" + this.name + "] " + string)
this.device = device
this.currentBrightness = this.device.light[0]["5851"]
this.currentState = this.device.light[0]["5850"]
this.previousBrightness = this.currentBrightness
this.color = {}
}
IkeaAccessory.prototype = {
// Respond to identify request
identify: function(callback) {
this.config.log("Hi!")
callback()
},
getServices: function() {
const accessoryInformation = new Service.AccessoryInformation()
accessoryInformation
.setCharacteristic(Characteristic.Name, this.device.name)
.setCharacteristic(Characteristic.Manufacturer, this.device.details["0"])
.setCharacteristic(Characteristic.Model, this.device.details["1"])
.setCharacteristic(Characteristic.FirmwareRevision, this.device.details["3"])
const self = this
const lightbulbService = new Service.Lightbulb(self.name)
lightbulbService
.addCharacteristic(Characteristic.StatusActive)
lightbulbService
.setCharacteristic(Characteristic.StatusActive, this.device.reachabilityState)
.setCharacteristic(Characteristic.On, this.device.light[0]["5850"])
.setCharacteristic(Characteristic.Brightness, parseInt(Math.round(this.device.light[0]["5851"] * 100 / 255)))
lightbulbService
.getCharacteristic(Characteristic.StatusActive)
.on('get', callback => {
utils.getDevice(self.config, self.device.instanceId).then(device => {
callback(null, device.reachabilityState)
})
})
lightbulbService
.getCharacteristic(Characteristic.On)
.on('get', callback => {
utils.getDevice(self.config, self.device.instanceId).then(device => {
self.currentBrightness = device.light[0]["5851"]
self.currentState = device.light[0]["5850"]
callback(null, self.currentState)
})
})
.on('set', (state, callback) => {
if (typeof state !== 'number') {
state = state ? 1 : 0;
}
if (self.currentState == 1 && state == 0) { // We're turned on but want to turn off.
self.currentState = 0
utils.setBrightness(self.config, self.device.instanceId, 0, result => callback())
} else if(self.currentState == 0 && state == 1) {
self.currentState = 1
utils.setBrightness(self.config, self.device.instanceId, (self.currentBrightness > 1 ? self.currentBrightness : 255), result => callback())
} else {
callback()
}
})
lightbulbService
.getCharacteristic(Characteristic.Brightness)
.on('get', callback => {
utils.getDevice(self.config, self.device.instanceId).then(device => {
self.currentBrightness = device.light[0]["5851"]
self.currentState = device.light[0]["5850"]
callback(null, parseInt(Math.round(self.currentBrightness * 100 / 255)))
})
})
.on('set', (powerOn, callback) => {
self.currentBrightness = Math.floor(255 * (powerOn / 100))
utils.setBrightness(self.config, self.device.instanceId, Math.round(255 * (powerOn / 100)), result => callback())
})
if(typeof this.device.light[0]["5706"] !== 'undefined'){
if(this.device.light[0]["5706"].length < 6){
this.device.light[0]["5706"] = "ffcea6" //Default value when it was offline
}
var hsl = utils.convertRGBToHSL(this.device.light[0]["5706"]);
lightbulbService
.addCharacteristic(Characteristic.Kelvin)
lightbulbService
.setCharacteristic(Characteristic.Kelvin, utils.getKelvin(this.device.light[0]["5709"]))
.setCharacteristic(Characteristic.Hue, hsl[0] * 360)
.setCharacteristic(Characteristic.Saturation, hsl[1] * 100)
lightbulbService
.getCharacteristic(Characteristic.Kelvin)
.on('get', callback => {
utils.getDevice(self.config, self.device.instanceId).then(device => {
self.currentKelvin = utils.getKelvin(device.light[0]["5709"])
callback(null, self.currentKelvin)
})
})
.on('set', (kelvin, callback) => {
utils.setKelvin(self.config, self.device.instanceId, kelvin, result => callback())
})
lightbulbService
.getCharacteristic(Characteristic.Hue)
.on('get', callback => {
utils.getDevice(self.config, self.device.instanceId).then(device => {
if(typeof device.light[0]["5706"] !== 'undefined' || device.light[0]["5706"].length < 6){
device.light[0]["5706"] = "ffcea6" //Default value when it fails polling
}
var hsl = utils.convertRGBToHSL(device.light[0]["5706"]);
callback(null, hsl[0] * 360)
})
})
.on('set', (hue, callback) => {
self.color.hue = hue / 360
if (typeof self.color.saturation !== 'undefined') {
utils.setColor(self.config, self.device.instanceId, self.color, result => callback())
self.color = {}
}else{
callback()
}
})
lightbulbService
.getCharacteristic(Characteristic.Saturation)
.on('get', callback => {
utils.getDevice(self.config, self.device.instanceId).then(device => {
if(typeof device.light[0]["5706"] !== 'undefined' || device.light[0]["5706"].length < 6){
device.light[0]["5706"] = "ffcea6" //Default value when it fails polling
}
var hsl = utils.convertRGBToHSL(device.light[0]["5706"]);
callback(null, hsl[1] * 100)
})
})
.on('set', (saturation, callback) => {
self.color.saturation = saturation / 100
if (typeof self.color.hue !== 'undefined') {
utils.setColor(self.config, self.device.instanceId, self.color, result => callback())
self.color = {}
}else{
callback()
}
})
}
return [accessoryInformation, lightbulbService]
}
}