]> git.basschouten.com Git - openhab-addons.git/blob
b8d35f435015ca64c7208e836406ede529b98a01
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.generic.internal.handler;
14
15 import static org.openhab.binding.mqtt.generic.internal.MqttBindingConstants.*;
16
17 import java.math.BigDecimal;
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
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;
31
32 /**
33  * Static test definitions, like thing, bridge and channel definitions
34  *
35  * @author David Graeff - Initial contribution
36  */
37 @NonNullByDefault
38 public class ThingChannelConstants {
39     // Common ThingUID and ChannelUIDs
40     public static final ThingUID TEST_GENERIC_THING = new ThingUID(GENERIC_MQTT_THING, "genericthing");
41
42     public static final ChannelTypeUID TEXT_CHANNEL = new ChannelTypeUID(BINDING_ID, MqttBindingConstants.STRING);
43     public static final ChannelTypeUID TEXT_WITH_JSON_CHANNEL = new ChannelTypeUID(BINDING_ID,
44             MqttBindingConstants.STRING);
45     public static final ChannelTypeUID ON_OFF_CHANNEL = new ChannelTypeUID(BINDING_ID, MqttBindingConstants.SWITCH);
46     public static final ChannelTypeUID NUMBER_CHANNEL = new ChannelTypeUID(BINDING_ID, MqttBindingConstants.NUMBER);
47     public static final ChannelTypeUID PERCENTAGE_CHANNEL = new ChannelTypeUID(BINDING_ID, MqttBindingConstants.DIMMER);
48     public static final ChannelTypeUID UNKNOWN_CHANNEL = new ChannelTypeUID(BINDING_ID, "unknown");
49
50     public static final ChannelUID TEXT_CHANNEL_UID = new ChannelUID(TEST_GENERIC_THING, "mytext");
51
52     public static final String JSON_PATH_JSON = "{ \"device\": { \"status\": { \"temperature\": 23.2 }}}";
53     public static final String JSON_PATH_PATTERN = "$.device.status.temperature";
54
55     public static final List<Channel> THING_CHANNEL_LIST = new ArrayList<>();
56     public static final List<Channel> THING_CHANNEL_LIST_WITH_JSON = new ArrayList<>();
57
58     /**
59      * Create a channel with exact the parameters we need for the tests
60      *
61      * @param id Channel ID
62      * @param acceptedType Accept type
63      * @param config The configuration
64      * @param channelTypeUID ChannelTypeUID provided by the static definitions
65      * @return
66      */
67     public static Channel cb(String id, String acceptedType, Configuration config, ChannelTypeUID channelTypeUID) {
68         return ChannelBuilder.create(new ChannelUID(TEST_GENERIC_THING, id), acceptedType).withConfiguration(config)
69                 .withType(channelTypeUID).build();
70     }
71
72     static {
73         THING_CHANNEL_LIST.add(cb("mytext", "String", textConfiguration(), TEXT_CHANNEL));
74         THING_CHANNEL_LIST.add(cb("onoff", "Switch", onoffConfiguration(), ON_OFF_CHANNEL));
75         THING_CHANNEL_LIST.add(cb("num", "Number", numberConfiguration(), NUMBER_CHANNEL));
76         THING_CHANNEL_LIST.add(cb("percent", "Number:Dimensionless", percentageConfiguration(), PERCENTAGE_CHANNEL));
77
78         THING_CHANNEL_LIST_WITH_JSON.add(cb("mytext", "String", textConfigurationWithJson(), TEXT_WITH_JSON_CHANNEL));
79         THING_CHANNEL_LIST_WITH_JSON.add(cb("onoff", "Switch", onoffConfiguration(), ON_OFF_CHANNEL));
80         THING_CHANNEL_LIST_WITH_JSON.add(cb("num", "Number", numberConfiguration(), NUMBER_CHANNEL));
81         THING_CHANNEL_LIST_WITH_JSON
82                 .add(cb("percent", "Number:Dimensionless", percentageConfiguration(), PERCENTAGE_CHANNEL));
83     }
84
85     static Configuration textConfiguration() {
86         Map<String, Object> data = new HashMap<>();
87         data.put("stateTopic", "test/state");
88         data.put("commandTopic", "test/command");
89         return new Configuration(data);
90     }
91
92     static Configuration textConfigurationWithJson() {
93         Map<String, Object> data = new HashMap<>();
94         data.put("stateTopic", "test/state");
95         data.put("commandTopic", "test/command");
96         data.put("transformationPattern", "JSONPATH:" + JSON_PATH_PATTERN);
97         return new Configuration(data);
98     }
99
100     private static Configuration numberConfiguration() {
101         Map<String, Object> data = new HashMap<>();
102         data.put("stateTopic", "test/state");
103         data.put("commandTopic", "test/command");
104         data.put("min", BigDecimal.valueOf(1));
105         data.put("max", BigDecimal.valueOf(99));
106         data.put("step", BigDecimal.valueOf(2));
107         data.put("isDecimal", true);
108         return new Configuration(data);
109     }
110
111     private static Configuration percentageConfiguration() {
112         Map<String, Object> data = new HashMap<>();
113         data.put("stateTopic", "test/state");
114         data.put("commandTopic", "test/command");
115         data.put("on", "ON");
116         data.put("off", "OFF");
117         return new Configuration(data);
118     }
119
120     private static Configuration onoffConfiguration() {
121         Map<String, Object> data = new HashMap<>();
122         data.put("stateTopic", "test/state");
123         data.put("commandTopic", "test/command");
124         data.put("on", "ON");
125         data.put("off", "OFF");
126         data.put("inverse", true);
127         return new Configuration(data);
128     }
129 }