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 {
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 final String invertedConfig = getAccessoryConfiguration(HomekitTaggedItem.INVERTED, "false");
50 final boolean inverted = invertedConfig.equalsIgnoreCase("yes") || invertedConfig.equalsIgnoreCase("true");
51 securedState = inverted ? OnOffType.OFF : OnOffType.ON;
52 unsecuredState = inverted ? OnOffType.ON : OnOffType.OFF;
53 getServices().add(new LockMechanismService(this));
57 public CompletableFuture<LockCurrentStateEnum> getLockCurrentState() {
58 final Optional<GenericItem> item = getItem(HomekitCharacteristicType.LOCK_CURRENT_STATE, GenericItem.class);
59 LockCurrentStateEnum lockState = LockCurrentStateEnum.UNKNOWN;
60 if (item.isPresent()) {
61 final State state = item.get().getState();
62 if (state instanceof DecimalType) {
63 lockState = LockCurrentStateEnum.fromCode(((DecimalType) state).intValue());
64 } else if (state instanceof OnOffType) {
65 lockState = state == securedState ? LockCurrentStateEnum.SECURED : LockCurrentStateEnum.UNSECURED;
68 return CompletableFuture.completedFuture(lockState);
72 public CompletableFuture<LockTargetStateEnum> getLockTargetState() {
73 final @Nullable OnOffType state = getStateAs(HomekitCharacteristicType.LOCK_TARGET_STATE, OnOffType.class);
75 return CompletableFuture.completedFuture(
76 state == securedState ? LockTargetStateEnum.SECURED : LockTargetStateEnum.UNSECURED);
78 return CompletableFuture.completedFuture(LockTargetStateEnum.UNSECURED);
79 // Apple HAP specification has only SECURED and UNSECURED values for lock target state.
80 // unknown does not supported for target state.
84 public CompletableFuture<Void> setLockTargetState(LockTargetStateEnum state) {
85 getItem(HomekitCharacteristicType.LOCK_TARGET_STATE, SwitchItem.class).ifPresent(item -> {
88 if (item instanceof SwitchItem) {
89 item.send(securedState);
93 if (item instanceof SwitchItem) {
94 item.send(unsecuredState);
101 return CompletableFuture.completedFuture(null);
105 public void subscribeLockCurrentState(HomekitCharacteristicChangeCallback callback) {
106 subscribe(HomekitCharacteristicType.LOCK_CURRENT_STATE, callback);
110 public void unsubscribeLockCurrentState() {
111 unsubscribe(HomekitCharacteristicType.LOCK_CURRENT_STATE);
115 public void subscribeLockTargetState(HomekitCharacteristicChangeCallback callback) {
116 subscribe(HomekitCharacteristicType.LOCK_TARGET_STATE, callback);
120 public void unsubscribeLockTargetState() {
121 unsubscribe(HomekitCharacteristicType.LOCK_TARGET_STATE);