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