-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
88 lines (79 loc) · 2.59 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
const Device = require('./Device');
let Service, Characteristic;
('use strict');
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory('@lyliya/homebridge-ledstrip-ble', 'LedStrip', LedStrip);
};
function LedStrip(log, config, api) {
this.log = log;
this.config = config;
this.homebridge = api;
this.bulb = new Service.Lightbulb(this.config.name);
// Set up Event Handler for bulb on/off
this.bulb
.getCharacteristic(Characteristic.On)
.on('get', this.getPower.bind(this))
.on('set', this.setPower.bind(this));
this.bulb
.getCharacteristic(Characteristic.Brightness)
.on('get', this.getBrightness.bind(this))
.on('set', this.setBrightness.bind(this));
this.bulb
.getCharacteristic(Characteristic.Hue)
.on('get', this.getHue.bind(this))
.on('set', this.setHue.bind(this));
this.bulb
.getCharacteristic(Characteristic.Saturation)
.on('get', this.getSaturation.bind(this))
.on('set', this.setSaturation.bind(this));
this.log('all event handler was setup.');
if (!this.config.uuid) return;
this.uuid = this.config.uuid;
this.log('Device UUID:', this.uuid);
this.device = new Device(this.uuid);
}
LedStrip.prototype = {
getServices: function () {
if (!this.bulb) return [];
this.log('Homekit asked to report service');
const infoService = new Service.AccessoryInformation();
infoService.setCharacteristic(Characteristic.Manufacturer, 'LedStrip');
return [infoService, this.bulb];
},
getPower: function (callback) {
this.log('Homekit Asked Power State', this.device.connected);
callback(null, this.device.power);
},
setPower: function (on, callback) {
this.log('Homekit Gave New Power State' + ' ' + on);
this.device.set_power(on);
callback(null);
},
getBrightness: function (callback) {
this.log('Homekit Asked Brightness');
callback(null, this.device.brightness);
},
setBrightness: function (brightness, callback) {
this.log('Homekit Set Brightness', brightness);
this.device.set_brightness(brightness);
callback(null);
},
getHue: function (callback) {
callback(null, this.device.hue);
},
setHue: function (hue, callback) {
this.log('Homekit Set Hue', hue);
this.device.set_hue(hue);
callback(null);
},
getSaturation: function (callback) {
callback(null, this.device.saturation);
},
setSaturation: function (saturation, callback) {
this.log('Homekit Set Saturation', saturation);
this.device.set_saturation(saturation);
callback(null);
}
};