]> git.basschouten.com Git - openhab-addons.git/blob
f45b12c0046137083e5750935fd547b1520f9a69
[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.haywardomnilogic.internal;
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 org.openhab.binding.hue.internal.handler.HueBridgeHandler}
38  * while leaving other state description fields as original.
39  *
40  * @author Matt Myers - Initial contribution
41  */
42 @Component(service = { DynamicStateDescriptionProvider.class, HaywardDynamicStateDescriptionProvider.class })
43 @NonNullByDefault
44 public class HaywardDynamicStateDescriptionProvider extends BaseDynamicStateDescriptionProvider {
45
46     private final Map<ChannelUID, StateDescriptionFragment> stateDescriptionFragments = new ConcurrentHashMap<>();
47
48     @Activate
49     public HaywardDynamicStateDescriptionProvider(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             postEvent(ThingEventFactory.createChannelDescriptionChangedEvent(channelUID,
62                     itemChannelLinkRegistry != null ? itemChannelLinkRegistry.getLinkedItemNames(channelUID) : Set.of(),
63                     stateDescriptionFragment, oldStateDescriptionFragment));
64         }
65     }
66
67     @Override
68     public @Nullable StateDescription getStateDescription(Channel channel,
69             @Nullable StateDescription originalStateDescription, @Nullable Locale locale) {
70         StateDescriptionFragment stateDescriptionFragment = stateDescriptionFragments.get(channel.getUID());
71         return stateDescriptionFragment != null ? stateDescriptionFragment.toStateDescription()
72                 : super.getStateDescription(channel, originalStateDescription, locale);
73     }
74 }