This repository has been archived by the owner on Dec 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
93 lines (80 loc) · 2.08 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
const colorsys = require('colorsys');
let Service;
let Characteristic;
module.exports = homebridge => {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory('homebridge-fake-rgb', 'Fake-RGB', RgbAccessory);
};
function RgbAccessory(log, config) {
this.log = log;
this.config = config;
this.name = config.name;
this.power = 0;
this.brightness = 100;
this.saturation = 0;
this.hue = 0;
this.log(`Initialized '${this.name}'`);
}
RgbAccessory.prototype.setColor = () => {
const color = colorsys.hsv_to_rgb({
h: this.hue,
s: this.saturation,
v: this.brightness
});
if (!this.power) {
color.r = 0;
color.g = 0;
color.b = 0;
}
this.log('set color to', color.r, color.g, color.b);
};
RgbAccessory.prototype.getServices = () => {
const lightbulbService = new Service.Lightbulb(this.name);
const bulb = this;
lightbulbService
.getCharacteristic(Characteristic.On)
.on('get', callback => {
callback(null, bulb.power);
})
.on('set', (value, callback) => {
bulb.power = value;
bulb.log(`power to ${value}`);
bulb.setColor();
callback();
});
lightbulbService
.addCharacteristic(Characteristic.Brightness)
.on('get', callback => {
callback(null, bulb.brightness);
})
.on('set', (value, callback) => {
bulb.brightness = value;
bulb.log(`brightness to ${value}`);
bulb.setColor();
callback();
});
lightbulbService
.addCharacteristic(Characteristic.Hue)
.on('get', callback => {
callback(null, bulb.hue);
})
.on('set', (value, callback) => {
bulb.hue = value;
bulb.log(`hue to ${value}`);
bulb.setColor();
callback();
});
lightbulbService
.addCharacteristic(Characteristic.Saturation)
.on('get', callback => {
callback(null, bulb.saturation);
})
.on('set', (value, callback) => {
bulb.saturation = value;
bulb.log(`saturation to ${value}`);
bulb.setColor();
callback();
});
return [lightbulbService];
};