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.accessories;
15 import static org.openhab.io.homekit.internal.HomekitAccessoryType.*;
16 import static org.openhab.io.homekit.internal.HomekitCharacteristicType.*;
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;
26 import java.util.Map.Entry;
27 import java.util.Objects;
28 import java.util.Optional;
29 import java.util.TreeMap;
30 import java.util.stream.Collectors;
31 import java.util.stream.Stream;
33 import org.eclipse.jdt.annotation.NonNullByDefault;
34 import org.eclipse.jdt.annotation.Nullable;
35 import org.openhab.core.items.GenericItem;
36 import org.openhab.core.items.GroupItem;
37 import org.openhab.core.items.Item;
38 import org.openhab.core.items.ItemRegistry;
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.io.homekit.internal.HomekitAccessoryType;
43 import org.openhab.io.homekit.internal.HomekitAccessoryUpdater;
44 import org.openhab.io.homekit.internal.HomekitCharacteristicType;
45 import org.openhab.io.homekit.internal.HomekitException;
46 import org.openhab.io.homekit.internal.HomekitOHItemProxy;
47 import org.openhab.io.homekit.internal.HomekitSettings;
48 import org.openhab.io.homekit.internal.HomekitTaggedItem;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
52 import io.github.hapjava.characteristics.Characteristic;
53 import io.github.hapjava.services.Service;
56 * Creates a HomekitAccessory for a given HomekitTaggedItem.
58 * @author Andy Lintner - Initial contribution
59 * @author Eugen Freiter - refactoring for optional characteristics
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
66 /** List of mandatory attributes for each accessory type. **/
67 private final static Map<HomekitAccessoryType, HomekitCharacteristicType[]> MANDATORY_CHARACTERISTICS = new HashMap<HomekitAccessoryType, HomekitCharacteristicType[]>() {
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 });
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 });
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>>() {
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);
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())));
149 if (taggedItem.getAccessoryType() == BATTERY) {
150 final boolean isChargeable = taggedItem.getConfigurationAsBoolean(HomekitBatteryImpl.BATTERY_TYPE, false);
152 characteristics.add(BATTERY_CHARGING_STATE);
155 return characteristics;
159 * creates HomeKit accessory for an openhab item.
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
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
170 @SuppressWarnings("null")
171 public static AbstractHomekitAccessoryImpl 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,
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");
183 AbstractHomekitAccessoryImpl accessoryImpl;
185 final @Nullable Class<? extends AbstractHomekitAccessoryImpl> accessoryImplClass = SERVICE_IMPL_MAP
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;
194 logger.warn("Unsupported HomeKit type: {}", accessoryType.getTag());
195 throw new HomekitException("Unsupported HomeKit type: " + accessoryType);
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);
205 * return HomeKit accessory types for an OH item based on meta data
207 * @param item OH item
208 * @param metadataRegistry meta data registry
209 * @return list of HomeKit accessory types and characteristics.
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));
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)));
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;
244 * return list of HomeKit relevant groups linked to an accessory
246 * @param item OH item
247 * @param itemRegistry item registry
248 * @param metadataRegistry metadata registry
249 * @return list of relevant group items
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);
258 return Stream.empty();
260 }).filter(groupItem -> !getAccessoryTypes(groupItem, metadataRegistry).isEmpty()).collect(Collectors.toList());
264 * collect all mandatory characteristics for a given tagged item, e.g. collect all mandatory HomeKit items from a
267 * @param taggedItem HomeKit tagged item
268 * @param metadataRegistry meta data registry
269 * @return list of mandatory
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);
279 addMandatoryCharacteristics(taggedItem, collectedCharacteristics, taggedItem.getItem(), metadataRegistry);
281 logger.trace("Mandatory characteristics: {}", collectedCharacteristics);
282 return collectedCharacteristics;
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
292 * @param mainItem main item
293 * @param characteristics list of characteristics
294 * @param item current item
295 * @param metadataRegistry meta date registry
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
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,
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))));
322 // item has characteristic tag on it, so, adding it as that characteristic.
324 final HomekitCharacteristicType characteristic = accessory.getValue();
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)));
338 * add optional characteristics for given accessory.
340 * @param taggedItem main item
341 * @param accessory accessory
342 * @param metadataRegistry metadata registry
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) -> {
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,
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());
375 * collect optional HomeKit characteristics for an OH item.
377 * @param taggedItem main OH item
378 * @param metadataRegistry OH metadata registry
379 * @return a map with characteristics and corresponding OH items
381 private static Map<HomekitCharacteristicType, GenericItem> getOptionalCharacteristics(HomekitTaggedItem taggedItem,
382 MetadataRegistry metadataRegistry) {
383 Map<HomekitCharacteristicType, GenericItem> characteristicItems = new TreeMap<>();
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)));
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()));
396 logger.trace("Optional characteristics for item {}: {}", taggedItem.getName(), characteristicItems.values());
397 return Collections.unmodifiableMap(characteristicItems);
401 * return true is characteristic is a mandatory characteristic for the accessory.
404 * @param characteristic characteristic
405 * @return true if characteristic is mandatory, false if not mandatory
407 private static boolean isMandatoryCharacteristic(HomekitTaggedItem item, HomekitCharacteristicType characteristic) {
408 return MANDATORY_CHARACTERISTICS.containsKey(item.getAccessoryType())
409 && getRequiredCharacteristics(item).contains(characteristic);
413 * check whether accessory is root accessory, i.e. without characteristic tag.
415 * @param accessory accessory
416 * @return true if accessory has not characteristic.
418 private static boolean isRootAccessory(Entry<HomekitAccessoryType, HomekitCharacteristicType> accessory) {
419 return ((accessory.getValue() == null) || (accessory.getValue() == EMPTY));
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.
426 * @param accessoryType accessory type
427 * @param characteristic characteristic
428 * @return true if characteristic belongs to the accessory type.
430 private static boolean belongsToType(HomekitAccessoryType accessoryType,
431 Entry<HomekitAccessoryType, HomekitCharacteristicType> characteristic) {
432 return ((characteristic.getKey() == accessoryType) || (characteristic.getKey() == DUMMY));