2 * Copyright (c) 2010-2023 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 java.util.List;
17 import java.util.concurrent.CompletableFuture;
19 import org.openhab.io.homekit.internal.HomekitAccessoryUpdater;
20 import org.openhab.io.homekit.internal.HomekitCharacteristicType;
21 import org.openhab.io.homekit.internal.HomekitSettings;
22 import org.openhab.io.homekit.internal.HomekitTaggedItem;
24 import io.github.hapjava.accessories.LockMechanismAccessory;
25 import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
26 import io.github.hapjava.characteristics.impl.lock.LockCurrentStateEnum;
27 import io.github.hapjava.characteristics.impl.lock.LockTargetStateEnum;
28 import io.github.hapjava.services.impl.LockMechanismService;
31 * Implements the support of Lock accessories, mapping them to OpenHAB Switch type
33 * @author blafois - Initial contribution.
36 public class HomekitLockImpl extends AbstractHomekitAccessoryImpl implements LockMechanismAccessory {
37 final Map<LockCurrentStateEnum, String> currentStateMapping;
38 final Map<LockTargetStateEnum, String> targetStateMapping;
40 public HomekitLockImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
41 HomekitAccessoryUpdater updater, HomekitSettings settings) {
42 super(taggedItem, mandatoryCharacteristics, updater, settings);
44 currentStateMapping = createMapping(HomekitCharacteristicType.LOCK_CURRENT_STATE, LockCurrentStateEnum.class);
45 targetStateMapping = createMapping(HomekitCharacteristicType.LOCK_TARGET_STATE, LockTargetStateEnum.class);
46 getServices().add(new LockMechanismService(this));
50 public CompletableFuture<LockCurrentStateEnum> getLockCurrentState() {
51 return CompletableFuture.completedFuture(getKeyFromMapping(HomekitCharacteristicType.LOCK_CURRENT_STATE,
52 currentStateMapping, LockCurrentStateEnum.UNKNOWN));
56 public CompletableFuture<LockTargetStateEnum> getLockTargetState() {
57 return CompletableFuture.completedFuture(getKeyFromMapping(HomekitCharacteristicType.LOCK_TARGET_STATE,
58 targetStateMapping, LockTargetStateEnum.UNSECURED));
62 public CompletableFuture<Void> setLockTargetState(LockTargetStateEnum state) {
63 HomekitCharacteristicFactory.setValueFromEnum(
64 getCharacteristic(HomekitCharacteristicType.LOCK_TARGET_STATE).get(), state, targetStateMapping);
65 return CompletableFuture.completedFuture(null);
69 public void subscribeLockCurrentState(HomekitCharacteristicChangeCallback callback) {
70 subscribe(HomekitCharacteristicType.LOCK_CURRENT_STATE, callback);
74 public void unsubscribeLockCurrentState() {
75 unsubscribe(HomekitCharacteristicType.LOCK_CURRENT_STATE);
79 public void subscribeLockTargetState(HomekitCharacteristicChangeCallback callback) {
80 subscribe(HomekitCharacteristicType.LOCK_TARGET_STATE, callback);
84 public void unsubscribeLockTargetState() {
85 unsubscribe(HomekitCharacteristicType.LOCK_TARGET_STATE);