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;
16 import java.util.Optional;
17 import java.util.concurrent.CompletableFuture;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.core.items.GenericItem;
21 import org.openhab.core.library.items.SwitchItem;
22 import org.openhab.core.library.types.DecimalType;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.types.State;
25 import org.openhab.io.homekit.internal.HomekitAccessoryUpdater;
26 import org.openhab.io.homekit.internal.HomekitCharacteristicType;
27 import org.openhab.io.homekit.internal.HomekitSettings;
28 import org.openhab.io.homekit.internal.HomekitTaggedItem;
30 import io.github.hapjava.accessories.LockMechanismAccessory;
31 import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
32 import io.github.hapjava.characteristics.impl.lock.LockCurrentStateEnum;
33 import io.github.hapjava.characteristics.impl.lock.LockTargetStateEnum;
34 import io.github.hapjava.services.impl.LockMechanismService;
37 * Implements the support of Lock accessories, mapping them to OpenHAB Switch type
39 * @author blafois - Initial contribution.
42 public class HomekitLockImpl extends AbstractHomekitAccessoryImpl implements LockMechanismAccessory {
43 final OnOffType securedState;
44 final OnOffType unsecuredState;
46 public HomekitLockImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
47 HomekitAccessoryUpdater updater, HomekitSettings settings) {
48 super(taggedItem, mandatoryCharacteristics, updater, settings);
49 securedState = taggedItem.isInverted() ? OnOffType.OFF : OnOffType.ON;
50 unsecuredState = taggedItem.isInverted() ? OnOffType.ON : OnOffType.OFF;
51 getServices().add(new LockMechanismService(this));
55 public CompletableFuture<LockCurrentStateEnum> getLockCurrentState() {
56 final Optional<GenericItem> item = getItem(HomekitCharacteristicType.LOCK_CURRENT_STATE, GenericItem.class);
57 LockCurrentStateEnum lockState = LockCurrentStateEnum.UNKNOWN;
58 if (item.isPresent()) {
59 final State state = item.get().getState();
60 if (state instanceof DecimalType) {
61 lockState = LockCurrentStateEnum.fromCode(((DecimalType) state).intValue());
62 } else if (state instanceof OnOffType) {
63 lockState = state.equals(securedState) ? LockCurrentStateEnum.SECURED : LockCurrentStateEnum.UNSECURED;
66 return CompletableFuture.completedFuture(lockState);
70 public CompletableFuture<LockTargetStateEnum> getLockTargetState() {
71 final @Nullable OnOffType state = getStateAs(HomekitCharacteristicType.LOCK_TARGET_STATE, OnOffType.class);
73 return CompletableFuture.completedFuture(
74 state == securedState ? LockTargetStateEnum.SECURED : LockTargetStateEnum.UNSECURED);
76 return CompletableFuture.completedFuture(LockTargetStateEnum.UNSECURED);
77 // Apple HAP specification has only SECURED and UNSECURED values for lock target state.
78 // unknown does not supported for target state.
82 public CompletableFuture<Void> setLockTargetState(LockTargetStateEnum state) {
83 getItem(HomekitCharacteristicType.LOCK_TARGET_STATE, SwitchItem.class).ifPresent(item -> {
86 if (item instanceof SwitchItem) {
87 item.send(securedState);
91 if (item instanceof SwitchItem) {
92 item.send(unsecuredState);
99 return CompletableFuture.completedFuture(null);
103 public void subscribeLockCurrentState(HomekitCharacteristicChangeCallback callback) {
104 subscribe(HomekitCharacteristicType.LOCK_CURRENT_STATE, callback);
108 public void unsubscribeLockCurrentState() {
109 unsubscribe(HomekitCharacteristicType.LOCK_CURRENT_STATE);
113 public void subscribeLockTargetState(HomekitCharacteristicChangeCallback callback) {
114 subscribe(HomekitCharacteristicType.LOCK_TARGET_STATE, callback);
118 public void unsubscribeLockTargetState() {
119 unsubscribe(HomekitCharacteristicType.LOCK_TARGET_STATE);