Skip to content

Commit

Permalink
Sort audio sinks (openhab#1744)
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
GitOrigin-RevId: 5d75bce
  • Loading branch information
cweitkamp authored and splatch committed Jul 11, 2023
1 parent 9483e3b commit 87fa26c
Showing 1 changed file with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
*/
package org.openhab.core.automation.module.media.internal;

import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.toList;

import java.io.File;
import java.nio.file.Paths;
import java.util.ArrayList;
Expand All @@ -25,7 +28,6 @@
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.OpenHAB;
import org.openhab.core.audio.AudioManager;
import org.openhab.core.audio.AudioSink;
import org.openhab.core.automation.Visibility;
import org.openhab.core.automation.type.ActionType;
import org.openhab.core.automation.type.ModuleType;
Expand Down Expand Up @@ -89,14 +91,14 @@ private List<ConfigDescriptionParameter> getConfigPlayDesc(@Nullable Locale loca
ConfigDescriptionParameter param1 = ConfigDescriptionParameterBuilder
.create(PlayActionHandler.PARAM_SOUND, Type.TEXT).withRequired(true).withLabel("Sound")
.withDescription("the sound to play").withOptions(getSoundOptions()).withLimitToOptions(true).build();
return Stream.of(param1, getAudioSinkConfigDescParam(locale)).collect(Collectors.toList());
return List.of(param1, getAudioSinkConfigDescParam(locale));
}

private List<ConfigDescriptionParameter> getConfigSayDesc(@Nullable Locale locale) {
ConfigDescriptionParameter param1 = ConfigDescriptionParameterBuilder
.create(SayActionHandler.PARAM_TEXT, Type.TEXT).withRequired(true).withLabel("Text")
.withDescription("the text to speak").build();
return Stream.of(param1, getAudioSinkConfigDescParam(locale)).collect(Collectors.toList());
return List.of(param1, getAudioSinkConfigDescParam(locale));
}

private ConfigDescriptionParameter getAudioSinkConfigDescParam(@Nullable Locale locale) {
Expand Down Expand Up @@ -125,6 +127,7 @@ private List<ParameterOption> getSoundOptions() {
options.add(new ParameterOption(fileName, capitalizedSoundName));
}
}
options.sort(comparing(o -> o.getLabel()));
}
return options;
}
Expand All @@ -135,12 +138,9 @@ private List<ParameterOption> getSoundOptions() {
* @return a list of parameter options representing the audio sinks
*/
private List<ParameterOption> getSinkOptions(@Nullable Locale locale) {
List<ParameterOption> options = new ArrayList<>();

for (AudioSink sink : audioManager.getAllSinks()) {
options.add(new ParameterOption(sink.getId(), sink.getLabel(locale)));
}
return options;
final Locale safeLocale = locale != null ? locale : Locale.getDefault();
return audioManager.getAllSinks().stream().sorted(comparing(s -> s.getLabel(safeLocale)))
.map(s -> new ParameterOption(s.getId(), s.getLabel(safeLocale))).collect(toList());
}

@Override
Expand Down

0 comments on commit 87fa26c

Please sign in to comment.