|
| 1 | +/** |
| 2 | + * Copyright (c) 2010-2024 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.nanoleaf.internal.handler; |
| 14 | + |
| 15 | +import java.math.BigDecimal; |
| 16 | +import java.util.Locale; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.Set; |
| 19 | +import java.util.concurrent.ConcurrentHashMap; |
| 20 | + |
| 21 | +import org.eclipse.jdt.annotation.NonNullByDefault; |
| 22 | +import org.eclipse.jdt.annotation.Nullable; |
| 23 | +import org.openhab.core.events.EventPublisher; |
| 24 | +import org.openhab.core.thing.Channel; |
| 25 | +import org.openhab.core.thing.ChannelUID; |
| 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.openhab.core.types.StateDescriptionFragmentBuilder; |
| 34 | +import org.osgi.service.component.annotations.Activate; |
| 35 | +import org.osgi.service.component.annotations.Component; |
| 36 | +import org.osgi.service.component.annotations.Reference; |
| 37 | + |
| 38 | +/** |
| 39 | + * The {@link NanoLeafStateDescriptionProvider} provides dynamic state description minimum and maximum vales of color |
| 40 | + * temperature channels whose capabilities are dynamically determined at runtime. |
| 41 | + * |
| 42 | + * @author Andrew Fiddian-Green - Initial contribution |
| 43 | + * |
| 44 | + */ |
| 45 | +@NonNullByDefault |
| 46 | +@Component(service = { DynamicStateDescriptionProvider.class, NanoLeafStateDescriptionProvider.class }) |
| 47 | +public class NanoLeafStateDescriptionProvider extends BaseDynamicStateDescriptionProvider { |
| 48 | + |
| 49 | + private final Map<ChannelUID, StateDescriptionFragment> stateDescriptionFragments = new ConcurrentHashMap<>(); |
| 50 | + |
| 51 | + @Activate |
| 52 | + public NanoLeafStateDescriptionProvider(final @Reference EventPublisher eventPublisher, |
| 53 | + final @Reference ItemChannelLinkRegistry itemChannelLinkRegistry, |
| 54 | + final @Reference ChannelTypeI18nLocalizationService channelTypeI18nLocalizationService) { |
| 55 | + this.eventPublisher = eventPublisher; |
| 56 | + this.itemChannelLinkRegistry = itemChannelLinkRegistry; |
| 57 | + this.channelTypeI18nLocalizationService = channelTypeI18nLocalizationService; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public @Nullable StateDescription getStateDescription(Channel channel, @Nullable StateDescription original, |
| 62 | + @Nullable Locale locale) { |
| 63 | + StateDescriptionFragment stateDescriptionFragment = stateDescriptionFragments.get(channel.getUID()); |
| 64 | + return stateDescriptionFragment != null ? stateDescriptionFragment.toStateDescription() |
| 65 | + : super.getStateDescription(channel, original, locale); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Set the state description minimum and maximum values and pattern in Kelvin for the given channel UID |
| 70 | + */ |
| 71 | + public void setMinMaxKelvin(ChannelUID channelUID, long minKelvin, long maxKelvin) { |
| 72 | + StateDescriptionFragment oldStateDescriptionFragment = stateDescriptionFragments.get(channelUID); |
| 73 | + StateDescriptionFragment newStateDescriptionFragment = StateDescriptionFragmentBuilder.create() |
| 74 | + .withMinimum(BigDecimal.valueOf(minKelvin)).withMaximum(BigDecimal.valueOf(maxKelvin)) |
| 75 | + .withStep(BigDecimal.valueOf(100)).withPattern("%.0f K").build(); |
| 76 | + if (!newStateDescriptionFragment.equals(oldStateDescriptionFragment)) { |
| 77 | + stateDescriptionFragments.put(channelUID, newStateDescriptionFragment); |
| 78 | + ItemChannelLinkRegistry itemChannelLinkRegistry = this.itemChannelLinkRegistry; |
| 79 | + postEvent(ThingEventFactory.createChannelDescriptionChangedEvent(channelUID, |
| 80 | + itemChannelLinkRegistry != null ? itemChannelLinkRegistry.getLinkedItemNames(channelUID) : Set.of(), |
| 81 | + newStateDescriptionFragment, oldStateDescriptionFragment)); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments