]> git.basschouten.com Git - openhab-addons.git/blob
cf3376b2291cbf1e1476495ee39eeb7f73a752d4
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.junit.jupiter.api.Test;
21 import org.openhab.binding.mqtt.generic.values.OnOffValue;
22 import org.openhab.core.library.types.OnOffType;
23
24 /**
25  * Tests for {@link Fan}
26  *
27  * @author Anton Kharuzhy - Initial contribution
28  */
29 @SuppressWarnings("ALL")
30 public class FanTests extends AbstractComponentTests {
31     public static final String CONFIG_TOPIC = "fan/0x0000000000000000_fan_zigbee2mqtt";
32
33     @Test
34     public void test() throws InterruptedException {
35         // @formatter:off
36         var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
37                 "{ " +
38                         "  \"availability\": [ " +
39                         "    { " +
40                         "      \"topic\": \"zigbee2mqtt/bridge/state\" " +
41                         "    } " +
42                         "  ], " +
43                         "  \"device\": { " +
44                         "    \"identifiers\": [ " +
45                         "      \"zigbee2mqtt_0x0000000000000000\" " +
46                         "    ], " +
47                         "    \"manufacturer\": \"Fans inc\", " +
48                         "    \"model\": \"Fan\", " +
49                         "    \"name\": \"FanBlower\", " +
50                         "    \"sw_version\": \"Zigbee2MQTT 1.18.2\" " +
51                         "  }, " +
52                         "  \"name\": \"fan\", " +
53                         "  \"payload_off\": \"OFF_\", " +
54                         "  \"payload_on\": \"ON_\", " +
55                         "  \"state_topic\": \"zigbee2mqtt/fan/state\", " +
56                         "  \"command_topic\": \"zigbee2mqtt/fan/set/state\" " +
57                         "}");
58         // @formatter:on
59
60         assertThat(component.channels.size(), is(1));
61         assertThat(component.getName(), is("fan"));
62
63         assertChannel(component, Fan.SWITCH_CHANNEL_ID, "zigbee2mqtt/fan/state", "zigbee2mqtt/fan/set/state", "fan",
64                 OnOffValue.class);
65
66         publishMessage("zigbee2mqtt/fan/state", "ON_");
67         assertState(component, Fan.SWITCH_CHANNEL_ID, OnOffType.ON);
68         publishMessage("zigbee2mqtt/fan/state", "ON_");
69         assertState(component, Fan.SWITCH_CHANNEL_ID, OnOffType.ON);
70         publishMessage("zigbee2mqtt/fan/state", "OFF_");
71         assertState(component, Fan.SWITCH_CHANNEL_ID, OnOffType.OFF);
72         publishMessage("zigbee2mqtt/fan/state", "ON_");
73         assertState(component, Fan.SWITCH_CHANNEL_ID, OnOffType.ON);
74
75         component.getChannel(Fan.SWITCH_CHANNEL_ID).getState().publishValue(OnOffType.OFF);
76         assertPublished("zigbee2mqtt/fan/set/state", "OFF_");
77         component.getChannel(Fan.SWITCH_CHANNEL_ID).getState().publishValue(OnOffType.ON);
78         assertPublished("zigbee2mqtt/fan/set/state", "ON_");
79     }
80
81     protected Set<String> getConfigTopics() {
82         return Set.of(CONFIG_TOPIC);
83     }
84 }