2 * Copyright (c) 2010-2022 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.junit.jupiter.api.Test;
21 import org.openhab.binding.mqtt.generic.values.OnOffValue;
22 import org.openhab.core.library.types.OnOffType;
25 * Tests for {@link Switch}
27 * @author Anton Kharuzhy - Initial contribution
29 @SuppressWarnings("ConstantConditions")
30 public class SwitchTests extends AbstractComponentTests {
31 public static final String CONFIG_TOPIC = "switch/0x847127fffe11dd6a_auto_lock_zigbee2mqtt";
34 public void testSwitchWithStateAndCommand() {
35 var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
36 "" + "{\n" + " \"availability\": [\n" + " {\n" + " \"topic\": \"zigbee2mqtt/bridge/state\"\n"
37 + " }\n" + " ],\n" + " \"command_topic\": \"zigbee2mqtt/th1/set/auto_lock\",\n"
38 + " \"device\": {\n" + " \"identifiers\": [\n"
39 + " \"zigbee2mqtt_0x847127fffe11dd6a\"\n" + " ],\n"
40 + " \"manufacturer\": \"TuYa\",\n"
41 + " \"model\": \"Radiator valve with thermostat (TS0601_thermostat)\",\n"
42 + " \"name\": \"th1\",\n" + " \"sw_version\": \"Zigbee2MQTT 1.18.2\"\n" + " },\n"
43 + " \"json_attributes_topic\": \"zigbee2mqtt/th1\",\n" + " \"name\": \"th1 auto lock\",\n"
44 + " \"payload_off\": \"MANUAL\",\n" + " \"payload_on\": \"AUTO\",\n"
45 + " \"state_off\": \"MANUAL\",\n" + " \"state_on\": \"AUTO\",\n"
46 + " \"state_topic\": \"zigbee2mqtt/th1\",\n"
47 + " \"unique_id\": \"0x847127fffe11dd6a_auto_lock_zigbee2mqtt\",\n"
48 + " \"value_template\": \"{{ value_json.auto_lock }}\"\n" + "}");
50 assertThat(component.channels.size(), is(1));
51 assertThat(component.getName(), is("th1 auto lock"));
53 assertChannel(component, Switch.SWITCH_CHANNEL_ID, "zigbee2mqtt/th1", "zigbee2mqtt/th1/set/auto_lock", "state",
56 publishMessage("zigbee2mqtt/th1", "{\"auto_lock\": \"MANUAL\"}");
57 assertState(component, Switch.SWITCH_CHANNEL_ID, OnOffType.OFF);
58 publishMessage("zigbee2mqtt/th1", "{\"auto_lock\": \"AUTO\"}");
59 assertState(component, Switch.SWITCH_CHANNEL_ID, OnOffType.ON);
61 component.getChannel(Switch.SWITCH_CHANNEL_ID).getState().publishValue(OnOffType.OFF);
62 assertPublished("zigbee2mqtt/th1/set/auto_lock", "MANUAL");
63 component.getChannel(Switch.SWITCH_CHANNEL_ID).getState().publishValue(OnOffType.ON);
64 assertPublished("zigbee2mqtt/th1/set/auto_lock", "AUTO");
68 public void testSwitchWithState() {
69 var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
70 "" + "{\n" + " \"availability\": [\n" + " {\n" + " \"topic\": \"zigbee2mqtt/bridge/state\"\n"
71 + " }\n" + " ],\n" + " \"device\": {\n" + " \"identifiers\": [\n"
72 + " \"zigbee2mqtt_0x847127fffe11dd6a\"\n" + " ],\n"
73 + " \"manufacturer\": \"TuYa\",\n"
74 + " \"model\": \"Radiator valve with thermostat (TS0601_thermostat)\",\n"
75 + " \"name\": \"th1\",\n" + " \"sw_version\": \"Zigbee2MQTT 1.18.2\"\n" + " },\n"
76 + " \"json_attributes_topic\": \"zigbee2mqtt/th1\",\n" + " \"name\": \"th1 auto lock\",\n"
77 + " \"state_off\": \"MANUAL\",\n" + " \"state_on\": \"AUTO\",\n"
78 + " \"state_topic\": \"zigbee2mqtt/th1\",\n"
79 + " \"unique_id\": \"0x847127fffe11dd6a_auto_lock_zigbee2mqtt\",\n"
80 + " \"value_template\": \"{{ value_json.auto_lock }}\"\n" + "}");
82 assertThat(component.channels.size(), is(1));
83 assertThat(component.getName(), is("th1 auto lock"));
85 assertChannel(component, Switch.SWITCH_CHANNEL_ID, "zigbee2mqtt/th1", "", "state", OnOffValue.class);
87 publishMessage("zigbee2mqtt/th1", "{\"auto_lock\": \"MANUAL\"}");
88 assertState(component, Switch.SWITCH_CHANNEL_ID, OnOffType.OFF);
89 publishMessage("zigbee2mqtt/th1", "{\"auto_lock\": \"AUTO\"}");
90 assertState(component, Switch.SWITCH_CHANNEL_ID, OnOffType.ON);
94 public void testSwitchWithCommand() {
95 var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
96 "" + "{\n" + " \"availability\": [\n" + " {\n" + " \"topic\": \"zigbee2mqtt/bridge/state\"\n"
97 + " }\n" + " ],\n" + " \"command_topic\": \"zigbee2mqtt/th1/set/auto_lock\",\n"
98 + " \"device\": {\n" + " \"identifiers\": [\n"
99 + " \"zigbee2mqtt_0x847127fffe11dd6a\"\n" + " ],\n"
100 + " \"manufacturer\": \"TuYa\",\n"
101 + " \"model\": \"Radiator valve with thermostat (TS0601_thermostat)\",\n"
102 + " \"name\": \"th1\",\n" + " \"sw_version\": \"Zigbee2MQTT 1.18.2\"\n" + " },\n"
103 + " \"json_attributes_topic\": \"zigbee2mqtt/th1\",\n" + " \"name\": \"th1 auto lock\",\n"
104 + " \"payload_off\": \"MANUAL\",\n" + " \"payload_on\": \"AUTO\",\n"
105 + " \"unique_id\": \"0x847127fffe11dd6a_auto_lock_zigbee2mqtt\",\n"
106 + " \"value_template\": \"{{ value_json.auto_lock }}\"\n" + "}");
108 assertThat(component.channels.size(), is(1));
109 assertThat(component.getName(), is("th1 auto lock"));
111 assertChannel(component, Switch.SWITCH_CHANNEL_ID, "", "zigbee2mqtt/th1/set/auto_lock", "state",
114 component.getChannel(Switch.SWITCH_CHANNEL_ID).getState().publishValue(OnOffType.OFF);
115 assertPublished("zigbee2mqtt/th1/set/auto_lock", "MANUAL");
116 component.getChannel(Switch.SWITCH_CHANNEL_ID).getState().publishValue(OnOffType.ON);
117 assertPublished("zigbee2mqtt/th1/set/auto_lock", "AUTO");
120 protected Set<String> getConfigTopics() {
121 return Set.of(CONFIG_TOPIC);