Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[homekit] Fix metadata changes not seen until restart #9484

Merged
merged 1 commit into from
Dec 23, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.common.ThreadPoolManager;
import org.openhab.core.common.registry.RegistryChangeListener;
import org.openhab.core.items.GroupItem;
import org.openhab.core.items.Item;
import org.openhab.core.items.ItemNotFoundException;
import org.openhab.core.items.ItemRegistry;
import org.openhab.core.items.ItemRegistryChangeListener;
import org.openhab.core.items.Metadata;
import org.openhab.core.items.MetadataRegistry;
import org.openhab.core.storage.Storage;
import org.openhab.core.storage.StorageService;
Expand All @@ -41,7 +43,7 @@
import io.github.hapjava.server.impl.HomekitRoot;

/**
* Listens for changes to the item registry. When changes are detected, check
* Listens for changes to the item and metadata registry. When changes are detected, check
* for HomeKit tags and, if present, add the items to the HomekitAccessoryRegistry.
*
* @author Andy Lintner - Initial contribution
Expand All @@ -55,6 +57,7 @@ public class HomekitChangeListener implements ItemRegistryChangeListener {
private final HomekitAccessoryRegistry accessoryRegistry = new HomekitAccessoryRegistry();
private final MetadataRegistry metadataRegistry;
private final Storage<String> storage;
private final RegistryChangeListener<Metadata> metadataChangeListener;
private HomekitAccessoryUpdater updater = new HomekitAccessoryUpdater();
private HomekitSettings settings;
private int lastAccessoryCount;
Expand Down Expand Up @@ -82,8 +85,39 @@ public class HomekitChangeListener implements ItemRegistryChangeListener {
storage = storageService.getStorage(HomekitAuthInfoImpl.STORAGE_KEY);
this.applyUpdatesDebouncer = new Debouncer("update-homekit-devices", scheduler, Duration.ofMillis(1000),
Clock.systemUTC(), this::applyUpdates);
metadataChangeListener = new RegistryChangeListener<Metadata>() {
@Override
public void added(final Metadata metadata) {
try {
markDirty(itemRegistry.getItem(metadata.getUID().getItemName()));
} catch (ItemNotFoundException e) {
logger.debug("Could not found item for metadata {}", metadata);
}
}

@Override
public void removed(final Metadata metadata) {
try {
markDirty(itemRegistry.getItem(metadata.getUID().getItemName()));
} catch (ItemNotFoundException e) {
logger.debug("Could not found item for metadata {}", metadata);
}
}

@Override
public void updated(final Metadata metadata, final Metadata e1) {
try {
markDirty(itemRegistry.getItem(metadata.getUID().getItemName()));
if (!metadata.getUID().getItemName().equals(e1.getUID().getItemName())) {
markDirty(itemRegistry.getItem(e1.getUID().getItemName()));
}
} catch (ItemNotFoundException e) {
logger.debug("Could not found item for metadata {}", metadata);
}
}
};
itemRegistry.addRegistryChangeListener(this);
metadataRegistry.addRegistryChangeListener(metadataChangeListener);
itemRegistry.getItems().forEach(this::createRootAccessories);
initialiseRevision();
logger.info("Created {} HomeKit items.", accessoryRegistry.getAllAccessories().size());
Expand Down Expand Up @@ -211,6 +245,7 @@ public void updateSettings(HomekitSettings settings) {

public void stop() {
this.itemRegistry.removeRegistryChangeListener(this);
this.metadataRegistry.removeRegistryChangeListener(metadataChangeListener);
}

public Map<String, HomekitAccessory> getAccessories() {
Expand Down