]> git.basschouten.com Git - openhab-addons.git/blob
c20b2c6f5814ef2f1e9fe28404fa343e2ac6f87e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.emotiva.internal;
14
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;
20
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;
26
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;
40
41 /**
42  * This class provides the list of valid inputs for a source or audio mode.
43  *
44  * @author Kai Kreuzer - Initial contribution
45  * @author Espen Fossen - Adapted to Emotiva binding
46  */
47 @NonNullByDefault
48 public class InputStateOptionProvider extends BaseDynamicStateDescriptionProvider implements ThingHandlerService {
49
50     private final Logger logger = LoggerFactory.getLogger(InputStateOptionProvider.class);
51
52     private @Nullable EmotivaProcessorHandler handler;
53
54     @Override
55     public void setThingHandler(ThingHandler handler) {
56         this.handler = (EmotivaProcessorHandler) handler;
57     }
58
59     @Override
60     public @Nullable ThingHandler getThingHandler() {
61         return handler;
62     }
63
64     @Override
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) {
69             return null;
70         }
71
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)));
84                 }
85                 logger.debug("Updated '{}' with '{}'", CHANNEL_MODE, options);
86                 setStateOptions(channel.getUID(), options);
87             }
88         }
89
90         return super.getStateDescription(channel, original, locale);
91     }
92
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)));
99             } else {
100                 options.add(new StateOption(sourceKey.name(), sourceKey.getLabel()));
101             }
102         }
103         logger.debug("Updated '{}' with '{}'", channel.getUID().getId(), options);
104         setStateOptions(channel.getUID(), options);
105     }
106 }