2 * Copyright (c) 2010-2023 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;
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;
26 * Tests for {@link Switch}
28 * @author Anton Kharuzhy - Initial contribution
31 public class SwitchTests extends AbstractComponentTests {
32 public static final String CONFIG_TOPIC = "switch/0x847127fffe11dd6a_auto_lock_zigbee2mqtt";
34 @SuppressWarnings("null")
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" + "}");
52 assertThat(component.channels.size(), is(1));
53 assertThat(component.getName(), is("th1 auto lock"));
55 assertChannel(component, Switch.SWITCH_CHANNEL_ID, "zigbee2mqtt/th1", "zigbee2mqtt/th1/set/auto_lock", "state",
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);
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");
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" + "}");
84 assertThat(component.channels.size(), is(1));
85 assertThat(component.getName(), is("th1 auto lock"));
87 assertChannel(component, Switch.SWITCH_CHANNEL_ID, "zigbee2mqtt/th1", "", "state", OnOffValue.class);
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);
95 @SuppressWarnings("null")
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" + "}");
111 assertThat(component.channels.size(), is(1));
112 assertThat(component.getName(), is("th1 auto lock"));
114 assertChannel(component, Switch.SWITCH_CHANNEL_ID, "", "zigbee2mqtt/th1/set/auto_lock", "state",
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");
124 protected Set<String> getConfigTopics() {
125 return Set.of(CONFIG_TOPIC);