Skip to content

Commit

Permalink
[mqtt][homie] Implement non-retained and non-settable properties (ope…
Browse files Browse the repository at this point in the history
…nhab#8246)

Signed-off-by: Aitor Iturrioz <[email protected]>
  • Loading branch information
bodiroga authored and markus7017 committed Sep 18, 2020
1 parent 88a6466 commit 50e7e42
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
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

0 comments on commit 50e7e42

Please sign in to comment.