2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mqtt.homeassistant.internal;
15 import java.util.List;
18 import org.apache.commons.lang.StringUtils;
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;
24 import com.google.gson.Gson;
25 import com.google.gson.annotations.JsonAdapter;
26 import com.google.gson.annotations.SerializedName;
29 * Base class for home assistant configurations.
31 * @author Jochen Klein - Initial contribution
34 public abstract class BaseChannelConfiguration {
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.
41 private static class Config extends BaseChannelConfiguration {
48 * Parse the configJSON into a subclass of {@link BaseChannelConfiguration}
53 * @return configuration object
55 public static <C extends BaseChannelConfiguration> C fromString(final String configJSON, final Gson gson,
56 final Class<C> clazz) {
57 return gson.fromJson(configJSON, clazz);
61 * Parse the base properties of the configJSON into a {@link BaseChannelConfiguration}
65 * @return configuration object
67 public static BaseChannelConfiguration fromString(final String configJSON, final Gson gson) {
68 return fromString(configJSON, gson, Config.class);
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;
79 protected @Nullable String availability_topic;
80 protected String payload_available = "online";
81 protected String payload_not_available = "offline";
83 @SerializedName(value = "~")
84 protected String tilde = "";
86 protected BaseChannelConfiguration(String defaultName) {
87 this.name = defaultName;
90 public @Nullable String expand(@Nullable String value) {
91 return value == null ? null : value.replaceAll("~", tilde);
94 protected @Nullable Device 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;
106 public String getId() {
107 return StringUtils.join(identifiers, "_");
111 @JsonAdapter(ConnectionDeserializer.class)
112 static class Connection {
113 protected @Nullable String type;
114 protected @Nullable String identifier;
117 public String getThingName() {
119 String result = null;
121 if (this.device != null) {
122 result = this.device.name;
124 if (result == null) {
130 public String getThingId(String defaultId) {
132 String result = null;
133 if (this.device != null) {
134 result = this.device.getId();
136 if (result == null) {
139 return UIDUtils.encode(result != null ? result : defaultId);
142 public Map<String, Object> appendToProperties(Map<String, Object> properties) {
143 final Device device_ = device;
144 if (device_ == null) {
147 final String manufacturer = device_.manufacturer;
148 if (manufacturer != null) {
149 properties.put(Thing.PROPERTY_VENDOR, manufacturer);
151 final String model = device_.model;
153 properties.put(Thing.PROPERTY_MODEL_ID, model);
155 final String sw_version = device_.sw_version;
156 if (sw_version != null) {
157 properties.put(Thing.PROPERTY_FIRMWARE_VERSION, sw_version);