]> git.basschouten.com Git - openhab-addons.git/blob
1f72be5b93d3ec44a7f9f0c85c90b20e4c8ef2e1
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.heos.internal.handler;
14
15 import java.util.List;
16 import java.util.Optional;
17 import java.util.function.Function;
18 import java.util.stream.Collectors;
19
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;
30
31 /**
32  * Dynamically create the users list of favorites and playlists.
33  *
34  * @author Martin van Wingerden - Initial contribution
35  */
36 @Component(service = { DynamicStateDescriptionProvider.class, HeosDynamicStateDescriptionProvider.class })
37 @NonNullByDefault
38 public class HeosDynamicStateDescriptionProvider extends BaseDynamicStateDescriptionProvider {
39
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();
43
44         // if no match was found we assume that it already was a value and not a label
45         return optionalValueByLabel.orElse(input);
46     }
47
48     public void setFavorites(ChannelUID channelUID, List<BrowseResult> favorites) {
49         setBrowseResultList(channelUID, favorites, d -> d.mediaId);
50     }
51
52     public void setPlaylists(ChannelUID channelUID, List<BrowseResult> playLists) {
53         setBrowseResultList(channelUID, playLists, d -> d.containerId);
54     }
55
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()));
62     }
63
64     private Optional<StateOption> getStateOption(Function<BrowseResult, @Nullable String> function,
65             BrowseResult browseResult) {
66         @Nullable
67         String identifier = function.apply(browseResult);
68         if (identifier != null) {
69             return Optional.of(new StateOption(identifier, browseResult.name));
70         } else {
71             return Optional.empty();
72         }
73     }
74
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()));
79     }
80 }