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