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