Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,19 @@ public Thing getThing() {
@Override
public void thingUpdated(Thing thing) {
dispose();
setThing(thing);
initialize();
}

/**
* Replaces the {@link Thing} handled by this handler and refreshes its resolved configuration.
* <p>
* Custom {@link #thingUpdated(Thing)} implementations should use this method instead of assigning directly to
* {@link #thing}, so the Thing and resolved configuration remain consistent.
*
* @param thing the updated Thing
*/
protected final void setThing(Thing thing) {
Comment thread
lsiepel marked this conversation as resolved.
Configuration resolvedConfiguration;
try {
resolvedConfiguration = ConfigUtil.resolveVariables(thing.getConfiguration());
Expand All @@ -169,7 +182,6 @@ public void thingUpdated(Thing thing) {
this.thing = thing;
this.resolvedConfig = resolvedConfiguration;
}
initialize();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ public void handleCommand(ChannelUID channelUID, Command command) {
public void initialize() {
this.configInInitialize = getConfig();
}

public void replaceThing(Thing thing) {
setThing(thing);
}
}

private static class ConfigUtilAccessor extends ConfigUtil {
Expand Down Expand Up @@ -124,6 +128,18 @@ public void testResolvedConfigAfterThingUpdate() {
assertEquals("resolved-bar", handler.getConfig().get("p2"));
}

@Test
public void testResolvedConfigAfterSetThing() {
handler.getConfig();
Thing thing = handler.editThing()
.withConfiguration(new Configuration(Map.of("p1", "${ENV:FOO}", "p2", "${ENV:BAR}"))).build();

handler.replaceThing(thing);

assertEquals("resolved-foo", handler.getConfig().get("p1"));
assertEquals("resolved-bar", handler.getConfig().get("p2"));
}

@Test
public void testResolvedConfigAfterUpdateThing() {
// Action: Update the Thing with new configuration
Expand Down