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.events.EventPublisher;
23 import org.openhab.core.thing.Channel;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.thing.ThingUID;
26 import org.openhab.core.thing.binding.BaseDynamicStateDescriptionProvider;
27 import org.openhab.core.thing.events.ThingEventFactory;
28 import org.openhab.core.thing.i18n.ChannelTypeI18nLocalizationService;
29 import org.openhab.core.thing.link.ItemChannelLinkRegistry;
30 import org.openhab.core.thing.type.DynamicStateDescriptionProvider;
31 import org.openhab.core.types.StateDescription;
32 import org.openhab.core.types.StateDescriptionFragment;
33 import org.osgi.service.component.annotations.Activate;
34 import org.osgi.service.component.annotations.Component;
35 import org.osgi.service.component.annotations.Reference;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * Dynamic channel state description provider.
41 * Overrides the state description for the controls, which receive its configuration in the runtime.
43 * @author Jan N. Klug - Initial contribution
46 @Component(service = { DynamicStateDescriptionProvider.class, DeconzDynamicStateDescriptionProvider.class })
47 public class DeconzDynamicStateDescriptionProvider extends BaseDynamicStateDescriptionProvider {
48 private final Logger logger = LoggerFactory.getLogger(DeconzDynamicStateDescriptionProvider.class);
50 private final Map<ChannelUID, StateDescriptionFragment> stateDescriptionFragments = new ConcurrentHashMap<>();
53 public DeconzDynamicStateDescriptionProvider(final @Reference EventPublisher eventPublisher, //
54 final @Reference ItemChannelLinkRegistry itemChannelLinkRegistry, //
55 final @Reference ChannelTypeI18nLocalizationService channelTypeI18nLocalizationService) {
56 this.eventPublisher = eventPublisher;
57 this.itemChannelLinkRegistry = itemChannelLinkRegistry;
58 this.channelTypeI18nLocalizationService = channelTypeI18nLocalizationService;
62 * Set a state description for a channel. This description will be used when preparing the channel state by
63 * the framework for presentation. A previous description, if existed, will be replaced.
67 * @param stateDescriptionFragment
68 * state description for the channel
70 public void setDescriptionFragment(ChannelUID channelUID, StateDescriptionFragment stateDescriptionFragment) {
71 StateDescriptionFragment oldStateDescriptionFragment = stateDescriptionFragments.get(channelUID);
72 if (!stateDescriptionFragment.equals(oldStateDescriptionFragment)) {
73 logger.trace("adding state description for channel {}", channelUID);
74 stateDescriptionFragments.put(channelUID, stateDescriptionFragment);
75 ItemChannelLinkRegistry localItemChannelLinkRegistry = itemChannelLinkRegistry;
76 postEvent(ThingEventFactory.createChannelDescriptionChangedEvent(channelUID,
77 localItemChannelLinkRegistry != null ? localItemChannelLinkRegistry.getLinkedItemNames(channelUID)
79 stateDescriptionFragment, oldStateDescriptionFragment));
84 * remove all descriptions for a given thing
86 * @param thingUID the thing's UID
88 public void removeDescriptionsForThing(ThingUID thingUID) {
89 logger.trace("removing state description for thing {}", thingUID);
90 stateDescriptionFragments.entrySet().removeIf(entry -> entry.getKey().getThingUID().equals(thingUID));
94 public @Nullable StateDescription getStateDescription(Channel channel,
95 @Nullable StateDescription originalStateDescription, @Nullable Locale locale) {
96 StateDescriptionFragment stateDescriptionFragment = stateDescriptionFragments.get(channel.getUID());
97 if (stateDescriptionFragment != null) {
98 logger.trace("returning new stateDescription for {}", channel.getUID());
99 return stateDescriptionFragment.toStateDescription();
101 return super.getStateDescription(channel, originalStateDescription, locale);