Skip to content

Commit

Permalink
[sonos] Add bass/treble/loudness controls
Browse files Browse the repository at this point in the history
Related to openhab#9874

Signed-off-by: Laurent Garnier <[email protected]>
  • Loading branch information
lolodomo committed Jan 22, 2021
1 parent 37bc57f commit 0c018af
Show file tree
Hide file tree
Showing 18 changed files with 199 additions and 0 deletions.
3 changes: 3 additions & 0 deletions bundles/org.openhab.binding.sonos/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ The devices support the following channels:
| alarm | Switch | W | Set the first occurring alarm either ON or OFF. Alarms first have to be defined through the Sonos Controller app | all |
| alarmproperties | String | R | Properties of the alarm currently running | all |
| alarmrunning | Switch | R | Set to ON if the alarm was triggered | all |
| bass | Number | RW | Set or get the bass level adjustment (value in range -10 / 10) | all |
| clearqueue | Switch | W | Suppress all songs from the current queue | all |
| control | Player | RW | Control the Zone Player, e.g. PLAY/PAUSE/NEXT/PREVIOUS | all |
| coordinator | String | R | UDN of the coordinator for the current group | all |
Expand All @@ -68,6 +69,7 @@ The devices support the following channels:
| analoglinein | Switch | R | Indicator set to ON when the analog line-in of the Zone Player is connected | Amp |
| digitallinein | Switch | R | Indicator set to ON when the digital line-in of the Zone Player is connected | Amp |
| localcoordinator | Switch | R | Indicator set to ON if the this Zone Player is the Zone Group Coordinator | all |
| loudness | Switch | RW | Enable or disable the loudness | all |
| mute | Switch | RW | Set or get the mute state of the master volume of the Zone Player | all |
| nightmode | Switch | RW | Enable or disable the night mode feature | PLAYBAR, PLAYBASE, Beam, Amp |
| notificationsound | String | W | Play a notification sound by a given URI | all |
Expand All @@ -93,6 +95,7 @@ The devices support the following channels:
| standalone | Switch | W | Make the Zone Player leave its Group and become a standalone Zone Player | all |
| state | String | R | The State channel contains state of the Zone Player, e.g. PLAYING, STOPPED, ... | all |
| stop | Switch | W | Write `ON` to this channel: Stops the Zone Player player. | all |
| treble | Number | RW | Set or get the treble level adjustment (value in range -10 / 10) | all |
| tuneinstationid | String | RW | Provide the current TuneIn station id or play the TuneIn radio given by its station id | all |
| volume | Dimmer | RW | Set or get the master volume of the Zone Player | all |
| zonegroupid | String | R | Id of the Zone Group the Zone Player belongs to | all |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public class SonosBindingConstants {
public static final String ALARM = "alarm";
public static final String ALARMPROPERTIES = "alarmproperties";
public static final String ALARMRUNNING = "alarmrunning";
public static final String BASS = "bass";
public static final String CLEARQUEUE = "clearqueue";
public static final String CONTROL = "control";
public static final String COORDINATOR = "coordinator";
Expand All @@ -95,6 +96,7 @@ public class SonosBindingConstants {
public static final String ANALOGLINEIN = "analoglinein";
public static final String DIGITALLINEIN = "digitallinein";
public static final String LOCALCOORDINATOR = "localcoordinator";
public static final String LOUDNESS = "loudness";
public static final String MUTE = "mute";
public static final String NIGHTMODE = "nightmode";
public static final String NOTIFICATIONSOUND = "notificationsound";
Expand All @@ -120,6 +122,7 @@ public class SonosBindingConstants {
public static final String STANDALONE = "standalone";
public static final String STATE = "state";
public static final String STOP = "stop";
public static final String TREBLE = "treble";
public static final String TUNEINSTATIONID = "tuneinstationid";
public static final String VOLUME = "volume";
public static final String ZONEGROUPID = "zonegroupid";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ public class ZonePlayerHandler extends BaseThingHandler implements UpnpIOPartici

private static final int TUNEIN_DEFAULT_SERVICE_TYPE = 65031;

private static final int MIN_BASS = -10;
private static final int MAX_BASS = 10;
private static final int MIN_TREBLE = -10;
private static final int MAX_TREBLE = 10;

private final Logger logger = LoggerFactory.getLogger(ZonePlayerHandler.class);

private final ThingRegistry localThingRegistry;
Expand Down Expand Up @@ -259,6 +264,15 @@ public void handleCommand(ChannelUID channelUID, Command command) {
case VOLUME:
setVolumeForGroup(command);
break;
case BASS:
setBass(command);
break;
case TREBLE:
setTreble(command);
break;
case LOUDNESS:
setLoudness(command);
break;
case ADD:
addMember(command);
break;
Expand Down Expand Up @@ -485,6 +499,15 @@ public void onValueReceived(@Nullable String variable, @Nullable String value, @
case "MuteMaster":
updateChannel(MUTE);
break;
case "Bass":
updateChannel(BASS);
break;
case "Treble":
updateChannel(TREBLE);
break;
case "LoudnessMaster":
updateChannel(LOUDNESS);
break;
case "NightMode":
updateChannel(NIGHTMODE);
break;
Expand Down Expand Up @@ -694,6 +717,24 @@ protected void updateChannel(String channelId) {
newState = new PercentType(value);
}
break;
case BASS:
value = getBass();
if (value != null) {
newState = new DecimalType(value);
}
break;
case TREBLE:
value = getTreble();
if (value != null) {
newState = new DecimalType(value);
}
break;
case LOUDNESS:
value = getLoudness();
if (value != null) {
newState = isLoudnessEnabled() ? OnOffType.ON : OnOffType.OFF;
}
break;
case MUTE:
value = getMute();
if (value != null) {
Expand Down Expand Up @@ -1238,6 +1279,22 @@ public long getRefreshedCurrenTrackNr() {
return stateMap.get("VolumeMaster");
}

public @Nullable String getBass() {
return stateMap.get("Bass");
}

public @Nullable String getTreble() {
return stateMap.get("Treble");
}

public @Nullable String getLoudness() {
return stateMap.get("LoudnessMaster");
}

public boolean isLoudnessEnabled() {
return "1".equals(getLoudness());
}

public @Nullable String getTransportState() {
return stateMap.get("TransportState");
}
Expand Down Expand Up @@ -1600,6 +1657,80 @@ public void setVolumeForGroup(Command command) {
}
}

public void setBass(Command command) {
if (command instanceof IncreaseDecreaseType || command instanceof DecimalType) {
Map<String, String> inputs = new HashMap<>();

String newValue = null;
String currentBass = getBass();
if (command == IncreaseDecreaseType.INCREASE && currentBass != null) {
int i = Integer.valueOf(currentBass);
newValue = String.valueOf(Math.min(MAX_BASS, i + 1));
} else if (command == IncreaseDecreaseType.DECREASE && currentBass != null) {
int i = Integer.valueOf(currentBass);
newValue = String.valueOf(Math.max(MIN_BASS, i - 1));
} else if (command instanceof DecimalType) {
newValue = String.valueOf(((DecimalType) command).intValue());
} else {
return;
}
inputs.put("DesiredBass", newValue);

Map<String, String> result = service.invokeAction(this, "RenderingControl", "SetBass", inputs);

for (String variable : result.keySet()) {
this.onValueReceived(variable, result.get(variable), "RenderingControl");
}
}
}

public void setTreble(Command command) {
if (command instanceof IncreaseDecreaseType || command instanceof DecimalType) {
Map<String, String> inputs = new HashMap<>();

String newValue = null;
String currentTreble = getTreble();
if (command == IncreaseDecreaseType.INCREASE && currentTreble != null) {
int i = Integer.valueOf(currentTreble);
newValue = String.valueOf(Math.min(MAX_TREBLE, i + 1));
} else if (command == IncreaseDecreaseType.DECREASE && currentTreble != null) {
int i = Integer.valueOf(currentTreble);
newValue = String.valueOf(Math.max(MIN_TREBLE, i - 1));
} else if (command instanceof DecimalType) {
newValue = String.valueOf(((DecimalType) command).intValue());
} else {
return;
}
inputs.put("DesiredTreble", newValue);

Map<String, String> result = service.invokeAction(this, "RenderingControl", "SetTreble", inputs);

for (String variable : result.keySet()) {
this.onValueReceived(variable, result.get(variable), "RenderingControl");
}
}
}

public void setLoudness(Command command) {
if (command instanceof OnOffType || command instanceof OpenClosedType || command instanceof UpDownType) {
Map<String, String> inputs = new HashMap<>();
inputs.put("Channel", "Master");

if (command.equals(OnOffType.ON) || command.equals(UpDownType.UP) || command.equals(OpenClosedType.OPEN)) {
inputs.put("DesiredLoudness", "True");
} else if (command.equals(OnOffType.OFF) || command.equals(UpDownType.DOWN)
|| command.equals(OpenClosedType.CLOSED)) {
inputs.put("DesiredLoudness", "False");
}

Map<String, String> result = service.invokeAction(this, "RenderingControl", "SetLoudness", inputs);

for (String variable : result.keySet()) {
this.onValueReceived(variable, result.get(variable), "RenderingControl");
}
}
}

/**
* Checks if the player receiving the command is part of a group that
* consists of randomly added players or contains bonded players
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<channel id="alarm" typeId="alarm"/>
<channel id="alarmproperties" typeId="alarmproperties"/>
<channel id="alarmrunning" typeId="alarmrunning"/>
<channel id="bass" typeId="bass"/>
<channel id="control" typeId="system.media-control"/>
<channel id="currentalbum" typeId="currentalbum"/>
<channel id="currentalbumart" typeId="currentalbumart"/>
Expand All @@ -27,6 +28,7 @@
<channel id="favorite" typeId="favorite"/>
<channel id="led" typeId="led"/>
<channel id="localcoordinator" typeId="localcoordinator"/>
<channel id="loudness" typeId="loudness"/>
<channel id="mute" typeId="system.mute"/>
<channel id="notificationsound" typeId="notificationsound"/>
<channel id="playlist" typeId="playlist"/>
Expand All @@ -45,6 +47,7 @@
<channel id="standalone" typeId="standalone"/>
<channel id="state" typeId="state"/>
<channel id="stop" typeId="stop"/>
<channel id="treble" typeId="treble"/>
<channel id="tuneinstationid" typeId="tuneinstationid"/>
<channel id="volume" typeId="system.volume"/>
<channel id="zonegroupid" typeId="zonegroupid"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<channel id="alarm" typeId="alarm"/>
<channel id="alarmproperties" typeId="alarmproperties"/>
<channel id="alarmrunning" typeId="alarmrunning"/>
<channel id="bass" typeId="bass"/>
<channel id="control" typeId="system.media-control"/>
<channel id="currentalbum" typeId="currentalbum"/>
<channel id="currentalbumart" typeId="currentalbumart"/>
Expand All @@ -26,6 +27,7 @@
<channel id="favorite" typeId="favorite"/>
<channel id="led" typeId="led"/>
<channel id="localcoordinator" typeId="localcoordinator"/>
<channel id="loudness" typeId="loudness"/>
<channel id="mute" typeId="system.mute"/>
<channel id="notificationsound" typeId="notificationsound"/>
<channel id="playlist" typeId="playlist"/>
Expand All @@ -45,6 +47,7 @@
<channel id="standalone" typeId="standalone"/>
<channel id="state" typeId="state"/>
<channel id="stop" typeId="stop"/>
<channel id="treble" typeId="treble"/>
<channel id="tuneinstationid" typeId="tuneinstationid"/>
<channel id="volume" typeId="system.volume"/>
<channel id="zonegroupid" typeId="zonegroupid"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<channel id="alarm" typeId="alarm"/>
<channel id="alarmproperties" typeId="alarmproperties"/>
<channel id="alarmrunning" typeId="alarmrunning"/>
<channel id="bass" typeId="bass"/>
<channel id="control" typeId="system.media-control"/>
<channel id="currentalbum" typeId="currentalbum"/>
<channel id="currentalbumart" typeId="currentalbumart"/>
Expand All @@ -26,6 +27,7 @@
<channel id="favorite" typeId="favorite"/>
<channel id="led" typeId="led"/>
<channel id="localcoordinator" typeId="localcoordinator"/>
<channel id="loudness" typeId="loudness"/>
<channel id="mute" typeId="system.mute"/>
<channel id="notificationsound" typeId="notificationsound"/>
<channel id="playlist" typeId="playlist"/>
Expand All @@ -45,6 +47,7 @@
<channel id="standalone" typeId="standalone"/>
<channel id="state" typeId="state"/>
<channel id="stop" typeId="stop"/>
<channel id="treble" typeId="treble"/>
<channel id="tuneinstationid" typeId="tuneinstationid"/>
<channel id="volume" typeId="system.volume"/>
<channel id="zonegroupid" typeId="zonegroupid"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<channel id="alarm" typeId="alarm"/>
<channel id="alarmproperties" typeId="alarmproperties"/>
<channel id="alarmrunning" typeId="alarmrunning"/>
<channel id="bass" typeId="bass"/>
<channel id="control" typeId="system.media-control"/>
<channel id="currentalbum" typeId="currentalbum"/>
<channel id="currentalbumart" typeId="currentalbumart"/>
Expand All @@ -27,6 +28,7 @@
<channel id="favorite" typeId="favorite"/>
<channel id="led" typeId="led"/>
<channel id="localcoordinator" typeId="localcoordinator"/>
<channel id="loudness" typeId="loudness"/>
<channel id="mute" typeId="system.mute"/>
<channel id="notificationsound" typeId="notificationsound"/>
<channel id="playlist" typeId="playlist"/>
Expand All @@ -46,6 +48,7 @@
<channel id="standalone" typeId="standalone"/>
<channel id="state" typeId="state"/>
<channel id="stop" typeId="stop"/>
<channel id="treble" typeId="treble"/>
<channel id="tuneinstationid" typeId="tuneinstationid"/>
<channel id="volume" typeId="system.volume"/>
<channel id="zonegroupid" typeId="zonegroupid"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<channel id="alarm" typeId="alarm"/>
<channel id="alarmproperties" typeId="alarmproperties"/>
<channel id="alarmrunning" typeId="alarmrunning"/>
<channel id="bass" typeId="bass"/>
<channel id="control" typeId="system.media-control"/>
<channel id="currentalbum" typeId="currentalbum"/>
<channel id="currentalbumart" typeId="currentalbumart"/>
Expand All @@ -27,6 +28,7 @@
<channel id="favorite" typeId="favorite"/>
<channel id="led" typeId="led"/>
<channel id="localcoordinator" typeId="localcoordinator"/>
<channel id="loudness" typeId="loudness"/>
<channel id="mute" typeId="system.mute"/>
<channel id="notificationsound" typeId="notificationsound"/>
<channel id="playlist" typeId="playlist"/>
Expand All @@ -46,6 +48,7 @@
<channel id="standalone" typeId="standalone"/>
<channel id="state" typeId="state"/>
<channel id="stop" typeId="stop"/>
<channel id="treble" typeId="treble"/>
<channel id="tuneinstationid" typeId="tuneinstationid"/>
<channel id="volume" typeId="system.volume"/>
<channel id="zonegroupid" typeId="zonegroupid"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<channel id="alarm" typeId="alarm"/>
<channel id="alarmproperties" typeId="alarmproperties"/>
<channel id="alarmrunning" typeId="alarmrunning"/>
<channel id="bass" typeId="bass"/>
<channel id="control" typeId="system.media-control"/>
<channel id="currentalbum" typeId="currentalbum"/>
<channel id="currentalbumart" typeId="currentalbumart"/>
Expand All @@ -27,6 +28,7 @@
<channel id="favorite" typeId="favorite"/>
<channel id="led" typeId="led"/>
<channel id="localcoordinator" typeId="localcoordinator"/>
<channel id="loudness" typeId="loudness"/>
<channel id="mute" typeId="system.mute"/>
<channel id="notificationsound" typeId="notificationsound"/>
<channel id="playlist" typeId="playlist"/>
Expand All @@ -46,6 +48,7 @@
<channel id="standalone" typeId="standalone"/>
<channel id="state" typeId="state"/>
<channel id="stop" typeId="stop"/>
<channel id="treble" typeId="treble"/>
<channel id="tuneinstationid" typeId="tuneinstationid"/>
<channel id="volume" typeId="system.volume"/>
<channel id="zonegroupid" typeId="zonegroupid"/>
Expand Down
Loading

0 comments on commit 0c018af

Please sign in to comment.