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