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 protected String name;
36 protected String icon = "";
37 protected int qos; // defaults to 0 according to HA specification
38 protected boolean retain; // defaults to false according to HA specification
39 protected @Nullable String value_template;
40 protected @Nullable String unique_id;
42 protected AvailabilityMode availability_mode = AvailabilityMode.LATEST;
43 protected @Nullable String availability_topic;
44 protected String payload_available = "online";
45 protected String payload_not_available = "offline";
48 * A list of MQTT topics subscribed to receive availability (online/offline) updates. Must not be used together with
51 protected @Nullable List<Availability> availability;
53 @SerializedName(value = "~")
54 protected String tilde = "";
56 protected @Nullable Device device;
59 * Parse the base properties of the configJSON into a {@link AbstractChannelConfiguration}
61 * @param configJSON channels configuration in JSON
63 * @return configuration object
65 public static AbstractChannelConfiguration fromString(final String configJSON, final Gson gson) {
66 return fromString(configJSON, gson, Config.class);
69 protected AbstractChannelConfiguration(String defaultName) {
70 this.name = defaultName;
73 public @Nullable String expand(@Nullable String value) {
74 return value == null ? null : value.replaceAll("~", tilde);
77 public String getThingName() {
80 if (this.device != null) {
81 result = this.device.name;
89 public String getThingId(String defaultId) {
91 if (this.device != null) {
92 result = this.device.getId();
97 return UIDUtils.encode(result != null ? result : defaultId);
100 public Map<String, Object> appendToProperties(Map<String, Object> properties) {
101 final Device device_ = device;
102 if (device_ == null) {
105 final String manufacturer = device_.manufacturer;
106 if (manufacturer != null) {
107 properties.put(Thing.PROPERTY_VENDOR, manufacturer);
109 final String model = device_.model;
111 properties.put(Thing.PROPERTY_MODEL_ID, model);
113 final String sw_version = device_.swVersion;
114 if (sw_version != null) {
115 properties.put(Thing.PROPERTY_FIRMWARE_VERSION, sw_version);
120 public String getName() {
124 public String getIcon() {
128 public int getQos() {
132 public boolean isRetain() {
137 public String getValueTemplate() {
138 return value_template;
142 public String getUniqueId() {
147 public String getAvailabilityTopic() {
148 return availability_topic;
151 public String getPayloadAvailable() {
152 return payload_available;
155 public String getPayloadNotAvailable() {
156 return payload_not_available;
160 public Device getDevice() {
165 public List<Availability> getAvailability() {
169 public String getTilde() {
173 public AvailabilityMode getAvailabilityMode() {
174 return availability_mode;
178 * This class is needed, to be able to parse only the common base attributes.
179 * Without this, {@link AbstractChannelConfiguration} cannot be instantiated, as it is abstract.
180 * This is needed during the discovery.
182 private static class Config extends AbstractChannelConfiguration {
189 * Parse the configJSON into a subclass of {@link AbstractChannelConfiguration}
191 * @param configJSON channels configuration in JSON
193 * @param clazz target configuration class
194 * @return configuration object
196 public static <C extends AbstractChannelConfiguration> C fromString(final String configJSON, final Gson gson,
197 final Class<C> clazz) {
198 return Objects.requireNonNull(gson.fromJson(configJSON, clazz));