]> git.basschouten.com Git - openhab-addons.git/blob
3a51c77bf2d5af6834fd6292b8c8b7e72dec98e0
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.mqtt.homeassistant.internal;
14
15 import java.io.IOException;
16 import java.util.ArrayList;
17 import java.util.HashMap;
18 import java.util.Iterator;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Map.Entry;
22 import java.util.Optional;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.mqtt.homeassistant.internal.component.AbstractComponent;
27 import org.openhab.core.thing.binding.generic.ChannelTransformation;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import com.fasterxml.jackson.databind.JsonNode;
32 import com.fasterxml.jackson.databind.ObjectMapper;
33 import com.hubspot.jinjava.Jinjava;
34 import com.hubspot.jinjava.interpret.FatalTemplateErrorsException;
35
36 /**
37  * Provides a channel transformation for a Home Assistant channel with a
38  * Jinja2 template, providing the additional context and extensions required by Home Assistant
39  * Based in part on the JinjaTransformationService
40  *
41  * @author Cody Cutrer - Initial contribution
42  */
43 @NonNullByDefault
44 public class HomeAssistantChannelTransformation extends ChannelTransformation {
45     private final Logger logger = LoggerFactory.getLogger(HomeAssistantChannelTransformation.class);
46
47     private final Jinjava jinjava;
48     private final AbstractComponent component;
49     private final String template;
50     private final ObjectMapper objectMapper = new ObjectMapper();
51
52     public HomeAssistantChannelTransformation(Jinjava jinjava, AbstractComponent component, String template) {
53         super((String) null);
54         this.jinjava = jinjava;
55         this.component = component;
56         this.template = template;
57     }
58
59     @Override
60     public boolean isEmpty() {
61         return template.isEmpty();
62     }
63
64     @Override
65     public Optional<String> apply(String value) {
66         String transformationResult;
67         Map<String, @Nullable Object> bindings = new HashMap<>();
68
69         logger.debug("about to transform '{}' by the function '{}'", value, template);
70
71         bindings.put("value", value);
72
73         try {
74             JsonNode tree = objectMapper.readTree(value);
75             bindings.put("value_json", toObject(tree));
76         } catch (IOException e) {
77             // ok, then value_json is null...
78         }
79
80         try {
81             transformationResult = jinjava.render(template, bindings);
82         } catch (FatalTemplateErrorsException e) {
83             logger.warn("Applying template {} for component {} failed: {}", template,
84                     component.getHaID().toShortTopic(), e.getMessage());
85             return Optional.empty();
86         }
87
88         logger.debug("transformation resulted in '{}'", transformationResult);
89
90         return Optional.of(transformationResult);
91     }
92
93     private static @Nullable Object toObject(JsonNode node) {
94         switch (node.getNodeType()) {
95             case ARRAY: {
96                 List<@Nullable Object> result = new ArrayList<>();
97                 for (JsonNode el : node) {
98                     result.add(toObject(el));
99                 }
100                 return result;
101             }
102             case NUMBER:
103                 return node.decimalValue();
104             case OBJECT: {
105                 Map<String, @Nullable Object> result = new HashMap<>();
106                 Iterator<Entry<String, JsonNode>> it = node.fields();
107                 while (it.hasNext()) {
108                     Entry<String, JsonNode> field = it.next();
109                     result.put(field.getKey(), toObject(field.getValue()));
110                 }
111                 return result;
112             }
113             case STRING:
114                 return node.asText();
115             case BOOLEAN:
116                 return node.asBoolean();
117             case NULL:
118             default:
119                 return null;
120         }
121     }
122 }