Skip to content

Commit

Permalink
add support to options so we can handle command that use enum
Browse files Browse the repository at this point in the history
Signed-off-by: Laurent ARNAL <[email protected]>
  • Loading branch information
lo92fr committed Dec 18, 2024
1 parent d66df9d commit 57ff83c
Showing 1 changed file with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.openhab.core.thing.type.ThingType;
import org.openhab.core.thing.type.ThingTypeBuilder;
import org.openhab.core.types.StateDescriptionFragmentBuilder;
import org.openhab.core.types.StateOption;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
Expand Down Expand Up @@ -80,7 +81,7 @@ public SmartthingsTypeRegistryImpl() {
@Override
public void registerCapabilities(SmartthingsCapabilitie capa) {
capabilitiesDict.put(capa.id, capa);
createChannelDefinition(capa);
createChannelTypes(capa);
}

@Override
Expand All @@ -98,7 +99,7 @@ public void setCloudBridgeHandler(SmartthingsCloudBridgeHandler bridgeHandler) {
this.bridgeHandler = bridgeHandler;
}

public void createChannelDefinition(SmartthingsCapabilitie capa) {
public void createChannelTypes(SmartthingsCapabilitie capa) {
SmartthingsChannelTypeProvider lcChannelTypeProvider = channelTypeProvider;

for (String key : capa.attributes.keySet()) {
Expand Down Expand Up @@ -170,6 +171,19 @@ public void createChannelDefinition(SmartthingsCapabilitie capa) {

Boolean display = false;

List<StateOption> options = new ArrayList<StateOption>();

if (prop.enumeration != null && channelDef == null) {
for (String opt : prop.enumeration) {
String optValue = opt;
String optName = StringUtils.capitalize(
StringUtils.join(StringUtils.splitByCharacterTypeCamelCase(opt), StringUtils.SPACE));

StateOption option = new StateOption(optValue, optName);
options.add(option);
}
}

if (display) {
logger.info("<channel-type id=\"{}\">", channelName);
logger.info(" <item-type>{}</item-type>", channelTp);
Expand Down Expand Up @@ -201,7 +215,8 @@ public void createChannelDefinition(SmartthingsCapabilitie capa) {
if (lcChannelTypeProvider != null) {
channelType = lcChannelTypeProvider.getInternalChannelType(channelTypeUID);
if (channelType == null) {
channelType = createChannelType(capa, channelName, category, label, channelTp, channelTypeUID);
channelType = createChannelType(capa, channelName, category, label, channelTp, channelTypeUID,
options);
lcChannelTypeProvider.addChannelType(channelType);
}
}
Expand All @@ -211,11 +226,15 @@ public void createChannelDefinition(SmartthingsCapabilitie capa) {
}

private ChannelType createChannelType(SmartthingsCapabilitie capa, String channelName, String category,
String description, String channelTp, ChannelTypeUID channelTypeUID) {
String description, String channelTp, ChannelTypeUID channelTypeUID, List<StateOption> options) {
ChannelType channelType;

StateDescriptionFragmentBuilder stateFragment = StateDescriptionFragmentBuilder.create();

if (!options.isEmpty()) {
stateFragment = stateFragment.withOptions(options);
}

final StateChannelTypeBuilder channelTypeBuilder = ChannelTypeBuilder
.state(channelTypeUID, channelName, channelTp).withStateDescriptionFragment(stateFragment.build());

Expand Down

0 comments on commit 57ff83c

Please sign in to comment.