fix restoring nested group items (#4651)

Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
This commit is contained in:
Mark Herwege
2025-03-18 21:51:50 +01:00
committed by GitHub
parent 25f3becef6
commit c90b45c68b
3 changed files with 19 additions and 11 deletions
@@ -215,7 +215,7 @@ public class PersistenceManagerImpl implements ItemRegistryChangeListener, State
} else if (itemCfg instanceof PersistenceGroupConfig persistenceGroupConfig) {
try {
Item gItem = itemRegistry.getItem(persistenceGroupConfig.getGroup());
if (gItem instanceof GroupItem gItem2 && gItem2.getAllMembers().contains(item)) {
if (gItem instanceof GroupItem gItem2 && gItem2.getAllStateMembers().contains(item)) {
applies = true;
}
} catch (ItemNotFoundException e) {
@@ -259,7 +259,7 @@ public class PersistenceManagerImpl implements ItemRegistryChangeListener, State
try {
Item gItem = itemRegistry.getItem(groupName);
if (gItem instanceof GroupItem groupItem) {
items.addAll(groupItem.getAllMembers());
items.addAll(groupItem.getAllStateMembers());
}
} catch (ItemNotFoundException e) {
logger.debug("Item group '{}' does not exist.", groupName);
@@ -242,14 +242,11 @@ public abstract class GenericItem implements ActiveItem {
public void setState(State state, @Nullable State lastState, @Nullable ZonedDateTime lastStateUpdate,
@Nullable ZonedDateTime lastStateChange) {
State oldState = this.state;
ZonedDateTime oldStateUpdate = this.lastStateUpdate;
this.state = state;
this.lastState = lastState;
this.lastStateUpdate = lastStateUpdate;
this.lastStateChange = lastStateChange;
if (oldStateUpdate != null && lastStateUpdate != null && !oldStateUpdate.equals(lastStateUpdate)) {
notifyListeners(oldState, state);
}
this.lastState = lastState != null ? lastState : this.lastState;
this.lastStateUpdate = lastStateUpdate != null ? lastStateUpdate : this.lastStateUpdate;
this.lastStateChange = lastStateChange != null ? lastStateChange : this.lastStateChange;
notifyListeners(oldState, state);
sendStateUpdatedEvent(state);
if (!oldState.equals(state)) {
sendStateChangedEvent(state, oldState);
@@ -122,8 +122,8 @@ public class GroupItem extends GenericItem implements StateChangeListener, Metad
}
/**
* Returns the direct members of this {@link GroupItem} and recursively all
* members of the potentially contained {@link GroupItem}s as well. The {@link GroupItem}s itself aren't contained.
* Returns the direct members of this {@link GroupItem} and recursively all members of the potentially contained
* {@link GroupItem}s as well. The {@link GroupItem}s itself aren't contained.
* The returned items are unique.
*
* @return all members of this and all contained {@link GroupItem}s
@@ -132,6 +132,17 @@ public class GroupItem extends GenericItem implements StateChangeListener, Metad
return Collections.unmodifiableSet(new LinkedHashSet<>(getMembers((Item i) -> !(i instanceof GroupItem))));
}
/**
* Returns the direct members of this {@link GroupItem} and recursively all members of the potentially contained
* {@link GroupItem}s as well. The {@link GroupItem}s itself are contained if they can have a state.
* The returned items are unique.
*
* @return all members of this and all contained {@link GroupItem}s
*/
public Set<Item> getAllStateMembers() {
return Collections.unmodifiableSet(new LinkedHashSet<>(getStateMembers(getMembers())));
}
private void collectMembers(Collection<Item> allMembers, Collection<Item> members) {
for (Item member : members) {
if (allMembers.contains(member)) {