Skip to content

Commit

Permalink
Merge pull request #46 from amitgandhinz/master
Browse files Browse the repository at this point in the history
Switch will mute/unmute instead of play/stop
  • Loading branch information
nfarina authored Dec 11, 2016
2 parents 0370f42 + 21a3a26 commit 8f1f7bd
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 30 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ Example config.json:
{
"accessory": "Sonos",
"name": "Bedroom Speakers",
"room": "Bedroom"
"room": "Bedroom",
"mute": true
}
]
}
Expand All @@ -17,6 +18,8 @@ The `room` parameter must match the room name in Sonos exactly.

Note that the name "Speakers" is used in the name for this example instead of something more intuitive like "Sonos" or "Music" or "Radio", as Siri has many stronger associations for those words. For instance, including "Sonos" in the name will likely cause Siri to just launch the Sonos app. And including "Music" in the name will cause Siri to launch the built-in Music app.

The "mute" parameter is optional. Setting it to `true` will mute/unmute the speaker instead of a stop/play.

# Alternative

You also might check out this [fork of `homebridge-sonos`](https://github.com/dominicstelljes/homebridge-sonos) by [dominicstelljes](https://github.com/dominicstelljes) that exposes the Sonos as a "lightbulb" instead of a switch. This will allow you control the volume through Siri - "Set the Speakers to 50%" for example. But it will have some side-effects. Check out his README for more details.
100 changes: 71 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ function SonosAccessory(log, config) {
this.config = config;
this.name = config["name"];
this.room = config["room"];
this.mute = config["mute"];

if (!this.room) throw new Error("You must provide a config value for 'room'.");

Expand Down Expand Up @@ -223,17 +224,32 @@ SonosAccessory.prototype.getOn = function(callback) {
return;
}

this.device.getCurrentState(function(err, state) {

if (err) {
callback(err);
}
else {
var on = (state == "playing");
callback(null, on);
}

}.bind(this));
if (!this.mute) {
this.device.getCurrentState(function(err, state) {
if (err) {
callback(err);
}
else {
this.log.warn("Current state for Sonos: " + state);
var on = (state == "playing");
callback(null, on);
}
}.bind(this));
}
else {
this.device.getMuted(function(err, state) {

if (err) {
callback(err);
}
else {
this.log.warn("Current state for Sonos: " + state);
var on = (state == false);
callback(null, on);
}
}.bind(this));

}
}

SonosAccessory.prototype.setOn = function(on, callback) {
Expand All @@ -245,27 +261,53 @@ SonosAccessory.prototype.setOn = function(on, callback) {

this.log("Setting power to " + on);

if (on) {
this.device.play(function(err, success) {
this.log("Playback attempt with success: " + success);
if (err) {
callback(err);
}
else {
callback(null);
}
}.bind(this));
if (!this.mute){
if (on) {
this.device.play(function(err, success) {
this.log("Playback attempt with success: " + success);
if (err) {
callback(err);
}
else {
callback(null);
}
}.bind(this));
}
else {
this.device.stop(function(err, success) {
this.log("Stop attempt with success: " + success);
if (err) {
callback(err);
}
else {
callback(null);
}
}.bind(this));
}
}
else {
this.device.stop(function(err, success) {
this.log("Stop attempt with success: " + success);
if (err) {
callback(err);
}
else {
callback(null);
}
if (on) {
this.device.setMuted(false, function(err, success) {
this.log("Unmute attempt with success: " + success);
if (err) {
callback(err);
}
else {
callback(null);
}
}.bind(this));
}
else {
this.device.setMuted(true, function(err, success) {
this.log("Mute attempt with success: " + success);
if (err) {
callback(err);
}
else {
callback(null);
}
}.bind(this));
}
}
}

Expand Down

0 comments on commit 8f1f7bd

Please sign in to comment.