[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 <cody@cutrer.us>
This commit is contained in:
Cody Cutrer 2023-01-27 16:38:38 -07:00 committed by GitHub
parent 6d5b8a8cf5
commit f3ca3255a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 1 deletions

View File

@ -40,6 +40,8 @@ public class Fan extends AbstractComponent<Fan.ChannelConfiguration> {
@SerializedName("state_topic") @SerializedName("state_topic")
protected @Nullable String stateTopic; protected @Nullable String stateTopic;
@SerializedName("command_template")
protected @Nullable String commandTemplate;
@SerializedName("command_topic") @SerializedName("command_topic")
protected String commandTopic = ""; protected String commandTopic = "";
@SerializedName("payload_on") @SerializedName("payload_on")
@ -56,7 +58,7 @@ public class Fan extends AbstractComponent<Fan.ChannelConfiguration> {
componentConfiguration.getUpdateListener()) componentConfiguration.getUpdateListener())
.stateTopic(channelConfiguration.stateTopic, channelConfiguration.getValueTemplate()) .stateTopic(channelConfiguration.stateTopic, channelConfiguration.getValueTemplate())
.commandTopic(channelConfiguration.commandTopic, channelConfiguration.isRetain(), .commandTopic(channelConfiguration.commandTopic, channelConfiguration.isRetain(),
channelConfiguration.getQos()) channelConfiguration.getQos(), channelConfiguration.commandTemplate)
.build(); .build();
} }
} }

View File

@ -80,6 +80,40 @@ public class FanTests extends AbstractComponentTests {
assertPublished("zigbee2mqtt/fan/set/state", "ON_"); 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 @Override
protected Set<String> getConfigTopics() { protected Set<String> getConfigTopics() {
return Set.of(CONFIG_TOPIC); return Set.of(CONFIG_TOPIC);