-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
176 lines (150 loc) · 6.16 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
var request = require('request');
var parseString = require('xml2js').parseString;
var Service, Characteristic;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-marantz-volume", "marantz-volume", ReceiverVolume);
}
function ReceiverVolume(log, config) {
this.log = log;
this.name = config['name'] || "Receiver Volume";
this.maxVolume = config['maxVolume'] || 70;
this.host = config['host'];
this.zone = (config['zone'] || 1) | 0; // default to 1, and make sure its an integer
this.controlPower = !!config['controlPower']; // default to false, and make sure its a bool
this.useFan = !!config['useFan']; // default to false, and make sure its a bool
this.controlMute = !!config['controlMute'] && this.controlPower === false;
this.mapMaxVolumeTo100 = !!config['mapMaxVolumeTo100'];
//cap maxVolume. Denon/Marantz percentage maxes at 98 in receiver settings
if(this.maxVolume > 98)
this.maxVolume = 98;
if (!this.host) {
this.log.warn('Config is missing host/IP of receiver');
callback(new Error('No host/IP defined.'));
return;
}
if (this.zone < 1 && this.zone > 2) {
this.log.warn('Zone number is not recognized (must be 1 or 2); assuming zone 1');
this.zone = 1;
}
this.zoneName = this.zone === 1 ? "MainZone" : "Zone2";
if (!this.controlPower) {
this.fakePowerState = 1; //default to on so that brightness will update in HomeKit apps
}
}
ReceiverVolume.prototype.getStatus = function(callback) {
var statusUrl = `http://${this.host}/goform/form${this.zoneName}_${this.zoneName}XmlStatusLite.xml`;
request.get(statusUrl, function (error, response, body) {
var xml = '';
if (!error && response.statusCode == 200) {
parseString(xml + body, function (err, result) {
callback(result.item);
}.bind(this));
}else{
callback(null);
}
}.bind(this));
}
ReceiverVolume.prototype.setControl = function (control, command, callback) {
var controlUrl = `http://${this.host}/goform/formiPhoneApp${control}.xml?${this.zone}+${command}`;
request.get(controlUrl, function (error, response, body) {
if (!error && response.statusCode == 200) {
callback(null);
} else {
callback(error);
}
}.bind(this));
}
ReceiverVolume.prototype.getPowerOn = function(callback) {
if (this.controlPower) {
this.getStatus(function(status) {
var powerState = status ? (status.Power[0].value[0] === "ON" ? 1 : 0) : 0;
this.log("Receiver %s Volume power state is %s", this.zoneName, powerState);
callback(null, powerState);
}.bind(this));
} else if (this.controlMute) {
this.getStatus(function(status) {
var powerState = status ? (status.Mute[0].value[0] === "on" ? 0 : 1) : 0;
this.log("Receiver %s Volume state is %s", this.zoneName, powerState);
callback(null, powerState);
}.bind(this));
} else {
this.log("Receiver %s Volume power state is %s", this.zoneName, this.fakePowerState);
callback(null, this.fakePowerState);
}
}
ReceiverVolume.prototype.setPowerOn = function(powerOn, callback) {
if (this.controlPower) {
var command = powerOn ? 'PowerOn' : 'PowerStandby';
this.log("Set receiver %s volume power state to %s", this.zoneName, command);
this.setControl('Power', command, callback);
} else if (this.controlMute) {
var command = powerOn ? 'MuteOff' : 'MuteOn';
this.log("Set receiver %s volume state to %s", this.zoneName, command);
this.setControl('Mute', command, callback);
} else {
this.fakePowerState = powerOn ? 1 : 0;
//this.fakePowerState = 1;
this.log("Set receiver %s volume power state to %s", this.zoneName, this.fakePowerState);
callback(null);
}
}
ReceiverVolume.prototype.setBrightness = function(newLevel, callback) {
if(this.mapMaxVolumeTo100){
var volumeMultiplier = this.maxVolume/100;
var newVolume = volumeMultiplier * newLevel;
}else{
//cap to max volume set in homebridge config.json
var newVolume = Math.min(newLevel, this.maxVolume);
}
//cap newVolume. //Denon/Marantz percentage maxes at 98 in receiver settings
if(newVolume > 98){
newVolume = 98;
}
//convert volume percentage to relative volume
var relativeVolume = (2 * (newVolume - 80)).toFixed(0) / 2.0;
//cap between -80 and 0
relativeVolume = Math.max(-80.0, Math.min(0.0, relativeVolume));
this.setControl('Volume', relativeVolume, callback);
}
ReceiverVolume.prototype.getBrightness = function(callback) {
this.getStatus(function(status) {
if(status){
var volume = parseInt(status.MasterVolume[0].value[0]) + 80;
this.log("Get receiver volume %s ", volume);
if(this.mapMaxVolumeTo100){
volume = volume * (100/this.maxVolume);
}
callback(null, volume);
}else{
this.log("Unable to get receiver status");
callback(null);
}
}.bind(this));
}
ReceiverVolume.prototype.getServices = function() {
if (this.useFan) {
var lightbulbService = new Service.Fan(this.name);
}
else {
var lightbulbService = new Service.Lightbulb(this.name);
}
lightbulbService
.getCharacteristic(Characteristic.On)
.on('get', this.getPowerOn.bind(this))
.on('set', this.setPowerOn.bind(this));
if (this.useFan) {
lightbulbService
.addCharacteristic(new Characteristic.RotationSpeed())
.on('get', this.getBrightness.bind(this))
.on('set', this.setBrightness.bind(this));
}
else {
lightbulbService
.addCharacteristic(new Characteristic.Brightness())
.on('get', this.getBrightness.bind(this))
.on('set', this.setBrightness.bind(this));
}
return [lightbulbService];
}