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.onkyo.internal;
15 import java.util.List;
16 import java.util.Locale;
18 import java.util.concurrent.ConcurrentHashMap;
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;
32 * The {@link OnkyoStateDescriptionProvider} class is a dynamic provider of state options while leaving other state
33 * description fields as original.
35 * @author Gregory Moyer - Initial contribution
36 * @author Stewart Cossey - Adapted for Onkyo Binding
38 @Component(service = { DynamicStateDescriptionProvider.class, OnkyoStateDescriptionProvider.class })
40 public class OnkyoStateDescriptionProvider implements DynamicStateDescriptionProvider {
41 private final Map<ChannelUID, List<StateOption>> channelOptionsMap = new ConcurrentHashMap<>();
43 public void setStateOptions(ChannelUID channelUID, List<StateOption> options) {
44 channelOptionsMap.put(channelUID, options);
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) {
55 StateDescriptionFragmentBuilder builder = (original == null) ? StateDescriptionFragmentBuilder.create()
56 : StateDescriptionFragmentBuilder.create(original);
57 return builder.withOptions(options).build().toStateDescription();
61 public void deactivate() {
62 channelOptionsMap.clear();