]> git.basschouten.com Git - openhab-addons.git/blob
423c594efe0a285f1d77874b02b6654cdcdd3c4d
[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.onewire.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.ThingUID;
24 import org.openhab.core.thing.type.DynamicStateDescriptionProvider;
25 import org.openhab.core.types.StateDescription;
26 import org.osgi.service.component.annotations.Component;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Dynamic channel state description provider.
32  * Overrides the state description for the controls, which receive its configuration in the runtime.
33  *
34  * @author Jan N. Klug - Initial contribution
35  */
36 @NonNullByDefault
37 @Component(service = { DynamicStateDescriptionProvider.class, OwDynamicStateDescriptionProvider.class })
38 public class OwDynamicStateDescriptionProvider implements DynamicStateDescriptionProvider {
39
40     private final Map<ChannelUID, StateDescription> descriptions = new ConcurrentHashMap<>();
41     private final Logger logger = LoggerFactory.getLogger(OwDynamicStateDescriptionProvider.class);
42
43     /**
44      * Set a state description for a channel. This description will be used when preparing the channel state by
45      * the framework for presentation. A previous description, if existed, will be replaced.
46      *
47      * @param channelUID
48      *            channel UID
49      * @param description
50      *            state description for the channel
51      */
52     public void setDescription(ChannelUID channelUID, StateDescription description) {
53         logger.trace("adding state description for channel {}", channelUID);
54         descriptions.put(channelUID, description);
55     }
56
57     /**
58      * remove all descriptions for a given thing
59      *
60      * @param thingUID the thing's UID
61      */
62     public void removeDescriptionsForThing(ThingUID thingUID) {
63         logger.trace("removing state description for thing {}", thingUID);
64         descriptions.entrySet().removeIf(entry -> entry.getKey().getThingUID().equals(thingUID));
65     }
66
67     @Override
68     public @Nullable StateDescription getStateDescription(Channel channel,
69             @Nullable StateDescription originalStateDescription, @Nullable Locale locale) {
70         if (descriptions.containsKey(channel.getUID())) {
71             logger.trace("returning new stateDescription for {}", channel.getUID());
72             return descriptions.get(channel.getUID());
73         } else {
74             return null;
75         }
76     }
77 }