2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.io.homekit.internal;
15 import java.time.Clock;
16 import java.time.Duration;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.List;
23 import java.util.Map.Entry;
24 import java.util.Optional;
26 import java.util.concurrent.ExecutionException;
27 import java.util.concurrent.ScheduledExecutorService;
28 import java.util.stream.Collectors;
30 import org.eclipse.jdt.annotation.NonNullByDefault;
31 import org.eclipse.jdt.annotation.Nullable;
32 import org.openhab.core.common.ThreadPoolManager;
33 import org.openhab.core.common.registry.RegistryChangeListener;
34 import org.openhab.core.items.GroupItem;
35 import org.openhab.core.items.Item;
36 import org.openhab.core.items.ItemNotFoundException;
37 import org.openhab.core.items.ItemRegistry;
38 import org.openhab.core.items.ItemRegistryChangeListener;
39 import org.openhab.core.items.Metadata;
40 import org.openhab.core.items.MetadataKey;
41 import org.openhab.core.items.MetadataRegistry;
42 import org.openhab.core.storage.Storage;
43 import org.openhab.io.homekit.internal.accessories.AbstractHomekitAccessoryImpl;
44 import org.openhab.io.homekit.internal.accessories.DummyHomekitAccessory;
45 import org.openhab.io.homekit.internal.accessories.HomekitAccessoryFactory;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
49 import io.github.hapjava.accessories.HomekitAccessory;
50 import io.github.hapjava.server.impl.HomekitRoot;
53 * Listens for changes to the item and metadata registry. When changes are detected, check
54 * for HomeKit tags and, if present, add the items to the HomekitAccessoryRegistry.
56 * @author Andy Lintner - Initial contribution
59 public class HomekitChangeListener implements ItemRegistryChangeListener {
60 private final Logger logger = LoggerFactory.getLogger(HomekitChangeListener.class);
61 private final static String REVISION_CONFIG = "revision";
62 private final static String ACCESSORY_COUNT = "accessory_count";
63 private final static String KNOWN_ACCESSORIES = "known_accessories";
64 private final ItemRegistry itemRegistry;
65 private final HomekitAccessoryRegistry accessoryRegistry = new HomekitAccessoryRegistry();
66 private final MetadataRegistry metadataRegistry;
67 private final Storage<Object> storage;
68 private final RegistryChangeListener<Metadata> metadataChangeListener;
69 private HomekitAccessoryUpdater updater = new HomekitAccessoryUpdater();
70 private HomekitSettings settings;
71 private int lastAccessoryCount;
72 private Map<String, String> knownAccessories = new HashMap<>();
74 private List<String> priorDummies = new ArrayList<>();
76 private final Set<String> pendingUpdates = new HashSet<>();
78 private final ScheduledExecutorService scheduler = ThreadPoolManager
79 .getScheduledPool(ThreadPoolManager.THREAD_POOL_NAME_COMMON);
82 * Rather than reacting to item added/removed/modified changes directly, we mark them as dirty (and the groups to
85 * We wait for a second to pass until no more items are changed. This allows us to add a group of items all at once,
86 * rather than for each update at a time, preventing us from showing an error message with each addition until the
89 private final Debouncer applyUpdatesDebouncer;
91 HomekitChangeListener(ItemRegistry itemRegistry, HomekitSettings settings, MetadataRegistry metadataRegistry,
92 Storage<Object> storage, int instance) {
93 this.itemRegistry = itemRegistry;
94 this.settings = settings;
95 this.metadataRegistry = metadataRegistry;
96 this.storage = storage;
97 this.instance = instance;
98 this.applyUpdatesDebouncer = new Debouncer("update-homekit-devices-" + instance, scheduler,
99 Duration.ofMillis(1000), Clock.systemUTC(), this::applyUpdates);
100 metadataChangeListener = new RegistryChangeListener<Metadata>() {
102 public void added(final Metadata metadata) {
103 final MetadataKey uid = metadata.getUID();
104 if (HomekitAccessoryFactory.METADATA_KEY.equalsIgnoreCase(uid.getNamespace())) {
106 markDirty(itemRegistry.getItem(uid.getItemName()));
107 } catch (ItemNotFoundException e) {
108 logger.trace("Could not find item for metadata {}", metadata);
114 public void removed(final Metadata metadata) {
115 final MetadataKey uid = metadata.getUID();
116 if (HomekitAccessoryFactory.METADATA_KEY.equalsIgnoreCase(uid.getNamespace())) {
118 markDirty(itemRegistry.getItem(uid.getItemName()));
119 } catch (ItemNotFoundException e) {
120 logger.trace("Could not find item for metadata {}", metadata);
126 public void updated(final Metadata oldMetadata, final Metadata newMetadata) {
127 final MetadataKey oldUid = oldMetadata.getUID();
128 final MetadataKey newUid = newMetadata.getUID();
129 if (HomekitAccessoryFactory.METADATA_KEY.equalsIgnoreCase(oldUid.getNamespace())
130 || HomekitAccessoryFactory.METADATA_KEY.equalsIgnoreCase(newUid.getNamespace())) {
132 // the item name is same in old and new metadata, so we can take any.
133 markDirty(itemRegistry.getItem(oldUid.getItemName()));
134 } catch (ItemNotFoundException e) {
135 logger.debug("Could not find item for metadata {}", oldMetadata);
140 itemRegistry.addRegistryChangeListener(this);
141 metadataRegistry.addRegistryChangeListener(metadataChangeListener);
142 initialiseRevision();
143 boolean changed = false;
144 for (var i : itemRegistry.getItems()) {
145 String oldValue = knownAccessories.get(i.getName());
146 createRootAccessories(i);
147 if (accessoryChanged(i.getName(), oldValue)) {
148 logger.debug("Accessory {} changed:\n{}\n{}", i.getName(), oldValue, knownAccessories.get(i.getName()));
152 // order of this conditional is important - checkMissingAccessories has side effects that need to always happen
153 if (checkMissingAccessories() || changed) {
154 makeNewConfigurationRevision();
156 logger.info("Created {} HomeKit items in instance {} (no change from prior configuration).",
157 accessoryRegistry.getAllAccessories().size(), instance);
158 if (settings.useDummyAccessories) {
159 checkForDummyAccessories();
164 private void initialiseRevision() {
167 String revisionString = (String) storage.get(REVISION_CONFIG);
168 if (revisionString == null) {
169 throw new NumberFormatException();
171 revision = Integer.parseInt(revisionString);
172 } catch (NumberFormatException e) {
174 accessoryRegistry.setConfigurationRevision(revision);
176 lastAccessoryCount = 0;
177 var localKnownAccessories = (Map<String, String>) storage.get(KNOWN_ACCESSORIES);
178 if (localKnownAccessories == null) {
179 knownAccessories = new HashMap<>();
182 String accessoryCountString = (String) storage.get(ACCESSORY_COUNT);
183 if (accessoryCountString == null) {
184 throw new NumberFormatException();
186 lastAccessoryCount = Integer.parseInt(accessoryCountString);
187 } catch (NumberFormatException e) {
190 knownAccessories = localKnownAccessories;
191 lastAccessoryCount = knownAccessories.size();
195 private boolean hasHomeKitMetadata(Item item) {
196 return metadataRegistry.get(new MetadataKey(HomekitAccessoryFactory.METADATA_KEY, item.getUID())) != null;
200 public synchronized void added(Item item) {
201 if (hasHomeKitMetadata(item)) {
207 public void allItemsChanged(Collection<String> oldItemNames) {
212 * Mark an item as dirty, plus any accessory groups to which it pertains, so that after a debounce period the
213 * accessory update can be applied.
215 * @param item The item that has been changed or removed.
217 private synchronized void markDirty(Item item) {
218 logger.trace("Mark dirty item {}", item.getName());
219 pendingUpdates.add(item.getName());
221 * If findMyAccessoryGroups fails because the accessory group has already been deleted, then we can count on a
222 * later update telling us that the accessory group was removed.
224 for (Item accessoryGroup : HomekitAccessoryFactory.getAccessoryGroups(item, itemRegistry, metadataRegistry)) {
225 pendingUpdates.add(accessoryGroup.getName());
229 * if metadata of a group item was changed, mark all group member as dirty.
231 if (item instanceof GroupItem) {
232 ((GroupItem) item).getMembers().forEach(groupMember -> pendingUpdates.add(groupMember.getName()));
234 applyUpdatesDebouncer.call();
238 public synchronized void removed(Item item) {
239 if (hasHomeKitMetadata(item)) {
244 private Optional<Item> getItemOptional(String name) {
246 return Optional.of(itemRegistry.getItem(name));
247 } catch (ItemNotFoundException e) {
248 return Optional.empty();
252 public void makeNewConfigurationRevision() {
253 final int newRevision = accessoryRegistry.makeNewConfigurationRevision();
254 lastAccessoryCount = accessoryRegistry.getAllAccessories().size();
255 logger.info("Created {} HomeKit items in instance {}.", accessoryRegistry.getAllAccessories().size(), instance);
256 logger.trace("Making new configuration revision {}", newRevision);
257 storage.put(REVISION_CONFIG, "" + newRevision);
258 storage.put(KNOWN_ACCESSORIES, knownAccessories);
261 public synchronized void pruneDummyAccessories() {
262 boolean removed = false;
263 for (HomekitAccessory accessory : accessoryRegistry.getAllAccessories().values()
264 .toArray(new HomekitAccessory[0])) {
265 if (accessory instanceof DummyHomekitAccessory) {
267 String name = accessory.getName().get();
268 logger.info("Pruning dummy accessory {}.", name);
269 knownAccessories.remove(name);
270 accessoryRegistry.remove(name);
272 } catch (ExecutionException | InterruptedException e) {
273 // will never happen; it's a always completed future
278 makeNewConfigurationRevision();
282 private synchronized void applyUpdates() {
283 logger.trace("Apply updates");
285 HomekitRoot bridge = accessoryRegistry.getBridge();
286 if (bridge != null) {
287 bridge.batchUpdate();
291 boolean changed = false;
292 boolean removed = false;
293 for (final String name : pendingUpdates) {
294 String oldValue = knownAccessories.get(name);
295 accessoryRegistry.remove(name);
296 logger.trace(" Add items {}", name);
297 getItemOptional(name).ifPresent(this::createRootAccessories);
298 if (accessoryChanged(name, oldValue)) {
302 pendingUpdates.clear();
303 if (checkMissingAccessories() || changed) {
304 makeNewConfigurationRevision();
306 checkForDummyAccessories();
308 if (bridge != null) {
309 bridge.completeUpdateBatch();
314 private boolean accessoryChanged(String name, @Nullable String oldValue) {
315 String newValue = knownAccessories.get(name);
316 if (oldValue == null && newValue == null) {
319 return oldValue == null && newValue != null || oldValue != null && newValue == null
320 || !oldValue.equals(newValue);
324 public void updated(Item oldElement, Item element) {
325 markDirty(oldElement);
329 public int getLastAccessoryCount() {
330 return lastAccessoryCount;
333 public synchronized void clearAccessories() {
334 accessoryRegistry.clear();
337 public synchronized void setBridge(HomekitRoot bridge) {
338 accessoryRegistry.setBridge(bridge);
341 public void setUpdater(HomekitAccessoryUpdater updater) {
342 this.updater = updater;
345 public void updateSettings(HomekitSettings settings) {
346 boolean wasUsingDummyAccessories = this.settings.useDummyAccessories;
347 this.settings = settings;
348 // If they turned off dummy accessories, immediately prune them
349 if (wasUsingDummyAccessories && !settings.useDummyAccessories) {
350 pruneDummyAccessories();
354 public synchronized void stop() {
355 this.itemRegistry.removeRegistryChangeListener(this);
356 this.metadataRegistry.removeRegistryChangeListener(metadataChangeListener);
357 applyUpdatesDebouncer.stop();
358 accessoryRegistry.unsetBridge();
361 public Map<String, HomekitAccessory> getAccessories() {
362 return this.accessoryRegistry.getAllAccessories();
365 public int getConfigurationRevision() {
366 return this.accessoryRegistry.getConfigurationRevision();
370 * select primary accessory type from list of types.
372 * - if accessory has only one type, it is the primary type
373 * - if accessory has no primary type defined per configuration, then the first type on the list is the primary type
374 * - if accessory has primary type defined per configuration and this type is on the list of types, then it is the
376 * - if accessory has primary type defined per configuration and this type is NOT on the list of types, then the
377 * first type on the list is the primary type
379 * @param item openhab item
380 * @param accessoryTypes list of accessory type attached to the item
381 * @return primary accessory type
383 private HomekitAccessoryType getPrimaryAccessoryType(Item item,
384 List<Entry<HomekitAccessoryType, HomekitCharacteristicType>> accessoryTypes,
385 @Nullable Map<String, Object> configuration) {
386 if (accessoryTypes.size() > 1 && configuration != null) {
387 final @Nullable Object value = configuration.get(HomekitTaggedItem.PRIMARY_SERVICE);
388 if (value instanceof String) {
389 return accessoryTypes.stream()
390 .filter(aType -> ((String) value).equalsIgnoreCase(aType.getKey().getTag())).findAny()
391 .orElse(accessoryTypes.get(0)).getKey();
394 // no primary accessory found or there is only one type, so return the first type from the list
395 return accessoryTypes.get(0).getKey();
399 * creates one or more HomeKit items for given openhab item.
400 * one OpenHAB item can be linked to several HomeKit accessories.
401 * OpenHAB item is a good candidate for a HomeKit accessory
403 * - it has HomeKit accessory types defined using HomeKit accessory metadata
404 * - AND is not part of a group with HomeKit metadata
406 * Switch light "Light" {homekit="Lighting"}
407 * Group gLight "Light Group" {homekit="Lighting"}
410 * - it has HomeKit accessory types defined using HomeKit accessory metadata
411 * - AND is part of groups with HomeKit metadata, but all groups have baseItem
413 * Group:Switch:OR(ON,OFF) gLight "Light Group " {homekit="Lighting"}
414 * Switch light "Light" (gLight) {homekit="Lighting.OnState"}
417 * In contrast, items which are part of groups without BaseItem are additional HomeKit characteristics of the
418 * accessory defined by that group and don't need to be created as accessory here.
420 * Group gLight "Light Group " {homekit="Lighting"}
421 * Switch light "Light" (gLight) {homekit="Lighting.OnState"}
422 * is not the root accessory but only a characteristic "OnState"
425 * // Single line HomeKit Accessory
426 * Switch light "Light" {homekit="Lighting"}
428 * // One HomeKit accessory defined using group
429 * Group gLight "Light Group" {homekit="Lighting"}
430 * Switch light "Light" (gLight) {homekit="Lighting.OnState"}
432 * // 2 HomeKit accessories: one is switch attached to group, another one a single switch
433 * Group:Switch:OR(ON,OFF) gLight "Light Group " {homekit="Lighting"}
434 * Switch light "Light" (gLight) {homekit="Lighting.OnState"}
436 * @param item openHAB item
438 private void createRootAccessories(Item item) {
439 final List<Entry<HomekitAccessoryType, HomekitCharacteristicType>> accessoryTypes = HomekitAccessoryFactory
440 .getAccessoryTypes(item, metadataRegistry);
441 final List<GroupItem> groups = HomekitAccessoryFactory.getAccessoryGroups(item, itemRegistry, metadataRegistry);
442 final @Nullable Map<String, Object> itemConfiguration = HomekitAccessoryFactory.getItemConfiguration(item,
444 if (accessoryTypes.isEmpty() || !(groups.isEmpty() || groups.stream().noneMatch(g -> g.getBaseItem() == null))
445 || !itemIsForThisBridge(item, itemConfiguration)) {
449 final HomekitAccessoryType primaryAccessoryType = getPrimaryAccessoryType(item, accessoryTypes,
451 logger.trace("Item {} is a HomeKit accessory of types {}. Primary type is {}", item.getName(), accessoryTypes,
452 primaryAccessoryType);
453 final HomekitOHItemProxy itemProxy = new HomekitOHItemProxy(item);
454 final HomekitTaggedItem taggedItem = new HomekitTaggedItem(new HomekitOHItemProxy(item), primaryAccessoryType,
457 final AbstractHomekitAccessoryImpl accessory = HomekitAccessoryFactory.create(taggedItem, metadataRegistry,
460 accessoryTypes.stream().filter(aType -> !primaryAccessoryType.equals(aType.getKey()))
461 .forEach(additionalAccessoryType -> {
462 final HomekitTaggedItem additionalTaggedItem = new HomekitTaggedItem(itemProxy,
463 additionalAccessoryType.getKey(), itemConfiguration);
465 final HomekitAccessory additionalAccessory = HomekitAccessoryFactory
466 .create(additionalTaggedItem, metadataRegistry, updater, settings);
467 accessory.getServices().add(additionalAccessory.getPrimaryService());
468 } catch (HomekitException e) {
469 logger.warn("Cannot create additional accessory {}", additionalTaggedItem);
472 knownAccessories.put(taggedItem.getName(), accessory.toJson());
473 accessoryRegistry.addRootAccessory(taggedItem.getName(), accessory);
474 } catch (HomekitException e) {
475 logger.warn("Cannot create accessory {}", taggedItem);
479 private boolean itemIsForThisBridge(Item item, @Nullable Map<String, Object> configuration) {
480 // non-tagged accessories belong to the first instance
481 if (configuration == null) {
482 return (instance == 1);
485 final @Nullable Object value = configuration.get(HomekitTaggedItem.INSTANCE);
487 return (instance == 1);
489 if (value instanceof Number) {
490 return (instance == ((Number) value).intValue());
492 logger.warn("Unrecognized instance tag {} ({}) for item {}; assigning to default instance.", value,
493 value.getClass(), item.getName());
494 return (instance == 1);
498 * Check for any missing accessories.
500 * If there are, return true so we know to increment the config version. UNLESS
501 * we're configured to use dummy accessories, in which case backfill it with a dummy.
503 * @return if we need to increment the configuration version
505 private boolean checkMissingAccessories() {
506 List<String> toRemove = new ArrayList<>();
507 for (Map.Entry<String, String> accessory : knownAccessories.entrySet()) {
508 if (!accessoryRegistry.getAllAccessories().containsKey(accessory.getKey())) {
509 if (settings.useDummyAccessories) {
510 logger.debug("Creating dummy accessory for missing item {}.", accessory.getKey());
511 accessoryRegistry.addRootAccessory(accessory.getKey(),
512 new DummyHomekitAccessory(accessory.getKey(), accessory.getValue()));
514 toRemove.add(accessory.getKey());
519 toRemove.forEach(k -> knownAccessories.remove(k));
520 return !toRemove.isEmpty();
523 private void checkForDummyAccessories() {
524 List<String> currentDummies = accessoryRegistry.getAllAccessories().values().stream()
525 .filter(a -> a instanceof DummyHomekitAccessory).map(a -> {
527 return a.getSerialNumber().get();
528 } catch (InterruptedException | ExecutionException e) {
531 }).collect(Collectors.toList());
533 List<String> resolvedDummies = new ArrayList(priorDummies);
534 resolvedDummies.removeAll(currentDummies);
535 List<String> newDummies = new ArrayList(currentDummies);
536 newDummies.removeAll(priorDummies);
538 if (resolvedDummies.size() <= 5) {
539 for (String item : resolvedDummies) {
540 logger.info("{} has been resolved to an actual accessory, and is no longer a dummy.", item);
542 } else if (currentDummies.isEmpty() && !resolvedDummies.isEmpty()) {
543 logger.info("All dummy accessories have been resolved to actual accessories.");
544 } else if (!resolvedDummies.isEmpty()) {
545 logger.info("{} dummy accessories have been resolved to actual accessories.", resolvedDummies.size());
548 if (newDummies.size() <= 5) {
549 for (String item : newDummies) {
551 "{} has been replaced with a dummy. See https://www.openhab.org/addons/integrations/homekit/#dummy-accessories for more information.",
554 } else if (!newDummies.isEmpty()) {
556 "{} accessories have been replaced with dummies. See https://www.openhab.org/addons/integrations/homekit/#dummy-accessories for more information.",
558 } else if (!currentDummies.isEmpty()) {
560 "{} accessories are still dummies. See https://www.openhab.org/addons/integrations/homekit/#dummy-accessories for more information.",
561 currentDummies.size());
563 priorDummies.clear();
564 priorDummies.addAll(currentDummies);