forked from vvpossible/homebridge_yeelight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·300 lines (244 loc) · 9.98 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
var yeeLight = require('./lib/yee.js');
var Service, Characteristic, Accessory, UUIDGen;
module.exports = function(homebridge) {
Accessory = homebridge.platformAccessory;
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
UUIDGen = homebridge.hap.uuid;
homebridge.registerPlatform("homebridge-yeelight", "yeelight", YeePlatform, true);
}
function YeePlatform(log, config, api) {
log("YeePlatform Init");
this.log = log;
this.config = config;
this.yeeAccessories = [];
var platform = this;
if (api) {
this.api = api;
this.api.on('didFinishLaunching', function() {
platform.log("DidFinishLaunching");
platform.yeeAgent = new yeeLight.YeeAgent("0.0.0.0", platform);
platform.yeeAgent.startDisc();
}.bind(this));
}
}
YeePlatform.prototype = {
onDevFound: function(dev) {
var that = this;
var uuid;
var found = 0;
var newAccessory = null;
var lightbulbService = null;
var nightModeService = null;
var name;
var isNightModeSupported = dev.model == 'ceiling3' || dev.model == 'ceiling4'
console.log(dev.model, 'here!!!!')
for (var index in this.yeeAccessories) {
var accessory = this.yeeAccessories[index];
if (accessory.context.did == dev.did) {
newAccessory = accessory;
found = 1;
break;
}
}
if (found) {
this.log("cached accessory: " + newAccessory.context.did);
lightbulbService = newAccessory.getService(Service.Lightbulb);
} else {
uuid = UUIDGen.generate(dev.did);
name = dev.did.name || dev.did.substring(dev.did.length-6);
this.log("found dev: " + name);
newAccessory = new Accessory(name, uuid);
newAccessory.context.did = dev.did;
newAccessory.context.model = dev.model;
lightbulbService = new Service.Lightbulb(name);
}
dev.ctx = newAccessory;
lightbulbService
.getCharacteristic(Characteristic.On)
.on('set', function(value, callback) { that.exeCmd(dev.did, "power", value, callback);})
.value = dev.power;
if (!found) {
lightbulbService
.addCharacteristic(Characteristic.Brightness)
.on('set', function(value, callback) { that.exeCmd(dev.did, "brightness", value, callback);})
.value = dev.bright;
if (dev.model == "color" || dev.model == "stripe" || dev.model == "bedside" || dev.model == "bslamp1" || dev.model == 'ceiling4') {
lightbulbService
.addCharacteristic(Characteristic.Hue)
.on('set', function(value, callback) { that.exeCmd(dev.did, "hue", value, callback);})
.value = dev.hue;
lightbulbService
.addCharacteristic(Characteristic.Saturation)
.on('set', function(value, callback) { that.exeCmd(dev.did, "saturation", value, callback);})
.value = dev.sat;
}
} else {
lightbulbService
.getCharacteristic(Characteristic.Brightness)
.on('set', function(value, callback) { that.exeCmd(dev.did, "brightness", value, callback);})
.value = dev.bright;
if (dev.model == "color" || dev.model == "stripe" || dev.model == "bedside" || dev.model == "bslamp1" || dev.model == 'ceiling4') {
lightbulbService
.getCharacteristic(Characteristic.Hue)
.on('set', function(value, callback) { that.exeCmd(dev.did, "hue", value, callback);})
.value = dev.hue;
lightbulbService
.getCharacteristic(Characteristic.Saturation)
.on('set', function(value, callback) { that.exeCmd(dev.did, "saturation", value, callback);})
.value = dev.sat;
}
}
if (isNightModeSupported) {
const colorModeValue = 0;
if (dev.color_mode == 5) {
colorModeValue = 1;
}
var nighModeName = 'Night Mode'
if (found) {
nightModeService = newAccessory.getService(Service.Switch);
} else {
nightModeService = new Service.Switch(nighModeName);
}
nightModeService
.getCharacteristic(Characteristic.On)
.on('set', function(value, callback) { that.exeCmd(dev.did, "night_mode", value, callback);})
.value = colorModeValue;
if (!found) {
newAccessory.addService(nightModeService, nighModeName);
}
}
if (dev.model.search(/color.*/g) !== -1 || dev.model.search(/strip.*/g) !== -1) {
} else {
if (dev.model !== 'mono' && dev.model !== 'ceiling2') {
lightbulbService.addOptionalCharacteristic(Characteristic.ColorTemperature);
lightbulbService.getCharacteristic(Characteristic.ColorTemperature)
.on('set', function(value, callback) { that.exeCmd(dev.did, "ct", value, callback)})
.on('get', function(callback){callback(null, dev.ct);})
.updateValue(dev.ct);
}
}
newAccessory.reachable = true;
if (!found) {
newAccessory.addService(lightbulbService, name);
this.yeeAccessories.push(newAccessory);
this.api.registerPlatformAccessories("homebridge-yeelight", "yeelight", [newAccessory]);
}
},
onDevConnected: function(dev) {
this.log("accesseory reachable");
this.log("dev connected " + dev.did + " " + dev.connected);
var accessory = dev.ctx;
accessory.updateReachability(true);
},
onDevDisconnected: function(dev) {
this.log("accesseory unreachable");
this.log("dev disconnected " + dev.did + " " + dev.connected);
var accessory = dev.ctx;
// updateReachability seems have bug, but remove the accessory will cause
// the name of the light gone, leave the user to decide...
if (1) {
accessory.updateReachability(false);
} else {
this.api.unregisterPlatformAccessories("homebridge-yeelight", "yeelight", [accessory]);
var idx = this.yeeAccessories.indexOf(accessory);
if (idx > -1) {
this.yeeAccessories.splice(idx, 1);
}
this.yeeAgent.delDevice(dev.did);
}
},
onDevPropChange: function(dev, prop, val) {
var accessory = dev.ctx;
var character;
var lightbulbService = accessory.getService(Service.Lightbulb);
var nightModeService = accessory.getService(Service.Switch);
this.log("update accessory prop: " + prop + "value: " + val);
if (prop == "power") {
character = lightbulbService.getCharacteristic(Characteristic.On)
} else if (prop == "bright") {
character = lightbulbService.getCharacteristic(Characteristic.Brightness)
} else if (prop == "sat") {
character = lightbulbService.getCharacteristic(Characteristic.Saturation)
} else if (prop == "hue") {
character = lightbulbService.getCharacteristic(Characteristic.Hue)
} else if (prop == "ct") {
character = lightbulbService.getCharacteristic(Characteristic.ColorTemperature)
} else if (prop == "night_mode") {
character = nightModeService.getCharacteristic(Characteristic.On)
} else {
return;
}
character.updateValue(val);
},
configureAccessory: function(accessory) {
var platform = this;
//accessory.updateReachability(false);
accessory.reachable = true;
accessory.on('identify', function(paired, callback) {
platform.log("identify ....");
});
this.yeeAccessories.push(accessory);
return;
},
exeCmd: function(did, characteristic, value, callback) {
dev = this.yeeAgent.getDevice(did);
if (dev == null) {
this.log("no device found for did: " + did);
return;
}
switch(characteristic.toLowerCase()) {
case 'identify':
this.log("identfy....");
dev.setBlink();
break;
case 'power':
dev.setPower(value);
break;
case 'hue':
dev.setColor(value, dev.sat);
break;
case 'brightness':
dev.setBright(value);
break;
case 'night_mode':
dev.setNightMode(value);
case 'saturation':
dev.setColor(dev.hue, value);
break;
case 'ct':
dev.setCT(value);
break;
default:
break;
}
if (callback)
callback();
},
/*
configurationRequestHandler : function(context, request, callback) {
this.log("Context: ", JSON.stringify(context));
this.log("Request: ", JSON.stringify(request));
// Check the request response
if (request && request.response && request.response.inputs && request.response.inputs.name) {
this.addAccessory(request.response.inputs.name);
return;
}
var respDict = {
"type": "Interface",
"interface": "input",
"title": "Add Accessory",
"items": [
{
"id": "name",
"title": "Name",
"placeholder": "Fancy Light"
},
]
}
context.ts = "Hello";
//invoke callback to update setup UI
callback(respDict);
}
*/
};