Disable AutoUpdateManager for GroupItems (#1862)

* Disable AutoUpdateManager for GroupItems

Fixes #1330 

Signed-off-by: Simon Lamon <simonlamon93@hotmail.com>
This commit is contained in:
silamon 2020-12-03 17:20:59 +01:00 committed by GitHub
parent 85fcb7b141
commit 34a3487e3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 7 deletions

View File

@ -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<ChannelUID> 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
}
}
}

View File

@ -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();
}
}