From f3ca3255a77cce5cec6bce64f27da83d7226edbf Mon Sep 17 00:00:00 2001 From: Cody Cutrer Date: Fri, 27 Jan 2023 16:38:38 -0700 Subject: [PATCH] [mqtt.homeassistant] support command_template for fan components (#14233) * [mqtt.homeassistant] support command_template for fan components * [mqtt.homeassistant] fix field declaration for consistency Signed-off-by: Cody Cutrer --- .../homeassistant/internal/component/Fan.java | 4 ++- .../internal/component/FanTests.java | 34 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/Fan.java b/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/Fan.java index 7c3bc8ddfe9..64107063288 100644 --- a/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/Fan.java +++ b/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/Fan.java @@ -40,6 +40,8 @@ public class Fan extends AbstractComponent { @SerializedName("state_topic") protected @Nullable String stateTopic; + @SerializedName("command_template") + protected @Nullable String commandTemplate; @SerializedName("command_topic") protected String commandTopic = ""; @SerializedName("payload_on") @@ -56,7 +58,7 @@ public class Fan extends AbstractComponent { componentConfiguration.getUpdateListener()) .stateTopic(channelConfiguration.stateTopic, channelConfiguration.getValueTemplate()) .commandTopic(channelConfiguration.commandTopic, channelConfiguration.isRetain(), - channelConfiguration.getQos()) + channelConfiguration.getQos(), channelConfiguration.commandTemplate) .build(); } } diff --git a/bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/FanTests.java b/bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/FanTests.java index f50ac49d1a0..fde17cfc1a2 100644 --- a/bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/FanTests.java +++ b/bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/FanTests.java @@ -80,6 +80,40 @@ public class FanTests extends AbstractComponentTests { assertPublished("zigbee2mqtt/fan/set/state", "ON_"); } + @SuppressWarnings("null") + @Test + public void testCommandTemplate() throws InterruptedException { + var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC), """ + { + "availability": [ + { + "topic": "zigbee2mqtt/bridge/state" + } + ], + "device": { + "identifiers": [ + "zigbee2mqtt_0x0000000000000000" + ], + "manufacturer": "Fans inc", + "model": "Fan", + "name": "FanBlower", + "sw_version": "Zigbee2MQTT 1.18.2" + }, + "name": "fan", + "payload_off": "OFF_", + "payload_on": "ON_", + "state_topic": "zigbee2mqtt/fan/state", + "command_topic": "zigbee2mqtt/fan/set/state", + "command_template": "set to {{ value }}" + } + """); + + assertThat(component.channels.size(), is(1)); + + component.getChannel(Fan.SWITCH_CHANNEL_ID).getState().publishValue(OnOffType.OFF); + assertPublished("zigbee2mqtt/fan/set/state", "set to OFF_"); + } + @Override protected Set getConfigTopics() { return Set.of(CONFIG_TOPIC);