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