]> git.basschouten.com Git - openhab-addons.git/blob
2d1e79c3744b7065b0e7d988a13559973872ece6
[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.loxone.internal;
14
15 import java.util.Locale;
16 import java.util.Map;
17 import java.util.concurrent.ConcurrentHashMap;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.core.thing.Channel;
22 import org.openhab.core.thing.ChannelUID;
23 import org.openhab.core.thing.type.DynamicStateDescriptionProvider;
24 import org.openhab.core.types.StateDescription;
25 import org.osgi.service.component.annotations.Component;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * Dynamic channel state description provider.
31  * Overrides the state description for the controls, which receive its configuration in the runtime.
32  *
33  * @author Pawel Pieczul - Initial contribution
34  */
35 @Component(service = { DynamicStateDescriptionProvider.class, LxDynamicStateDescriptionProvider.class })
36 @NonNullByDefault
37 public class LxDynamicStateDescriptionProvider implements DynamicStateDescriptionProvider {
38
39     private Map<ChannelUID, StateDescription> descriptions = new ConcurrentHashMap<>();
40     private Logger logger = LoggerFactory.getLogger(LxDynamicStateDescriptionProvider.class);
41
42     /**
43      * Set a state description for a channel. This description will be used when preparing the channel state by
44      * the framework for presentation. A previous description, if existed, will be replaced.
45      *
46      * @param channelUID channel UID
47      * @param description state description for the channel
48      */
49     void setDescription(ChannelUID channelUID, StateDescription description) {
50         logger.debug("Adding state description for channel {}", channelUID);
51         descriptions.put(channelUID, description);
52     }
53
54     /**
55      * Clear all registered state descriptions
56      */
57     void removeAllDescriptions() {
58         logger.debug("Removing all state descriptions");
59         descriptions.clear();
60     }
61
62     /**
63      * Removes a state description for a given channel ID
64      *
65      * @param channelUID channel ID to remove description for
66      */
67     void removeDescription(ChannelUID channelUID) {
68         logger.debug("Removing state description for channel {}", channelUID);
69         descriptions.remove(channelUID);
70     }
71
72     @Override
73     public @Nullable StateDescription getStateDescription(Channel channel,
74             @Nullable StateDescription originalStateDescription, @Nullable Locale locale) {
75         StateDescription description = descriptions.get(channel.getUID());
76         logger.trace("Providing state description for channel {}", channel.getUID());
77         return description;
78     }
79 }