2 * Copyright (c) 2010-2022 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.mqtt.generic;
15 import java.util.Locale;
17 import java.util.concurrent.ConcurrentHashMap;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.mqtt.generic.internal.MqttThingHandlerFactory;
22 import org.openhab.binding.mqtt.generic.internal.handler.GenericMQTTThingHandler;
23 import org.openhab.core.thing.Channel;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.thing.type.DynamicStateDescriptionProvider;
26 import org.openhab.core.types.StateDescription;
27 import org.osgi.service.component.annotations.Component;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * If the user configures a generic channel and defines for example minimum/maximum/readonly,
33 * we need to dynamically override the xml default state.
34 * This service is started on-demand only, as soon as {@link MqttThingHandlerFactory} requires it.
36 * It is filled with new state descriptions within the {@link GenericMQTTThingHandler}.
38 * @author David Graeff - Initial contribution
40 @Component(service = { DynamicStateDescriptionProvider.class, MqttChannelStateDescriptionProvider.class })
42 public class MqttChannelStateDescriptionProvider implements DynamicStateDescriptionProvider {
44 private final Map<ChannelUID, StateDescription> descriptions = new ConcurrentHashMap<>();
45 private final Logger logger = LoggerFactory.getLogger(MqttChannelStateDescriptionProvider.class);
48 * Set a state description for a channel. This description will be used when preparing the channel state by
49 * the framework for presentation. A previous description, if existed, will be replaced.
51 * @param channelUID channel UID
52 * @param description state description for the channel
54 public void setDescription(ChannelUID channelUID, StateDescription description) {
55 logger.debug("Adding state description for channel {}", channelUID);
56 descriptions.put(channelUID, description);
60 * Clear all registered state descriptions
62 public void removeAllDescriptions() {
63 logger.debug("Removing all state descriptions");
68 public @Nullable StateDescription getStateDescription(Channel channel,
69 @Nullable StateDescription originalStateDescription, @Nullable Locale locale) {
70 StateDescription description = descriptions.get(channel.getUID());
71 logger.trace("Providing state description for channel {}", channel.getUID());
76 * Removes the given channel state description.
78 * @param channel The channel
80 public void remove(ChannelUID channel) {
81 descriptions.remove(channel);