added migrated 2.x add-ons
Signed-off-by: Kai Kreuzer <kai@openhab.org>
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2020 Contributors to the openHAB project
|
||||
*
|
||||
* See the NOTICE file(s) distributed with this work for additional
|
||||
* information.
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.openhab.binding.mqtt.homeassistant.internal;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNull;
|
||||
import org.junit.Test;
|
||||
import org.openhab.binding.mqtt.homeassistant.internal.BaseChannelConfiguration.Connection;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
/**
|
||||
* @author Jochen Klein - Initial contribution
|
||||
*/
|
||||
public class HAConfigurationTests {
|
||||
|
||||
private Gson gson = new GsonBuilder().registerTypeAdapterFactory(new ChannelConfigurationTypeAdapterFactory())
|
||||
.create();
|
||||
|
||||
private static String readTestJson(final String name) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
|
||||
try (BufferedReader in = new BufferedReader(
|
||||
new InputStreamReader(HAConfigurationTests.class.getResourceAsStream(name), "UTF-8"))) {
|
||||
String line;
|
||||
|
||||
while ((line = in.readLine()) != null) {
|
||||
result.append(line).append('\n');
|
||||
}
|
||||
return result.toString();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAbbreviations() {
|
||||
String json = readTestJson("configA.json");
|
||||
|
||||
BaseChannelConfiguration config = BaseChannelConfiguration.fromString(json, gson);
|
||||
|
||||
assertThat(config.name, is("A"));
|
||||
assertThat(config.icon, is("2"));
|
||||
assertThat(config.qos, is(1));
|
||||
assertThat(config.retain, is(true));
|
||||
assertThat(config.value_template, is("B"));
|
||||
assertThat(config.unique_id, is("C"));
|
||||
assertThat(config.availability_topic, is("D/E"));
|
||||
assertThat(config.payload_available, is("F"));
|
||||
assertThat(config.payload_not_available, is("G"));
|
||||
|
||||
assertThat(config.device, is(notNullValue()));
|
||||
|
||||
BaseChannelConfiguration.Device device = config.device;
|
||||
if (device != null) {
|
||||
assertThat(device.identifiers, contains("H"));
|
||||
assertThat(device.connections, is(notNullValue()));
|
||||
List<@NonNull Connection> connections = device.connections;
|
||||
if (connections != null) {
|
||||
assertThat(connections.get(0).type, is("I1"));
|
||||
assertThat(connections.get(0).identifier, is("I2"));
|
||||
}
|
||||
assertThat(device.name, is("J"));
|
||||
assertThat(device.model, is("K"));
|
||||
assertThat(device.sw_version, is("L"));
|
||||
assertThat(device.manufacturer, is("M"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTildeSubstritution() {
|
||||
String json = readTestJson("configB.json");
|
||||
|
||||
ComponentSwitch.ChannelConfiguration config = BaseChannelConfiguration.fromString(json, gson,
|
||||
ComponentSwitch.ChannelConfiguration.class);
|
||||
|
||||
assertThat(config.availability_topic, is("D/E"));
|
||||
assertThat(config.state_topic, is("O/D/"));
|
||||
assertThat(config.command_topic, is("P~Q"));
|
||||
assertThat(config.device, is(notNullValue()));
|
||||
|
||||
BaseChannelConfiguration.Device device = config.device;
|
||||
if (device != null) {
|
||||
assertThat(device.identifiers, contains("H"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSampleFanConfig() {
|
||||
String json = readTestJson("configFan.json");
|
||||
|
||||
ComponentFan.ChannelConfiguration config = BaseChannelConfiguration.fromString(json, gson,
|
||||
ComponentFan.ChannelConfiguration.class);
|
||||
assertThat(config.name, is("Bedroom Fan"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeviceListConfig() {
|
||||
String json = readTestJson("configDeviceList.json");
|
||||
|
||||
ComponentFan.ChannelConfiguration config = BaseChannelConfiguration.fromString(json, gson,
|
||||
ComponentFan.ChannelConfiguration.class);
|
||||
assertThat(config.device, is(notNullValue()));
|
||||
|
||||
BaseChannelConfiguration.Device device = config.device;
|
||||
if (device != null) {
|
||||
assertThat(device.identifiers, is(Arrays.asList("A", "B", "C")));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeviceSingleStringConfig() {
|
||||
String json = readTestJson("configDeviceSingleString.json");
|
||||
|
||||
ComponentFan.ChannelConfiguration config = BaseChannelConfiguration.fromString(json, gson,
|
||||
ComponentFan.ChannelConfiguration.class);
|
||||
assertThat(config.device, is(notNullValue()));
|
||||
|
||||
BaseChannelConfiguration.Device device = config.device;
|
||||
if (device != null) {
|
||||
assertThat(device.identifiers, is(Arrays.asList("A")));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2020 Contributors to the openHAB project
|
||||
*
|
||||
* See the NOTICE file(s) distributed with this work for additional
|
||||
* information.
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.openhab.binding.mqtt.homeassistant.internal;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.core.IsCollectionContaining.hasItem;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.openhab.core.config.core.Configuration;
|
||||
|
||||
/**
|
||||
* @author Jochen Klein - Initial contribution
|
||||
*/
|
||||
public class HaIDTests {
|
||||
|
||||
@Test
|
||||
public void testWithoutNode() {
|
||||
HaID subject = new HaID("homeassistant/switch/name/config");
|
||||
|
||||
assertThat(subject.objectID, is("name"));
|
||||
|
||||
assertThat(subject.component, is("switch"));
|
||||
assertThat(subject.getTopic("suffix"), is("homeassistant/switch/name/suffix"));
|
||||
|
||||
Configuration config = new Configuration();
|
||||
subject.toConfig(config);
|
||||
|
||||
HaID restore = HaID.fromConfig("homeassistant", config);
|
||||
|
||||
assertThat(restore, is(subject));
|
||||
|
||||
HandlerConfiguration haConfig = new HandlerConfiguration(subject.baseTopic,
|
||||
Collections.singletonList(subject.toShortTopic()));
|
||||
|
||||
Collection<HaID> restoreList = HaID.fromConfig(haConfig);
|
||||
assertThat(restoreList, hasItem(new HaID("homeassistant/switch/name/config")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithNode() {
|
||||
HaID subject = new HaID("homeassistant/switch/node/name/config");
|
||||
|
||||
assertThat(subject.objectID, is("name"));
|
||||
|
||||
assertThat(subject.component, is("switch"));
|
||||
assertThat(subject.getTopic("suffix"), is("homeassistant/switch/node/name/suffix"));
|
||||
|
||||
Configuration config = new Configuration();
|
||||
subject.toConfig(config);
|
||||
|
||||
HaID restore = HaID.fromConfig("homeassistant", config);
|
||||
|
||||
assertThat(restore, is(subject));
|
||||
|
||||
HandlerConfiguration haConfig = new HandlerConfiguration(subject.baseTopic,
|
||||
Collections.singletonList(subject.toShortTopic()));
|
||||
|
||||
Collection<HaID> restoreList = HaID.fromConfig(haConfig);
|
||||
assertThat(restoreList, hasItem(new HaID("homeassistant/switch/node/name/config")));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2020 Contributors to the openHAB project
|
||||
*
|
||||
* See the NOTICE file(s) distributed with this work for additional
|
||||
* information.
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.openhab.binding.mqtt.homeassistant.internal.handler;
|
||||
|
||||
import static org.openhab.binding.mqtt.homeassistant.generic.internal.MqttBindingConstants.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.openhab.core.config.core.Configuration;
|
||||
import org.openhab.core.thing.Channel;
|
||||
import org.openhab.core.thing.ThingUID;
|
||||
import org.openhab.core.thing.type.ChannelTypeUID;
|
||||
|
||||
/**
|
||||
* Static test definitions, like thing, bridge and channel definitions
|
||||
*
|
||||
* @author David Graeff - Initial contribution
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public class ThingChannelConstants {
|
||||
// Common ThingUID and ChannelUIDs
|
||||
public static final ThingUID testHomeAssistantThing = new ThingUID(HOMEASSISTANT_MQTT_THING, "device234");
|
||||
|
||||
public static final ChannelTypeUID unknownChannel = new ChannelTypeUID(BINDING_ID, "unknown");
|
||||
|
||||
public static final String jsonPathJSON = "{ \"device\": { \"status\": { \"temperature\": 23.2 }}}";
|
||||
public static final String jsonPathPattern = "$.device.status.temperature";
|
||||
|
||||
public static final List<Channel> thingChannelList = new ArrayList<>();
|
||||
public static final List<Channel> thingChannelListWithJson = new ArrayList<>();
|
||||
|
||||
static Configuration textConfiguration() {
|
||||
Map<String, Object> data = new HashMap<>();
|
||||
data.put("stateTopic", "test/state");
|
||||
data.put("commandTopic", "test/command");
|
||||
return new Configuration(data);
|
||||
}
|
||||
|
||||
static Configuration textConfigurationWithJson() {
|
||||
Map<String, Object> data = new HashMap<>();
|
||||
data.put("stateTopic", "test/state");
|
||||
data.put("commandTopic", "test/command");
|
||||
data.put("transformationPattern", "JSONPATH:" + jsonPathPattern);
|
||||
return new Configuration(data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"name": "A",
|
||||
"icon": "2",
|
||||
"qos": 1,
|
||||
"retain": true,
|
||||
"val_tpl": "B",
|
||||
"uniq_id": "C",
|
||||
"avty_t": "~E",
|
||||
"pl_avail": "F",
|
||||
"pl_not_avail": "G",
|
||||
"device": {
|
||||
"ids": [
|
||||
"H"
|
||||
],
|
||||
"cns": [
|
||||
[
|
||||
"I1",
|
||||
"I2"
|
||||
]
|
||||
],
|
||||
"name": "J",
|
||||
"mdl": "K",
|
||||
"sw": "L",
|
||||
"mf": "M"
|
||||
},
|
||||
"~": "D/"
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"name": "A",
|
||||
"icon": "2",
|
||||
"qos": 1,
|
||||
"retain": true,
|
||||
"val_tpl": "B",
|
||||
"uniq_id": "C",
|
||||
"avty_t": "~E",
|
||||
"pl_avail": "F",
|
||||
"pl_not_avail": "G",
|
||||
"optimistic": true,
|
||||
"state_topic": "O/~",
|
||||
"command_topic": "P~Q",
|
||||
"device": {
|
||||
"ids": "H",
|
||||
"cns": [
|
||||
[
|
||||
"I1",
|
||||
"I2"
|
||||
]
|
||||
],
|
||||
"name": "J",
|
||||
"mdl": "K",
|
||||
"sw": "L",
|
||||
"mf": "M"
|
||||
},
|
||||
"~": "D/"
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"device": {
|
||||
"ids": [
|
||||
"A",
|
||||
"B",
|
||||
"C"
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"device": {
|
||||
"ids": "A"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"name": "Bedroom Fan",
|
||||
"state_topic": "bedroom_fan/on/state",
|
||||
"command_topic": "bedroom_fan/on/set",
|
||||
"oscillation_state_topic": "bedroom_fan/oscillation/state",
|
||||
"oscillation_command_topic": "bedroom_fan/oscillation/set",
|
||||
"speed_state_topic": "bedroom_fan/speed/state",
|
||||
"speed_command_topic": "bedroom_fan/speed/set",
|
||||
"qos": 0,
|
||||
"payload_on": "true",
|
||||
"payload_off": "false",
|
||||
"payload_oscillation_on": "true",
|
||||
"payload_oscillation_off": "false",
|
||||
"payload_low_speed": "low",
|
||||
"payload_medium_speed": "medium",
|
||||
"payload_high_speed": "high",
|
||||
"speeds": [
|
||||
"low",
|
||||
"medium",
|
||||
"high"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user