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.Characteristic;
27 import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
28 import io.github.hapjava.characteristics.impl.lock.LockCurrentStateEnum;
29 import io.github.hapjava.characteristics.impl.lock.LockTargetStateEnum;
30 import io.github.hapjava.services.impl.LockMechanismService;
33 * Implements the support of Lock accessories, mapping them to OpenHAB Switch type
35 * @author blafois - Initial contribution.
38 public class HomekitLockImpl extends AbstractHomekitAccessoryImpl implements LockMechanismAccessory {
39 final Map<LockCurrentStateEnum, String> currentStateMapping;
40 final Map<LockTargetStateEnum, String> targetStateMapping;
42 public HomekitLockImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
43 List<Characteristic> mandatoryRawCharacteristics, HomekitAccessoryUpdater updater,
44 HomekitSettings settings) {
45 super(taggedItem, mandatoryCharacteristics, mandatoryRawCharacteristics, updater, settings);
47 currentStateMapping = createMapping(HomekitCharacteristicType.LOCK_CURRENT_STATE, LockCurrentStateEnum.class);
48 targetStateMapping = createMapping(HomekitCharacteristicType.LOCK_TARGET_STATE, LockTargetStateEnum.class);
52 public void init() throws HomekitException {
54 addService(new LockMechanismService(this));
58 public CompletableFuture<LockCurrentStateEnum> getLockCurrentState() {
59 return CompletableFuture.completedFuture(getKeyFromMapping(HomekitCharacteristicType.LOCK_CURRENT_STATE,
60 currentStateMapping, LockCurrentStateEnum.UNKNOWN));
64 public CompletableFuture<LockTargetStateEnum> getLockTargetState() {
65 return CompletableFuture.completedFuture(getKeyFromMapping(HomekitCharacteristicType.LOCK_TARGET_STATE,
66 targetStateMapping, LockTargetStateEnum.UNSECURED));
70 public CompletableFuture<Void> setLockTargetState(LockTargetStateEnum state) {
71 HomekitCharacteristicFactory.setValueFromEnum(
72 getCharacteristic(HomekitCharacteristicType.LOCK_TARGET_STATE).get(), state, targetStateMapping);
73 return CompletableFuture.completedFuture(null);
77 public void subscribeLockCurrentState(HomekitCharacteristicChangeCallback callback) {
78 subscribe(HomekitCharacteristicType.LOCK_CURRENT_STATE, callback);
82 public void unsubscribeLockCurrentState() {
83 unsubscribe(HomekitCharacteristicType.LOCK_CURRENT_STATE);
87 public void subscribeLockTargetState(HomekitCharacteristicChangeCallback callback) {
88 subscribe(HomekitCharacteristicType.LOCK_TARGET_STATE, callback);
92 public void unsubscribeLockTargetState() {
93 unsubscribe(HomekitCharacteristicType.LOCK_TARGET_STATE);