-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
86 lines (69 loc) · 2.53 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
var Service, Characteristic, sm;
const SamsungMultiroom = require('samsung-multiroom');
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory('homebridge-samsung-multiroom', 'SamsungMultiroom', SamsungMultiroomAccessory);
}
function SamsungMultiroomAccessory(log,config, api){
this.log = log;
this.host = config['host'];
this.name = config['name'];
this.port = config['port'] || 55001;
sm = new SamsungMultiroom({
host: this.host,
port: this.port
});
this.volumeService = new Service.Lightbulb(this.name, "volumeService");
this.volumeService
.getCharacteristic(Characteristic.On)
.on('get', this.getMuteState.bind(this))
.on('set', this.setMuteState.bind(this));
this.volumeService
.addCharacteristic(new Characteristic.Brightness())
.on('get', this.getVolume.bind(this))
.on('set', this.setVolume.bind(this));
}
SamsungMultiroomAccessory.prototype.getMuteState = function(callback) {
sm.getMute( (error, mute) => {
if(error){
this.log(`${this.name} speaker couldn't return mute state.`);
return callback(null, false);
}
this.log(`${this.name} speaker muted: %s`, mute ? "Yes" : "No");
callback(null, !mute);
});
}
SamsungMultiroomAccessory.prototype.setMuteState = function(state, callback) {
sm.setMute(!state, (error, mute) => {
if(error){
this.log(`${this.name} speaker couldn't set mute state.`);
return callback(null, false);
}
this.log(`${this.name} speaker muted: %s`, !state ? "Yes" : "No");
callback(null, true);
});
}
SamsungMultiroomAccessory.prototype.getVolume = function(callback) {
sm.getVolume( (error, volume) => {
if(error){
this.log(`${this.name} speaker couldn't return volume.`);
return callback(null, false);
}
this.log(`${this.name} speaker volume: ${volume}`);
callback(null, parseInt(volume));
});
}
SamsungMultiroomAccessory.prototype.setVolume = function(level, callback) {
sm.setVolume(level, (error, mute) => {
if(error){
this.log(`${this.name} speaker couldn't set volume to ${level}.`);
return callback(null, false);
}
this.log(`${this.name} speaker set volume to ${level}.`);
callback(null, true);
});
}
SamsungMultiroomAccessory.prototype.getServices = function() {
return [this.volumeService];
}