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