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