]> git.basschouten.com Git - openhab-addons.git/blob
00db2dc330a4b0a43f0ba7e59441d81908480a87
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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;
14
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Objects;
18
19 import org.apache.commons.lang.StringUtils;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.core.thing.Thing;
23 import org.openhab.core.util.UIDUtils;
24
25 import com.google.gson.Gson;
26 import com.google.gson.annotations.JsonAdapter;
27 import com.google.gson.annotations.SerializedName;
28
29 /**
30  * Base class for home assistant configurations.
31  *
32  * @author Jochen Klein - Initial contribution
33  */
34 @NonNullByDefault
35 public abstract class BaseChannelConfiguration {
36
37     /**
38      * This class is needed, to be able to parse only the common base attributes.
39      * Without this, {@link BaseChannelConfiguration} cannot be instantiated, as it is abstract.
40      * This is needed during the discovery.
41      */
42     private static class Config extends BaseChannelConfiguration {
43         public Config() {
44             super("private");
45         }
46     }
47
48     /**
49      * Parse the configJSON into a subclass of {@link BaseChannelConfiguration}
50      *
51      * @param configJSON
52      * @param gson
53      * @param clazz
54      * @return configuration object
55      */
56     public static <C extends BaseChannelConfiguration> C fromString(final String configJSON, final Gson gson,
57             final Class<C> clazz) {
58         return Objects.requireNonNull(gson.fromJson(configJSON, clazz));
59     }
60
61     /**
62      * Parse the base properties of the configJSON into a {@link BaseChannelConfiguration}
63      *
64      * @param configJSON
65      * @param gson
66      * @return configuration object
67      */
68     public static BaseChannelConfiguration fromString(final String configJSON, final Gson gson) {
69         return fromString(configJSON, gson, Config.class);
70     }
71
72     public String name;
73
74     protected String icon = "";
75     protected int qos; // defaults to 0 according to HA specification
76     protected boolean retain; // defaults to false according to HA specification
77     protected @Nullable String value_template;
78     protected @Nullable String unique_id;
79
80     protected @Nullable String availability_topic;
81     protected String payload_available = "online";
82     protected String payload_not_available = "offline";
83
84     @SerializedName(value = "~")
85     protected String tilde = "";
86
87     protected BaseChannelConfiguration(String defaultName) {
88         this.name = defaultName;
89     }
90
91     public @Nullable String expand(@Nullable String value) {
92         return value == null ? null : value.replaceAll("~", tilde);
93     }
94
95     protected @Nullable Device device;
96
97     static class Device {
98         @JsonAdapter(ListOrStringDeserializer.class)
99         protected @Nullable List<String> identifiers;
100         protected @Nullable List<Connection> connections;
101         protected @Nullable String manufacturer;
102         protected @Nullable String model;
103         protected @Nullable String name;
104         protected @Nullable String sw_version;
105
106         @Nullable
107         public String getId() {
108             return StringUtils.join(identifiers, "_");
109         }
110     }
111
112     @JsonAdapter(ConnectionDeserializer.class)
113     static class Connection {
114         protected @Nullable String type;
115         protected @Nullable String identifier;
116     }
117
118     public String getThingName() {
119         @Nullable
120         String result = null;
121
122         if (this.device != null) {
123             result = this.device.name;
124         }
125         if (result == null) {
126             result = name;
127         }
128         return result;
129     }
130
131     public String getThingId(String defaultId) {
132         @Nullable
133         String result = null;
134         if (this.device != null) {
135             result = this.device.getId();
136         }
137         if (result == null) {
138             result = unique_id;
139         }
140         return UIDUtils.encode(result != null ? result : defaultId);
141     }
142
143     public Map<String, Object> appendToProperties(Map<String, Object> properties) {
144         final Device device_ = device;
145         if (device_ == null) {
146             return properties;
147         }
148         final String manufacturer = device_.manufacturer;
149         if (manufacturer != null) {
150             properties.put(Thing.PROPERTY_VENDOR, manufacturer);
151         }
152         final String model = device_.model;
153         if (model != null) {
154             properties.put(Thing.PROPERTY_MODEL_ID, model);
155         }
156         final String sw_version = device_.sw_version;
157         if (sw_version != null) {
158             properties.put(Thing.PROPERTY_FIRMWARE_VERSION, sw_version);
159         }
160         return properties;
161     }
162 }