]> git.basschouten.com Git - openhab-addons.git/blob
b4210b6ff5e1d4d9240f50b1a2fde8792ed56f2a
[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.onkyo.internal;
14
15 import java.util.List;
16 import java.util.Locale;
17 import java.util.Map;
18 import java.util.concurrent.ConcurrentHashMap;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.core.thing.Channel;
23 import org.openhab.core.thing.ChannelUID;
24 import org.openhab.core.thing.type.DynamicStateDescriptionProvider;
25 import org.openhab.core.types.StateDescription;
26 import org.openhab.core.types.StateDescriptionFragmentBuilder;
27 import org.openhab.core.types.StateOption;
28 import org.osgi.service.component.annotations.Component;
29 import org.osgi.service.component.annotations.Deactivate;
30
31 /**
32  * The {@link OnkyoStateDescriptionProvider} class is a dynamic provider of state options while leaving other state
33  * description fields as original.
34  *
35  * @author Gregory Moyer - Initial contribution
36  * @author Stewart Cossey - Adapted for Onkyo Binding
37  */
38 @Component(service = { DynamicStateDescriptionProvider.class, OnkyoStateDescriptionProvider.class })
39 @NonNullByDefault
40 public class OnkyoStateDescriptionProvider implements DynamicStateDescriptionProvider {
41     private final Map<ChannelUID, List<StateOption>> channelOptionsMap = new ConcurrentHashMap<>();
42
43     public void setStateOptions(ChannelUID channelUID, List<StateOption> options) {
44         channelOptionsMap.put(channelUID, options);
45     }
46
47     @Override
48     public @Nullable StateDescription getStateDescription(Channel channel, @Nullable StateDescription original,
49             @Nullable Locale locale) {
50         List<StateOption> options = channelOptionsMap.get(channel.getUID());
51         if (options == null) {
52             return null;
53         }
54
55         StateDescriptionFragmentBuilder builder = (original == null) ? StateDescriptionFragmentBuilder.create()
56                 : StateDescriptionFragmentBuilder.create(original);
57         return builder.withOptions(options).build().toStateDescription();
58     }
59
60     @Deactivate
61     public void deactivate() {
62         channelOptionsMap.clear();
63     }
64 }