From 34a3487e3e29c60adcce8074ff9273e0d51a91d7 Mon Sep 17 00:00:00 2001 From: silamon <32477463+silamon@users.noreply.github.com> Date: Thu, 3 Dec 2020 17:20:59 +0100 Subject: [PATCH] Disable AutoUpdateManager for GroupItems (#1862) * Disable AutoUpdateManager for GroupItems Fixes #1330 Signed-off-by: Simon Lamon --- .../thing/internal/AutoUpdateManager.java | 22 +++++++++++++------ .../thing/internal/AutoUpdateManagerTest.java | 15 +++++++++++++ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/AutoUpdateManager.java b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/AutoUpdateManager.java index 04c714539..6786debb2 100644 --- a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/AutoUpdateManager.java +++ b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/AutoUpdateManager.java @@ -12,12 +12,14 @@ */ package org.openhab.core.thing.internal; +import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.eclipse.jdt.annotation.NonNullByDefault; import org.openhab.core.events.EventPublisher; +import org.openhab.core.items.GroupItem; import org.openhab.core.items.Item; import org.openhab.core.items.Metadata; import org.openhab.core.items.MetadataKey; @@ -142,7 +144,7 @@ public class AutoUpdateManager { if (command instanceof State) { final State state = (State) command; - Recommendation autoUpdate = shouldAutoUpdate(itemName); + Recommendation autoUpdate = shouldAutoUpdate(item); // consider user-override via item meta-data MetadataKey key = new MetadataKey(AUTOUPDATE_KEY, itemName); @@ -185,9 +187,15 @@ public class AutoUpdateManager { } } - private Recommendation shouldAutoUpdate(String itemName) { + private Recommendation shouldAutoUpdate(Item item) { + String itemName = item.getName(); Recommendation ret = Recommendation.REQUIRED; + // check if the item is a group item + if (item instanceof GroupItem) { + return Recommendation.DONT; + } + List linkedChannelUIDs = new ArrayList<>(); for (ItemChannelLink link : itemChannelLinkRegistry.getLinks(itemName)) { linkedChannelUIDs.add(link.getLinkedUID()); @@ -279,14 +287,14 @@ public class AutoUpdateManager { // Look for class hierarchy for (Class state : item.getAcceptedDataTypes()) { try { - if (!state.isEnum() && state.newInstance().getClass().isAssignableFrom(newState.getClass())) { + if (!state.isEnum() && state.getDeclaredConstructor().newInstance().getClass() + .isAssignableFrom(newState.getClass())) { isAccepted = true; break; } - } catch (InstantiationException e) { - logger.warn("InstantiationException on {}", e.getMessage(), e); // Should never happen - } catch (IllegalAccessException e) { - logger.warn("IllegalAccessException on {}", e.getMessage(), e); // Should never happen + } catch (InstantiationException | IllegalAccessException | NoSuchMethodException + | InvocationTargetException e) { + logger.warn("Exception on {}", e.getMessage(), e); // Should never happen } } } diff --git a/bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/internal/AutoUpdateManagerTest.java b/bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/internal/AutoUpdateManagerTest.java index 2d8f1f716..640cdc4cc 100644 --- a/bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/internal/AutoUpdateManagerTest.java +++ b/bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/internal/AutoUpdateManagerTest.java @@ -32,6 +32,7 @@ import org.mockito.quality.Strictness; import org.openhab.core.events.Event; import org.openhab.core.events.EventPublisher; import org.openhab.core.items.GenericItem; +import org.openhab.core.items.GroupItem; import org.openhab.core.items.MetadataRegistry; import org.openhab.core.items.events.ItemCommandEvent; import org.openhab.core.items.events.ItemEventFactory; @@ -72,6 +73,8 @@ public class AutoUpdateManagerTest { private static final ChannelUID CHANNEL_UID_HANDLER_MISSING = new ChannelUID(THING_UID_HANDLER_MISSING, "channel1"); private ItemCommandEvent event; private GenericItem item; + private ItemCommandEvent groupEvent; + private GroupItem groupItem; private @Mock ChannelTypeRegistry channelTypeRegistryMock; private @Mock EventPublisher eventPublisherMock; @@ -92,6 +95,9 @@ public class AutoUpdateManagerTest { event = ItemEventFactory.createCommandEvent(ITEM_NAME, new StringType("AFTER")); item = new StringItem(ITEM_NAME); item.setState(new StringType("BEFORE")); + groupEvent = ItemEventFactory.createCommandEvent("groupTest", new StringType("AFTER")); + groupItem = new GroupItem("groupTest", new StringItem("test")); + groupItem.setState(new StringType("BEFORE")); when(iclRegistryMock.getLinks(eq(ITEM_NAME))).then(answer -> links); @@ -349,4 +355,13 @@ public class AutoUpdateManagerTest { assertPredictionEvent("AFTER", null); assertStateEvent("AFTER", AutoUpdateManager.EVENT_SOURCE_OPTIMISTIC); // no? } + + @Test + public void testAutoUpdateDisabledForGroupItems() { + groupItem.addMember(item); + aum.receiveCommand(groupEvent, groupItem); + groupItem.removeMember(item); + + assertNothingHappened(); + } }