2 * Copyright (c) 2010-2023 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.mielecloud.internal.config;
15 import java.util.List;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.mielecloud.internal.MieleCloudBindingConstants;
19 import org.openhab.core.config.discovery.DiscoveryResult;
20 import org.openhab.core.thing.Bridge;
21 import org.openhab.core.thing.Thing;
24 * Generator for templates which can be copy-pasted into .things files by the user.
26 * @author Björn Lange - Initial Contribution
29 public class ThingsTemplateGenerator {
31 * Creates a template for the bridge.
33 * @param bridgeId Id of the bridge (last part of the thing UID).
34 * @param locale Locale for accessing the Miele cloud service.
35 * @return The template.
37 public String createBridgeConfigurationTemplate(String bridgeId, String email, String locale) {
38 var builder = new StringBuilder();
39 builder.append("Bridge ");
40 builder.append(MieleCloudBindingConstants.THING_TYPE_BRIDGE.getAsString());
42 builder.append(bridgeId);
43 builder.append(" [ email=\"");
44 builder.append(email);
45 builder.append("\", locale=\"");
46 builder.append(locale);
47 builder.append("\" ]");
48 return builder.toString();
52 * Creates a complete template containing the bridge and all paired devices.
54 * @param bridge The bridge which is used to pair the things.
55 * @param pairedThings The paired things.
56 * @param discoveryResults The discovery results which can be paired.
57 * @return The template.
59 public String createBridgeAndThingConfigurationTemplate(Bridge bridge, List<Thing> pairedThings,
60 List<DiscoveryResult> discoveryResults) {
61 StringBuilder result = new StringBuilder();
62 result.append(createBridgeConfigurationTemplate(bridge.getUID().getId(),
63 bridge.getConfiguration().get(MieleCloudBindingConstants.CONFIG_PARAM_EMAIL).toString(),
65 result.append(" {\n");
67 for (Thing thing : pairedThings) {
68 result.append(" ").append(createThingConfigurationTemplate(thing)).append("\n");
71 for (DiscoveryResult discoveryResult : discoveryResults) {
72 result.append(" ").append(createThingConfigurationTemplate(discoveryResult)).append("\n");
76 return result.toString();
79 private String getLocale(Bridge bridge) {
80 var locale = bridge.getConfiguration().get(MieleCloudBindingConstants.CONFIG_PARAM_LOCALE);
81 if (locale instanceof String) {
82 return (String) locale;
88 private String createThingConfigurationTemplate(Thing thing) {
89 StringBuilder result = new StringBuilder();
90 result.append("Thing ").append(thing.getThingTypeUID().getId()).append(" ").append(thing.getUID().getId())
93 final String label = thing.getLabel();
95 result.append("\"").append(label).append("\" ");
99 result.append("deviceIdentifier=\"");
101 thing.getConfiguration().get(MieleCloudBindingConstants.CONFIG_PARAM_DEVICE_IDENTIFIER).toString());
104 return result.toString();
107 private String createThingConfigurationTemplate(DiscoveryResult discoveryResult) {
108 return "Thing " + discoveryResult.getThingTypeUID().getId() + " " + discoveryResult.getThingUID().getId()
109 + " \"" + discoveryResult.getLabel() + "\" [ deviceIdentifier=\""
110 + getProperty(discoveryResult, MieleCloudBindingConstants.CONFIG_PARAM_DEVICE_IDENTIFIER) + "\" ]";
113 private String getProperty(DiscoveryResult discoveryResult, String propertyName) {
114 var value = discoveryResult.getProperties().get(MieleCloudBindingConstants.CONFIG_PARAM_DEVICE_IDENTIFIER);
118 return value.toString();