]> git.basschouten.com Git - openhab-addons.git/blob
ae3b0e40db8976ec9116725c5df422b0e5c9a063
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.Optional;
17 import java.util.concurrent.CompletableFuture;
18
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;
29
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;
35
36 /**
37  * Implements the support of Lock accessories, mapping them to OpenHAB Switch type
38  *
39  * @author blafois - Initial contribution.
40  *
41  */
42 public class HomekitLockImpl extends AbstractHomekitAccessoryImpl implements LockMechanismAccessory {
43     final OnOffType securedState;
44     final OnOffType unsecuredState;
45
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));
52     }
53
54     @Override
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;
64             }
65         }
66         return CompletableFuture.completedFuture(lockState);
67     }
68
69     @Override
70     public CompletableFuture<LockTargetStateEnum> getLockTargetState() {
71         final @Nullable OnOffType state = getStateAs(HomekitCharacteristicType.LOCK_TARGET_STATE, OnOffType.class);
72         if (state != null) {
73             return CompletableFuture.completedFuture(
74                     state == securedState ? LockTargetStateEnum.SECURED : LockTargetStateEnum.UNSECURED);
75         }
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.
79     }
80
81     @Override
82     public CompletableFuture<Void> setLockTargetState(LockTargetStateEnum state) {
83         getItem(HomekitCharacteristicType.LOCK_TARGET_STATE, SwitchItem.class).ifPresent(item -> {
84             switch (state) {
85                 case SECURED:
86                     if (item instanceof SwitchItem) {
87                         item.send(securedState);
88                     }
89                     break;
90                 case UNSECURED:
91                     if (item instanceof SwitchItem) {
92                         item.send(unsecuredState);
93                     }
94                     break;
95                 default:
96                     break;
97             }
98         });
99         return CompletableFuture.completedFuture(null);
100     }
101
102     @Override
103     public void subscribeLockCurrentState(HomekitCharacteristicChangeCallback callback) {
104         subscribe(HomekitCharacteristicType.LOCK_CURRENT_STATE, callback);
105     }
106
107     @Override
108     public void unsubscribeLockCurrentState() {
109         unsubscribe(HomekitCharacteristicType.LOCK_CURRENT_STATE);
110     }
111
112     @Override
113     public void subscribeLockTargetState(HomekitCharacteristicChangeCallback callback) {
114         subscribe(HomekitCharacteristicType.LOCK_TARGET_STATE, callback);
115     }
116
117     @Override
118     public void unsubscribeLockTargetState() {
119         unsubscribe(HomekitCharacteristicType.LOCK_TARGET_STATE);
120     }
121 }