2 * Copyright (c) 2010-2021 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;
17 import java.util.Objects;
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;
25 import com.google.gson.Gson;
26 import com.google.gson.annotations.JsonAdapter;
27 import com.google.gson.annotations.SerializedName;
30 * Base class for home assistant configurations.
32 * @author Jochen Klein - Initial contribution
35 public abstract class BaseChannelConfiguration {
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.
42 private static class Config extends BaseChannelConfiguration {
49 * Parse the configJSON into a subclass of {@link BaseChannelConfiguration}
54 * @return configuration object
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));
62 * Parse the base properties of the configJSON into a {@link BaseChannelConfiguration}
66 * @return configuration object
68 public static BaseChannelConfiguration fromString(final String configJSON, final Gson gson) {
69 return fromString(configJSON, gson, Config.class);
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;
80 protected @Nullable String availability_topic;
81 protected String payload_available = "online";
82 protected String payload_not_available = "offline";
84 @SerializedName(value = "~")
85 protected String tilde = "";
87 protected BaseChannelConfiguration(String defaultName) {
88 this.name = defaultName;
91 public @Nullable String expand(@Nullable String value) {
92 return value == null ? null : value.replaceAll("~", tilde);
95 protected @Nullable Device 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;
107 public String getId() {
108 return StringUtils.join(identifiers, "_");
112 @JsonAdapter(ConnectionDeserializer.class)
113 static class Connection {
114 protected @Nullable String type;
115 protected @Nullable String identifier;
118 public String getThingName() {
120 String result = null;
122 if (this.device != null) {
123 result = this.device.name;
125 if (result == null) {
131 public String getThingId(String defaultId) {
133 String result = null;
134 if (this.device != null) {
135 result = this.device.getId();
137 if (result == null) {
140 return UIDUtils.encode(result != null ? result : defaultId);
143 public Map<String, Object> appendToProperties(Map<String, Object> properties) {
144 final Device device_ = device;
145 if (device_ == null) {
148 final String manufacturer = device_.manufacturer;
149 if (manufacturer != null) {
150 properties.put(Thing.PROPERTY_VENDOR, manufacturer);
152 final String model = device_.model;
154 properties.put(Thing.PROPERTY_MODEL_ID, model);
156 final String sw_version = device_.sw_version;
157 if (sw_version != null) {
158 properties.put(Thing.PROPERTY_FIRMWARE_VERSION, sw_version);