2 * Copyright (c) 2010-2024 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;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.mqtt.homeassistant.internal.exception.ConfigurationException;
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.JsonSyntaxException;
26 import com.google.gson.annotations.SerializedName;
29 * Base class for home assistant configurations.
31 * @author Jochen Klein - Initial contribution
34 public abstract class AbstractChannelConfiguration {
35 public static final char PARENT_TOPIC_PLACEHOLDER = '~';
36 private static final String DEFAULT_THING_NAME = "Home Assistant Device";
38 protected @Nullable String name;
40 protected String icon = "";
41 protected int qos; // defaults to 0 according to HA specification
42 protected boolean retain; // defaults to false according to HA specification
43 @SerializedName("value_template")
44 protected @Nullable String valueTemplate;
45 @SerializedName("unique_id")
46 protected @Nullable String uniqueId;
48 @SerializedName("availability_mode")
49 protected AvailabilityMode availabilityMode = AvailabilityMode.LATEST;
50 @SerializedName("availability_topic")
51 protected @Nullable String availabilityTopic;
52 @SerializedName("payload_available")
53 protected String payloadAvailable = "online";
54 @SerializedName("payload_not_available")
55 protected String payloadNotAvailable = "offline";
56 @SerializedName("availability_template")
57 protected @Nullable String availabilityTemplate;
59 @SerializedName("enabled_by_default")
60 protected boolean enabledByDefault = true;
63 * A list of MQTT topics subscribed to receive availability (online/offline) updates. Must not be used together with
66 protected @Nullable List<Availability> availability;
68 @SerializedName(value = "~")
69 protected String parentTopic = "";
71 protected @Nullable Device device;
74 * Parse the base properties of the configJSON into an {@link AbstractChannelConfiguration}
76 * @param configJSON channels configuration in JSON
78 * @return configuration object
80 public static AbstractChannelConfiguration fromString(final String configJSON, final Gson gson) {
81 return fromString(configJSON, gson, Config.class);
84 protected AbstractChannelConfiguration(String defaultName) {
85 this.name = defaultName;
88 public String getThingName() {
91 if (this.device != null) {
92 result = this.device.name;
98 result = DEFAULT_THING_NAME;
103 public String getThingId(String defaultId) {
104 String result = null;
105 if (this.device != null) {
106 result = this.device.getId();
108 if (result == null) {
111 return UIDUtils.encode(result != null ? result : defaultId);
114 public Map<String, Object> appendToProperties(Map<String, Object> properties) {
115 final Device d = device;
119 final String manufacturer = d.manufacturer;
120 if (manufacturer != null) {
121 properties.put(Thing.PROPERTY_VENDOR, manufacturer);
123 final String model = d.model;
125 properties.put(Thing.PROPERTY_MODEL_ID, model);
127 final String swVersion = d.swVersion;
128 if (swVersion != null) {
129 properties.put(Thing.PROPERTY_FIRMWARE_VERSION, swVersion);
134 public @Nullable String getName() {
138 public String getIcon() {
142 public int getQos() {
146 public boolean isRetain() {
151 public String getValueTemplate() {
152 return valueTemplate;
156 public String getUniqueId() {
161 public String getAvailabilityTopic() {
162 return availabilityTopic;
165 public String getPayloadAvailable() {
166 return payloadAvailable;
169 public String getPayloadNotAvailable() {
170 return payloadNotAvailable;
174 public String getAvailabilityTemplate() {
175 return availabilityTemplate;
178 public boolean isEnabledByDefault() {
179 return enabledByDefault;
183 public Device getDevice() {
188 public List<Availability> getAvailability() {
192 public String getParentTopic() {
196 public AvailabilityMode getAvailabilityMode() {
197 return availabilityMode;
201 * This class is needed, to be able to parse only the common base attributes.
202 * Without this, {@link AbstractChannelConfiguration} cannot be instantiated, as it is abstract.
203 * This is needed during the discovery.
205 private static class Config extends AbstractChannelConfiguration {
212 * Parse the configJSON into a subclass of {@link AbstractChannelConfiguration}
214 * @param configJSON channels configuration in JSON
216 * @param clazz target configuration class
217 * @return configuration object
219 public static <C extends AbstractChannelConfiguration> C fromString(final String configJSON, final Gson gson,
220 final Class<C> clazz) {
223 final C config = gson.fromJson(configJSON, clazz);
224 if (config == null) {
225 throw new ConfigurationException("Channel configuration is empty");
228 } catch (JsonSyntaxException e) {
229 throw new ConfigurationException("Cannot parse channel configuration JSON", e);