]> git.basschouten.com Git - openhab-addons.git/blob
0b8353b9e7e76541730fc1dbf24adf3c7cd5832c
[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.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
18
19 import java.io.BufferedReader;
20 import java.io.IOException;
21 import java.io.InputStreamReader;
22 import java.util.Arrays;
23 import java.util.List;
24
25 import org.eclipse.jdt.annotation.NonNull;
26 import org.junit.jupiter.api.Test;
27 import org.openhab.binding.mqtt.homeassistant.internal.config.ChannelConfigurationTypeAdapterFactory;
28 import org.openhab.binding.mqtt.homeassistant.internal.config.dto.AbstractChannelConfiguration;
29 import org.openhab.binding.mqtt.homeassistant.internal.config.dto.Connection;
30 import org.openhab.binding.mqtt.homeassistant.internal.config.dto.Device;
31
32 import com.google.gson.Gson;
33 import com.google.gson.GsonBuilder;
34
35 /**
36  * @author Jochen Klein - Initial contribution
37  */
38 public class HAConfigurationTests {
39
40     private Gson gson = new GsonBuilder().registerTypeAdapterFactory(new ChannelConfigurationTypeAdapterFactory())
41             .create();
42
43     private static String readTestJson(final String name) {
44         StringBuilder result = new StringBuilder();
45
46         try (BufferedReader in = new BufferedReader(
47                 new InputStreamReader(HAConfigurationTests.class.getResourceAsStream(name), "UTF-8"))) {
48             String line;
49
50             while ((line = in.readLine()) != null) {
51                 result.append(line).append('\n');
52             }
53             return result.toString();
54         } catch (IOException e) {
55             throw new RuntimeException(e);
56         }
57     }
58
59     @Test
60     public void testAbbreviations() {
61         String json = readTestJson("configA.json");
62
63         AbstractChannelConfiguration config = AbstractChannelConfiguration.fromString(json, gson);
64
65         assertThat(config.getName(), is("A"));
66         assertThat(config.getIcon(), is("2"));
67         assertThat(config.getQos(), is(1));
68         assertThat(config.isRetain(), is(true));
69         assertThat(config.getValueTemplate(), is("B"));
70         assertThat(config.getUniqueId(), is("C"));
71         assertThat(config.getAvailabilityTopic(), is("D/E"));
72         assertThat(config.getPayloadAvailable(), is("F"));
73         assertThat(config.getPayloadNotAvailable(), is("G"));
74
75         assertThat(config.getDevice(), is(notNullValue()));
76
77         Device device = config.getDevice();
78         if (device != null) {
79             assertThat(device.getIdentifiers(), contains("H"));
80             assertThat(device.getConnections(), is(notNullValue()));
81             List<@NonNull Connection> connections = device.getConnections();
82             if (connections != null) {
83                 assertThat(connections.get(0).getType(), is("I1"));
84                 assertThat(connections.get(0).getIdentifier(), is("I2"));
85             }
86             assertThat(device.getName(), is("J"));
87             assertThat(device.getModel(), is("K"));
88             assertThat(device.getSwVersion(), is("L"));
89             assertThat(device.getManufacturer(), is("M"));
90         }
91     }
92
93     @Test
94     public void testTildeSubstritution() {
95         String json = readTestJson("configB.json");
96
97         Switch.ChannelConfiguration config = AbstractChannelConfiguration.fromString(json, gson,
98                 Switch.ChannelConfiguration.class);
99
100         assertThat(config.getAvailabilityTopic(), is("D/E"));
101         assertThat(config.stateTopic, is("O/D/"));
102         assertThat(config.commandTopic, is("P~Q"));
103         assertThat(config.getDevice(), is(notNullValue()));
104
105         Device device = config.getDevice();
106         if (device != null) {
107             assertThat(device.getIdentifiers(), contains("H"));
108         }
109     }
110
111     @Test
112     public void testSampleFanConfig() {
113         String json = readTestJson("configFan.json");
114
115         Fan.ChannelConfiguration config = AbstractChannelConfiguration.fromString(json, gson,
116                 Fan.ChannelConfiguration.class);
117         assertThat(config.getName(), is("Bedroom Fan"));
118     }
119
120     @Test
121     public void testDeviceListConfig() {
122         String json = readTestJson("configDeviceList.json");
123
124         Fan.ChannelConfiguration config = AbstractChannelConfiguration.fromString(json, gson,
125                 Fan.ChannelConfiguration.class);
126         assertThat(config.getDevice(), is(notNullValue()));
127
128         Device device = config.getDevice();
129         if (device != null) {
130             assertThat(device.getIdentifiers(), is(Arrays.asList("A", "B", "C")));
131         }
132     }
133
134     @Test
135     public void testDeviceSingleStringConfig() {
136         String json = readTestJson("configDeviceSingleString.json");
137
138         Fan.ChannelConfiguration config = AbstractChannelConfiguration.fromString(json, gson,
139                 Fan.ChannelConfiguration.class);
140         assertThat(config.getDevice(), is(notNullValue()));
141
142         Device device = config.getDevice();
143         if (device != null) {
144             assertThat(device.getIdentifiers(), is(Arrays.asList("A")));
145         }
146     }
147
148     @Test
149     public void testTS0601ClimateConfig() {
150         String json = readTestJson("configTS0601ClimateThermostat.json");
151         Climate.ChannelConfiguration config = AbstractChannelConfiguration.fromString(json, gson,
152                 Climate.ChannelConfiguration.class);
153         assertThat(config.getDevice(), is(notNullValue()));
154         assertThat(config.getDevice().getIdentifiers(), is(notNullValue()));
155         assertThat(config.getDevice().getIdentifiers().get(0), is("zigbee2mqtt_0x847127fffe11dd6a"));
156         assertThat(config.getDevice().getManufacturer(), is("TuYa"));
157         assertThat(config.getDevice().getModel(), is("Radiator valve with thermostat (TS0601_thermostat)"));
158         assertThat(config.getDevice().getName(), is("th1"));
159         assertThat(config.getDevice().getSwVersion(), is("Zigbee2MQTT 1.18.2"));
160
161         assertThat(config.actionTemplate, is(
162                 "{% set values = {'idle':'off','heat':'heating','cool':'cooling','fan only':'fan'} %}{{ values[value_json.running_state] }}"));
163         assertThat(config.actionTopic, is("zigbee2mqtt/th1"));
164         assertThat(config.awayModeCommandTopic, is("zigbee2mqtt/th1/set/away_mode"));
165         assertThat(config.awayModeStateTemplate, is("{{ value_json.away_mode }}"));
166         assertThat(config.awayModeStateTopic, is("zigbee2mqtt/th1"));
167         assertThat(config.currentTemperatureTemplate, is("{{ value_json.local_temperature }}"));
168         assertThat(config.currentTemperatureTopic, is("zigbee2mqtt/th1"));
169         assertThat(config.holdCommandTopic, is("zigbee2mqtt/th1/set/preset"));
170         assertThat(config.holdModes, is(List.of("schedule", "manual", "boost", "complex", "comfort", "eco")));
171         assertThat(config.holdStateTemplate, is("{{ value_json.preset }}"));
172         assertThat(config.holdStateTopic, is("zigbee2mqtt/th1"));
173         assertThat(config.jsonAttributesTopic, is("zigbee2mqtt/th1"));
174         assertThat(config.maxTemp, is(35f));
175         assertThat(config.minTemp, is(5f));
176         assertThat(config.modeCommandTopic, is("zigbee2mqtt/th1/set/system_mode"));
177         assertThat(config.modeStateTemplate, is("{{ value_json.system_mode }}"));
178         assertThat(config.modeStateTopic, is("zigbee2mqtt/th1"));
179         assertThat(config.modes, is(List.of("heat", "auto", "off")));
180         assertThat(config.getName(), is("th1"));
181         assertThat(config.tempStep, is(0.5f));
182         assertThat(config.temperatureCommandTopic, is("zigbee2mqtt/th1/set/current_heating_setpoint"));
183         assertThat(config.temperatureStateTemplate, is("{{ value_json.current_heating_setpoint }}"));
184         assertThat(config.temperatureStateTopic, is("zigbee2mqtt/th1"));
185         assertThat(config.temperatureUnit, is("C"));
186         assertThat(config.getUniqueId(), is("0x847127fffe11dd6a_climate_zigbee2mqtt"));
187
188         assertThat(config.initial, is(21));
189         assertThat(config.sendIfOff, is(true));
190     }
191
192     @Test
193     public void testClimateConfig() {
194         String json = readTestJson("configClimate.json");
195         Climate.ChannelConfiguration config = AbstractChannelConfiguration.fromString(json, gson,
196                 Climate.ChannelConfiguration.class);
197         assertThat(config.actionTemplate, is("a"));
198         assertThat(config.actionTopic, is("b"));
199         assertThat(config.auxCommandTopic, is("c"));
200         assertThat(config.auxStateTemplate, is("d"));
201         assertThat(config.auxStateTopic, is("e"));
202         assertThat(config.awayModeCommandTopic, is("f"));
203         assertThat(config.awayModeStateTemplate, is("g"));
204         assertThat(config.awayModeStateTopic, is("h"));
205         assertThat(config.currentTemperatureTemplate, is("i"));
206         assertThat(config.currentTemperatureTopic, is("j"));
207         assertThat(config.fanModeCommandTemplate, is("k"));
208         assertThat(config.fanModeCommandTopic, is("l"));
209         assertThat(config.fanModeStateTemplate, is("m"));
210         assertThat(config.fanModeStateTopic, is("n"));
211         assertThat(config.fanModes, is(List.of("p1", "p2")));
212         assertThat(config.holdCommandTemplate, is("q"));
213         assertThat(config.holdCommandTopic, is("r"));
214         assertThat(config.holdStateTemplate, is("s"));
215         assertThat(config.holdStateTopic, is("t"));
216         assertThat(config.holdModes, is(List.of("u1", "u2", "u3")));
217         assertThat(config.jsonAttributesTemplate, is("v"));
218         assertThat(config.jsonAttributesTopic, is("w"));
219         assertThat(config.modeCommandTemplate, is("x"));
220         assertThat(config.modeCommandTopic, is("y"));
221         assertThat(config.modeStateTemplate, is("z"));
222         assertThat(config.modeStateTopic, is("A"));
223         assertThat(config.modes, is(List.of("B1", "B2")));
224         assertThat(config.swingCommandTemplate, is("C"));
225         assertThat(config.swingCommandTopic, is("D"));
226         assertThat(config.swingStateTemplate, is("E"));
227         assertThat(config.swingStateTopic, is("F"));
228         assertThat(config.swingModes, is(List.of("G1")));
229         assertThat(config.temperatureCommandTemplate, is("H"));
230         assertThat(config.temperatureCommandTopic, is("I"));
231         assertThat(config.temperatureStateTemplate, is("J"));
232         assertThat(config.temperatureStateTopic, is("K"));
233         assertThat(config.temperatureHighCommandTemplate, is("L"));
234         assertThat(config.temperatureHighCommandTopic, is("N"));
235         assertThat(config.temperatureHighStateTemplate, is("O"));
236         assertThat(config.temperatureHighStateTopic, is("P"));
237         assertThat(config.temperatureLowCommandTemplate, is("Q"));
238         assertThat(config.temperatureLowCommandTopic, is("R"));
239         assertThat(config.temperatureLowStateTemplate, is("S"));
240         assertThat(config.temperatureLowStateTopic, is("T"));
241         assertThat(config.powerCommandTopic, is("U"));
242         assertThat(config.initial, is(10));
243         assertThat(config.maxTemp, is(40f));
244         assertThat(config.minTemp, is(0f));
245         assertThat(config.temperatureUnit, is("F"));
246         assertThat(config.tempStep, is(1f));
247         assertThat(config.precision, is(0.5f));
248         assertThat(config.sendIfOff, is(false));
249     }
250 }