]> git.basschouten.com Git - openhab-addons.git/blob
3ade2674ca1dd9f769c5fb7ec53860fee7e16e17
[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.hue.internal.handler;
14
15 import java.util.Locale;
16 import java.util.Map;
17 import java.util.Set;
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.events.EventPublisher;
23 import org.openhab.core.thing.Channel;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.thing.binding.BaseDynamicStateDescriptionProvider;
26 import org.openhab.core.thing.events.ThingEventFactory;
27 import org.openhab.core.thing.i18n.ChannelTypeI18nLocalizationService;
28 import org.openhab.core.thing.link.ItemChannelLinkRegistry;
29 import org.openhab.core.thing.type.DynamicStateDescriptionProvider;
30 import org.openhab.core.types.StateDescription;
31 import org.openhab.core.types.StateDescriptionFragment;
32 import org.osgi.service.component.annotations.Activate;
33 import org.osgi.service.component.annotations.Component;
34 import org.osgi.service.component.annotations.Reference;
35
36 /**
37  * Dynamic provider of state options for {@link HueBridgeHandler} while leaving other state description fields as
38  * original.
39  *
40  * @author Hengrui Jiang - Initial contribution
41  */
42 @Component(service = { DynamicStateDescriptionProvider.class, HueStateDescriptionProvider.class })
43 @NonNullByDefault
44 public class HueStateDescriptionProvider extends BaseDynamicStateDescriptionProvider {
45
46     private final Map<ChannelUID, StateDescriptionFragment> stateDescriptionFragments = new ConcurrentHashMap<>();
47
48     @Activate
49     public HueStateDescriptionProvider(final @Reference EventPublisher eventPublisher, //
50             final @Reference ItemChannelLinkRegistry itemChannelLinkRegistry, //
51             final @Reference ChannelTypeI18nLocalizationService channelTypeI18nLocalizationService) {
52         this.eventPublisher = eventPublisher;
53         this.itemChannelLinkRegistry = itemChannelLinkRegistry;
54         this.channelTypeI18nLocalizationService = channelTypeI18nLocalizationService;
55     }
56
57     public void setStateDescriptionFragment(ChannelUID channelUID, StateDescriptionFragment stateDescriptionFragment) {
58         StateDescriptionFragment oldStateDescriptionFragment = stateDescriptionFragments.get(channelUID);
59         if (!stateDescriptionFragment.equals(oldStateDescriptionFragment)) {
60             stateDescriptionFragments.put(channelUID, stateDescriptionFragment);
61             ItemChannelLinkRegistry itemChannelLinkRegistry = this.itemChannelLinkRegistry;
62             postEvent(ThingEventFactory.createChannelDescriptionChangedEvent(channelUID,
63                     itemChannelLinkRegistry != null ? itemChannelLinkRegistry.getLinkedItemNames(channelUID) : Set.of(),
64                     stateDescriptionFragment, oldStateDescriptionFragment));
65         }
66     }
67
68     @Override
69     public @Nullable StateDescription getStateDescription(Channel channel,
70             @Nullable StateDescription originalStateDescription, @Nullable Locale locale) {
71         StateDescriptionFragment stateDescriptionFragment = stateDescriptionFragments.get(channel.getUID());
72         return stateDescriptionFragment != null ? stateDescriptionFragment.toStateDescription()
73                 : super.getStateDescription(channel, originalStateDescription, locale);
74     }
75 }