2 * Copyright (c) 2010-2024 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.shelly.internal.provider;
15 import static org.openhab.binding.shelly.internal.ShellyBindingConstants.BINDING_ID;
17 import java.util.List;
18 import java.util.Locale;
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;
40 * This class provides the list of valid inputs for the input channel of a source.
42 * @author Markus Michels - Initial contribution
46 @Component(service = { DynamicStateDescriptionProvider.class, ShellyStateDescriptionProvider.class })
47 public class ShellyStateDescriptionProvider extends BaseDynamicStateDescriptionProvider {
48 private final ThingRegistry thingRegistry;
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;
61 @SuppressWarnings("null")
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) {
70 Thing thing = thingRegistry.get(channel.getUID().getThingUID());
71 ShellyThingInterface handler = (ShellyThingInterface) thing.getHandler();
72 if (handler == null) {
76 List<StateOption> stateOptions = handler.getStateOptions(uid);
77 return stateOptions == null ? null
78 : StateDescriptionFragmentBuilder.create(originalStateDescription).withOptions(stateOptions).build()
79 .toStateDescription();