]> git.basschouten.com Git - openhab-addons.git/blob
970669b494f742006ec20b9c3fabc59533b29e87
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.io.homekit.internal;
14
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;
22 import java.util.Map;
23 import java.util.Map.Entry;
24 import java.util.Optional;
25 import java.util.Set;
26 import java.util.concurrent.ExecutionException;
27 import java.util.concurrent.ScheduledExecutorService;
28 import java.util.stream.Collectors;
29
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;
48
49 import io.github.hapjava.accessories.HomekitAccessory;
50 import io.github.hapjava.server.impl.HomekitRoot;
51
52 /**
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.
55  *
56  * @author Andy Lintner - Initial contribution
57  */
58 @NonNullByDefault
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<>();
73     private int instance;
74     private List<String> priorDummies = new ArrayList<>();
75
76     private final Set<String> pendingUpdates = new HashSet<>();
77
78     private final ScheduledExecutorService scheduler = ThreadPoolManager
79             .getScheduledPool(ThreadPoolManager.THREAD_POOL_NAME_COMMON);
80
81     /**
82      * Rather than reacting to item added/removed/modified changes directly, we mark them as dirty (and the groups to
83      * which they belong)
84      *
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
87      * group is complete.
88      */
89     private final Debouncer applyUpdatesDebouncer;
90
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>() {
101             @Override
102             public void added(final Metadata metadata) {
103                 final MetadataKey uid = metadata.getUID();
104                 if (HomekitAccessoryFactory.METADATA_KEY.equalsIgnoreCase(uid.getNamespace())) {
105                     try {
106                         markDirty(itemRegistry.getItem(uid.getItemName()));
107                     } catch (ItemNotFoundException e) {
108                         logger.trace("Could not find item for metadata {}", metadata);
109                     }
110                 }
111             }
112
113             @Override
114             public void removed(final Metadata metadata) {
115                 final MetadataKey uid = metadata.getUID();
116                 if (HomekitAccessoryFactory.METADATA_KEY.equalsIgnoreCase(uid.getNamespace())) {
117                     try {
118                         markDirty(itemRegistry.getItem(uid.getItemName()));
119                     } catch (ItemNotFoundException e) {
120                         logger.trace("Could not find item for metadata {}", metadata);
121                     }
122                 }
123             }
124
125             @Override
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())) {
131                     try {
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);
136                     }
137                 }
138             }
139         };
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()));
149                 changed = true;
150             }
151         }
152         // order of this conditional is important - checkMissingAccessories has side effects that need to always happen
153         if (checkMissingAccessories() || changed) {
154             makeNewConfigurationRevision();
155         } else {
156             logger.info("Created {} HomeKit items in instance {} (no change from prior configuration).",
157                     accessoryRegistry.getAllAccessories().size(), instance);
158             if (settings.useDummyAccessories) {
159                 checkForDummyAccessories();
160             }
161         }
162     }
163
164     private void initialiseRevision() {
165         int revision = 1;
166         try {
167             String revisionString = (String) storage.get(REVISION_CONFIG);
168             if (revisionString == null) {
169                 throw new NumberFormatException();
170             }
171             revision = Integer.parseInt(revisionString);
172         } catch (NumberFormatException e) {
173         }
174         accessoryRegistry.setConfigurationRevision(revision);
175
176         lastAccessoryCount = 0;
177         var localKnownAccessories = (Map<String, String>) storage.get(KNOWN_ACCESSORIES);
178         if (localKnownAccessories == null) {
179             knownAccessories = new HashMap<>();
180             // Back-compat
181             try {
182                 String accessoryCountString = (String) storage.get(ACCESSORY_COUNT);
183                 if (accessoryCountString == null) {
184                     throw new NumberFormatException();
185                 }
186                 lastAccessoryCount = Integer.parseInt(accessoryCountString);
187             } catch (NumberFormatException e) {
188             }
189         } else {
190             knownAccessories = localKnownAccessories;
191             lastAccessoryCount = knownAccessories.size();
192         }
193     }
194
195     private boolean hasHomeKitMetadata(Item item) {
196         return metadataRegistry.get(new MetadataKey(HomekitAccessoryFactory.METADATA_KEY, item.getUID())) != null;
197     }
198
199     @Override
200     public synchronized void added(Item item) {
201         if (hasHomeKitMetadata(item)) {
202             markDirty(item);
203         }
204     }
205
206     @Override
207     public void allItemsChanged(Collection<String> oldItemNames) {
208         clearAccessories();
209     }
210
211     /**
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.
214      *
215      * @param item The item that has been changed or removed.
216      */
217     private synchronized void markDirty(Item item) {
218         logger.trace("Mark dirty item {}", item.getName());
219         pendingUpdates.add(item.getName());
220         /*
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.
223          */
224         for (Item accessoryGroup : HomekitAccessoryFactory.getAccessoryGroups(item, itemRegistry, metadataRegistry)) {
225             pendingUpdates.add(accessoryGroup.getName());
226         }
227
228         /*
229          * if metadata of a group item was changed, mark all group member as dirty.
230          */
231         if (item instanceof GroupItem) {
232             ((GroupItem) item).getMembers().forEach(groupMember -> pendingUpdates.add(groupMember.getName()));
233         }
234         applyUpdatesDebouncer.call();
235     }
236
237     @Override
238     public synchronized void removed(Item item) {
239         if (hasHomeKitMetadata(item)) {
240             markDirty(item);
241         }
242     }
243
244     private Optional<Item> getItemOptional(String name) {
245         try {
246             return Optional.of(itemRegistry.getItem(name));
247         } catch (ItemNotFoundException e) {
248             return Optional.empty();
249         }
250     }
251
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);
259     }
260
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) {
266                 try {
267                     String name = accessory.getName().get();
268                     logger.info("Pruning dummy accessory {}.", name);
269                     knownAccessories.remove(name);
270                     accessoryRegistry.remove(name);
271                     removed = true;
272                 } catch (ExecutionException | InterruptedException e) {
273                     // will never happen; it's a always completed future
274                 }
275             }
276         }
277         if (removed) {
278             makeNewConfigurationRevision();
279         }
280     }
281
282     private synchronized void applyUpdates() {
283         logger.trace("Apply updates");
284
285         HomekitRoot bridge = accessoryRegistry.getBridge();
286         if (bridge != null) {
287             bridge.batchUpdate();
288         }
289
290         try {
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)) {
299                     changed = true;
300                 }
301             }
302             pendingUpdates.clear();
303             if (checkMissingAccessories() || changed) {
304                 makeNewConfigurationRevision();
305             }
306             checkForDummyAccessories();
307         } finally {
308             if (bridge != null) {
309                 bridge.completeUpdateBatch();
310             }
311         }
312     }
313
314     private boolean accessoryChanged(String name, @Nullable String oldValue) {
315         String newValue = knownAccessories.get(name);
316         if (oldValue == null && newValue == null) {
317             return false;
318         }
319         return oldValue == null && newValue != null || oldValue != null && newValue == null
320                 || !oldValue.equals(newValue);
321     }
322
323     @Override
324     public void updated(Item oldElement, Item element) {
325         markDirty(oldElement);
326         markDirty(element);
327     }
328
329     public int getLastAccessoryCount() {
330         return lastAccessoryCount;
331     }
332
333     public synchronized void clearAccessories() {
334         accessoryRegistry.clear();
335     }
336
337     public synchronized void setBridge(HomekitRoot bridge) {
338         accessoryRegistry.setBridge(bridge);
339     }
340
341     public void setUpdater(HomekitAccessoryUpdater updater) {
342         this.updater = updater;
343     }
344
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();
351         }
352     }
353
354     public synchronized void stop() {
355         this.itemRegistry.removeRegistryChangeListener(this);
356         this.metadataRegistry.removeRegistryChangeListener(metadataChangeListener);
357         applyUpdatesDebouncer.stop();
358         accessoryRegistry.unsetBridge();
359     }
360
361     public Map<String, HomekitAccessory> getAccessories() {
362         return this.accessoryRegistry.getAllAccessories();
363     }
364
365     public int getConfigurationRevision() {
366         return this.accessoryRegistry.getConfigurationRevision();
367     }
368
369     /**
370      * select primary accessory type from list of types.
371      * selection logic:
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
375      * primary
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
378      *
379      * @param item openhab item
380      * @param accessoryTypes list of accessory type attached to the item
381      * @return primary accessory type
382      */
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();
392             }
393         }
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();
396     }
397
398     /**
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
402      * IF
403      * - it has HomeKit accessory types defined using HomeKit accessory metadata
404      * - AND is not part of a group with HomeKit metadata
405      * e.g.
406      * Switch light "Light" {homekit="Lighting"}
407      * Group gLight "Light Group" {homekit="Lighting"}
408      *
409      * OR
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
412      * e.g.
413      * Group:Switch:OR(ON,OFF) gLight "Light Group " {homekit="Lighting"}
414      * Switch light "Light" (gLight) {homekit="Lighting.OnState"}
415      *
416      *
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.
419      * e.g.
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"
423      *
424      * Examples:
425      * // Single line HomeKit Accessory
426      * Switch light "Light" {homekit="Lighting"}
427      *
428      * // One HomeKit accessory defined using group
429      * Group gLight "Light Group" {homekit="Lighting"}
430      * Switch light "Light" (gLight) {homekit="Lighting.OnState"}
431      *
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"}
435      *
436      * @param item openHAB item
437      */
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,
443                 metadataRegistry);
444         if (accessoryTypes.isEmpty() || !(groups.isEmpty() || groups.stream().noneMatch(g -> g.getBaseItem() == null))
445                 || !itemIsForThisBridge(item, itemConfiguration)) {
446             return;
447         }
448
449         final HomekitAccessoryType primaryAccessoryType = getPrimaryAccessoryType(item, accessoryTypes,
450                 itemConfiguration);
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,
455                 itemConfiguration);
456         try {
457             final AbstractHomekitAccessoryImpl accessory = HomekitAccessoryFactory.create(taggedItem, metadataRegistry,
458                     updater, settings);
459
460             accessoryTypes.stream().filter(aType -> !primaryAccessoryType.equals(aType.getKey()))
461                     .forEach(additionalAccessoryType -> {
462                         final HomekitTaggedItem additionalTaggedItem = new HomekitTaggedItem(itemProxy,
463                                 additionalAccessoryType.getKey(), itemConfiguration);
464                         try {
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);
470                         }
471                     });
472             knownAccessories.put(taggedItem.getName(), accessory.toJson());
473             accessoryRegistry.addRootAccessory(taggedItem.getName(), accessory);
474         } catch (HomekitException e) {
475             logger.warn("Cannot create accessory {}", taggedItem);
476         }
477     }
478
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);
483         }
484
485         final @Nullable Object value = configuration.get(HomekitTaggedItem.INSTANCE);
486         if (value == null) {
487             return (instance == 1);
488         }
489         if (value instanceof Number) {
490             return (instance == ((Number) value).intValue());
491         }
492         logger.warn("Unrecognized instance tag {} ({}) for item {}; assigning to default instance.", value,
493                 value.getClass(), item.getName());
494         return (instance == 1);
495     }
496
497     /**
498      * Check for any missing accessories.
499      *
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.
502      *
503      * @return if we need to increment the configuration version
504      */
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()));
513                 } else {
514                     toRemove.add(accessory.getKey());
515                 }
516             }
517         }
518
519         toRemove.forEach(k -> knownAccessories.remove(k));
520         return !toRemove.isEmpty();
521     }
522
523     private void checkForDummyAccessories() {
524         List<String> currentDummies = accessoryRegistry.getAllAccessories().values().stream()
525                 .filter(a -> a instanceof DummyHomekitAccessory).map(a -> {
526                     try {
527                         return a.getSerialNumber().get();
528                     } catch (InterruptedException | ExecutionException e) {
529                         return "<unknown>";
530                     }
531                 }).collect(Collectors.toList());
532
533         List<String> resolvedDummies = new ArrayList(priorDummies);
534         resolvedDummies.removeAll(currentDummies);
535         List<String> newDummies = new ArrayList(currentDummies);
536         newDummies.removeAll(priorDummies);
537
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);
541             }
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());
546         }
547
548         if (newDummies.size() <= 5) {
549             for (String item : newDummies) {
550                 logger.warn(
551                         "{} has been replaced with a dummy. See https://www.openhab.org/addons/integrations/homekit/#dummy-accessories for more information.",
552                         item);
553             }
554         } else if (!newDummies.isEmpty()) {
555             logger.warn(
556                     "{} accessories have been replaced with dummies. See https://www.openhab.org/addons/integrations/homekit/#dummy-accessories for more information.",
557                     newDummies.size());
558         } else if (!currentDummies.isEmpty()) {
559             logger.warn(
560                     "{} accessories are still dummies. See https://www.openhab.org/addons/integrations/homekit/#dummy-accessories for more information.",
561                     currentDummies.size());
562         }
563         priorDummies.clear();
564         priorDummies.addAll(currentDummies);
565     }
566 }