]> git.basschouten.com Git - openhab-addons.git/blob
80736a73c2182c5e7184c9d538dd3f5d8a040ace
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.io.homekit.internal.accessories;
14
15 import java.util.List;
16 import java.util.Map;
17 import java.util.concurrent.CompletableFuture;
18
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;
24
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;
30
31 /**
32  * Implements the support of Lock accessories, mapping them to OpenHAB Switch type
33  *
34  * @author blafois - Initial contribution.
35  *
36  */
37 public class HomekitLockImpl extends AbstractHomekitAccessoryImpl implements LockMechanismAccessory {
38     final Map<LockCurrentStateEnum, String> currentStateMapping;
39     final Map<LockTargetStateEnum, String> targetStateMapping;
40
41     public HomekitLockImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
42             HomekitAccessoryUpdater updater, HomekitSettings settings) {
43         super(taggedItem, mandatoryCharacteristics, updater, settings);
44
45         currentStateMapping = createMapping(HomekitCharacteristicType.LOCK_CURRENT_STATE, LockCurrentStateEnum.class);
46         targetStateMapping = createMapping(HomekitCharacteristicType.LOCK_TARGET_STATE, LockTargetStateEnum.class);
47     }
48
49     @Override
50     public void init() throws HomekitException {
51         super.init();
52         getServices().add(new LockMechanismService(this));
53     }
54
55     @Override
56     public CompletableFuture<LockCurrentStateEnum> getLockCurrentState() {
57         return CompletableFuture.completedFuture(getKeyFromMapping(HomekitCharacteristicType.LOCK_CURRENT_STATE,
58                 currentStateMapping, LockCurrentStateEnum.UNKNOWN));
59     }
60
61     @Override
62     public CompletableFuture<LockTargetStateEnum> getLockTargetState() {
63         return CompletableFuture.completedFuture(getKeyFromMapping(HomekitCharacteristicType.LOCK_TARGET_STATE,
64                 targetStateMapping, LockTargetStateEnum.UNSECURED));
65     }
66
67     @Override
68     public CompletableFuture<Void> setLockTargetState(LockTargetStateEnum state) {
69         HomekitCharacteristicFactory.setValueFromEnum(
70                 getCharacteristic(HomekitCharacteristicType.LOCK_TARGET_STATE).get(), state, targetStateMapping);
71         return CompletableFuture.completedFuture(null);
72     }
73
74     @Override
75     public void subscribeLockCurrentState(HomekitCharacteristicChangeCallback callback) {
76         subscribe(HomekitCharacteristicType.LOCK_CURRENT_STATE, callback);
77     }
78
79     @Override
80     public void unsubscribeLockCurrentState() {
81         unsubscribe(HomekitCharacteristicType.LOCK_CURRENT_STATE);
82     }
83
84     @Override
85     public void subscribeLockTargetState(HomekitCharacteristicChangeCallback callback) {
86         subscribe(HomekitCharacteristicType.LOCK_TARGET_STATE, callback);
87     }
88
89     @Override
90     public void unsubscribeLockTargetState() {
91         unsubscribe(HomekitCharacteristicType.LOCK_TARGET_STATE);
92     }
93 }