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.emotiva.internal;
15 import static org.openhab.binding.emotiva.internal.EmotivaBindingConstants.BINDING_ID;
16 import static org.openhab.binding.emotiva.internal.EmotivaBindingConstants.CHANNEL_MODE;
17 import static org.openhab.binding.emotiva.internal.EmotivaBindingConstants.CHANNEL_SOURCE;
18 import static org.openhab.binding.emotiva.internal.EmotivaBindingConstants.CHANNEL_ZONE2_SOURCE;
19 import static org.openhab.binding.emotiva.internal.EmotivaBindingConstants.EMOTIVA_SOURCE_COMMAND_PREFIX;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.EnumMap;
24 import java.util.List;
25 import java.util.Locale;
27 import org.eclipse.jdt.annotation.NonNullByDefault;
28 import org.eclipse.jdt.annotation.Nullable;
29 import org.openhab.binding.emotiva.internal.protocol.EmotivaControlCommands;
30 import org.openhab.binding.emotiva.internal.protocol.EmotivaSubscriptionTags;
31 import org.openhab.core.thing.Channel;
32 import org.openhab.core.thing.binding.BaseDynamicStateDescriptionProvider;
33 import org.openhab.core.thing.binding.ThingHandler;
34 import org.openhab.core.thing.binding.ThingHandlerService;
35 import org.openhab.core.thing.type.ChannelTypeUID;
36 import org.openhab.core.types.StateDescription;
37 import org.openhab.core.types.StateOption;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * This class provides the list of valid inputs for a source or audio mode.
44 * @author Kai Kreuzer - Initial contribution
45 * @author Espen Fossen - Adapted to Emotiva binding
48 public class InputStateOptionProvider extends BaseDynamicStateDescriptionProvider implements ThingHandlerService {
50 private final Logger logger = LoggerFactory.getLogger(InputStateOptionProvider.class);
52 private @Nullable EmotivaProcessorHandler handler;
55 public void setThingHandler(ThingHandler handler) {
56 this.handler = (EmotivaProcessorHandler) handler;
60 public @Nullable ThingHandler getThingHandler() {
65 public @Nullable StateDescription getStateDescription(Channel channel, @Nullable StateDescription original,
66 @Nullable Locale locale) {
67 ChannelTypeUID typeUID = channel.getChannelTypeUID();
68 if (typeUID == null || !BINDING_ID.equals(typeUID.getBindingId()) || original == null) {
72 List<StateOption> options = new ArrayList<>();
73 EmotivaProcessorHandler localHandler = handler;
74 if (localHandler != null) {
75 if (channel.getUID().getId().equals(CHANNEL_SOURCE)) {
76 setStateOptionsForSource(channel, options, localHandler.getSourcesMainZone());
77 } else if (channel.getUID().getId().equals(CHANNEL_ZONE2_SOURCE)) {
78 setStateOptionsForSource(channel, options, localHandler.getSourcesZone2());
79 } else if (channel.getUID().getId().equals(CHANNEL_MODE)) {
80 EnumMap<EmotivaSubscriptionTags, String> modes = localHandler.getModes();
81 Collection<EmotivaSubscriptionTags> modeKeys = modes.keySet();
82 for (EmotivaSubscriptionTags modeKey : modeKeys) {
83 options.add(new StateOption(modeKey.name(), modes.get(modeKey)));
85 logger.debug("Updated '{}' with '{}'", CHANNEL_MODE, options);
86 setStateOptions(channel.getUID(), options);
90 return super.getStateDescription(channel, original, locale);
93 private void setStateOptionsForSource(Channel channel, List<StateOption> options,
94 EnumMap<EmotivaControlCommands, String> sources) {
95 Collection<EmotivaControlCommands> sourceKeys = sources.keySet();
96 for (EmotivaControlCommands sourceKey : sourceKeys) {
97 if (sourceKey.name().startsWith(EMOTIVA_SOURCE_COMMAND_PREFIX)) {
98 options.add(new StateOption(sourceKey.name(), sources.get(sourceKey)));
100 options.add(new StateOption(sourceKey.name(), sourceKey.getLabel()));
103 logger.debug("Updated '{}' with '{}'", channel.getUID().getId(), options);
104 setStateOptions(channel.getUID(), options);