Skip to content

Commit

Permalink
Fix thing upgrades for bridges (openhab#3858)
Browse files Browse the repository at this point in the history
Bridges could not be updated because the updated bridge was a `ThingImpl` instead of a `BridgeImpl`. Also the copy-creator in the `BridgeBuilder` was missing. This should also be cherry-picked to 4.0.x.

Signed-off-by: Jan N. Klug <[email protected]>
  • Loading branch information
J-N-K authored Oct 30, 2023
1 parent d75d6df commit 7b0f77b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ public static BridgeBuilder create(ThingTypeUID thingTypeUID, ThingUID thingUID)
return new BridgeBuilder(thingTypeUID, thingUID);
}

/**
* Create a new bridge {@link BridgeBuilder} for a copy of the given bridge
*
* @param bridge the {@link Bridge} to create this builder from
* @return the created {@link BridgeBuilder}
*
*/
public static BridgeBuilder create(Bridge bridge) {
return BridgeBuilder.create(bridge.getThingTypeUID(), bridge.getUID()).withBridge(bridge.getBridgeUID())
.withChannels(bridge.getChannels()).withConfiguration(bridge.getConfiguration())
.withLabel(bridge.getLabel()).withLocation(bridge.getLocation()).withProperties(bridge.getProperties());
}

@Override
public Bridge build() {
final BridgeImpl bridge = new BridgeImpl(thingTypeUID, thingUID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import org.openhab.core.thing.binding.ThingHandler;
import org.openhab.core.thing.binding.ThingHandlerCallback;
import org.openhab.core.thing.binding.ThingHandlerFactory;
import org.openhab.core.thing.binding.builder.BridgeBuilder;
import org.openhab.core.thing.binding.builder.ThingBuilder;
import org.openhab.core.thing.binding.builder.ThingStatusInfoBuilder;
import org.openhab.core.thing.events.ThingEventFactory;
Expand Down Expand Up @@ -238,7 +239,7 @@ protected void thingUpdated(final Thing thing) {
throw new IllegalArgumentException(MessageFormat.format(
"Cannot update thing {0} because it is not known to the registry", thing.getUID().getAsString()));
}
final Provider<Thing> provider = thingRegistry.getProvider(thing);
final Provider<Thing> provider = thingRegistry.getProvider(oldThing);
if (provider == null) {
throw new IllegalArgumentException(MessageFormat.format(
"Provider for thing {0} cannot be determined because it is not known to the registry",
Expand Down Expand Up @@ -1089,7 +1090,8 @@ private boolean checkAndPerformUpdate(Thing thing, ThingHandlerFactory factory)
}

// create a thing builder and apply the update instructions
ThingBuilder thingBuilder = ThingBuilder.create(thing);
ThingBuilder thingBuilder = thing instanceof Bridge ? BridgeBuilder.create((Bridge) thing)
: ThingBuilder.create(thing);
instructions.forEach(instruction -> instruction.perform(thing, thingBuilder));
int newThingTypeVersion = instructions.get(instructions.size() - 1).getThingTypeVersion();
thingBuilder.withProperty(PROPERTY_THING_TYPE_VERSION, String.valueOf(newThingTypeVersion));
Expand Down

0 comments on commit 7b0f77b

Please sign in to comment.