Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mqtt][homie] Implement non-retained and non-settable properties #8246

Merged
merged 1 commit into from
Sep 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.eclipse.smarthome.core.library.types.OnOffType;
import org.eclipse.smarthome.core.library.types.StringType;
import org.eclipse.smarthome.core.types.Command;
import org.eclipse.smarthome.core.types.CommandDescriptionBuilder;
import org.eclipse.smarthome.core.types.CommandOption;

/**
* Implements an on/off boolean value.
Expand Down Expand Up @@ -95,4 +97,12 @@ public String getMQTTpublishValue(@Nullable String pattern) {

return String.format(formatPattern, state == OnOffType.ON ? onCommand : offCommand);
}

@Override
public CommandDescriptionBuilder createCommandDescription() {
CommandDescriptionBuilder builder = super.createCommandDescription();
builder = builder.withCommandOption(new CommandOption(onCommand, onCommand));
builder = builder.withCommandOption(new CommandOption(offCommand, offCommand));
return builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.eclipse.smarthome.core.library.CoreItemFactory;
import org.eclipse.smarthome.core.library.types.StringType;
import org.eclipse.smarthome.core.types.Command;
import org.eclipse.smarthome.core.types.CommandDescriptionBuilder;
import org.eclipse.smarthome.core.types.CommandOption;
import org.eclipse.smarthome.core.types.StateDescriptionFragmentBuilder;
import org.eclipse.smarthome.core.types.StateOption;

Expand Down Expand Up @@ -84,4 +86,16 @@ public StateDescriptionFragmentBuilder createStateDescription(boolean readOnly)
}
return builder;
}

@Override
public CommandDescriptionBuilder createCommandDescription() {
CommandDescriptionBuilder builder = super.createCommandDescription();
final Set<String> commands = this.states;
if (states != null) {
for (String command : commands) {
builder = builder.withCommandOption(new CommandOption(command, command));
}
}
return builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.eclipse.smarthome.core.library.types.PercentType;
import org.eclipse.smarthome.core.library.types.RawType;
import org.eclipse.smarthome.core.types.Command;
import org.eclipse.smarthome.core.types.CommandDescriptionBuilder;
import org.eclipse.smarthome.core.types.State;
import org.eclipse.smarthome.core.types.StateDescriptionFragmentBuilder;
import org.eclipse.smarthome.core.types.UnDefType;
Expand Down Expand Up @@ -169,4 +170,13 @@ public void update(byte data[]) throws IllegalArgumentException {
public StateDescriptionFragmentBuilder createStateDescription(boolean readOnly) {
return StateDescriptionFragmentBuilder.create().withReadOnly(readOnly).withPattern("%s");
}

/**
* Return the command description builder for this value state.
*
* @return A command description builder
*/
public CommandDescriptionBuilder createCommandDescription() {
return CommandDescriptionBuilder.create();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.eclipse.smarthome.core.thing.ChannelUID;
import org.eclipse.smarthome.core.thing.DefaultSystemChannelTypeProvider;
import org.eclipse.smarthome.core.thing.binding.builder.ChannelBuilder;
import org.eclipse.smarthome.core.thing.type.AutoUpdatePolicy;
import org.eclipse.smarthome.core.thing.type.ChannelType;
import org.eclipse.smarthome.core.thing.type.ChannelTypeBuilder;
import org.eclipse.smarthome.core.thing.type.ChannelTypeUID;
Expand Down Expand Up @@ -137,13 +138,22 @@ public void attributesReceived() {
* @return Returns the ChannelType to be used to build the Channel.
*/
private ChannelType createChannelType(PropertyAttributes attributes, ChannelState channelState) {
// Retained property -> State channel
if (attributes.retained) {
return ChannelTypeBuilder.state(channelTypeUID, attributes.name, channelState.getItemType())
.withConfigDescriptionURI(URI.create(MqttBindingConstants.CONFIG_HOMIE_CHANNEL))
.withStateDescription(channelState.getCache().createStateDescription(!attributes.settable).build()
.toStateDescription())
.build();
} else {
// Non-retained and settable property -> State channel
if (attributes.settable) {
return ChannelTypeBuilder.state(channelTypeUID, attributes.name, channelState.getItemType())
.withConfigDescriptionURI(URI.create(MqttBindingConstants.CONFIG_HOMIE_CHANNEL))
.withCommandDescription(channelState.getCache().createCommandDescription().build())
.withAutoUpdatePolicy(AutoUpdatePolicy.VETO).build();
}
// Non-retained and non settable property -> Trigger channel
if (attributes.datatype.equals(DataTypeEnum.enum_)) {
if (attributes.format.contains("PRESSED") && attributes.format.contains("RELEASED")) {
return DefaultSystemChannelTypeProvider.SYSTEM_RAWBUTTON;
Expand Down