]> git.basschouten.com Git - openhab-addons.git/blob
0910641b6f1dc0ddb66e887d013e21cc8091c509
[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
14 package org.openhab.binding.shelly.internal.provider;
15
16 import static org.openhab.binding.shelly.internal.ShellyBindingConstants.BINDING_ID;
17
18 import java.util.List;
19 import java.util.Locale;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.shelly.internal.handler.ShellyThingInterface;
24 import org.openhab.core.events.EventPublisher;
25 import org.openhab.core.thing.Channel;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingRegistry;
28 import org.openhab.core.thing.binding.BaseDynamicStateDescriptionProvider;
29 import org.openhab.core.thing.i18n.ChannelTypeI18nLocalizationService;
30 import org.openhab.core.thing.link.ItemChannelLinkRegistry;
31 import org.openhab.core.thing.type.ChannelTypeUID;
32 import org.openhab.core.thing.type.DynamicStateDescriptionProvider;
33 import org.openhab.core.types.StateDescription;
34 import org.openhab.core.types.StateDescriptionFragmentBuilder;
35 import org.openhab.core.types.StateOption;
36 import org.osgi.service.component.annotations.Activate;
37 import org.osgi.service.component.annotations.Component;
38 import org.osgi.service.component.annotations.Reference;
39
40 /**
41  * This class provides the list of valid inputs for the input channel of a source.
42  *
43  * @author Markus Michels - Initial contribution
44  *
45  */
46 @NonNullByDefault
47 @Component(service = { DynamicStateDescriptionProvider.class, ShellyStateDescriptionProvider.class })
48 public class ShellyStateDescriptionProvider extends BaseDynamicStateDescriptionProvider {
49     private final ThingRegistry thingRegistry;
50
51     @Activate
52     public ShellyStateDescriptionProvider(final @Reference EventPublisher eventPublisher, //
53             final @Reference ItemChannelLinkRegistry itemChannelLinkRegistry, //
54             final @Reference ChannelTypeI18nLocalizationService channelTypeI18nLocalizationService,
55             @Reference ThingRegistry thingRegistry) {
56         this.eventPublisher = eventPublisher;
57         this.itemChannelLinkRegistry = itemChannelLinkRegistry;
58         this.channelTypeI18nLocalizationService = channelTypeI18nLocalizationService;
59         this.thingRegistry = thingRegistry;
60     }
61
62     @SuppressWarnings("null")
63     @Override
64     public @Nullable StateDescription getStateDescription(Channel channel,
65             @Nullable StateDescription originalStateDescription, @Nullable Locale locale) {
66         ChannelTypeUID uid = channel.getChannelTypeUID();
67         if (uid == null || !BINDING_ID.equals(uid.getBindingId()) || originalStateDescription == null) {
68             return null;
69         }
70
71         Thing thing = thingRegistry.get(channel.getUID().getThingUID());
72         ShellyThingInterface handler = (ShellyThingInterface) thing.getHandler();
73         if (handler == null) {
74             return null;
75         }
76
77         List<StateOption> stateOptions = handler.getStateOptions(uid);
78         return stateOptions == null ? null
79                 : StateDescriptionFragmentBuilder.create(originalStateDescription).withOptions(stateOptions).build()
80                         .toStateDescription();
81     }
82 }