]> git.basschouten.com Git - openhab-addons.git/blob
298ccca8d892b27629cbd082fef3515f962025dc
[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.accessories;
14
15 import static org.openhab.io.homekit.internal.HomekitAccessoryType.*;
16 import static org.openhab.io.homekit.internal.HomekitCharacteristicType.*;
17
18 import java.lang.reflect.InvocationTargetException;
19 import java.util.AbstractMap.SimpleEntry;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Map.Entry;
27 import java.util.Objects;
28 import java.util.Optional;
29 import java.util.stream.Collectors;
30 import java.util.stream.Stream;
31
32 import org.eclipse.jdt.annotation.NonNullByDefault;
33 import org.eclipse.jdt.annotation.Nullable;
34 import org.openhab.core.items.GenericItem;
35 import org.openhab.core.items.GroupItem;
36 import org.openhab.core.items.Item;
37 import org.openhab.core.items.ItemRegistry;
38 import org.openhab.core.items.Metadata;
39 import org.openhab.core.items.MetadataKey;
40 import org.openhab.core.items.MetadataRegistry;
41 import org.openhab.io.homekit.internal.HomekitAccessoryType;
42 import org.openhab.io.homekit.internal.HomekitAccessoryUpdater;
43 import org.openhab.io.homekit.internal.HomekitCharacteristicType;
44 import org.openhab.io.homekit.internal.HomekitException;
45 import org.openhab.io.homekit.internal.HomekitOHItemProxy;
46 import org.openhab.io.homekit.internal.HomekitSettings;
47 import org.openhab.io.homekit.internal.HomekitTaggedItem;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 import io.github.hapjava.accessories.HomekitAccessory;
52 import io.github.hapjava.characteristics.Characteristic;
53 import io.github.hapjava.services.Service;
54
55 /**
56  * Creates a HomekitAccessory for a given HomekitTaggedItem.
57  *
58  * @author Andy Lintner - Initial contribution
59  * @author Eugen Freiter - refactoring for optional characteristics
60  */
61 @NonNullByDefault
62 public class HomekitAccessoryFactory {
63     private static final Logger logger = LoggerFactory.getLogger(HomekitAccessoryFactory.class);
64     public final static String METADATA_KEY = "homekit"; // prefix for HomeKit meta information in items.xml
65
66     /** List of mandatory attributes for each accessory type. **/
67     private final static Map<HomekitAccessoryType, HomekitCharacteristicType[]> MANDATORY_CHARACTERISTICS = new HashMap<HomekitAccessoryType, HomekitCharacteristicType[]>() {
68         {
69             put(LEAK_SENSOR, new HomekitCharacteristicType[] { LEAK_DETECTED_STATE });
70             put(MOTION_SENSOR, new HomekitCharacteristicType[] { MOTION_DETECTED_STATE });
71             put(OCCUPANCY_SENSOR, new HomekitCharacteristicType[] { OCCUPANCY_DETECTED_STATE });
72             put(CONTACT_SENSOR, new HomekitCharacteristicType[] { CONTACT_SENSOR_STATE });
73             put(SMOKE_SENSOR, new HomekitCharacteristicType[] { SMOKE_DETECTED_STATE });
74             put(HUMIDITY_SENSOR, new HomekitCharacteristicType[] { RELATIVE_HUMIDITY });
75             put(AIR_QUALITY_SENSOR, new HomekitCharacteristicType[] { AIR_QUALITY });
76             put(SWITCH, new HomekitCharacteristicType[] { ON_STATE });
77             put(CARBON_DIOXIDE_SENSOR, new HomekitCharacteristicType[] { CARBON_DIOXIDE_DETECTED_STATE });
78             put(CARBON_MONOXIDE_SENSOR, new HomekitCharacteristicType[] { CARBON_MONOXIDE_DETECTED_STATE });
79             put(WINDOW_COVERING, new HomekitCharacteristicType[] { TARGET_POSITION, CURRENT_POSITION, POSITION_STATE });
80             put(LIGHTBULB, new HomekitCharacteristicType[] { ON_STATE });
81             put(FAN, new HomekitCharacteristicType[] { ACTIVE_STATUS });
82             put(LIGHT_SENSOR, new HomekitCharacteristicType[] { LIGHT_LEVEL });
83             put(TEMPERATURE_SENSOR, new HomekitCharacteristicType[] { CURRENT_TEMPERATURE });
84             put(THERMOSTAT, new HomekitCharacteristicType[] { CURRENT_HEATING_COOLING_STATE,
85                     TARGET_HEATING_COOLING_STATE, CURRENT_TEMPERATURE, TARGET_TEMPERATURE });
86             put(LOCK, new HomekitCharacteristicType[] { LOCK_CURRENT_STATE, LOCK_TARGET_STATE });
87             put(VALVE, new HomekitCharacteristicType[] { ACTIVE_STATUS, INUSE_STATUS });
88             put(SECURITY_SYSTEM,
89                     new HomekitCharacteristicType[] { SECURITY_SYSTEM_CURRENT_STATE, SECURITY_SYSTEM_TARGET_STATE });
90             put(OUTLET, new HomekitCharacteristicType[] { ON_STATE, INUSE_STATUS });
91             put(SPEAKER, new HomekitCharacteristicType[] { MUTE });
92             put(SMART_SPEAKER, new HomekitCharacteristicType[] { CURRENT_MEDIA_STATE, TARGET_MEDIA_STATE });
93             put(GARAGE_DOOR_OPENER,
94                     new HomekitCharacteristicType[] { CURRENT_DOOR_STATE, TARGET_DOOR_STATE, OBSTRUCTION_STATUS });
95             put(HEATER_COOLER, new HomekitCharacteristicType[] { ACTIVE_STATUS, CURRENT_HEATER_COOLER_STATE,
96                     TARGET_HEATER_COOLER_STATE, CURRENT_TEMPERATURE });
97             put(WINDOW, new HomekitCharacteristicType[] { CURRENT_POSITION, TARGET_POSITION, POSITION_STATE });
98             put(DOOR, new HomekitCharacteristicType[] { CURRENT_POSITION, TARGET_POSITION, POSITION_STATE });
99             put(BATTERY, new HomekitCharacteristicType[] { BATTERY_LEVEL, BATTERY_LOW_STATUS });
100             put(FILTER_MAINTENANCE, new HomekitCharacteristicType[] { FILTER_CHANGE_INDICATION });
101             put(SLAT, new HomekitCharacteristicType[] { CURRENT_SLAT_STATE });
102             put(FAUCET, new HomekitCharacteristicType[] { ACTIVE_STATUS });
103             put(MICROPHONE, new HomekitCharacteristicType[] { MUTE });
104         }
105     };
106
107     /** List of service implementation for each accessory type. **/
108     private final static Map<HomekitAccessoryType, Class<? extends AbstractHomekitAccessoryImpl>> SERVICE_IMPL_MAP = new HashMap<HomekitAccessoryType, Class<? extends AbstractHomekitAccessoryImpl>>() {
109         {
110             put(LEAK_SENSOR, HomekitLeakSensorImpl.class);
111             put(MOTION_SENSOR, HomekitMotionSensorImpl.class);
112             put(OCCUPANCY_SENSOR, HomekitOccupancySensorImpl.class);
113             put(CONTACT_SENSOR, HomekitContactSensorImpl.class);
114             put(SMOKE_SENSOR, HomekitSmokeSensorImpl.class);
115             put(HUMIDITY_SENSOR, HomekitHumiditySensorImpl.class);
116             put(AIR_QUALITY_SENSOR, HomekitAirQualitySensorImpl.class);
117             put(SWITCH, HomekitSwitchImpl.class);
118             put(CARBON_DIOXIDE_SENSOR, HomekitCarbonDioxideSensorImpl.class);
119             put(CARBON_MONOXIDE_SENSOR, HomekitCarbonMonoxideSensorImpl.class);
120             put(WINDOW_COVERING, HomekitWindowCoveringImpl.class);
121             put(LIGHTBULB, HomekitLightbulbImpl.class);
122             put(FAN, HomekitFanImpl.class);
123             put(LIGHT_SENSOR, HomekitLightSensorImpl.class);
124             put(TEMPERATURE_SENSOR, HomekitTemperatureSensorImpl.class);
125             put(THERMOSTAT, HomekitThermostatImpl.class);
126             put(LOCK, HomekitLockImpl.class);
127             put(VALVE, HomekitValveImpl.class);
128             put(SECURITY_SYSTEM, HomekitSecuritySystemImpl.class);
129             put(OUTLET, HomekitOutletImpl.class);
130             put(SPEAKER, HomekitSpeakerImpl.class);
131             put(SMART_SPEAKER, HomekitSmartSpeakerImpl.class);
132             put(GARAGE_DOOR_OPENER, HomekitGarageDoorOpenerImpl.class);
133             put(DOOR, HomekitDoorImpl.class);
134             put(WINDOW, HomekitWindowImpl.class);
135             put(HEATER_COOLER, HomekitHeaterCoolerImpl.class);
136             put(BATTERY, HomekitBatteryImpl.class);
137             put(FILTER_MAINTENANCE, HomekitFilterMaintenanceImpl.class);
138             put(SLAT, HomekitSlatImpl.class);
139             put(FAUCET, HomekitFaucetImpl.class);
140             put(MICROPHONE, HomekitMicrophoneImpl.class);
141         }
142     };
143
144     private static List<HomekitCharacteristicType> getRequiredCharacteristics(HomekitTaggedItem taggedItem) {
145         final List<HomekitCharacteristicType> characteristics = new ArrayList<>();
146         if (MANDATORY_CHARACTERISTICS.containsKey(taggedItem.getAccessoryType())) {
147             characteristics.addAll(Arrays.asList(MANDATORY_CHARACTERISTICS.get(taggedItem.getAccessoryType())));
148         }
149         if (taggedItem.getAccessoryType() == BATTERY) {
150             final boolean isChargeable = taggedItem.getConfigurationAsBoolean(HomekitBatteryImpl.BATTERY_TYPE, false);
151             if (isChargeable) {
152                 characteristics.add(BATTERY_CHARGING_STATE);
153             }
154         }
155         return characteristics;
156     }
157
158     /**
159      * creates HomeKit accessory for an openhab item.
160      *
161      * @param taggedItem openhab item tagged as HomeKit item
162      * @param metadataRegistry openhab metadata registry required to get item meta information
163      * @param updater OH HomeKit update class that ensure the status sync between OH item and corresponding HomeKit
164      *            characteristic.
165      * @param settings OH settings
166      * @return HomeKit accessory
167      * @throws HomekitException exception in case HomeKit accessory could not be created, e.g. due missing mandatory
168      *             characteristic
169      */
170     @SuppressWarnings("null")
171     public static HomekitAccessory create(HomekitTaggedItem taggedItem, MetadataRegistry metadataRegistry,
172             HomekitAccessoryUpdater updater, HomekitSettings settings) throws HomekitException {
173         final HomekitAccessoryType accessoryType = taggedItem.getAccessoryType();
174         logger.trace("Constructing {} of accessory type {}", taggedItem.getName(), accessoryType.getTag());
175         final List<HomekitTaggedItem> foundCharacteristics = getMandatoryCharacteristicsFromItem(taggedItem,
176                 metadataRegistry);
177         final List<HomekitCharacteristicType> mandatoryCharacteristics = getRequiredCharacteristics(taggedItem);
178         if (foundCharacteristics.size() < mandatoryCharacteristics.size()) {
179             logger.warn("Accessory of type {} must have following characteristics {}. Found only {}",
180                     accessoryType.getTag(), mandatoryCharacteristics, foundCharacteristics);
181             throw new HomekitException("Missing mandatory characteristics");
182         }
183         AbstractHomekitAccessoryImpl accessoryImpl;
184         try {
185             final @Nullable Class<? extends AbstractHomekitAccessoryImpl> accessoryImplClass = SERVICE_IMPL_MAP
186                     .get(accessoryType);
187             if (accessoryImplClass != null) {
188                 accessoryImpl = accessoryImplClass.getConstructor(HomekitTaggedItem.class, List.class,
189                         HomekitAccessoryUpdater.class, HomekitSettings.class)
190                         .newInstance(taggedItem, foundCharacteristics, updater, settings);
191                 addOptionalCharacteristics(taggedItem, accessoryImpl, metadataRegistry);
192                 return accessoryImpl;
193             } else {
194                 logger.warn("Unsupported HomeKit type: {}", accessoryType.getTag());
195                 throw new HomekitException("Unsupported HomeKit type: " + accessoryType);
196             }
197         } catch (NoSuchMethodException | IllegalAccessException | InstantiationException
198                 | InvocationTargetException e) {
199             logger.warn("Cannot instantiate accessory implementation for accessory {}", accessoryType.getTag(), e);
200             throw new HomekitException("Cannot instantiate accessory implementation for accessory " + accessoryType);
201         }
202     }
203
204     /**
205      * return HomeKit accessory types for an OH item based on meta data
206      *
207      * @param item OH item
208      * @param metadataRegistry meta data registry
209      * @return list of HomeKit accessory types and characteristics.
210      */
211     public static List<Entry<HomekitAccessoryType, HomekitCharacteristicType>> getAccessoryTypes(Item item,
212             MetadataRegistry metadataRegistry) {
213         final List<Entry<HomekitAccessoryType, HomekitCharacteristicType>> accessories = new ArrayList<>();
214         final @Nullable Metadata metadata = metadataRegistry.get(new MetadataKey(METADATA_KEY, item.getUID()));
215         if (metadata != null) {
216             String[] tags = metadata.getValue().split(",");
217             for (String tag : tags) {
218                 final String[] meta = tag.split("\\.");
219                 Optional<HomekitAccessoryType> accessoryType = HomekitAccessoryType.valueOfTag(meta[0].trim());
220                 if (accessoryType.isPresent()) { // it accessory, check for characteristic
221                     HomekitAccessoryType type = accessoryType.get();
222                     if (meta.length > 1) {
223                         // it has characteristic as well
224                         accessories.add(new SimpleEntry<>(type,
225                                 HomekitCharacteristicType.valueOfTag(meta[1].trim()).orElse(EMPTY)));
226                     } else {// it has no characteristic
227                         accessories.add(new SimpleEntry<>(type, EMPTY));
228                     }
229                 } else { // it is no accessory, so, maybe it is a characteristic
230                     HomekitCharacteristicType.valueOfTag(meta[0].trim())
231                             .ifPresent(c -> accessories.add(new SimpleEntry<>(DUMMY, c)));
232                 }
233             }
234         }
235         return accessories;
236     }
237
238     public static @Nullable Map<String, Object> getItemConfiguration(Item item, MetadataRegistry metadataRegistry) {
239         final @Nullable Metadata metadata = metadataRegistry.get(new MetadataKey(METADATA_KEY, item.getUID()));
240         return metadata != null ? metadata.getConfiguration() : null;
241     }
242
243     /**
244      * return list of HomeKit relevant groups linked to an accessory
245      *
246      * @param item OH item
247      * @param itemRegistry item registry
248      * @param metadataRegistry metadata registry
249      * @return list of relevant group items
250      */
251     public static List<GroupItem> getAccessoryGroups(Item item, ItemRegistry itemRegistry,
252             MetadataRegistry metadataRegistry) {
253         return (item instanceof GroupItem) ? Collections.emptyList() : item.getGroupNames().stream().flatMap(name -> {
254             final @Nullable Item groupItem = itemRegistry.get(name);
255             if ((groupItem instanceof GroupItem) && ((GroupItem) groupItem).getBaseItem() == null) {
256                 return Stream.of((GroupItem) groupItem);
257             } else {
258                 return Stream.empty();
259             }
260         }).filter(groupItem -> !getAccessoryTypes(groupItem, metadataRegistry).isEmpty()).collect(Collectors.toList());
261     }
262
263     /**
264      * collect all mandatory characteristics for a given tagged item, e.g. collect all mandatory HomeKit items from a
265      * GroupItem
266      *
267      * @param taggedItem HomeKit tagged item
268      * @param metadataRegistry meta data registry
269      * @return list of mandatory
270      */
271     private static List<HomekitTaggedItem> getMandatoryCharacteristicsFromItem(HomekitTaggedItem taggedItem,
272             MetadataRegistry metadataRegistry) {
273         List<HomekitTaggedItem> collectedCharacteristics = new ArrayList<>();
274         if (taggedItem.isGroup()) {
275             for (Item item : ((GroupItem) taggedItem.getItem()).getMembers()) {
276                 addMandatoryCharacteristics(taggedItem, collectedCharacteristics, item, metadataRegistry);
277             }
278         } else {
279             addMandatoryCharacteristics(taggedItem, collectedCharacteristics, taggedItem.getItem(), metadataRegistry);
280         }
281         logger.trace("Mandatory characteristics: {}", collectedCharacteristics);
282         return collectedCharacteristics;
283     }
284
285     /**
286      * add mandatory HomeKit items for a given main item to a list of characteristics.
287      * Main item is use only to determine, which characteristics are mandatory.
288      * The characteristics are added to item.
289      * e.g. mainItem could be a group tagged as "thermostat" and item could be item linked to the group and marked as
290      * TargetTemperature
291      *
292      * @param mainItem main item
293      * @param characteristics list of characteristics
294      * @param item current item
295      * @param metadataRegistry meta date registry
296      */
297     private static void addMandatoryCharacteristics(HomekitTaggedItem mainItem, List<HomekitTaggedItem> characteristics,
298             Item item, MetadataRegistry metadataRegistry) {
299         // get list of mandatory characteristics
300         List<HomekitCharacteristicType> mandatoryCharacteristics = getRequiredCharacteristics(mainItem);
301         if (mandatoryCharacteristics.isEmpty()) {
302             // no mandatory characteristics linked to accessory type of mainItem. we are done
303             return;
304         }
305         // check whether we are adding characteristic to the main item, and if yes, use existing item proxy.
306         // if we are adding not to the main item (typical for groups), create new proxy item.
307         final HomekitOHItemProxy itemProxy = mainItem.getItem().equals(item) ? mainItem.getProxyItem()
308                 : new HomekitOHItemProxy(item);
309         // an item can have several tags, e.g. "ActiveStatus, InUse". we iterate here over all his tags
310         for (Entry<HomekitAccessoryType, HomekitCharacteristicType> accessory : getAccessoryTypes(item,
311                 metadataRegistry)) {
312             // if the item has only accessory tag, e.g. TemperatureSensor,
313             // then we will link all mandatory characteristic to this item,
314             // e.g. we will link CurrentTemperature in case of TemperatureSensor.
315             // Note that accessories that are members of other accessories do _not_
316             // count - we're already constructing another root accessory.
317             if (isRootAccessory(accessory) && mainItem.getItem().equals(item)) {
318                 mandatoryCharacteristics.forEach(c -> characteristics.add(new HomekitTaggedItem(itemProxy,
319                         accessory.getKey(), c, mainItem.isGroup() ? (GroupItem) mainItem.getItem() : null,
320                         HomekitAccessoryFactory.getItemConfiguration(item, metadataRegistry))));
321             } else {
322                 // item has characteristic tag on it, so, adding it as that characteristic.
323
324                 final HomekitCharacteristicType characteristic = accessory.getValue();
325
326                 // check whether it is a mandatory characteristic. optional will be added later by another method.
327                 if (belongsToType(mainItem.getAccessoryType(), accessory)
328                         && isMandatoryCharacteristic(mainItem, characteristic)) {
329                     characteristics.add(new HomekitTaggedItem(itemProxy, accessory.getKey(), characteristic,
330                             mainItem.isGroup() ? (GroupItem) mainItem.getItem() : null,
331                             HomekitAccessoryFactory.getItemConfiguration(item, metadataRegistry)));
332                 }
333             }
334         }
335     }
336
337     /**
338      * add optional characteristic for given accessory.
339      *
340      * @param taggedItem main item
341      * @param accessory accessory
342      * @param metadataRegistry metadata registry
343      */
344     private static void addOptionalCharacteristics(HomekitTaggedItem taggedItem, AbstractHomekitAccessoryImpl accessory,
345             MetadataRegistry metadataRegistry) {
346         Map<HomekitCharacteristicType, GenericItem> characteristics = getOptionalCharacteristics(
347                 accessory.getRootAccessory(), metadataRegistry);
348         Service service = accessory.getPrimaryService();
349         HashMap<String, HomekitOHItemProxy> proxyItems = new HashMap<>();
350         proxyItems.put(taggedItem.getItem().getUID(), taggedItem.getProxyItem());
351         // an accessory can have multiple optional characteristics. iterate over them.
352         characteristics.forEach((type, item) -> {
353             try {
354                 // check whether a proxyItem already exists, if not create one.
355                 final HomekitOHItemProxy proxyItem = Objects
356                         .requireNonNull(proxyItems.computeIfAbsent(item.getUID(), k -> new HomekitOHItemProxy(item)));
357                 final HomekitTaggedItem optionalItem = new HomekitTaggedItem(proxyItem,
358                         accessory.getRootAccessory().getAccessoryType(), type,
359                         accessory.getRootAccessory().getRootDeviceGroupItem(),
360                         getItemConfiguration(item, metadataRegistry));
361                 final Characteristic characteristic = HomekitCharacteristicFactory.createCharacteristic(optionalItem,
362                         accessory.getUpdater());
363                 // find the corresponding add method at service and call it.
364                 service.getClass().getMethod("addOptionalCharacteristic", characteristic.getClass()).invoke(service,
365                         characteristic);
366                 accessory.addCharacteristic(optionalItem);
367             } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | HomekitException e) {
368                 logger.warn("Unsupported optional HomeKit characteristic: service type {}, characteristic type {}",
369                         service.getType(), type.getTag());
370             }
371         });
372     }
373
374     /**
375      * collect optional HomeKit characteristics for an OH item.
376      *
377      * @param taggedItem main OH item
378      * @param metadataRegistry OH metadata registry
379      * @return a map with characteristics and corresponding OH items
380      */
381     private static Map<HomekitCharacteristicType, GenericItem> getOptionalCharacteristics(HomekitTaggedItem taggedItem,
382             MetadataRegistry metadataRegistry) {
383         Map<HomekitCharacteristicType, GenericItem> characteristicItems = new HashMap<>();
384         if (taggedItem.isGroup()) {
385             GroupItem groupItem = (GroupItem) taggedItem.getItem();
386             groupItem.getMembers().forEach(item -> getAccessoryTypes(item, metadataRegistry).stream()
387                     .filter(c -> !isRootAccessory(c)).filter(c -> belongsToType(taggedItem.getAccessoryType(), c))
388                     .filter(c -> !isMandatoryCharacteristic(taggedItem, c.getValue()))
389                     .forEach(characteristic -> characteristicItems.put(characteristic.getValue(), (GenericItem) item)));
390         } else {
391             getAccessoryTypes(taggedItem.getItem(), metadataRegistry).stream().filter(c -> !isRootAccessory(c))
392                     .filter(c -> !isMandatoryCharacteristic(taggedItem, c.getValue()))
393                     .forEach(characteristic -> characteristicItems.put(characteristic.getValue(),
394                             (GenericItem) taggedItem.getItem()));
395         }
396         logger.trace("Optional characteristics for item {}: {}", taggedItem.getName(), characteristicItems.values());
397         return Collections.unmodifiableMap(characteristicItems);
398     }
399
400     /**
401      * return true is characteristic is a mandatory characteristic for the accessory.
402      *
403      * @param item item
404      * @param characteristic characteristic
405      * @return true if characteristic is mandatory, false if not mandatory
406      */
407     private static boolean isMandatoryCharacteristic(HomekitTaggedItem item, HomekitCharacteristicType characteristic) {
408         return MANDATORY_CHARACTERISTICS.containsKey(item.getAccessoryType())
409                 && getRequiredCharacteristics(item).contains(characteristic);
410     }
411
412     /**
413      * check whether accessory is root accessory, i.e. without characteristic tag.
414      *
415      * @param accessory accessory
416      * @return true if accessory has not characteristic.
417      */
418     private static boolean isRootAccessory(Entry<HomekitAccessoryType, HomekitCharacteristicType> accessory) {
419         return ((accessory.getValue() == null) || (accessory.getValue() == EMPTY));
420     }
421
422     /**
423      * check whether characteristic belongs to the specific accessory type.
424      * characteristic with no accessory type mentioned in metadata are considered as candidates for all types.
425      *
426      * @param accessoryType accessory type
427      * @param characteristic characteristic
428      * @return true if characteristic belongs to the accessory type.
429      */
430     private static boolean belongsToType(HomekitAccessoryType accessoryType,
431             Entry<HomekitAccessoryType, HomekitCharacteristicType> characteristic) {
432         return ((characteristic.getKey() == accessoryType) || (characteristic.getKey() == DUMMY));
433     }
434 }