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