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.config.dto;
15 import java.util.List;
17 import java.util.Objects;
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.SerializedName;
28 * Base class for home assistant configurations.
30 * @author Jochen Klein - Initial contribution
33 public abstract class AbstractChannelConfiguration {
34 public static final char PARENT_TOPIC_PLACEHOLDER = '~';
36 protected String name;
38 protected String icon = "";
39 protected int qos; // defaults to 0 according to HA specification
40 protected boolean retain; // defaults to false according to HA specification
41 @SerializedName("value_template")
42 protected @Nullable String valueTemplate;
43 @SerializedName("unique_id")
44 protected @Nullable String uniqueId;
46 @SerializedName("availability_mode")
47 protected AvailabilityMode availabilityMode = AvailabilityMode.LATEST;
48 @SerializedName("availability_topic")
49 protected @Nullable String availabilityTopic;
50 @SerializedName("payload_available")
51 protected String payloadAvailable = "online";
52 @SerializedName("payload_not_available")
53 protected String payloadNotAvailable = "offline";
56 * A list of MQTT topics subscribed to receive availability (online/offline) updates. Must not be used together with
59 protected @Nullable List<Availability> availability;
61 @SerializedName(value = "~")
62 protected String parentTopic = "";
64 protected @Nullable Device device;
67 * Parse the base properties of the configJSON into a {@link AbstractChannelConfiguration}
69 * @param configJSON channels configuration in JSON
71 * @return configuration object
73 public static AbstractChannelConfiguration fromString(final String configJSON, final Gson gson) {
74 return fromString(configJSON, gson, Config.class);
77 protected AbstractChannelConfiguration(String defaultName) {
78 this.name = defaultName;
81 public String getThingName() {
84 if (this.device != null) {
85 result = this.device.name;
93 public String getThingId(String defaultId) {
95 if (this.device != null) {
96 result = this.device.getId();
101 return UIDUtils.encode(result != null ? result : defaultId);
104 public Map<String, Object> appendToProperties(Map<String, Object> properties) {
105 final Device d = device;
109 final String manufacturer = d.manufacturer;
110 if (manufacturer != null) {
111 properties.put(Thing.PROPERTY_VENDOR, manufacturer);
113 final String model = d.model;
115 properties.put(Thing.PROPERTY_MODEL_ID, model);
117 final String swVersion = d.swVersion;
118 if (swVersion != null) {
119 properties.put(Thing.PROPERTY_FIRMWARE_VERSION, swVersion);
124 public String getName() {
128 public String getIcon() {
132 public int getQos() {
136 public boolean isRetain() {
141 public String getValueTemplate() {
142 return valueTemplate;
146 public String getUniqueId() {
151 public String getAvailabilityTopic() {
152 return availabilityTopic;
155 public String getPayloadAvailable() {
156 return payloadAvailable;
159 public String getPayloadNotAvailable() {
160 return payloadNotAvailable;
164 public Device getDevice() {
169 public List<Availability> getAvailability() {
173 public String getParentTopic() {
177 public AvailabilityMode getAvailabilityMode() {
178 return availabilityMode;
182 * This class is needed, to be able to parse only the common base attributes.
183 * Without this, {@link AbstractChannelConfiguration} cannot be instantiated, as it is abstract.
184 * This is needed during the discovery.
186 private static class Config extends AbstractChannelConfiguration {
193 * Parse the configJSON into a subclass of {@link AbstractChannelConfiguration}
195 * @param configJSON channels configuration in JSON
197 * @param clazz target configuration class
198 * @return configuration object
200 public static <C extends AbstractChannelConfiguration> C fromString(final String configJSON, final Gson gson,
201 final Class<C> clazz) {
202 return Objects.requireNonNull(gson.fromJson(configJSON, clazz));