2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mqtt.homeassistant.internal.component;
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.junit.jupiter.api.Assertions.assertNotNull;
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.Objects;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.junit.jupiter.api.Test;
26 import org.openhab.binding.mqtt.generic.values.TextValue;
27 import org.openhab.binding.mqtt.homeassistant.internal.ComponentChannel;
28 import org.openhab.core.config.core.Configuration;
31 * Tests for {@link DeviceTrigger}
33 * @author Cody Cutrer - Initial contribution
36 public class DeviceTriggerTests extends AbstractComponentTests {
37 public static final String CONFIG_TOPIC_1 = "device_automation/0x8cf681fffe2fd2a6/press";
38 public static final String CONFIG_TOPIC_2 = "device_automation/0x8cf681fffe2fd2a6/release";
40 @SuppressWarnings("null")
42 public void test() throws InterruptedException {
43 var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC_1), """
45 "automation_type": "trigger",
47 "configuration_url": "#/device/0x8cf681fffe2fd2a6/info",
49 "zigbee2mqtt_0x8cf681fffe2fd2a6"
51 "manufacturer": "IKEA",
52 "model": "TRADFRI shortcut button (E1812)",
53 "name": "Charge Now Button",
54 "sw_version": "2.3.015"
58 "topic": "zigbee2mqtt/Charge Now Button/action",
63 assertThat(component.channels.size(), is(1));
64 assertThat(component.getName(), is("MQTT Device Trigger"));
66 assertChannel(component, "on", "zigbee2mqtt/Charge Now Button/action", "", "MQTT Device Trigger",
69 spyOnChannelUpdates(component, "on");
70 publishMessage("zigbee2mqtt/Charge Now Button/action", "on");
71 assertTriggered(component, "on", "on");
73 publishMessage("zigbee2mqtt/Charge Now Button/action", "off");
74 assertNotTriggered(component, "on", "off");
77 @SuppressWarnings("null")
79 public void testMerge() throws InterruptedException {
80 var component1 = (DeviceTrigger) discoverComponent(configTopicToMqtt(CONFIG_TOPIC_1), """
82 "automation_type": "trigger",
84 "configuration_url": "#/device/0x8cf681fffe2fd2a6/info",
86 "zigbee2mqtt_0x8cf681fffe2fd2a6"
88 "manufacturer": "IKEA",
89 "model": "TRADFRI shortcut button (E1812)",
90 "name": "Charge Now Button",
91 "sw_version": "2.3.015"
95 "topic": "zigbee2mqtt/Charge Now Button/action",
96 "type": "button_long_press"
99 var component2 = (DeviceTrigger) discoverComponent(configTopicToMqtt(CONFIG_TOPIC_2), """
101 "automation_type": "trigger",
103 "configuration_url": "#/device/0x8cf681fffe2fd2a6/info",
105 "zigbee2mqtt_0x8cf681fffe2fd2a6"
107 "manufacturer": "IKEA",
108 "model": "TRADFRI shortcut button (E1812)",
109 "name": "Charge Now Button",
110 "sw_version": "2.3.015"
112 "payload": "release",
113 "subtype": "turn_on",
114 "topic": "zigbee2mqtt/Charge Now Button/action",
115 "type": "button_long_release"
119 assertThat(component1.channels.size(), is(1));
121 ComponentChannel channel = Objects.requireNonNull(component1.getChannel("turn_on"));
122 TextValue value = (TextValue) channel.getState().getCache();
123 Set<String> payloads = value.getStates();
124 assertNotNull(payloads);
125 assertThat(payloads.size(), is(2));
126 assertThat(payloads.contains("press"), is(true));
127 assertThat(payloads.contains("release"), is(true));
128 Configuration channelConfig = channel.getChannel().getConfiguration();
129 Object config = channelConfig.get("config");
130 assertNotNull(config);
131 assertThat(config.getClass(), is(ArrayList.class));
132 List<?> configList = (List<?>) config;
133 assertThat(configList.size(), is(2));
135 spyOnChannelUpdates(component1, "turn_on");
136 publishMessage("zigbee2mqtt/Charge Now Button/action", "press");
137 assertTriggered(component1, "turn_on", "press");
139 publishMessage("zigbee2mqtt/Charge Now Button/action", "release");
140 assertTriggered(component1, "turn_on", "release");
142 publishMessage("zigbee2mqtt/Charge Now Button/action", "otherwise");
143 assertNotTriggered(component1, "turn_on", "otherwise");
147 protected Set<String> getConfigTopics() {
148 return Set.of(CONFIG_TOPIC_1, CONFIG_TOPIC_2);
152 protected boolean useNewStyleChannels() {