From e95ab29e6f57abfd77a714309fcfdc0e6619834a Mon Sep 17 00:00:00 2001 From: Cody Cutrer Date: Fri, 25 Oct 2024 10:12:04 -0500 Subject: [PATCH] [mqtt.homeassistant] bring AlarmControlPanel in line with current documentation (#17607) * [mqtt.homeassistant] bring AlarmControlPanel in line with current documentation Signed-off-by: Cody Cutrer Signed-off-by: Ciprian Pascu --- .../internal/component/AlarmControlPanel.java | 108 +++++++++++++----- .../AlarmControlPanelDeprecatedTests.java | 100 ++++++++++++++++ .../component/AlarmControlPanelTests.java | 26 ++--- 3 files changed, 193 insertions(+), 41 deletions(-) create mode 100644 bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/AlarmControlPanelDeprecatedTests.java diff --git a/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/AlarmControlPanel.java b/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/AlarmControlPanel.java index a915c11f43b..2976d254867 100644 --- a/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/AlarmControlPanel.java +++ b/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/AlarmControlPanel.java @@ -12,6 +12,9 @@ */ package org.openhab.binding.mqtt.homeassistant.internal.component; +import java.util.ArrayList; +import java.util.List; + import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.mqtt.generic.values.TextValue; @@ -31,10 +34,29 @@ import com.google.gson.annotations.SerializedName; */ @NonNullByDefault public class AlarmControlPanel extends AbstractComponent { - public static final String STATE_CHANNEL_ID = "alarm"; // Randomly chosen channel "ID" - public static final String SWITCH_DISARM_CHANNEL_ID = "disarm"; // Randomly chosen channel "ID" - public static final String SWITCH_ARM_HOME_CHANNEL_ID = "armhome"; // Randomly chosen channel "ID" - public static final String SWITCH_ARM_AWAY_CHANNEL_ID = "armaway"; // Randomly chosen channel "ID" + public static final String STATE_CHANNEL_ID = "state"; + public static final String STATE_CHANNEL_ID_DEPRECATED = "alarm"; + public static final String SWITCH_DISARM_CHANNEL_ID = "disarm"; + public static final String SWITCH_ARM_HOME_CHANNEL_ID = "armhome"; + public static final String SWITCH_ARM_AWAY_CHANNEL_ID = "armaway"; + + public static final String FEATURE_ARM_HOME = "arm_home"; + public static final String FEATURE_ARM_AWAY = "arm_away"; + public static final String FEATURE_ARM_NIGHT = "arm_night"; + public static final String FEATURE_ARM_VACATION = "arm_vacation"; + public static final String FEATURE_ARM_CUSTOM_BYPASS = "arm_custom_bypass"; + public static final String FEATURE_TRIGGER = "trigger"; + + public static final String STATE_ARMED_AWAY = "armed_away"; + public static final String STATE_ARMED_CUSTOM_BYPASS = "armed_custom_bypass"; + public static final String STATE_ARMED_HOME = "armed_home"; + public static final String STATE_ARMED_NIGHT = "armed_night"; + public static final String STATE_ARMED_VACATION = "armed_vacation"; + public static final String STATE_ARMING = "arming"; + public static final String STATE_DISARMED = "disarmed"; + public static final String STATE_DISARMING = "disarming"; + public static final String STATE_PENDING = "pending"; + public static final String STATE_TRIGGERED = "triggered"; /** * Configuration class for MQTT component @@ -48,40 +70,73 @@ public class AlarmControlPanel extends AbstractComponent supportedFeatures = List.of(FEATURE_ARM_HOME, FEATURE_ARM_AWAY, FEATURE_ARM_NIGHT, + FEATURE_ARM_VACATION, FEATURE_ARM_CUSTOM_BYPASS, FEATURE_TRIGGER); } public AlarmControlPanel(ComponentFactory.ComponentConfiguration componentConfiguration, boolean newStyleChannels) { super(componentConfiguration, ChannelConfiguration.class, newStyleChannels); - final String[] stateEnum = { channelConfiguration.stateDisarmed, channelConfiguration.stateArmedHome, - channelConfiguration.stateArmedAway, channelConfiguration.statePending, - channelConfiguration.stateTriggered }; - buildChannel(STATE_CHANNEL_ID, ComponentChannelType.STRING, new TextValue(stateEnum), getName(), - componentConfiguration.getUpdateListener()) - .stateTopic(channelConfiguration.stateTopic, channelConfiguration.getValueTemplate())// - .build(); + List stateEnum = new ArrayList(List.of(STATE_DISARMED, STATE_TRIGGERED, STATE_ARMING, STATE_DISARMING, + STATE_PENDING, STATE_TRIGGERED)); + List commandEnum = new ArrayList(List.of(channelConfiguration.payloadDisarm)); + if (channelConfiguration.supportedFeatures.contains(FEATURE_ARM_HOME)) { + stateEnum.add(STATE_ARMED_HOME); + commandEnum.add(channelConfiguration.payloadArmHome); + } + if (channelConfiguration.supportedFeatures.contains(FEATURE_ARM_AWAY)) { + stateEnum.add(STATE_ARMED_AWAY); + commandEnum.add(channelConfiguration.payloadArmAway); + } + if (channelConfiguration.supportedFeatures.contains(FEATURE_ARM_NIGHT)) { + stateEnum.add(STATE_ARMED_NIGHT); + commandEnum.add(channelConfiguration.payloadArmNight); + } + if (channelConfiguration.supportedFeatures.contains(FEATURE_ARM_VACATION)) { + stateEnum.add(STATE_ARMED_VACATION); + commandEnum.add(channelConfiguration.payloadArmVacation); + } + if (channelConfiguration.supportedFeatures.contains(FEATURE_ARM_CUSTOM_BYPASS)) { + stateEnum.add(STATE_ARMED_CUSTOM_BYPASS); + commandEnum.add(channelConfiguration.payloadArmCustomBypass); + } + if (channelConfiguration.supportedFeatures.contains(FEATURE_TRIGGER)) { + commandEnum.add(channelConfiguration.payloadTrigger); + } String commandTopic = channelConfiguration.commandTopic; - if (commandTopic != null) { + TextValue value = (newStyleChannels && commandTopic != null) + ? new TextValue(stateEnum.toArray(new String[0]), commandEnum.toArray(new String[0])) + : new TextValue(stateEnum.toArray(new String[0])); + var builder = buildChannel(newStyleChannels ? STATE_CHANNEL_ID : STATE_CHANNEL_ID_DEPRECATED, + ComponentChannelType.STRING, value, getName(), componentConfiguration.getUpdateListener()) + .stateTopic(channelConfiguration.stateTopic, channelConfiguration.getValueTemplate()); + + if (newStyleChannels && commandTopic != null) { + builder.commandTopic(commandTopic, channelConfiguration.isRetain(), channelConfiguration.getQos()); + } + builder.build(); + + if (!newStyleChannels && commandTopic != null) { buildChannel(SWITCH_DISARM_CHANNEL_ID, ComponentChannelType.STRING, new TextValue(new String[] { channelConfiguration.payloadDisarm }), getName(), componentConfiguration.getUpdateListener()) @@ -97,6 +152,7 @@ public class AlarmControlPanel extends AbstractComponent getConfigTopics() { + return Set.of(CONFIG_TOPIC); + } +} diff --git a/bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/AlarmControlPanelTests.java b/bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/AlarmControlPanelTests.java index 37f557ba781..4ab20cac1bd 100644 --- a/bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/AlarmControlPanelTests.java +++ b/bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/AlarmControlPanelTests.java @@ -65,31 +65,22 @@ public class AlarmControlPanelTests extends AbstractComponentTests { """); // @formatter:on - assertThat(component.channels.size(), is(4)); + assertThat(component.channels.size(), is(1)); assertThat(component.getName(), is("alarm")); - assertChannel(component, AlarmControlPanel.STATE_CHANNEL_ID, "zigbee2mqtt/alarm/state", "", "alarm", - TextValue.class); - assertChannel(component, AlarmControlPanel.SWITCH_DISARM_CHANNEL_ID, "", "zigbee2mqtt/alarm/set/state", "alarm", - TextValue.class); - assertChannel(component, AlarmControlPanel.SWITCH_ARM_AWAY_CHANNEL_ID, "", "zigbee2mqtt/alarm/set/state", - "alarm", TextValue.class); - assertChannel(component, AlarmControlPanel.SWITCH_ARM_HOME_CHANNEL_ID, "", "zigbee2mqtt/alarm/set/state", - "alarm", TextValue.class); + assertChannel(component, AlarmControlPanel.STATE_CHANNEL_ID, "zigbee2mqtt/alarm/state", + "zigbee2mqtt/alarm/set/state", "alarm", TextValue.class); publishMessage("zigbee2mqtt/alarm/state", "armed_home"); assertState(component, AlarmControlPanel.STATE_CHANNEL_ID, new StringType("armed_home")); publishMessage("zigbee2mqtt/alarm/state", "armed_away"); assertState(component, AlarmControlPanel.STATE_CHANNEL_ID, new StringType("armed_away")); - component.getChannel(AlarmControlPanel.SWITCH_DISARM_CHANNEL_ID).getState() - .publishValue(new StringType("DISARM_")); + component.getChannel(AlarmControlPanel.STATE_CHANNEL_ID).getState().publishValue(new StringType("DISARM_")); assertPublished("zigbee2mqtt/alarm/set/state", "DISARM_"); - component.getChannel(AlarmControlPanel.SWITCH_ARM_AWAY_CHANNEL_ID).getState() - .publishValue(new StringType("ARM_AWAY_")); + component.getChannel(AlarmControlPanel.STATE_CHANNEL_ID).getState().publishValue(new StringType("ARM_AWAY_")); assertPublished("zigbee2mqtt/alarm/set/state", "ARM_AWAY_"); - component.getChannel(AlarmControlPanel.SWITCH_ARM_HOME_CHANNEL_ID).getState() - .publishValue(new StringType("ARM_HOME_")); + component.getChannel(AlarmControlPanel.STATE_CHANNEL_ID).getState().publishValue(new StringType("ARM_HOME_")); assertPublished("zigbee2mqtt/alarm/set/state", "ARM_HOME_"); } @@ -97,4 +88,9 @@ public class AlarmControlPanelTests extends AbstractComponentTests { protected Set getConfigTopics() { return Set.of(CONFIG_TOPIC); } + + @Override + protected boolean useNewStyleChannels() { + return true; + } }