]> git.basschouten.com Git - openhab-addons.git/blob
ef29930563758d9bb2dea4cc32e4df8f4e3b9893
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mielecloud.internal.config;
14
15 import java.util.List;
16
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;
22
23 /**
24  * Generator for templates which can be copy-pasted into .things files by the user.
25  *
26  * @author Björn Lange - Initial Contribution
27  */
28 @NonNullByDefault
29 public class ThingsTemplateGenerator {
30     /**
31      * Creates a template for the bridge.
32      *
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.
36      */
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());
41         builder.append(":");
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();
49     }
50
51     /**
52      * Creates a complete template containing the bridge and all paired devices.
53      *
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.
58      */
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(),
64                 getLocale(bridge)));
65         result.append(" {\n");
66
67         for (Thing thing : pairedThings) {
68             result.append("    ").append(createThingConfigurationTemplate(thing)).append("\n");
69         }
70
71         for (DiscoveryResult discoveryResult : discoveryResults) {
72             result.append("    ").append(createThingConfigurationTemplate(discoveryResult)).append("\n");
73         }
74
75         result.append("}");
76         return result.toString();
77     }
78
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;
83         } else {
84             return "en";
85         }
86     }
87
88     private String createThingConfigurationTemplate(Thing thing) {
89         StringBuilder result = new StringBuilder();
90         result.append("Thing ").append(thing.getThingTypeUID().getId()).append(" ").append(thing.getUID().getId())
91                 .append(" ");
92
93         final String label = thing.getLabel();
94         if (label != null) {
95             result.append("\"").append(label).append("\" ");
96         }
97
98         result.append("[ ");
99         result.append("deviceIdentifier=\"");
100         result.append(
101                 thing.getConfiguration().get(MieleCloudBindingConstants.CONFIG_PARAM_DEVICE_IDENTIFIER).toString());
102         result.append("\"");
103         result.append(" ]");
104         return result.toString();
105     }
106
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) + "\" ]";
111     }
112
113     private String getProperty(DiscoveryResult discoveryResult, String propertyName) {
114         var value = discoveryResult.getProperties().get(MieleCloudBindingConstants.CONFIG_PARAM_DEVICE_IDENTIFIER);
115         if (value == null) {
116             return "";
117         } else {
118             return value.toString();
119         }
120     }
121 }