]> git.basschouten.com Git - openhab-addons.git/blob
b195bb7d65d172dda87e8825bc993fc8564b7996
[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.config.dto;
14
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Objects;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.core.thing.Thing;
22 import org.openhab.core.util.UIDUtils;
23
24 import com.google.gson.Gson;
25 import com.google.gson.annotations.SerializedName;
26
27 /**
28  * Base class for home assistant configurations.
29  *
30  * @author Jochen Klein - Initial contribution
31  */
32 @NonNullByDefault
33 public abstract class AbstractChannelConfiguration {
34     public static final char PARENT_TOPIC_PLACEHOLDER = '~';
35
36     protected String name;
37
38     protected String icon = "";
39     protected int qos; // defaults to 0 according to HA specification
40     protected boolean retain; // defaults to false according to HA specification
41     @SerializedName("value_template")
42     protected @Nullable String valueTemplate;
43     @SerializedName("unique_id")
44     protected @Nullable String uniqueId;
45
46     @SerializedName("availability_mode")
47     protected AvailabilityMode availabilityMode = AvailabilityMode.LATEST;
48     @SerializedName("availability_topic")
49     protected @Nullable String availabilityTopic;
50     @SerializedName("payload_available")
51     protected String payloadAvailable = "online";
52     @SerializedName("payload_not_available")
53     protected String payloadNotAvailable = "offline";
54
55     /**
56      * A list of MQTT topics subscribed to receive availability (online/offline) updates. Must not be used together with
57      * availability_topic
58      */
59     protected @Nullable List<Availability> availability;
60
61     @SerializedName(value = "~")
62     protected String parentTopic = "";
63
64     protected @Nullable Device device;
65
66     /**
67      * Parse the base properties of the configJSON into a {@link AbstractChannelConfiguration}
68      *
69      * @param configJSON channels configuration in JSON
70      * @param gson parser
71      * @return configuration object
72      */
73     public static AbstractChannelConfiguration fromString(final String configJSON, final Gson gson) {
74         return fromString(configJSON, gson, Config.class);
75     }
76
77     protected AbstractChannelConfiguration(String defaultName) {
78         this.name = defaultName;
79     }
80
81     public String getThingName() {
82         String result = null;
83
84         if (this.device != null) {
85             result = this.device.name;
86         }
87         if (result == null) {
88             result = name;
89         }
90         return result;
91     }
92
93     public String getThingId(String defaultId) {
94         String result = null;
95         if (this.device != null) {
96             result = this.device.getId();
97         }
98         if (result == null) {
99             result = uniqueId;
100         }
101         return UIDUtils.encode(result != null ? result : defaultId);
102     }
103
104     public Map<String, Object> appendToProperties(Map<String, Object> properties) {
105         final Device d = device;
106         if (d == null) {
107             return properties;
108         }
109         final String manufacturer = d.manufacturer;
110         if (manufacturer != null) {
111             properties.put(Thing.PROPERTY_VENDOR, manufacturer);
112         }
113         final String model = d.model;
114         if (model != null) {
115             properties.put(Thing.PROPERTY_MODEL_ID, model);
116         }
117         final String swVersion = d.swVersion;
118         if (swVersion != null) {
119             properties.put(Thing.PROPERTY_FIRMWARE_VERSION, swVersion);
120         }
121         return properties;
122     }
123
124     public String getName() {
125         return name;
126     }
127
128     public String getIcon() {
129         return icon;
130     }
131
132     public int getQos() {
133         return qos;
134     }
135
136     public boolean isRetain() {
137         return retain;
138     }
139
140     @Nullable
141     public String getValueTemplate() {
142         return valueTemplate;
143     }
144
145     @Nullable
146     public String getUniqueId() {
147         return uniqueId;
148     }
149
150     @Nullable
151     public String getAvailabilityTopic() {
152         return availabilityTopic;
153     }
154
155     public String getPayloadAvailable() {
156         return payloadAvailable;
157     }
158
159     public String getPayloadNotAvailable() {
160         return payloadNotAvailable;
161     }
162
163     @Nullable
164     public Device getDevice() {
165         return device;
166     }
167
168     @Nullable
169     public List<Availability> getAvailability() {
170         return availability;
171     }
172
173     public String getParentTopic() {
174         return parentTopic;
175     }
176
177     public AvailabilityMode getAvailabilityMode() {
178         return availabilityMode;
179     }
180
181     /**
182      * This class is needed, to be able to parse only the common base attributes.
183      * Without this, {@link AbstractChannelConfiguration} cannot be instantiated, as it is abstract.
184      * This is needed during the discovery.
185      */
186     private static class Config extends AbstractChannelConfiguration {
187         public Config() {
188             super("private");
189         }
190     }
191
192     /**
193      * Parse the configJSON into a subclass of {@link AbstractChannelConfiguration}
194      *
195      * @param configJSON channels configuration in JSON
196      * @param gson parser
197      * @param clazz target configuration class
198      * @return configuration object
199      */
200     public static <C extends AbstractChannelConfiguration> C fromString(final String configJSON, final Gson gson,
201             final Class<C> clazz) {
202         return Objects.requireNonNull(gson.fromJson(configJSON, clazz));
203     }
204 }