]> git.basschouten.com Git - openhab-addons.git/blob
b17396604016a6359ead018fd43d41eb619e9a95
[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.amplipi.internal;
14
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.Locale;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.amplipi.internal.model.Stream;
22 import org.openhab.core.thing.Channel;
23 import org.openhab.core.thing.binding.BaseDynamicStateDescriptionProvider;
24 import org.openhab.core.thing.binding.ThingHandler;
25 import org.openhab.core.thing.binding.ThingHandlerService;
26 import org.openhab.core.thing.type.ChannelTypeUID;
27 import org.openhab.core.types.StateDescription;
28 import org.openhab.core.types.StateOption;
29
30 /**
31  * This class provides the list of valid inputs for the input channel of a source.
32  *
33  * @author Kai Kreuzer - Initial contribution
34  *
35  */
36 @NonNullByDefault
37 public class InputStateOptionProvider extends BaseDynamicStateDescriptionProvider implements ThingHandlerService {
38
39     private @Nullable AmpliPiHandler handler;
40
41     @Override
42     public void setThingHandler(ThingHandler handler) {
43         this.handler = (AmpliPiHandler) handler;
44     }
45
46     @Override
47     public @Nullable ThingHandler getThingHandler() {
48         return handler;
49     }
50
51     @Override
52     public @Nullable StateDescription getStateDescription(Channel channel, @Nullable StateDescription original,
53             @Nullable Locale locale) {
54         ChannelTypeUID typeUID = channel.getChannelTypeUID();
55         List<StateOption> options = new ArrayList<>();
56         options.add(new StateOption("local", "RCA"));
57         if (typeUID != null && AmpliPiBindingConstants.CHANNEL_INPUT.equals(typeUID.getId()) && handler != null) {
58             List<Stream> streams = handler.getStreams();
59             for (Stream stream : streams) {
60                 options.add(new StateOption("stream=" + stream.getId(), getLabel(stream)));
61             }
62             setStateOptions(channel.getUID(), options);
63         }
64         return super.getStateDescription(channel, original, locale);
65     }
66
67     private @Nullable String getLabel(Stream stream) {
68         if ("internetradio".equals(stream.getType())) {
69             return stream.getName();
70         } else {
71             return stream.getType().substring(0, 1).toUpperCase() + stream.getType().substring(1);
72         }
73     }
74 }