mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-01-10 07:02:02 +01:00
[mqtt.homeassistant] Use Jinjava directly (#17378)
* [mqtt.homeassistant] Use Jinjava directly Signed-off-by: Cody Cutrer <cody@cutrer.us>
This commit is contained in:
parent
65fce20081
commit
110fd9b1ca
@ -78,13 +78,35 @@ public class ChannelState implements MqttMessageSubscriber {
|
||||
*/
|
||||
public ChannelState(ChannelConfig config, ChannelUID channelUID, Value cachedValue,
|
||||
@Nullable ChannelStateUpdateListener channelStateUpdateListener) {
|
||||
this(config, channelUID, cachedValue, channelStateUpdateListener,
|
||||
new ChannelTransformation(config.transformationPattern),
|
||||
new ChannelTransformation(config.transformationPatternOut));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new channel state.
|
||||
*
|
||||
* @param config The channel configuration
|
||||
* @param channelUID The channelUID is used for the {@link ChannelStateUpdateListener} to notify about value changes
|
||||
* @param cachedValue MQTT only notifies us once about a value, during the subscribe. The channel state therefore
|
||||
* needs a cache for the current value.
|
||||
* @param channelStateUpdateListener A channel state update listener
|
||||
* @param incomingTransformation A transformation to apply to incoming values
|
||||
* @param outgoingTransformation A transformation to apply to outgoing values
|
||||
*/
|
||||
public ChannelState(ChannelConfig config, ChannelUID channelUID, Value cachedValue,
|
||||
@Nullable ChannelStateUpdateListener channelStateUpdateListener,
|
||||
@Nullable ChannelTransformation incomingTransformation,
|
||||
@Nullable ChannelTransformation outgoingTransformation) {
|
||||
this.config = config;
|
||||
this.channelStateUpdateListener = channelStateUpdateListener;
|
||||
this.channelUID = channelUID;
|
||||
this.cachedValue = cachedValue;
|
||||
this.readOnly = config.commandTopic.isBlank();
|
||||
this.incomingTransformation = new ChannelTransformation(config.transformationPattern);
|
||||
this.outgoingTransformation = new ChannelTransformation(config.transformationPatternOut);
|
||||
this.incomingTransformation = incomingTransformation == null ? new ChannelTransformation((String) null)
|
||||
: incomingTransformation;
|
||||
this.outgoingTransformation = outgoingTransformation == null ? new ChannelTransformation((String) null)
|
||||
: outgoingTransformation;
|
||||
}
|
||||
|
||||
public boolean isReadOnly() {
|
||||
|
@ -6,15 +6,6 @@ Devices that use [Home Assistant MQTT Discovery](https://www.home-assistant.io/i
|
||||
Components that share a common `device.identifiers` will automatically be grouped together as a single Thing.
|
||||
Each component will be represented as a Channel Group, with the attributes of that component being individual channels.
|
||||
|
||||
## Requirements
|
||||
|
||||
The Home Assistant MQTT binding requires two transformations to be installed:
|
||||
|
||||
- JINJA-Transformations
|
||||
- JSONPath-Transformations
|
||||
|
||||
These can be installed under `Settings` → `Addon` → `Transformations`
|
||||
|
||||
## Discovery
|
||||
|
||||
Any device that publishes the component configuration under the `homeassistant` prefix in MQTT will have their components automatically discovered and added to the Inbox.
|
||||
|
@ -35,21 +35,22 @@
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.openhab.addons.bundles</groupId>
|
||||
<artifactId>org.openhab.transform.jinja</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<scope>test</scope>
|
||||
<groupId>org.openhab.osgiify</groupId>
|
||||
<artifactId>com.hubspot.jinjava.jinjava</artifactId>
|
||||
<version>2.7.2_0</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.hubspot.jinjava</groupId>
|
||||
<artifactId>jinjava</artifactId>
|
||||
<version>2.7.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.re2j</groupId>
|
||||
<artifactId>re2j</artifactId>
|
||||
<groupId>org.openhab.osgiify</groupId>
|
||||
<artifactId>com.google.re2j.re2j</artifactId>
|
||||
<version>1.2</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.obermuhlner</groupId>
|
||||
<artifactId>big-math</artifactId>
|
||||
<version>2.3.2</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
@ -5,6 +5,10 @@
|
||||
<feature name="openhab-binding-mqtt-homeassistant" description="MQTT Binding Homeassistant" version="${project.version}">
|
||||
<feature>openhab-runtime-base</feature>
|
||||
<feature>openhab-transport-mqtt</feature>
|
||||
<feature dependency="true">openhab.tp-commons-net</feature>
|
||||
<bundle dependency="true">mvn:org.openhab.osgiify/com.hubspot.jinjava.jinjava/2.7.2_0</bundle>
|
||||
<bundle dependency="true">mvn:org.openhab.osgiify/com.google.re2j.re2j/1.2</bundle>
|
||||
<bundle dependency="true">mvn:ch.obermuhlner/big-math/2.3.2</bundle>
|
||||
<bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.mqtt/${project.version}</bundle>
|
||||
<bundle start-level="81">mvn:org.openhab.addons.bundles/org.openhab.binding.mqtt.generic/${project.version}</bundle>
|
||||
<bundle start-level="82">mvn:org.openhab.addons.bundles/org.openhab.binding.mqtt.homeassistant/${project.version}</bundle>
|
||||
|
@ -31,6 +31,8 @@ import org.osgi.service.component.annotations.Activate;
|
||||
import org.osgi.service.component.annotations.Component;
|
||||
import org.osgi.service.component.annotations.Reference;
|
||||
|
||||
import com.hubspot.jinjava.Jinjava;
|
||||
|
||||
/**
|
||||
* The {@link MqttThingHandlerFactory} is responsible for creating things and thing
|
||||
* handlers.
|
||||
@ -43,6 +45,7 @@ public class MqttThingHandlerFactory extends BaseThingHandlerFactory {
|
||||
private final MqttChannelTypeProvider typeProvider;
|
||||
private final MqttChannelStateDescriptionProvider stateDescriptionProvider;
|
||||
private final ChannelTypeRegistry channelTypeRegistry;
|
||||
private final Jinjava jinjava = new Jinjava();
|
||||
|
||||
private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
|
||||
.of(MqttBindingConstants.HOMEASSISTANT_MQTT_THING).collect(Collectors.toSet());
|
||||
@ -72,7 +75,7 @@ public class MqttThingHandlerFactory extends BaseThingHandlerFactory {
|
||||
|
||||
if (supportsThingType(thingTypeUID)) {
|
||||
return new HomeAssistantThingHandler(thing, typeProvider, stateDescriptionProvider, channelTypeRegistry,
|
||||
10000, 2000);
|
||||
jinjava, 10000, 2000);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
@ -12,7 +12,6 @@
|
||||
*/
|
||||
package org.openhab.binding.mqtt.homeassistant.internal;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
@ -30,6 +29,7 @@ import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
|
||||
import org.openhab.core.thing.Channel;
|
||||
import org.openhab.core.thing.ChannelUID;
|
||||
import org.openhab.core.thing.binding.builder.ChannelBuilder;
|
||||
import org.openhab.core.thing.binding.generic.ChannelTransformation;
|
||||
import org.openhab.core.thing.type.AutoUpdatePolicy;
|
||||
import org.openhab.core.thing.type.ChannelDefinition;
|
||||
import org.openhab.core.thing.type.ChannelDefinitionBuilder;
|
||||
@ -223,21 +223,26 @@ public class ComponentChannel {
|
||||
ChannelUID channelUID;
|
||||
ChannelState channelState;
|
||||
Channel channel;
|
||||
ChannelTransformation incomingTransformation = null, outgoingTransformation = null;
|
||||
|
||||
channelUID = component.buildChannelUID(channelID);
|
||||
ChannelConfigBuilder channelConfigBuilder = ChannelConfigBuilder.create().withRetain(retain).withQos(qos)
|
||||
.withStateTopic(stateTopic).withCommandTopic(commandTopic).makeTrigger(trigger)
|
||||
.withFormatter(format);
|
||||
|
||||
if (templateIn != null) {
|
||||
channelConfigBuilder.withTransformationPattern(List.of(JINJA + ":" + templateIn));
|
||||
String localTemplateIn = templateIn;
|
||||
if (localTemplateIn != null) {
|
||||
incomingTransformation = new HomeAssistantChannelTransformation(component.getJinjava(), component,
|
||||
localTemplateIn);
|
||||
}
|
||||
if (templateOut != null) {
|
||||
channelConfigBuilder.withTransformationPatternOut(List.of(JINJA + ":" + templateOut));
|
||||
String localTemplateOut = templateOut;
|
||||
if (localTemplateOut != null) {
|
||||
outgoingTransformation = new HomeAssistantChannelTransformation(component.getJinjava(), component,
|
||||
localTemplateOut);
|
||||
}
|
||||
|
||||
channelState = new HomeAssistantChannelState(channelConfigBuilder.build(), channelUID, valueState,
|
||||
channelStateUpdateListener, commandFilter);
|
||||
channelStateUpdateListener, commandFilter, incomingTransformation, outgoingTransformation);
|
||||
|
||||
// disabled by default components should always show up as advanced
|
||||
if (!component.isEnabledByDefault()) {
|
||||
|
@ -37,6 +37,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.hubspot.jinjava.Jinjava;
|
||||
|
||||
/**
|
||||
* Responsible for subscribing to the HomeAssistant MQTT components wildcard topic, either
|
||||
@ -55,6 +56,7 @@ public class DiscoverComponents implements MqttMessageSubscriber {
|
||||
|
||||
protected final CompletableFuture<@Nullable Void> discoverFinishedFuture = new CompletableFuture<>();
|
||||
private final Gson gson;
|
||||
private final Jinjava jinjava;
|
||||
|
||||
private @Nullable ScheduledFuture<?> stopDiscoveryFuture;
|
||||
private WeakReference<@Nullable MqttBrokerConnection> connectionRef = new WeakReference<>(null);
|
||||
@ -78,11 +80,12 @@ public class DiscoverComponents implements MqttMessageSubscriber {
|
||||
*/
|
||||
public DiscoverComponents(ThingUID thingUID, ScheduledExecutorService scheduler,
|
||||
ChannelStateUpdateListener channelStateUpdateListener, AvailabilityTracker tracker, Gson gson,
|
||||
boolean newStyleChannels) {
|
||||
Jinjava jinjava, boolean newStyleChannels) {
|
||||
this.thingUID = thingUID;
|
||||
this.scheduler = scheduler;
|
||||
this.updateListener = channelStateUpdateListener;
|
||||
this.gson = gson;
|
||||
this.jinjava = jinjava;
|
||||
this.tracker = tracker;
|
||||
this.newStyleChannels = newStyleChannels;
|
||||
}
|
||||
@ -100,7 +103,7 @@ public class DiscoverComponents implements MqttMessageSubscriber {
|
||||
if (config.length() > 0) {
|
||||
try {
|
||||
component = ComponentFactory.createComponent(thingUID, haID, config, updateListener, tracker, scheduler,
|
||||
gson, newStyleChannels);
|
||||
gson, jinjava, newStyleChannels);
|
||||
component.setConfigSeen();
|
||||
|
||||
logger.trace("Found HomeAssistant component {}", haID);
|
||||
|
@ -22,6 +22,7 @@ import org.openhab.binding.mqtt.generic.ChannelState;
|
||||
import org.openhab.binding.mqtt.generic.ChannelStateUpdateListener;
|
||||
import org.openhab.binding.mqtt.generic.values.Value;
|
||||
import org.openhab.core.thing.ChannelUID;
|
||||
import org.openhab.core.thing.binding.generic.ChannelTransformation;
|
||||
import org.openhab.core.types.Command;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -48,9 +49,11 @@ public class HomeAssistantChannelState extends ChannelState {
|
||||
* <code>false</code> ignored. Can be <code>null</code> to publish all commands.
|
||||
*/
|
||||
public HomeAssistantChannelState(ChannelConfig config, ChannelUID channelUID, Value cachedValue,
|
||||
@Nullable ChannelStateUpdateListener channelStateUpdateListener,
|
||||
@Nullable Predicate<Command> commandFilter) {
|
||||
super(config, channelUID, cachedValue, channelStateUpdateListener);
|
||||
@Nullable ChannelStateUpdateListener channelStateUpdateListener, @Nullable Predicate<Command> commandFilter,
|
||||
@Nullable ChannelTransformation incomingTransformation,
|
||||
@Nullable ChannelTransformation outgoingTransformation) {
|
||||
super(config, channelUID, cachedValue, channelStateUpdateListener, incomingTransformation,
|
||||
outgoingTransformation);
|
||||
this.commandFilter = commandFilter;
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,122 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2024 Contributors to the openHAB project
|
||||
*
|
||||
* See the NOTICE file(s) distributed with this work for additional
|
||||
* information.
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.openhab.binding.mqtt.homeassistant.internal;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.openhab.binding.mqtt.homeassistant.internal.component.AbstractComponent;
|
||||
import org.openhab.core.thing.binding.generic.ChannelTransformation;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.hubspot.jinjava.Jinjava;
|
||||
import com.hubspot.jinjava.interpret.FatalTemplateErrorsException;
|
||||
|
||||
/**
|
||||
* Provides a channel transformation for a Home Assistant channel with a
|
||||
* Jinja2 template, providing the additional context and extensions required by Home Assistant
|
||||
* Based in part on the JinjaTransformationService
|
||||
*
|
||||
* @author Cody Cutrer - Initial contribution
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public class HomeAssistantChannelTransformation extends ChannelTransformation {
|
||||
private final Logger logger = LoggerFactory.getLogger(HomeAssistantChannelTransformation.class);
|
||||
|
||||
private final Jinjava jinjava;
|
||||
private final AbstractComponent component;
|
||||
private final String template;
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
public HomeAssistantChannelTransformation(Jinjava jinjava, AbstractComponent component, String template) {
|
||||
super((String) null);
|
||||
this.jinjava = jinjava;
|
||||
this.component = component;
|
||||
this.template = template;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return template.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> apply(String value) {
|
||||
String transformationResult;
|
||||
Map<String, @Nullable Object> bindings = new HashMap<>();
|
||||
|
||||
logger.debug("about to transform '{}' by the function '{}'", value, template);
|
||||
|
||||
bindings.put("value", value);
|
||||
|
||||
try {
|
||||
JsonNode tree = objectMapper.readTree(value);
|
||||
bindings.put("value_json", toObject(tree));
|
||||
} catch (IOException e) {
|
||||
// ok, then value_json is null...
|
||||
}
|
||||
|
||||
try {
|
||||
transformationResult = jinjava.render(template, bindings);
|
||||
} catch (FatalTemplateErrorsException e) {
|
||||
logger.warn("Applying template {} for component {} failed: {}", template,
|
||||
component.getHaID().toShortTopic(), e.getMessage());
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
logger.debug("transformation resulted in '{}'", transformationResult);
|
||||
|
||||
return Optional.of(transformationResult);
|
||||
}
|
||||
|
||||
private static @Nullable Object toObject(JsonNode node) {
|
||||
switch (node.getNodeType()) {
|
||||
case ARRAY: {
|
||||
List<@Nullable Object> result = new ArrayList<>();
|
||||
for (JsonNode el : node) {
|
||||
result.add(toObject(el));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
case NUMBER:
|
||||
return node.decimalValue();
|
||||
case OBJECT: {
|
||||
Map<String, @Nullable Object> result = new HashMap<>();
|
||||
Iterator<Entry<String, JsonNode>> it = node.fields();
|
||||
while (it.hasNext()) {
|
||||
Entry<String, JsonNode> field = it.next();
|
||||
result.put(field.getKey(), toObject(field.getValue()));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
case STRING:
|
||||
return node.asText();
|
||||
case BOOLEAN:
|
||||
return node.asBoolean();
|
||||
case NULL:
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -49,6 +49,7 @@ import org.openhab.core.types.CommandDescription;
|
||||
import org.openhab.core.types.StateDescription;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.hubspot.jinjava.Jinjava;
|
||||
|
||||
/**
|
||||
* A HomeAssistant component is comparable to a channel group.
|
||||
@ -334,6 +335,10 @@ public abstract class AbstractComponent<C extends AbstractChannelConfiguration>
|
||||
return componentConfiguration.getGson();
|
||||
}
|
||||
|
||||
public Jinjava getJinjava() {
|
||||
return componentConfiguration.getJinjava();
|
||||
}
|
||||
|
||||
public C getChannelConfiguration() {
|
||||
return channelConfiguration;
|
||||
}
|
||||
|
@ -24,6 +24,7 @@ import org.openhab.binding.mqtt.homeassistant.internal.exception.UnsupportedComp
|
||||
import org.openhab.core.thing.ThingUID;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.hubspot.jinjava.Jinjava;
|
||||
|
||||
/**
|
||||
* A factory to create HomeAssistant MQTT components. Those components are specified at:
|
||||
@ -46,9 +47,9 @@ public class ComponentFactory {
|
||||
*/
|
||||
public static AbstractComponent<?> createComponent(ThingUID thingUID, HaID haID, String channelConfigurationJSON,
|
||||
ChannelStateUpdateListener updateListener, AvailabilityTracker tracker, ScheduledExecutorService scheduler,
|
||||
Gson gson, boolean newStyleChannels) throws ConfigurationException {
|
||||
Gson gson, Jinjava jinjava, boolean newStyleChannels) throws ConfigurationException {
|
||||
ComponentConfiguration componentConfiguration = new ComponentConfiguration(thingUID, haID,
|
||||
channelConfigurationJSON, gson, updateListener, tracker, scheduler);
|
||||
channelConfigurationJSON, gson, jinjava, updateListener, tracker, scheduler);
|
||||
switch (haID.component) {
|
||||
case "alarm_control_panel":
|
||||
return new AlarmControlPanel(componentConfiguration, newStyleChannels);
|
||||
@ -96,6 +97,7 @@ public class ComponentFactory {
|
||||
private final ChannelStateUpdateListener updateListener;
|
||||
private final AvailabilityTracker tracker;
|
||||
private final Gson gson;
|
||||
private final Jinjava jinjava;
|
||||
private final ScheduledExecutorService scheduler;
|
||||
|
||||
/**
|
||||
@ -106,13 +108,14 @@ public class ComponentFactory {
|
||||
* @param configJSON The configuration string
|
||||
* @param gson A Gson instance
|
||||
*/
|
||||
protected ComponentConfiguration(ThingUID thingUID, HaID haID, String configJSON, Gson gson,
|
||||
protected ComponentConfiguration(ThingUID thingUID, HaID haID, String configJSON, Gson gson, Jinjava jinjava,
|
||||
ChannelStateUpdateListener updateListener, AvailabilityTracker tracker,
|
||||
ScheduledExecutorService scheduler) {
|
||||
this.thingUID = thingUID;
|
||||
this.haID = haID;
|
||||
this.configJSON = configJSON;
|
||||
this.gson = gson;
|
||||
this.jinjava = jinjava;
|
||||
this.updateListener = updateListener;
|
||||
this.tracker = tracker;
|
||||
this.scheduler = scheduler;
|
||||
@ -138,6 +141,10 @@ public class ComponentFactory {
|
||||
return gson;
|
||||
}
|
||||
|
||||
public Jinjava getJinjava() {
|
||||
return jinjava;
|
||||
}
|
||||
|
||||
public AvailabilityTracker getTracker() {
|
||||
return tracker;
|
||||
}
|
||||
|
@ -59,6 +59,7 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.hubspot.jinjava.Jinjava;
|
||||
|
||||
/**
|
||||
* Handles HomeAssistant MQTT object things. Such an HA Object can have multiple HA Components with different instances
|
||||
@ -90,6 +91,7 @@ public class HomeAssistantThingHandler extends AbstractMQTTThingHandler
|
||||
protected final MqttChannelTypeProvider channelTypeProvider;
|
||||
protected final MqttChannelStateDescriptionProvider stateDescriptionProvider;
|
||||
protected final ChannelTypeRegistry channelTypeRegistry;
|
||||
protected final Jinjava jinjava;
|
||||
public final int attributeReceiveTimeout;
|
||||
protected final DelayedBatchProcessing<AbstractComponent<?>> delayedProcessing;
|
||||
protected final DiscoverComponents discoverComponents;
|
||||
@ -115,18 +117,20 @@ public class HomeAssistantThingHandler extends AbstractMQTTThingHandler
|
||||
*/
|
||||
public HomeAssistantThingHandler(Thing thing, MqttChannelTypeProvider channelTypeProvider,
|
||||
MqttChannelStateDescriptionProvider stateDescriptionProvider, ChannelTypeRegistry channelTypeRegistry,
|
||||
int subscribeTimeout, int attributeReceiveTimeout) {
|
||||
Jinjava jinjava, int subscribeTimeout, int attributeReceiveTimeout) {
|
||||
super(thing, subscribeTimeout);
|
||||
this.gson = new GsonBuilder().registerTypeAdapterFactory(new ChannelConfigurationTypeAdapterFactory()).create();
|
||||
this.channelTypeProvider = channelTypeProvider;
|
||||
this.stateDescriptionProvider = stateDescriptionProvider;
|
||||
this.channelTypeRegistry = channelTypeRegistry;
|
||||
this.jinjava = jinjava;
|
||||
this.attributeReceiveTimeout = attributeReceiveTimeout;
|
||||
this.delayedProcessing = new DelayedBatchProcessing<>(attributeReceiveTimeout, this, scheduler);
|
||||
|
||||
newStyleChannels = "true".equals(thing.getProperties().get("newStyleChannels"));
|
||||
|
||||
this.discoverComponents = new DiscoverComponents(thing.getUID(), scheduler, this, this, gson, newStyleChannels);
|
||||
this.discoverComponents = new DiscoverComponents(thing.getUID(), scheduler, this, this, gson, jinjava,
|
||||
newStyleChannels);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -156,7 +160,7 @@ public class HomeAssistantThingHandler extends AbstractMQTTThingHandler
|
||||
} else {
|
||||
try {
|
||||
component = ComponentFactory.createComponent(thingUID, haID, channelConfigurationJSON, this, this,
|
||||
scheduler, gson, newStyleChannels);
|
||||
scheduler, gson, jinjava, newStyleChannels);
|
||||
if (typeID.equals(MqttBindingConstants.HOMEASSISTANT_MQTT_THING)) {
|
||||
typeID = calculateThingTypeUID(component);
|
||||
}
|
||||
|
@ -32,7 +32,6 @@ import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.mockito.junit.jupiter.MockitoSettings;
|
||||
import org.mockito.quality.Strictness;
|
||||
@ -59,8 +58,6 @@ import org.openhab.core.thing.type.ThingTypeBuilder;
|
||||
import org.openhab.core.thing.type.ThingTypeRegistry;
|
||||
import org.openhab.core.transform.TransformationHelper;
|
||||
import org.openhab.core.transform.TransformationService;
|
||||
import org.openhab.transform.jinja.internal.JinjaTransformationService;
|
||||
import org.openhab.transform.jinja.internal.profiles.JinjaTransformationProfile;
|
||||
import org.osgi.framework.BundleContext;
|
||||
import org.osgi.framework.ServiceReference;
|
||||
|
||||
@ -107,14 +104,8 @@ public abstract class AbstractHomeAssistantTests extends JavaTest {
|
||||
|
||||
private @NonNullByDefault({}) TransformationHelper transformationHelper;
|
||||
|
||||
private final JinjaTransformationService jinjaTransformationService = new JinjaTransformationService();
|
||||
|
||||
@BeforeEach
|
||||
public void beforeEachAbstractHomeAssistantTests() {
|
||||
Mockito.when(serviceRefMock.getProperty(any())).thenReturn(JinjaTransformationProfile.PROFILE_TYPE_UID.getId());
|
||||
|
||||
Mockito.when(bundleContextMock.getService(serviceRefMock)).thenReturn(jinjaTransformationService);
|
||||
|
||||
transformationHelper = new TransformationHelper(bundleContextMock);
|
||||
transformationHelper.setTransformationService(serviceRefMock);
|
||||
|
||||
|
@ -49,6 +49,8 @@ import org.openhab.core.thing.type.ChannelTypeRegistry;
|
||||
import org.openhab.core.types.Command;
|
||||
import org.openhab.core.types.State;
|
||||
|
||||
import com.hubspot.jinjava.Jinjava;
|
||||
|
||||
/**
|
||||
* Abstract class for components tests.
|
||||
*
|
||||
@ -288,8 +290,8 @@ public abstract class AbstractComponentTests extends AbstractHomeAssistantTests
|
||||
public LatchThingHandler(Thing thing, MqttChannelTypeProvider channelTypeProvider,
|
||||
MqttChannelStateDescriptionProvider stateDescriptionProvider, ChannelTypeRegistry channelTypeRegistry,
|
||||
int subscribeTimeout, int attributeReceiveTimeout) {
|
||||
super(thing, channelTypeProvider, stateDescriptionProvider, channelTypeRegistry, subscribeTimeout,
|
||||
attributeReceiveTimeout);
|
||||
super(thing, channelTypeProvider, stateDescriptionProvider, channelTypeRegistry, new Jinjava(),
|
||||
subscribeTimeout, attributeReceiveTimeout);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -39,6 +39,8 @@ import org.openhab.core.thing.Channel;
|
||||
import org.openhab.core.thing.binding.ThingHandlerCallback;
|
||||
import org.openhab.core.types.StateDescription;
|
||||
|
||||
import com.hubspot.jinjava.Jinjava;
|
||||
|
||||
/**
|
||||
* Tests for {@link HomeAssistantThingHandler}
|
||||
*
|
||||
@ -75,7 +77,7 @@ public class HomeAssistantThingHandlerTests extends AbstractHomeAssistantTests {
|
||||
when(callbackMock.getBridge(eq(BRIDGE_UID))).thenReturn(bridgeThing);
|
||||
|
||||
thingHandler = new HomeAssistantThingHandler(haThing, channelTypeProvider, stateDescriptionProvider,
|
||||
channelTypeRegistry, SUBSCRIBE_TIMEOUT, ATTRIBUTE_RECEIVE_TIMEOUT);
|
||||
channelTypeRegistry, new Jinjava(), SUBSCRIBE_TIMEOUT, ATTRIBUTE_RECEIVE_TIMEOUT);
|
||||
thingHandler.setConnection(bridgeConnection);
|
||||
thingHandler.setCallback(callbackMock);
|
||||
nonSpyThingHandler = thingHandler;
|
||||
|
@ -22,6 +22,10 @@
|
||||
<feature name="openhab-binding-mqtt" description="MQTT Binding" version="${project.version}">
|
||||
<feature>openhab-runtime-base</feature>
|
||||
<feature>openhab-transport-mqtt</feature>
|
||||
<feature dependency="true">openhab.tp-commons-net</feature>
|
||||
<bundle dependency="true">mvn:org.openhab.osgiify/com.hubspot.jinjava.jinjava/2.7.2_0</bundle>
|
||||
<bundle dependency="true">mvn:org.openhab.osgiify/com.google.re2j.re2j/1.2</bundle>
|
||||
<bundle dependency="true">mvn:ch.obermuhlner/big-math/2.3.2</bundle>
|
||||
<bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.mqtt/${project.version}</bundle>
|
||||
<bundle start-level="81">mvn:org.openhab.addons.bundles/org.openhab.binding.mqtt.espmilighthub/${project.version}</bundle>
|
||||
<bundle start-level="81">mvn:org.openhab.addons.bundles/org.openhab.binding.mqtt.generic/${project.version}</bundle>
|
||||
|
@ -116,4 +116,18 @@ Import-Package: \
|
||||
org.openhab.core.io.transport.mqtt;version='[4.3.0,4.3.1)',\
|
||||
org.openhab.core.test;version='[4.3.0,4.3.1)',\
|
||||
org.openhab.core.thing;version='[4.3.0,4.3.1)',\
|
||||
org.openhab.core.transform;version='[4.3.0,4.3.1)'
|
||||
org.openhab.core.transform;version='[4.3.0,4.3.1)',\
|
||||
ch.obermuhlner.math.big;version='[2.3.2,2.3.3)',\
|
||||
com.fasterxml.jackson.core.jackson-annotations;version='[2.17.1,2.17.2)',\
|
||||
com.fasterxml.jackson.core.jackson-core;version='[2.17.1,2.17.2)',\
|
||||
com.fasterxml.jackson.core.jackson-databind;version='[2.17.1,2.17.2)',\
|
||||
com.fasterxml.jackson.dataformat.jackson-dataformat-yaml;version='[2.17.1,2.17.2)',\
|
||||
com.google.guava;version='[33.2.0,33.2.1)',\
|
||||
com.google.guava.failureaccess;version='[1.0.2,1.0.3)',\
|
||||
com.google.re2j.re2j;version='[1.2.0,1.2.1)',\
|
||||
com.hubspot.jinjava.jinjava;version='[2.7.2,2.7.3)',\
|
||||
javassist;version='[3.29.2,3.29.3)',\
|
||||
org.apache.commons.commons-net;version='[3.9.0,3.9.1)',\
|
||||
org.apache.commons.lang3;version='[3.14.0,3.14.1)',\
|
||||
org.osgi.service.cm;version='[1.6.0,1.6.1)',\
|
||||
org.yaml.snakeyaml;version='[2.2.0,2.2.1)'
|
||||
|
@ -46,6 +46,7 @@ import org.openhab.core.test.java.JavaOSGiTest;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.hubspot.jinjava.Jinjava;
|
||||
|
||||
/**
|
||||
* Tests the {@link DiscoverComponents} class.
|
||||
@ -79,9 +80,10 @@ public class DiscoverComponentsTest extends JavaOSGiTest {
|
||||
ScheduledExecutorService scheduler = new ScheduledThreadPoolExecutor(1);
|
||||
|
||||
Gson gson = new GsonBuilder().registerTypeAdapterFactory(new ChannelConfigurationTypeAdapterFactory()).create();
|
||||
Jinjava jinjava = new Jinjava();
|
||||
|
||||
DiscoverComponents discover = spy(new DiscoverComponents(ThingChannelConstants.TEST_HOME_ASSISTANT_THING,
|
||||
scheduler, channelStateUpdateListener, availabilityTracker, gson, true));
|
||||
scheduler, channelStateUpdateListener, availabilityTracker, gson, jinjava, true));
|
||||
|
||||
HandlerConfiguration config = new HandlerConfiguration("homeassistant", List.of("switch/object"));
|
||||
|
||||
|
@ -57,6 +57,7 @@ import org.openhab.core.types.UnDefType;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.hubspot.jinjava.Jinjava;
|
||||
|
||||
/**
|
||||
* A full implementation test, that starts the embedded MQTT broker and publishes a homeassistant MQTT discovery device
|
||||
@ -143,10 +144,11 @@ public class HomeAssistantMQTTImplementationTest extends MqttOSGiTest {
|
||||
|
||||
final Map<String, AbstractComponent<?>> haComponents = new HashMap<>();
|
||||
Gson gson = new GsonBuilder().registerTypeAdapterFactory(new ChannelConfigurationTypeAdapterFactory()).create();
|
||||
Jinjava jinjava = new Jinjava();
|
||||
|
||||
ScheduledExecutorService scheduler = new ScheduledThreadPoolExecutor(4);
|
||||
DiscoverComponents discover = spy(new DiscoverComponents(ThingChannelConstants.TEST_HOME_ASSISTANT_THING,
|
||||
scheduler, channelStateUpdateListener, availabilityTracker, gson, true));
|
||||
scheduler, channelStateUpdateListener, availabilityTracker, gson, jinjava, true));
|
||||
|
||||
// The DiscoverComponents object calls ComponentDiscovered callbacks.
|
||||
// In the following implementation we add the found component to the `haComponents` map
|
||||
|
Loading…
Reference in New Issue
Block a user