]> git.basschouten.com Git - openhab-addons.git/blob
96becd8e2193caa6f984b86e2e013be466a4117d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mqtt.homeassistant.internal.component;
14
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.junit.jupiter.api.Assertions.assertNotNull;
18
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.Objects;
22 import java.util.Set;
23
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;
29
30 /**
31  * Tests for {@link DeviceTrigger}
32  *
33  * @author Cody Cutrer - Initial contribution
34  */
35 @NonNullByDefault
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";
39
40     @SuppressWarnings("null")
41     @Test
42     public void test() throws InterruptedException {
43         var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC_1), """
44                 {
45                     "automation_type": "trigger",
46                     "device": {
47                         "configuration_url": "#/device/0x8cf681fffe2fd2a6/info",
48                         "identifiers": [
49                             "zigbee2mqtt_0x8cf681fffe2fd2a6"
50                         ],
51                         "manufacturer": "IKEA",
52                         "model": "TRADFRI shortcut button (E1812)",
53                         "name": "Charge Now Button",
54                         "sw_version": "2.3.015"
55                     },
56                     "payload": "on",
57                     "subtype": "on",
58                     "topic": "zigbee2mqtt/Charge Now Button/action",
59                     "type": "action"
60                 }
61                     """);
62
63         assertThat(component.channels.size(), is(1));
64         assertThat(component.getName(), is("MQTT Device Trigger"));
65
66         assertChannel(component, "on", "zigbee2mqtt/Charge Now Button/action", "", "MQTT Device Trigger",
67                 TextValue.class);
68
69         spyOnChannelUpdates(component, "on");
70         publishMessage("zigbee2mqtt/Charge Now Button/action", "on");
71         assertTriggered(component, "on", "on");
72
73         publishMessage("zigbee2mqtt/Charge Now Button/action", "off");
74         assertNotTriggered(component, "on", "off");
75     }
76
77     @SuppressWarnings("null")
78     @Test
79     public void testMerge() throws InterruptedException {
80         var component1 = (DeviceTrigger) discoverComponent(configTopicToMqtt(CONFIG_TOPIC_1), """
81                 {
82                     "automation_type": "trigger",
83                     "device": {
84                         "configuration_url": "#/device/0x8cf681fffe2fd2a6/info",
85                         "identifiers": [
86                             "zigbee2mqtt_0x8cf681fffe2fd2a6"
87                         ],
88                         "manufacturer": "IKEA",
89                         "model": "TRADFRI shortcut button (E1812)",
90                         "name": "Charge Now Button",
91                         "sw_version": "2.3.015"
92                     },
93                     "payload": "press",
94                     "subtype": "turn_on",
95                     "topic": "zigbee2mqtt/Charge Now Button/action",
96                     "type": "button_long_press"
97                 }
98                     """);
99         var component2 = (DeviceTrigger) discoverComponent(configTopicToMqtt(CONFIG_TOPIC_2), """
100                 {
101                     "automation_type": "trigger",
102                     "device": {
103                         "configuration_url": "#/device/0x8cf681fffe2fd2a6/info",
104                         "identifiers": [
105                             "zigbee2mqtt_0x8cf681fffe2fd2a6"
106                         ],
107                         "manufacturer": "IKEA",
108                         "model": "TRADFRI shortcut button (E1812)",
109                         "name": "Charge Now Button",
110                         "sw_version": "2.3.015"
111                     },
112                     "payload": "release",
113                     "subtype": "turn_on",
114                     "topic": "zigbee2mqtt/Charge Now Button/action",
115                     "type": "button_long_release"
116                 }
117                     """);
118
119         assertThat(component1.channels.size(), is(1));
120
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));
134
135         spyOnChannelUpdates(component1, "turn_on");
136         publishMessage("zigbee2mqtt/Charge Now Button/action", "press");
137         assertTriggered(component1, "turn_on", "press");
138
139         publishMessage("zigbee2mqtt/Charge Now Button/action", "release");
140         assertTriggered(component1, "turn_on", "release");
141
142         publishMessage("zigbee2mqtt/Charge Now Button/action", "otherwise");
143         assertNotTriggered(component1, "turn_on", "otherwise");
144     }
145
146     @Override
147     protected Set<String> getConfigTopics() {
148         return Set.of(CONFIG_TOPIC_1, CONFIG_TOPIC_2);
149     }
150
151     @Override
152     protected boolean useNewStyleChannels() {
153         return true;
154     }
155 }