]> git.basschouten.com Git - openhab-addons.git/blob
d21876aa1685cfaabc6bbcd5d1e1130c894b5149
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.ColorValue;
22 import org.openhab.core.library.types.HSBType;
23 import org.openhab.core.library.types.OnOffType;
24
25 /**
26  * Tests for {@link Light}
27  * The current {@link Light} is non-compliant with the Specification and must be rewritten from scratch.
28  *
29  * @author Anton Kharuzhy - Initial contribution
30  */
31 public class LightTests extends AbstractComponentTests {
32     public static final String CONFIG_TOPIC = "light/0x0000000000000000_light_zigbee2mqtt";
33
34     @Test
35     public void test() throws InterruptedException {
36         // @formatter:off
37         var component = (Light) discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
38                 "{ " +
39                         "  \"availability\": [ " +
40                         "    { " +
41                         "      \"topic\": \"zigbee2mqtt/bridge/state\" " +
42                         "    } " +
43                         "  ], " +
44                         "  \"device\": { " +
45                         "    \"identifiers\": [ " +
46                         "      \"zigbee2mqtt_0x0000000000000000\" " +
47                         "    ], " +
48                         "    \"manufacturer\": \"Lights inc\", " +
49                         "    \"model\": \"light v1\", " +
50                         "    \"name\": \"Light\", " +
51                         "    \"sw_version\": \"Zigbee2MQTT 1.18.2\" " +
52                         "  }, " +
53                         "  \"name\": \"light\", " +
54                         "  \"state_topic\": \"zigbee2mqtt/light/state\", " +
55                         "  \"command_topic\": \"zigbee2mqtt/light/set/state\", " +
56                         "  \"state_value_template\": \"{{ value_json.power }}\", " +
57                         "  \"payload_on\": \"ON_\", " +
58                         "  \"payload_off\": \"OFF_\", " +
59                         "  \"rgb_state_topic\": \"zigbee2mqtt/light/rgb\", " +
60                         "  \"rgb_command_topic\": \"zigbee2mqtt/light/set/rgb\", " +
61                         "  \"rgb_value_template\": \"{{ value_json.rgb }}\", " +
62                         "  \"brightness_state_topic\": \"zigbee2mqtt/light/brightness\", " +
63                         "  \"brightness_command_topic\": \"zigbee2mqtt/light/set/brightness\", " +
64                         "  \"brightness_value_template\": \"{{ value_json.br }}\" " +
65                         "}");
66         // @formatter:on
67
68         assertThat(component.channels.size(), is(1));
69         assertThat(component.getName(), is("light"));
70
71         assertChannel(component, Light.COLOR_CHANNEL_ID, "zigbee2mqtt/light/rgb", "zigbee2mqtt/light/set/rgb", "light",
72                 ColorValue.class);
73
74         assertChannel(component.switchChannel, "zigbee2mqtt/light/state", "zigbee2mqtt/light/set/state", "light",
75                 ColorValue.class);
76         assertChannel(component.brightnessChannel, "zigbee2mqtt/light/brightness", "zigbee2mqtt/light/set/brightness",
77                 "light", ColorValue.class);
78
79         publishMessage("zigbee2mqtt/light/rgb", "{\"rgb\": \"255,255,255\"}");
80         assertState(component, Light.COLOR_CHANNEL_ID, HSBType.fromRGB(255, 255, 255));
81         publishMessage("zigbee2mqtt/light/rgb", "{\"rgb\": \"10,20,30\"}");
82         assertState(component, Light.COLOR_CHANNEL_ID, HSBType.fromRGB(10, 20, 30));
83
84         component.switchChannel.getState().publishValue(OnOffType.OFF);
85         assertPublished("zigbee2mqtt/light/set/state", "0,0,0");
86     }
87
88     protected Set<String> getConfigTopics() {
89         return Set.of(CONFIG_TOPIC);
90     }
91 }