forked from Adeptive/homebridge-soundtouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
169 lines (136 loc) · 5.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
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
var soundtouch = require('soundtouch');
var inherits = require('util').inherits;
var Service, Characteristic, VolumeCharacteristic;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
// we can only do this after we receive the homebridge API object
makeVolumeCharacteristic();
homebridge.registerAccessory("homebridge-soundtouch", "SoundTouch", SoundTouchAccessory);
};
//
// SoundTouch Accessory
//
function SoundTouchAccessory(log, config) {
this.log = log;
this.config = config;
this.name = config["name"];
this.room = config["room"];
if (!this.room) throw new Error("You must provide a config value for 'room'.");
this.service = new Service.Switch(this.name);
this.service
.getCharacteristic(Characteristic.On)
.on('get', this._getOn.bind(this))
.on('set', this._setOn.bind(this));
this.service
.addCharacteristic(VolumeCharacteristic)
.on('get', this._getVolume.bind(this))
.on('set', this._setVolume.bind(this));
// begin searching for a SoundTouch device with the given name
this.search();
}
SoundTouchAccessory.prototype.search = function() {
var accessory = this;
accessory.soundtouch = soundtouch;
accessory.soundtouch.search(function(device) {
if (accessory.room != device.name) {
accessory.log("Ignoring device because the room name '%s' does not match the desired name '%s'.", device.name, accessory.room);
return;
}
accessory.log("Found Bose SoundTouch device: %s", device.name);
accessory.device = device;
//we found the device, so stop looking
soundtouch.stopSearching();
}, function(device) {
accessory.log("Bose SoundTouch device goes offline: %s", device.name);
});
};
SoundTouchAccessory.prototype.getInformationService = function() {
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Name, this.name)
.setCharacteristic(Characteristic.Manufacturer, 'Bose SoundTouch')
.setCharacteristic(Characteristic.Model, '1.0.0')
.setCharacteristic(Characteristic.SerialNumber, this.room);
return informationService;
};
SoundTouchAccessory.prototype.getServices = function() {
return [this.service, this.getInformationService()];
};
SoundTouchAccessory.prototype._getOn = function(callback) {
if (!this.device) {
this.log.warn("Ignoring request; SoundTouch device has not yet been discovered.");
callback(new Error("SoundTouch has not been discovered yet."));
return;
}
var accessory = this;
this.device.isAlive(function(isOn) {
accessory.log('Check if is playing: %s', isOn);
callback(null, isOn);
});
};
SoundTouchAccessory.prototype._setOn = function(on, callback) {
if (!this.device) {
this.log.warn("Ignoring request; SoundTouch device has not yet been discovered.");
callback(new Error("SoundTouch has not been discovered yet."));
return;
}
var accessory = this;
if (on) {
this.device.powerOn(function(isTurnedOn) {
accessory.log(isTurnedOn ? 'Power On' : 'Was already powered on');
accessory.device.play(function(json) {
accessory.log('Playing...');
callback(null);
});
});
} else {
this.device.powerOff(function() {
accessory.log('Powering Off...');
callback(null);
});
}
};
SoundTouchAccessory.prototype._getVolume = function(callback) {
if (!this.device) {
this.log.warn("Ignoring request; SoundTouch device has not yet been discovered.");
callback(new Error("SoundTOuch has not been discovered yet."));
return;
}
var accessory = this;
this.device.getVolume(function(json) {
var volume = json.volume.actualvolume;
accessory.log("Current volume: %s", volume);
callback(null, volume * 1);
});
};
SoundTouchAccessory.prototype._setVolume = function(volume, callback) {
if (!this.device) {
this.log.warn("Ignoring request; SoundTouch device has not yet been discovered.");
callback(new Error("SoundTOuch has not been discovered yet."));
return;
}
var accessory = this;
this.device.setVolume(volume, function() {
accessory.log('Setting volume to %s', volume);
callback(null);
});
};
//
// Custom Characteristic for Volume
//
function makeVolumeCharacteristic() {
VolumeCharacteristic = function() {
Characteristic.call(this, 'Volume', '91288267-5678-49B2-8D22-F57BE995AA00');
this.setProps({
format: Characteristic.Formats.INT,
unit: Characteristic.Units.PERCENTAGE,
maxValue: 100,
minValue: 0,
minStep: 1,
perms: [Characteristic.Perms.READ, Characteristic.Perms.WRITE, Characteristic.Perms.NOTIFY]
});
this.value = this.getDefaultValue();
};
inherits(VolumeCharacteristic, Characteristic);
}