]> git.basschouten.com Git - openhab-addons.git/blob
14e2a05f82d9ec19bd72c63190bc6159a5b231f4
[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.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;
31
32 /**
33  * Implements the support of Lock accessories, mapping them to OpenHAB Switch type
34  *
35  * @author blafois - Initial contribution.
36  *
37  */
38 public class HomekitLockImpl extends AbstractHomekitAccessoryImpl implements LockMechanismAccessory {
39     final Map<LockCurrentStateEnum, String> currentStateMapping;
40     final Map<LockTargetStateEnum, String> targetStateMapping;
41
42     public HomekitLockImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
43             List<Characteristic> mandatoryRawCharacteristics, HomekitAccessoryUpdater updater,
44             HomekitSettings settings) {
45         super(taggedItem, mandatoryCharacteristics, mandatoryRawCharacteristics, updater, settings);
46
47         currentStateMapping = createMapping(HomekitCharacteristicType.LOCK_CURRENT_STATE, LockCurrentStateEnum.class);
48         targetStateMapping = createMapping(HomekitCharacteristicType.LOCK_TARGET_STATE, LockTargetStateEnum.class);
49     }
50
51     @Override
52     public void init() throws HomekitException {
53         super.init();
54         addService(new LockMechanismService(this));
55     }
56
57     @Override
58     public CompletableFuture<LockCurrentStateEnum> getLockCurrentState() {
59         return CompletableFuture.completedFuture(getKeyFromMapping(HomekitCharacteristicType.LOCK_CURRENT_STATE,
60                 currentStateMapping, LockCurrentStateEnum.UNKNOWN));
61     }
62
63     @Override
64     public CompletableFuture<LockTargetStateEnum> getLockTargetState() {
65         return CompletableFuture.completedFuture(getKeyFromMapping(HomekitCharacteristicType.LOCK_TARGET_STATE,
66                 targetStateMapping, LockTargetStateEnum.UNSECURED));
67     }
68
69     @Override
70     public CompletableFuture<Void> setLockTargetState(LockTargetStateEnum state) {
71         HomekitCharacteristicFactory.setValueFromEnum(
72                 getCharacteristic(HomekitCharacteristicType.LOCK_TARGET_STATE).get(), state, targetStateMapping);
73         return CompletableFuture.completedFuture(null);
74     }
75
76     @Override
77     public void subscribeLockCurrentState(HomekitCharacteristicChangeCallback callback) {
78         subscribe(HomekitCharacteristicType.LOCK_CURRENT_STATE, callback);
79     }
80
81     @Override
82     public void unsubscribeLockCurrentState() {
83         unsubscribe(HomekitCharacteristicType.LOCK_CURRENT_STATE);
84     }
85
86     @Override
87     public void subscribeLockTargetState(HomekitCharacteristicChangeCallback callback) {
88         subscribe(HomekitCharacteristicType.LOCK_TARGET_STATE, callback);
89     }
90
91     @Override
92     public void unsubscribeLockTargetState() {
93         unsubscribe(HomekitCharacteristicType.LOCK_TARGET_STATE);
94     }
95 }