2 * Copyright (c) 2010-2023 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.amplipi.internal;
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.Locale;
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;
31 * This class provides the list of valid inputs for the input channel of a source.
33 * @author Kai Kreuzer - Initial contribution
37 public class InputStateOptionProvider extends BaseDynamicStateDescriptionProvider implements ThingHandlerService {
39 private @Nullable AmpliPiHandler handler;
42 public void setThingHandler(ThingHandler handler) {
43 this.handler = (AmpliPiHandler) handler;
47 public @Nullable ThingHandler getThingHandler() {
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)));
62 setStateOptions(channel.getUID(), options);
64 return super.getStateDescription(channel, original, locale);
67 private @Nullable String getLabel(Stream stream) {
68 if (stream.getType().equals("internetradio")) {
69 return stream.getName();
71 return stream.getType().substring(0, 1).toUpperCase() + stream.getType().substring(1);