2 * Copyright (c) 2010-2021 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 {
44 public HomekitLockImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
45 HomekitAccessoryUpdater updater, HomekitSettings settings) {
46 super(taggedItem, mandatoryCharacteristics, updater, settings);
47 getServices().add(new LockMechanismService(this));
51 public CompletableFuture<LockCurrentStateEnum> getLockCurrentState() {
52 final Optional<GenericItem> item = getItem(HomekitCharacteristicType.LOCK_CURRENT_STATE, GenericItem.class);
53 LockCurrentStateEnum lockState = LockCurrentStateEnum.UNKNOWN;
54 if (item.isPresent()) {
55 final State state = item.get().getState();
56 if (state instanceof DecimalType) {
57 lockState = LockCurrentStateEnum.fromCode(((DecimalType) state).intValue());
58 } else if (state instanceof OnOffType) {
59 lockState = state == OnOffType.ON ? LockCurrentStateEnum.SECURED : LockCurrentStateEnum.UNSECURED;
62 return CompletableFuture.completedFuture(lockState);
66 public CompletableFuture<LockTargetStateEnum> getLockTargetState() {
67 final @Nullable OnOffType state = getStateAs(HomekitCharacteristicType.LOCK_TARGET_STATE, OnOffType.class);
69 return CompletableFuture.completedFuture(
70 state == OnOffType.ON ? LockTargetStateEnum.SECURED : LockTargetStateEnum.UNSECURED);
72 return CompletableFuture.completedFuture(LockTargetStateEnum.UNSECURED);
73 // Apple HAP specification has only SECURED and UNSECURED values for lock target state.
74 // unknown does not supported for target state.
78 public CompletableFuture<Void> setLockTargetState(LockTargetStateEnum state) {
79 getItem(HomekitCharacteristicType.LOCK_TARGET_STATE, SwitchItem.class).ifPresent(item -> {
83 if (item instanceof SwitchItem) {
84 item.send(OnOffType.ON);
89 if (item instanceof SwitchItem) {
90 item.send(OnOffType.OFF);
97 return CompletableFuture.completedFuture(null);
101 public void subscribeLockCurrentState(HomekitCharacteristicChangeCallback callback) {
102 subscribe(HomekitCharacteristicType.LOCK_CURRENT_STATE, callback);
106 public void unsubscribeLockCurrentState() {
107 unsubscribe(HomekitCharacteristicType.LOCK_CURRENT_STATE);
111 public void subscribeLockTargetState(HomekitCharacteristicChangeCallback callback) {
112 subscribe(HomekitCharacteristicType.LOCK_TARGET_STATE, callback);
116 public void unsubscribeLockTargetState() {
117 unsubscribe(HomekitCharacteristicType.LOCK_TARGET_STATE);