2 * Copyright (c) 2010-2024 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.HomekitException;
22 import org.openhab.io.homekit.internal.HomekitSettings;
23 import org.openhab.io.homekit.internal.HomekitTaggedItem;
25 import io.github.hapjava.accessories.LockMechanismAccessory;
26 import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
27 import io.github.hapjava.characteristics.impl.lock.LockCurrentStateEnum;
28 import io.github.hapjava.characteristics.impl.lock.LockTargetStateEnum;
29 import io.github.hapjava.services.impl.LockMechanismService;
32 * Implements the support of Lock accessories, mapping them to OpenHAB Switch type
34 * @author blafois - Initial contribution.
37 public class HomekitLockImpl extends AbstractHomekitAccessoryImpl implements LockMechanismAccessory {
38 final Map<LockCurrentStateEnum, String> currentStateMapping;
39 final Map<LockTargetStateEnum, String> targetStateMapping;
41 public HomekitLockImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
42 HomekitAccessoryUpdater updater, HomekitSettings settings) {
43 super(taggedItem, mandatoryCharacteristics, updater, settings);
45 currentStateMapping = createMapping(HomekitCharacteristicType.LOCK_CURRENT_STATE, LockCurrentStateEnum.class);
46 targetStateMapping = createMapping(HomekitCharacteristicType.LOCK_TARGET_STATE, LockTargetStateEnum.class);
50 public void init() throws HomekitException {
52 addService(new LockMechanismService(this));
56 public CompletableFuture<LockCurrentStateEnum> getLockCurrentState() {
57 return CompletableFuture.completedFuture(getKeyFromMapping(HomekitCharacteristicType.LOCK_CURRENT_STATE,
58 currentStateMapping, LockCurrentStateEnum.UNKNOWN));
62 public CompletableFuture<LockTargetStateEnum> getLockTargetState() {
63 return CompletableFuture.completedFuture(getKeyFromMapping(HomekitCharacteristicType.LOCK_TARGET_STATE,
64 targetStateMapping, LockTargetStateEnum.UNSECURED));
68 public CompletableFuture<Void> setLockTargetState(LockTargetStateEnum state) {
69 HomekitCharacteristicFactory.setValueFromEnum(
70 getCharacteristic(HomekitCharacteristicType.LOCK_TARGET_STATE).get(), state, targetStateMapping);
71 return CompletableFuture.completedFuture(null);
75 public void subscribeLockCurrentState(HomekitCharacteristicChangeCallback callback) {
76 subscribe(HomekitCharacteristicType.LOCK_CURRENT_STATE, callback);
80 public void unsubscribeLockCurrentState() {
81 unsubscribe(HomekitCharacteristicType.LOCK_CURRENT_STATE);
85 public void subscribeLockTargetState(HomekitCharacteristicChangeCallback callback) {
86 subscribe(HomekitCharacteristicType.LOCK_TARGET_STATE, callback);
90 public void unsubscribeLockTargetState() {
91 unsubscribe(HomekitCharacteristicType.LOCK_TARGET_STATE);