]> git.basschouten.com Git - openhab-addons.git/commitdiff
[mqtt.homeassistant] support command_template for fan components (#14233)
authorCody Cutrer <cody@cutrer.us>
Fri, 27 Jan 2023 23:38:38 +0000 (16:38 -0700)
committerGitHub <noreply@github.com>
Fri, 27 Jan 2023 23:38:38 +0000 (00:38 +0100)
* [mqtt.homeassistant] support command_template for fan components
* [mqtt.homeassistant] fix field declaration for consistency

Signed-off-by: Cody Cutrer <cody@cutrer.us>
bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/Fan.java
bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/FanTests.java

index 7c3bc8ddfe9dbf30c351553e77df62562ca6da1d..6410706328891dc3d08efb590cf7f166244c3505 100644 (file)
@@ -40,6 +40,8 @@ public class Fan extends AbstractComponent<Fan.ChannelConfiguration> {
 
         @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<Fan.ChannelConfiguration> {
                 componentConfiguration.getUpdateListener())
                         .stateTopic(channelConfiguration.stateTopic, channelConfiguration.getValueTemplate())
                         .commandTopic(channelConfiguration.commandTopic, channelConfiguration.isRetain(),
-                                channelConfiguration.getQos())
+                                channelConfiguration.getQos(), channelConfiguration.commandTemplate)
                         .build();
     }
 }
index f50ac49d1a08379320ace0b06c13c35c15bdf64c..fde17cfc1a2f6c4231d4fd1d3f88c534609cc232 100644 (file)
@@ -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<String> getConfigTopics() {
         return Set.of(CONFIG_TOPIC);