From 7b0f77bac30cfafac9df89dbb42a7084e7c3de4c Mon Sep 17 00:00:00 2001 From: J-N-K Date: Mon, 30 Oct 2023 18:31:32 +0100 Subject: [PATCH] Fix thing upgrades for bridges (#3858) 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 --- .../core/thing/binding/builder/BridgeBuilder.java | 13 +++++++++++++ .../core/thing/internal/ThingManagerImpl.java | 6 ++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/binding/builder/BridgeBuilder.java b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/binding/builder/BridgeBuilder.java index 49a25e73e29..d96dce95054 100644 --- a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/binding/builder/BridgeBuilder.java +++ b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/binding/builder/BridgeBuilder.java @@ -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); diff --git a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/ThingManagerImpl.java b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/ThingManagerImpl.java index 380e3cd998c..e7482f7ec08 100644 --- a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/ThingManagerImpl.java +++ b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/ThingManagerImpl.java @@ -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; @@ -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 provider = thingRegistry.getProvider(thing); + final Provider 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", @@ -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));