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.deconz.internal;
15 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.ThingUID;
25 import org.openhab.core.thing.binding.BaseDynamicStateDescriptionProvider;
26 import org.openhab.core.thing.events.ThingEventFactory;
27 import org.openhab.core.thing.link.ItemChannelLinkRegistry;
28 import org.openhab.core.thing.type.DynamicStateDescriptionProvider;
29 import org.openhab.core.types.StateDescription;
30 import org.openhab.core.types.StateDescriptionFragment;
31 import org.osgi.service.component.annotations.Component;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * Dynamic channel state description provider.
37 * Overrides the state description for the controls, which receive its configuration in the runtime.
39 * @author Jan N. Klug - Initial contribution
42 @Component(service = { DynamicStateDescriptionProvider.class, DeconzDynamicStateDescriptionProvider.class })
43 public class DeconzDynamicStateDescriptionProvider extends BaseDynamicStateDescriptionProvider {
44 private final Logger logger = LoggerFactory.getLogger(DeconzDynamicStateDescriptionProvider.class);
46 private final Map<ChannelUID, StateDescriptionFragment> stateDescriptionFragments = new ConcurrentHashMap<>();
49 * Set a state description for a channel. This description will be used when preparing the channel state by
50 * the framework for presentation. A previous description, if existed, will be replaced.
54 * @param stateDescriptionFragment
55 * state description for the channel
57 public void setDescriptionFragment(ChannelUID channelUID, StateDescriptionFragment stateDescriptionFragment) {
58 StateDescriptionFragment oldStateDescriptionFragment = stateDescriptionFragments.get(channelUID);
59 if (!stateDescriptionFragment.equals(oldStateDescriptionFragment)) {
60 logger.trace("adding state description for channel {}", channelUID);
61 stateDescriptionFragments.put(channelUID, stateDescriptionFragment);
62 ItemChannelLinkRegistry itemChannelLinkRegistry = this.itemChannelLinkRegistry;
63 postEvent(ThingEventFactory.createChannelDescriptionChangedEvent(channelUID,
64 itemChannelLinkRegistry != null ? itemChannelLinkRegistry.getLinkedItemNames(channelUID) : Set.of(),
65 stateDescriptionFragment, oldStateDescriptionFragment));
70 * remove all descriptions for a given thing
72 * @param thingUID the thing's UID
74 public void removeDescriptionsForThing(ThingUID thingUID) {
75 logger.trace("removing state description for thing {}", thingUID);
76 stateDescriptionFragments.entrySet().removeIf(entry -> entry.getKey().getThingUID().equals(thingUID));
80 public @Nullable StateDescription getStateDescription(Channel channel,
81 @Nullable StateDescription originalStateDescription, @Nullable Locale locale) {
82 StateDescriptionFragment stateDescriptionFragment = stateDescriptionFragments.get(channel.getUID());
83 if (stateDescriptionFragment != null) {
84 logger.trace("returning new stateDescription for {}", channel.getUID());
85 return stateDescriptionFragment.toStateDescription();
87 return super.getStateDescription(channel, originalStateDescription, locale);