Skip to content

Commit

Permalink
[mqtt.homeassistant] Implement optimistic components with AutoUpdateP…
Browse files Browse the repository at this point in the history
…olicy.RECOMMEND (openhab#17520)

Signed-off-by: Cody Cutrer <[email protected]>
  • Loading branch information
ccutrer authored Oct 8, 2024
1 parent b36877e commit ab59bc8
Show file tree
Hide file tree
Showing 13 changed files with 290 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,18 @@ public Builder withFormat(String format) {
return this;
}

// If the component explicitly specifies optimistic, or it's missing a state topic
// put it in optimistic mode (which, in openHAB parlance, means to auto-update the
// item).
public Builder inferOptimistic(@Nullable Boolean optimistic) {
String localStateTopic = stateTopic;
if (optimistic == null && (localStateTopic == null || localStateTopic.isBlank())
|| optimistic != null && optimistic == true) {
this.autoUpdatePolicy = AutoUpdatePolicy.RECOMMEND;
}
return this;
}

public ComponentChannel build() {
return build(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ static class ChannelConfiguration extends AbstractChannelConfiguration {
super("MQTT HVAC");
}

protected @Nullable Boolean optimistic;

@SerializedName("action_template")
protected @Nullable String actionTemplate;
@SerializedName("action_topic")
Expand Down Expand Up @@ -297,7 +299,7 @@ private ComponentChannel buildOptionalChannel(String channelId, ComponentChannel
.stateTopic(stateTopic, stateTemplate, channelConfiguration.getValueTemplate())
.commandTopic(commandTopic, channelConfiguration.isRetain(), channelConfiguration.getQos(),
commandTemplate)
.commandFilter(commandFilter).build();
.inferOptimistic(channelConfiguration.optimistic).commandFilter(commandFilter).build();
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.openhab.core.library.types.StopMoveType;
import org.openhab.core.library.types.StringType;
import org.openhab.core.library.types.UpDownType;
import org.openhab.core.thing.type.AutoUpdatePolicy;

import com.google.gson.annotations.SerializedName;

Expand All @@ -48,6 +49,8 @@ static class ChannelConfiguration extends AbstractChannelConfiguration {
super("MQTT Cover");
}

protected @Nullable Boolean optimistic;

@SerializedName("state_topic")
protected @Nullable String stateTopic;
@SerializedName("command_topic")
Expand Down Expand Up @@ -88,6 +91,12 @@ static class ChannelConfiguration extends AbstractChannelConfiguration {
public Cover(ComponentFactory.ComponentConfiguration componentConfiguration, boolean newStyleChannels) {
super(componentConfiguration, ChannelConfiguration.class, newStyleChannels);

boolean optimistic = false;
Boolean localOptimistic = channelConfiguration.optimistic;
if (localOptimistic != null && localOptimistic == true
|| channelConfiguration.stateTopic == null && channelConfiguration.positionTopic == null) {
optimistic = true;
}
String stateTopic = channelConfiguration.stateTopic;

// State can indicate additional information than just
Expand Down Expand Up @@ -149,7 +158,7 @@ public Cover(ComponentFactory.ComponentConfiguration componentConfiguration, boo
return false;
}
return true;
}).build();
}).withAutoUpdatePolicy(optimistic ? AutoUpdatePolicy.RECOMMEND : null).build();
finalizeChannels();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.openhab.core.library.types.PercentType;
import org.openhab.core.library.types.StringType;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.type.AutoUpdatePolicy;
import org.openhab.core.types.Command;
import org.openhab.core.types.State;
import org.openhab.core.types.UnDefType;
Expand Down Expand Up @@ -60,13 +61,14 @@ public DefaultSchemaLight(ComponentFactory.ComponentConfiguration builder, boole

@Override
protected void buildChannels() {
AutoUpdatePolicy autoUpdatePolicy = optimistic ? AutoUpdatePolicy.RECOMMEND : null;
ComponentChannel localOnOffChannel;
localOnOffChannel = onOffChannel = buildChannel(ON_OFF_CHANNEL_ID, ComponentChannelType.SWITCH, onOffValue,
"On/Off State", this)
.stateTopic(channelConfiguration.stateTopic, channelConfiguration.stateValueTemplate)
.commandTopic(channelConfiguration.commandTopic, channelConfiguration.isRetain(),
channelConfiguration.getQos())
.commandFilter(this::handleRawOnOffCommand).build(false);
.withAutoUpdatePolicy(autoUpdatePolicy).commandFilter(this::handleRawOnOffCommand).build(false);

@Nullable
ComponentChannel localBrightnessChannel = null;
Expand All @@ -76,30 +78,31 @@ protected void buildChannels() {
.stateTopic(channelConfiguration.brightnessStateTopic, channelConfiguration.brightnessValueTemplate)
.commandTopic(channelConfiguration.brightnessCommandTopic, channelConfiguration.isRetain(),
channelConfiguration.getQos())
.withFormat("%.0f").commandFilter(this::handleBrightnessCommand).build(false);
.withAutoUpdatePolicy(autoUpdatePolicy).withFormat("%.0f")
.commandFilter(this::handleBrightnessCommand).build(false);
}

if (channelConfiguration.whiteCommandTopic != null) {
buildChannel(WHITE_CHANNEL_ID, ComponentChannelType.DIMMER, brightnessValue,
"Go directly to white of a specific brightness", this)
.commandTopic(channelConfiguration.whiteCommandTopic, channelConfiguration.isRetain(),
channelConfiguration.getQos())
.isAdvanced(true).build();
.withAutoUpdatePolicy(autoUpdatePolicy).isAdvanced(true).build();
}

if (channelConfiguration.colorModeStateTopic != null) {
buildChannel(COLOR_MODE_CHANNEL_ID, ComponentChannelType.STRING, new TextValue(), "Current color mode",
this)
.stateTopic(channelConfiguration.colorModeStateTopic, channelConfiguration.colorModeValueTemplate)
.build();
.inferOptimistic(channelConfiguration.optimistic).build();
}

if (channelConfiguration.colorTempStateTopic != null || channelConfiguration.colorTempCommandTopic != null) {
buildChannel(COLOR_TEMP_CHANNEL_ID, ComponentChannelType.NUMBER, colorTempValue, "Color Temperature", this)
.stateTopic(channelConfiguration.colorTempStateTopic, channelConfiguration.colorTempValueTemplate)
.commandTopic(channelConfiguration.colorTempCommandTopic, channelConfiguration.isRetain(),
channelConfiguration.getQos())
.build();
.inferOptimistic(channelConfiguration.optimistic).build();
}

if (effectValue != null
Expand All @@ -109,7 +112,7 @@ protected void buildChannels() {
.stateTopic(channelConfiguration.effectStateTopic, channelConfiguration.effectValueTemplate)
.commandTopic(channelConfiguration.effectCommandTopic, channelConfiguration.isRetain(),
channelConfiguration.getQos())
.build();
.inferOptimistic(channelConfiguration.optimistic).build();
}

boolean hasColorChannel = false;
Expand Down Expand Up @@ -170,7 +173,7 @@ protected void buildChannels() {
}
colorChannel = buildChannel(COLOR_CHANNEL_ID, ComponentChannelType.COLOR, colorValue, "Color", this)
.commandTopic(DUMMY_TOPIC, channelConfiguration.isRetain(), channelConfiguration.getQos())
.commandFilter(this::handleColorCommand).build();
.commandFilter(this::handleColorCommand).withAutoUpdatePolicy(autoUpdatePolicy).build();
} else if (localBrightnessChannel != null) {
hiddenChannels.add(localOnOffChannel);
channels.put(BRIGHTNESS_CHANNEL_ID, localBrightnessChannel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ static class ChannelConfiguration extends AbstractChannelConfiguration {
super("MQTT Fan");
}

protected @Nullable Boolean optimistic;

@SerializedName("state_topic")
protected @Nullable String stateTopic;
@SerializedName("command_template")
Expand Down Expand Up @@ -136,6 +138,7 @@ public Fan(ComponentFactory.ComponentConfiguration componentConfiguration, boole
.stateTopic(channelConfiguration.stateTopic, channelConfiguration.getValueTemplate())
.commandTopic(channelConfiguration.commandTopic, channelConfiguration.isRetain(),
channelConfiguration.getQos(), channelConfiguration.commandTemplate)
.inferOptimistic(channelConfiguration.optimistic)
.build(channelConfiguration.percentageCommandTopic == null);

rawSpeedState = UnDefType.NULL;
Expand All @@ -152,7 +155,8 @@ public Fan(ComponentFactory.ComponentConfiguration componentConfiguration, boole
.stateTopic(channelConfiguration.percentageStateTopic, channelConfiguration.percentageValueTemplate)
.commandTopic(channelConfiguration.percentageCommandTopic, channelConfiguration.isRetain(),
channelConfiguration.getQos(), channelConfiguration.percentageCommandTemplate)
.commandFilter(this::handlePercentageCommand).build();
.inferOptimistic(channelConfiguration.optimistic).commandFilter(this::handlePercentageCommand)
.build();
} else {
primaryChannel = onOffChannel;
speedChannel = null;
Expand All @@ -167,7 +171,7 @@ public Fan(ComponentFactory.ComponentConfiguration componentConfiguration, boole
.stateTopic(channelConfiguration.presetModeStateTopic, channelConfiguration.presetModeValueTemplate)
.commandTopic(channelConfiguration.presetModeCommandTopic, channelConfiguration.isRetain(),
channelConfiguration.getQos(), channelConfiguration.presetModeCommandTemplate)
.build();
.inferOptimistic(channelConfiguration.optimistic).build();
}

if (channelConfiguration.oscillationCommandTopic != null) {
Expand All @@ -179,7 +183,7 @@ public Fan(ComponentFactory.ComponentConfiguration componentConfiguration, boole
channelConfiguration.oscillationValueTemplate)
.commandTopic(channelConfiguration.oscillationCommandTopic, channelConfiguration.isRetain(),
channelConfiguration.getQos(), channelConfiguration.oscillationCommandTemplate)
.build();
.inferOptimistic(channelConfiguration.optimistic).build();
}

if (channelConfiguration.directionCommandTopic != null) {
Expand All @@ -189,7 +193,7 @@ public Fan(ComponentFactory.ComponentConfiguration componentConfiguration, boole
.stateTopic(channelConfiguration.directionStateTopic, channelConfiguration.directionValueTemplate)
.commandTopic(channelConfiguration.directionCommandTopic, channelConfiguration.isRetain(),
channelConfiguration.getQos(), channelConfiguration.directionCommandTemplate)
.build();
.inferOptimistic(channelConfiguration.optimistic).build();
}
finalizeChannels();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.openhab.core.library.types.StringType;
import org.openhab.core.library.unit.Units;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.type.AutoUpdatePolicy;
import org.openhab.core.types.Command;
import org.openhab.core.types.State;
import org.openhab.core.types.UnDefType;
Expand Down Expand Up @@ -79,6 +80,7 @@ public JSONSchemaLight(ComponentFactory.ComponentConfiguration builder, boolean
@Override
protected void buildChannels() {
boolean hasColorChannel = false;
AutoUpdatePolicy autoUpdatePolicy = optimistic ? AutoUpdatePolicy.RECOMMEND : null;
List<LightColorMode> supportedColorModes = channelConfiguration.supportedColorModes;
if (supportedColorModes != null) {
if (LightColorMode.hasColorChannel(supportedColorModes)) {
Expand All @@ -88,33 +90,38 @@ protected void buildChannels() {
if (supportedColorModes.contains(LightColorMode.COLOR_MODE_COLOR_TEMP)) {
buildChannel(COLOR_TEMP_CHANNEL_ID, ComponentChannelType.NUMBER, colorTempValue, "Color Temperature",
this).commandTopic(DUMMY_TOPIC, true, 1)
.commandFilter(command -> handleColorTempCommand(command)).build();
.commandFilter(command -> handleColorTempCommand(command))
.withAutoUpdatePolicy(autoUpdatePolicy).build();

if (hasColorChannel) {
colorModeValue = new TextValue(
supportedColorModes.stream().map(LightColorMode::serializedName).toArray(String[]::new));
buildChannel(COLOR_MODE_CHANNEL_ID, ComponentChannelType.STRING, colorModeValue, "Color Mode", this)
.isAdvanced(true).build();
.withAutoUpdatePolicy(autoUpdatePolicy).isAdvanced(true).build();

}
}
}

if (hasColorChannel) {
colorChannel = buildChannel(COLOR_CHANNEL_ID, ComponentChannelType.COLOR, colorValue, "Color", this)
.commandTopic(DUMMY_TOPIC, true, 1).commandFilter(this::handleCommand).build();
.commandTopic(DUMMY_TOPIC, true, 1).commandFilter(this::handleCommand)
.withAutoUpdatePolicy(autoUpdatePolicy).build();
} else if (channelConfiguration.brightness) {
brightnessChannel = buildChannel(BRIGHTNESS_CHANNEL_ID, ComponentChannelType.DIMMER, brightnessValue,
"Brightness", this).commandTopic(DUMMY_TOPIC, true, 1).commandFilter(this::handleCommand).build();
"Brightness", this).commandTopic(DUMMY_TOPIC, true, 1).commandFilter(this::handleCommand)
.withAutoUpdatePolicy(autoUpdatePolicy).build();
} else {
onOffChannel = buildChannel(ON_OFF_CHANNEL_ID, ComponentChannelType.SWITCH, onOffValue, "On/Off State",
this).commandTopic(DUMMY_TOPIC, true, 1).commandFilter(this::handleCommand).build();
this).commandTopic(DUMMY_TOPIC, true, 1).commandFilter(this::handleCommand)
.withAutoUpdatePolicy(autoUpdatePolicy).build();
}

if (effectValue != null) {
buildChannel(EFFECT_CHANNEL_ID, ComponentChannelType.STRING, Objects.requireNonNull(effectValue),
"Lighting Effect", this).commandTopic(DUMMY_TOPIC, true, 1)
.commandFilter(command -> handleEffectCommand(command)).build();
.commandFilter(command -> handleEffectCommand(command)).withAutoUpdatePolicy(autoUpdatePolicy)
.build();

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.openhab.binding.mqtt.generic.values.NumberValue;
import org.openhab.binding.mqtt.homeassistant.internal.ComponentChannelType;
import org.openhab.binding.mqtt.homeassistant.internal.config.dto.AbstractChannelConfiguration;
import org.openhab.binding.mqtt.homeassistant.internal.exception.ConfigurationException;
import org.openhab.core.types.util.UnitUtils;

import com.google.gson.annotations.SerializedName;
Expand Down Expand Up @@ -73,13 +72,6 @@ static class ChannelConfiguration extends AbstractChannelConfiguration {
public Number(ComponentFactory.ComponentConfiguration componentConfiguration, boolean newStyleChannels) {
super(componentConfiguration, ChannelConfiguration.class, newStyleChannels);

boolean optimistic = channelConfiguration.optimistic != null ? channelConfiguration.optimistic
: channelConfiguration.stateTopic.isBlank();

if (optimistic && !channelConfiguration.stateTopic.isBlank()) {
throw new ConfigurationException("Component:Number does not support forced optimistic mode");
}

NumberValue value = new NumberValue(channelConfiguration.min, channelConfiguration.max,
channelConfiguration.step, UnitUtils.parseUnit(channelConfiguration.unitOfMeasurement));

Expand All @@ -88,7 +80,7 @@ public Number(ComponentFactory.ComponentConfiguration componentConfiguration, bo
.stateTopic(channelConfiguration.stateTopic, channelConfiguration.getValueTemplate())
.commandTopic(channelConfiguration.commandTopic, channelConfiguration.isRetain(),
channelConfiguration.getQos(), channelConfiguration.commandTemplate)
.build();
.inferOptimistic(channelConfiguration.optimistic).build();
finalizeChannels();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.openhab.binding.mqtt.generic.values.TextValue;
import org.openhab.binding.mqtt.homeassistant.internal.ComponentChannelType;
import org.openhab.binding.mqtt.homeassistant.internal.config.dto.AbstractChannelConfiguration;
import org.openhab.binding.mqtt.homeassistant.internal.exception.ConfigurationException;

import com.google.gson.annotations.SerializedName;

Expand Down Expand Up @@ -58,21 +57,14 @@ static class ChannelConfiguration extends AbstractChannelConfiguration {
public Select(ComponentFactory.ComponentConfiguration componentConfiguration, boolean newStyleChannels) {
super(componentConfiguration, ChannelConfiguration.class, newStyleChannels);

boolean optimistic = channelConfiguration.optimistic != null ? channelConfiguration.optimistic
: channelConfiguration.stateTopic.isBlank();

if (optimistic && !channelConfiguration.stateTopic.isBlank()) {
throw new ConfigurationException("Component:Select does not support forced optimistic mode");
}

TextValue value = new TextValue(channelConfiguration.options);

buildChannel(SELECT_CHANNEL_ID, ComponentChannelType.STRING, value, getName(),
componentConfiguration.getUpdateListener())
.stateTopic(channelConfiguration.stateTopic, channelConfiguration.getValueTemplate())
.commandTopic(channelConfiguration.commandTopic, channelConfiguration.isRetain(),
channelConfiguration.getQos(), channelConfiguration.commandTemplate)
.build();
.inferOptimistic(channelConfiguration.optimistic).build();
finalizeChannels();
}
}
Loading

0 comments on commit ab59bc8

Please sign in to comment.