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;
18 import java.math.BigDecimal;
19 import java.math.MathContext;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.junit.jupiter.api.Test;
24 import org.openhab.binding.mqtt.generic.values.ColorValue;
25 import org.openhab.binding.mqtt.generic.values.OnOffValue;
26 import org.openhab.binding.mqtt.generic.values.PercentageValue;
27 import org.openhab.core.library.types.HSBType;
28 import org.openhab.core.library.types.OnOffType;
29 import org.openhab.core.library.types.PercentType;
32 * Tests for {@link Light} conforming to the JSON schema
34 * @author Cody Cutrer - Initial contribution
37 public class JSONSchemaLightTests extends AbstractComponentTests {
38 public static final String CONFIG_TOPIC = "light/0x0000000000000000_light_zigbee2mqtt";
41 public void testRgb() throws InterruptedException {
43 var component = (Light) discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
48 "topic": "zigbee2mqtt/bridge/state" \
53 "zigbee2mqtt_0x0000000000000000" \
55 "manufacturer": "Lights inc", \
56 "model": "light v1", \
58 "sw_version": "Zigbee2MQTT 1.18.2" \
62 "state_topic": "zigbee2mqtt/light/state", \
63 "command_topic": "zigbee2mqtt/light/set/state", \
66 "supported_color_modes": ["onoff", "brightness", "rgb"]\
71 assertThat(component.channels.size(), is(1));
72 assertThat(component.getName(), is("light"));
74 assertChannel(component, Light.COLOR_CHANNEL_ID, "", "dummy", "Color", ColorValue.class);
76 publishMessage("zigbee2mqtt/light/state", "{ \"state\": \"ON\" }");
77 assertState(component, Light.COLOR_CHANNEL_ID, HSBType.WHITE);
78 publishMessage("zigbee2mqtt/light/state", "{ \"color\": {\"r\": 10, \"g\": 20, \"b\": 30 } }");
79 assertState(component, Light.COLOR_CHANNEL_ID, HSBType.fromRGB(10, 20, 30));
80 publishMessage("zigbee2mqtt/light/state", "{ \"brightness\": 255 }");
81 assertState(component, Light.COLOR_CHANNEL_ID, new HSBType("210,67,100"));
83 sendCommand(component, Light.COLOR_CHANNEL_ID, HSBType.BLUE);
84 assertPublished("zigbee2mqtt/light/set/state",
85 "{\"state\":\"ON\",\"brightness\":255,\"color\":{\"r\":0,\"g\":0,\"b\":255}}");
87 // OnOff commands should route to the correct topic
88 sendCommand(component, Light.COLOR_CHANNEL_ID, OnOffType.OFF);
89 assertPublished("zigbee2mqtt/light/set/state", "{\"state\":\"OFF\"}");
91 sendCommand(component, Light.COLOR_CHANNEL_ID, OnOffType.ON);
92 assertPublished("zigbee2mqtt/light/set/state", "{\"state\":\"ON\"}");
94 sendCommand(component, Light.COLOR_CHANNEL_ID, new PercentType(50));
95 assertPublished("zigbee2mqtt/light/set/state", "{\"state\":\"ON\",\"brightness\":127}");
99 public void testBrightnessAndOnOff() throws InterruptedException {
101 var component = (Light) discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
106 "state_topic": "zigbee2mqtt/light/state", \
107 "command_topic": "zigbee2mqtt/light/set/state", \
113 assertThat(component.channels.size(), is(1));
114 assertThat(component.getName(), is("light"));
116 assertChannel(component, Light.BRIGHTNESS_CHANNEL_ID, "", "dummy", "Brightness", PercentageValue.class);
118 publishMessage("zigbee2mqtt/light/state", "{ \"state\": \"ON\", \"brightness\": 128 }");
119 assertState(component, Light.BRIGHTNESS_CHANNEL_ID,
120 new PercentType(new BigDecimal(128 * 100).divide(new BigDecimal(255), MathContext.DECIMAL128)));
122 publishMessage("zigbee2mqtt/light/state", "{ \"state\": \"OFF\", \"brightness\": 128 }");
123 assertState(component, Light.BRIGHTNESS_CHANNEL_ID, PercentType.ZERO);
125 sendCommand(component, Light.BRIGHTNESS_CHANNEL_ID, PercentType.HUNDRED);
126 assertPublished("zigbee2mqtt/light/set/state", "{\"state\":\"ON\",\"brightness\":255}");
128 sendCommand(component, Light.BRIGHTNESS_CHANNEL_ID, OnOffType.OFF);
129 assertPublished("zigbee2mqtt/light/set/state", "{\"state\":\"OFF\"}");
133 public void testOnOffOnly() throws InterruptedException {
135 var component = (Light) discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
140 "state_topic": "zigbee2mqtt/light/state", \
141 "command_topic": "zigbee2mqtt/light/set/state"\
146 assertThat(component.channels.size(), is(1));
147 assertThat(component.getName(), is("light"));
149 assertChannel(component, Light.ON_OFF_CHANNEL_ID, "", "dummy", "On/Off State", OnOffValue.class);
151 publishMessage("zigbee2mqtt/light/state", "{ \"state\": \"ON\" }");
152 assertState(component, Light.ON_OFF_CHANNEL_ID, OnOffType.ON);
153 publishMessage("zigbee2mqtt/light/state", "{ \"state\": \"OFF\" }");
154 assertState(component, Light.ON_OFF_CHANNEL_ID, OnOffType.OFF);
156 sendCommand(component, Light.ON_OFF_CHANNEL_ID, OnOffType.OFF);
157 assertPublished("zigbee2mqtt/light/set/state", "{\"state\":\"OFF\"}");
158 sendCommand(component, Light.ON_OFF_CHANNEL_ID, OnOffType.ON);
159 assertPublished("zigbee2mqtt/light/set/state", "{\"state\":\"ON\"}");
163 protected Set<String> getConfigTopics() {
164 return Set.of(CONFIG_TOPIC);