2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.heos.internal.handler;
15 import java.util.List;
16 import java.util.Optional;
17 import java.util.function.Function;
18 import java.util.stream.Collectors;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.heos.internal.json.payload.BrowseResult;
23 import org.openhab.binding.heos.internal.json.payload.Media;
24 import org.openhab.binding.heos.internal.json.payload.YesNoEnum;
25 import org.openhab.core.thing.ChannelUID;
26 import org.openhab.core.thing.binding.BaseDynamicStateDescriptionProvider;
27 import org.openhab.core.thing.type.DynamicStateDescriptionProvider;
28 import org.openhab.core.types.StateOption;
29 import org.osgi.service.component.annotations.Component;
32 * Dynamically create the users list of favorites and playlists.
34 * @author Martin van Wingerden - Initial contribution
36 @Component(service = { DynamicStateDescriptionProvider.class, HeosDynamicStateDescriptionProvider.class })
38 public class HeosDynamicStateDescriptionProvider extends BaseDynamicStateDescriptionProvider {
40 String getValueByLabel(ChannelUID channelUID, String input) {
41 Optional<String> optionalValueByLabel = channelOptionsMap.get(channelUID).stream()
42 .filter(o -> input.equals(o.getLabel())).map(StateOption::getValue).findFirst();
44 // if no match was found we assume that it already was a value and not a label
45 return optionalValueByLabel.orElse(input);
48 public void setFavorites(ChannelUID channelUID, List<BrowseResult> favorites) {
49 setBrowseResultList(channelUID, favorites, d -> d.mediaId);
52 public void setPlaylists(ChannelUID channelUID, List<BrowseResult> playLists) {
53 setBrowseResultList(channelUID, playLists, d -> d.containerId);
56 private void setBrowseResultList(ChannelUID channelUID, List<BrowseResult> playlists,
57 Function<BrowseResult, @Nullable String> function) {
58 setStateOptions(channelUID,
59 playlists.stream().filter(browseResult -> browseResult.playable == YesNoEnum.YES)
60 .map(browseResult -> getStateOption(function, browseResult)).filter(Optional::isPresent)
61 .map(Optional::get).collect(Collectors.toList()));
64 private Optional<StateOption> getStateOption(Function<BrowseResult, @Nullable String> function,
65 BrowseResult browseResult) {
67 String identifier = function.apply(browseResult);
68 if (identifier != null) {
69 return Optional.of(new StateOption(identifier, browseResult.name));
71 return Optional.empty();
75 public void setQueue(ChannelUID channelUID, List<Media> queue) {
76 setStateOptions(channelUID,
77 queue.stream().map(m -> new StateOption(String.valueOf(m.queueId), m.combinedSongArtist()))
78 .collect(Collectors.toList()));