2 * Copyright (c) 2010-2021 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.generic.internal.handler;
15 import static org.openhab.binding.mqtt.generic.internal.MqttBindingConstants.*;
17 import java.math.BigDecimal;
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.List;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.openhab.binding.mqtt.generic.internal.MqttBindingConstants;
25 import org.openhab.core.config.core.Configuration;
26 import org.openhab.core.thing.Channel;
27 import org.openhab.core.thing.ChannelUID;
28 import org.openhab.core.thing.ThingUID;
29 import org.openhab.core.thing.binding.builder.ChannelBuilder;
30 import org.openhab.core.thing.type.ChannelTypeUID;
33 * Static test definitions, like thing, bridge and channel definitions
35 * @author David Graeff - Initial contribution
38 public class ThingChannelConstants {
39 // Common ThingUID and ChannelUIDs
40 public static final ThingUID testGenericThing = new ThingUID(GENERIC_MQTT_THING, "genericthing");
42 public static final ChannelTypeUID textChannel = new ChannelTypeUID(BINDING_ID, MqttBindingConstants.STRING);
43 public static final ChannelTypeUID textWithJsonChannel = new ChannelTypeUID(BINDING_ID,
44 MqttBindingConstants.STRING);
45 public static final ChannelTypeUID onoffChannel = new ChannelTypeUID(BINDING_ID, MqttBindingConstants.SWITCH);
46 public static final ChannelTypeUID numberChannel = new ChannelTypeUID(BINDING_ID, MqttBindingConstants.NUMBER);
47 public static final ChannelTypeUID percentageChannel = new ChannelTypeUID(BINDING_ID, MqttBindingConstants.DIMMER);
48 public static final ChannelTypeUID unknownChannel = new ChannelTypeUID(BINDING_ID, "unknown");
50 public static final ChannelUID textChannelUID = new ChannelUID(testGenericThing, "mytext");
52 public static final String jsonPathJSON = "{ \"device\": { \"status\": { \"temperature\": 23.2 }}}";
53 public static final String jsonPathPattern = "$.device.status.temperature";
55 public static final List<Channel> thingChannelList = new ArrayList<>();
56 public static final List<Channel> thingChannelListWithJson = new ArrayList<>();
59 * Create a channel with exact the parameters we need for the tests
61 * @param id Channel ID
62 * @param acceptedType Accept type
63 * @param config The configuration
64 * @param channelTypeUID ChannelTypeUID provided by the static definitions
67 public static Channel cb(String id, String acceptedType, Configuration config, ChannelTypeUID channelTypeUID) {
68 return ChannelBuilder.create(new ChannelUID(testGenericThing, id), acceptedType).withConfiguration(config)
69 .withType(channelTypeUID).build();
73 thingChannelList.add(cb("mytext", "TextItemType", textConfiguration(), textChannel));
74 thingChannelList.add(cb("onoff", "OnOffType", onoffConfiguration(), onoffChannel));
75 thingChannelList.add(cb("num", "NumberType", numberConfiguration(), numberChannel));
76 thingChannelList.add(cb("percent", "NumberType", percentageConfiguration(), percentageChannel));
78 thingChannelListWithJson.add(cb("mytext", "TextItemType", textConfigurationWithJson(), textWithJsonChannel));
79 thingChannelListWithJson.add(cb("onoff", "OnOffType", onoffConfiguration(), onoffChannel));
80 thingChannelListWithJson.add(cb("num", "NumberType", numberConfiguration(), numberChannel));
81 thingChannelListWithJson.add(cb("percent", "NumberType", percentageConfiguration(), percentageChannel));
84 static Configuration textConfiguration() {
85 Map<String, Object> data = new HashMap<>();
86 data.put("stateTopic", "test/state");
87 data.put("commandTopic", "test/command");
88 return new Configuration(data);
91 static Configuration textConfigurationWithJson() {
92 Map<String, Object> data = new HashMap<>();
93 data.put("stateTopic", "test/state");
94 data.put("commandTopic", "test/command");
95 data.put("transformationPattern", "JSONPATH:" + jsonPathPattern);
96 return new Configuration(data);
99 private static Configuration numberConfiguration() {
100 Map<String, Object> data = new HashMap<>();
101 data.put("stateTopic", "test/state");
102 data.put("commandTopic", "test/command");
103 data.put("min", BigDecimal.valueOf(1));
104 data.put("max", BigDecimal.valueOf(99));
105 data.put("step", BigDecimal.valueOf(2));
106 data.put("isDecimal", true);
107 return new Configuration(data);
110 private static Configuration percentageConfiguration() {
111 Map<String, Object> data = new HashMap<>();
112 data.put("stateTopic", "test/state");
113 data.put("commandTopic", "test/command");
114 data.put("on", "ON");
115 data.put("off", "OFF");
116 return new Configuration(data);
119 private static Configuration onoffConfiguration() {
120 Map<String, Object> data = new HashMap<>();
121 data.put("stateTopic", "test/state");
122 data.put("commandTopic", "test/command");
123 data.put("on", "ON");
124 data.put("off", "OFF");
125 data.put("inverse", true);
126 return new Configuration(data);