]> git.basschouten.com Git - openhab-addons.git/blob
fdb250539d3f629a54e39ff024a4e49f803cef8c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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
18 import java.util.Set;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.junit.jupiter.api.Test;
22 import org.openhab.binding.mqtt.generic.values.OnOffValue;
23 import org.openhab.core.library.types.OnOffType;
24
25 /**
26  * Tests for {@link Switch}
27  *
28  * @author Anton Kharuzhy - Initial contribution
29  */
30 @NonNullByDefault
31 public class SwitchTests extends AbstractComponentTests {
32     public static final String CONFIG_TOPIC = "switch/0x847127fffe11dd6a_auto_lock_zigbee2mqtt";
33
34     @SuppressWarnings("null")
35     @Test
36     public void testSwitchWithStateAndCommand() {
37         var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
38                 "" + "{\n" + "  \"availability\": [\n" + "    {\n" + "      \"topic\": \"zigbee2mqtt/bridge/state\"\n"
39                         + "    }\n" + "  ],\n" + "  \"command_topic\": \"zigbee2mqtt/th1/set/auto_lock\",\n"
40                         + "  \"device\": {\n" + "    \"identifiers\": [\n"
41                         + "      \"zigbee2mqtt_0x847127fffe11dd6a\"\n" + "    ],\n"
42                         + "    \"manufacturer\": \"TuYa\",\n"
43                         + "    \"model\": \"Radiator valve with thermostat (TS0601_thermostat)\",\n"
44                         + "    \"name\": \"th1\",\n" + "    \"sw_version\": \"Zigbee2MQTT 1.18.2\"\n" + "  },\n"
45                         + "  \"json_attributes_topic\": \"zigbee2mqtt/th1\",\n" + "  \"name\": \"th1 auto lock\",\n"
46                         + "  \"payload_off\": \"MANUAL\",\n" + "  \"payload_on\": \"AUTO\",\n"
47                         + "  \"state_off\": \"MANUAL\",\n" + "  \"state_on\": \"AUTO\",\n"
48                         + "  \"state_topic\": \"zigbee2mqtt/th1\",\n"
49                         + "  \"unique_id\": \"0x847127fffe11dd6a_auto_lock_zigbee2mqtt\",\n"
50                         + "  \"value_template\": \"{{ value_json.auto_lock }}\"\n" + "}");
51
52         assertThat(component.channels.size(), is(1));
53         assertThat(component.getName(), is("th1 auto lock"));
54
55         assertChannel(component, Switch.SWITCH_CHANNEL_ID, "zigbee2mqtt/th1", "zigbee2mqtt/th1/set/auto_lock", "state",
56                 OnOffValue.class);
57
58         publishMessage("zigbee2mqtt/th1", "{\"auto_lock\": \"MANUAL\"}");
59         assertState(component, Switch.SWITCH_CHANNEL_ID, OnOffType.OFF);
60         publishMessage("zigbee2mqtt/th1", "{\"auto_lock\": \"AUTO\"}");
61         assertState(component, Switch.SWITCH_CHANNEL_ID, OnOffType.ON);
62
63         component.getChannel(Switch.SWITCH_CHANNEL_ID).getState().publishValue(OnOffType.OFF);
64         assertPublished("zigbee2mqtt/th1/set/auto_lock", "MANUAL");
65         component.getChannel(Switch.SWITCH_CHANNEL_ID).getState().publishValue(OnOffType.ON);
66         assertPublished("zigbee2mqtt/th1/set/auto_lock", "AUTO");
67     }
68
69     @Test
70     public void testSwitchWithState() {
71         var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
72                 "" + "{\n" + "  \"availability\": [\n" + "    {\n" + "      \"topic\": \"zigbee2mqtt/bridge/state\"\n"
73                         + "    }\n" + "  ],\n" + "  \"device\": {\n" + "    \"identifiers\": [\n"
74                         + "      \"zigbee2mqtt_0x847127fffe11dd6a\"\n" + "    ],\n"
75                         + "    \"manufacturer\": \"TuYa\",\n"
76                         + "    \"model\": \"Radiator valve with thermostat (TS0601_thermostat)\",\n"
77                         + "    \"name\": \"th1\",\n" + "    \"sw_version\": \"Zigbee2MQTT 1.18.2\"\n" + "  },\n"
78                         + "  \"json_attributes_topic\": \"zigbee2mqtt/th1\",\n" + "  \"name\": \"th1 auto lock\",\n"
79                         + "  \"state_off\": \"MANUAL\",\n" + "  \"state_on\": \"AUTO\",\n"
80                         + "  \"state_topic\": \"zigbee2mqtt/th1\",\n"
81                         + "  \"unique_id\": \"0x847127fffe11dd6a_auto_lock_zigbee2mqtt\",\n"
82                         + "  \"value_template\": \"{{ value_json.auto_lock }}\"\n" + "}");
83
84         assertThat(component.channels.size(), is(1));
85         assertThat(component.getName(), is("th1 auto lock"));
86
87         assertChannel(component, Switch.SWITCH_CHANNEL_ID, "zigbee2mqtt/th1", "", "state", OnOffValue.class);
88
89         publishMessage("zigbee2mqtt/th1", "{\"auto_lock\": \"MANUAL\"}");
90         assertState(component, Switch.SWITCH_CHANNEL_ID, OnOffType.OFF);
91         publishMessage("zigbee2mqtt/th1", "{\"auto_lock\": \"AUTO\"}");
92         assertState(component, Switch.SWITCH_CHANNEL_ID, OnOffType.ON);
93     }
94
95     @SuppressWarnings("null")
96     @Test
97     public void testSwitchWithCommand() {
98         var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
99                 "" + "{\n" + "  \"availability\": [\n" + "    {\n" + "      \"topic\": \"zigbee2mqtt/bridge/state\"\n"
100                         + "    }\n" + "  ],\n" + "  \"command_topic\": \"zigbee2mqtt/th1/set/auto_lock\",\n"
101                         + "  \"device\": {\n" + "    \"identifiers\": [\n"
102                         + "      \"zigbee2mqtt_0x847127fffe11dd6a\"\n" + "    ],\n"
103                         + "    \"manufacturer\": \"TuYa\",\n"
104                         + "    \"model\": \"Radiator valve with thermostat (TS0601_thermostat)\",\n"
105                         + "    \"name\": \"th1\",\n" + "    \"sw_version\": \"Zigbee2MQTT 1.18.2\"\n" + "  },\n"
106                         + "  \"json_attributes_topic\": \"zigbee2mqtt/th1\",\n" + "  \"name\": \"th1 auto lock\",\n"
107                         + "  \"payload_off\": \"MANUAL\",\n" + "  \"payload_on\": \"AUTO\",\n"
108                         + "  \"unique_id\": \"0x847127fffe11dd6a_auto_lock_zigbee2mqtt\",\n"
109                         + "  \"value_template\": \"{{ value_json.auto_lock }}\"\n" + "}");
110
111         assertThat(component.channels.size(), is(1));
112         assertThat(component.getName(), is("th1 auto lock"));
113
114         assertChannel(component, Switch.SWITCH_CHANNEL_ID, "", "zigbee2mqtt/th1/set/auto_lock", "state",
115                 OnOffValue.class);
116
117         component.getChannel(Switch.SWITCH_CHANNEL_ID).getState().publishValue(OnOffType.OFF);
118         assertPublished("zigbee2mqtt/th1/set/auto_lock", "MANUAL");
119         component.getChannel(Switch.SWITCH_CHANNEL_ID).getState().publishValue(OnOffType.ON);
120         assertPublished("zigbee2mqtt/th1/set/auto_lock", "AUTO");
121     }
122
123     @Override
124     protected Set<String> getConfigTopics() {
125         return Set.of(CONFIG_TOPIC);
126     }
127 }