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.haywardomnilogic.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.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;
37 * Dynamic provider of state options for {@link org.openhab.binding.hue.internal.handler.HueBridgeHandler}
38 * while leaving other state description fields as original.
40 * @author Matt Myers - Initial contribution
42 @Component(service = { DynamicStateDescriptionProvider.class, HaywardDynamicStateDescriptionProvider.class })
44 public class HaywardDynamicStateDescriptionProvider extends BaseDynamicStateDescriptionProvider {
46 private final Map<ChannelUID, StateDescriptionFragment> stateDescriptionFragments = new ConcurrentHashMap<>();
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;
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));
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);