]> git.basschouten.com Git - openhab-addons.git/blob
23d83b1e84dde206eb7498d169525c5166666a1e
[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 static org.openhab.io.homekit.internal.HomekitCharacteristicType.SECURITY_SYSTEM_CURRENT_STATE;
16 import static org.openhab.io.homekit.internal.HomekitCharacteristicType.SECURITY_SYSTEM_TARGET_STATE;
17
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.concurrent.CompletableFuture;
22
23 import org.openhab.io.homekit.internal.HomekitAccessoryUpdater;
24 import org.openhab.io.homekit.internal.HomekitCharacteristicType;
25 import org.openhab.io.homekit.internal.HomekitException;
26 import org.openhab.io.homekit.internal.HomekitSettings;
27 import org.openhab.io.homekit.internal.HomekitTaggedItem;
28
29 import io.github.hapjava.accessories.SecuritySystemAccessory;
30 import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
31 import io.github.hapjava.characteristics.impl.securitysystem.CurrentSecuritySystemStateEnum;
32 import io.github.hapjava.characteristics.impl.securitysystem.TargetSecuritySystemStateEnum;
33 import io.github.hapjava.services.impl.SecuritySystemService;
34
35 /**
36  * Implements SecuritySystem as a GroupedAccessory made up of multiple items:
37  * <ul>
38  * <li>CurrentSecuritySystemState: String type</li>
39  * <li>TargetSecuritySystemState: String type</li>
40  * </ul>
41  * 
42  * @author Cody Cutrer - Initial contribution
43  */
44 public class HomekitSecuritySystemImpl extends AbstractHomekitAccessoryImpl implements SecuritySystemAccessory {
45     private final Map<CurrentSecuritySystemStateEnum, String> currentStateMapping;
46     private final Map<TargetSecuritySystemStateEnum, String> targetStateMapping;
47     private final List<CurrentSecuritySystemStateEnum> customCurrentStateList = new ArrayList<>();
48     private final List<TargetSecuritySystemStateEnum> customTargetStateList = new ArrayList<>();
49
50     public HomekitSecuritySystemImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
51             HomekitAccessoryUpdater updater, HomekitSettings settings) {
52         super(taggedItem, mandatoryCharacteristics, updater, settings);
53         currentStateMapping = createMapping(SECURITY_SYSTEM_CURRENT_STATE, CurrentSecuritySystemStateEnum.class,
54                 customCurrentStateList);
55         targetStateMapping = createMapping(SECURITY_SYSTEM_TARGET_STATE, TargetSecuritySystemStateEnum.class,
56                 customTargetStateList);
57     }
58
59     @Override
60     public void init() throws HomekitException {
61         super.init();
62         addService(new SecuritySystemService(this));
63     }
64
65     @Override
66     public CurrentSecuritySystemStateEnum[] getCurrentSecuritySystemStateValidValues() {
67         return customCurrentStateList.isEmpty()
68                 ? currentStateMapping.keySet().toArray(new CurrentSecuritySystemStateEnum[0])
69                 : customCurrentStateList.toArray(new CurrentSecuritySystemStateEnum[0]);
70     }
71
72     @Override
73     public TargetSecuritySystemStateEnum[] getTargetSecuritySystemStateValidValues() {
74         return customTargetStateList.isEmpty()
75                 ? targetStateMapping.keySet().toArray(new TargetSecuritySystemStateEnum[0])
76                 : customTargetStateList.toArray(new TargetSecuritySystemStateEnum[0]);
77     }
78
79     @Override
80     public CompletableFuture<CurrentSecuritySystemStateEnum> getCurrentSecuritySystemState() {
81         return CompletableFuture.completedFuture(getKeyFromMapping(SECURITY_SYSTEM_CURRENT_STATE, currentStateMapping,
82                 CurrentSecuritySystemStateEnum.DISARMED));
83     }
84
85     @Override
86     public void setTargetSecuritySystemState(TargetSecuritySystemStateEnum state) {
87         HomekitCharacteristicFactory.setValueFromEnum(
88                 getCharacteristic(HomekitCharacteristicType.SECURITY_SYSTEM_TARGET_STATE).get(), state,
89                 targetStateMapping);
90     }
91
92     @Override
93     public CompletableFuture<TargetSecuritySystemStateEnum> getTargetSecuritySystemState() {
94         return CompletableFuture.completedFuture(getKeyFromMapping(SECURITY_SYSTEM_TARGET_STATE, targetStateMapping,
95                 TargetSecuritySystemStateEnum.DISARM));
96     }
97
98     @Override
99     public void subscribeCurrentSecuritySystemState(HomekitCharacteristicChangeCallback callback) {
100         subscribe(SECURITY_SYSTEM_CURRENT_STATE, callback);
101     }
102
103     @Override
104     public void unsubscribeCurrentSecuritySystemState() {
105         unsubscribe(SECURITY_SYSTEM_CURRENT_STATE);
106     }
107
108     @Override
109     public void subscribeTargetSecuritySystemState(HomekitCharacteristicChangeCallback callback) {
110         subscribe(SECURITY_SYSTEM_TARGET_STATE, callback);
111     }
112
113     @Override
114     public void unsubscribeTargetSecuritySystemState() {
115         unsubscribe(SECURITY_SYSTEM_TARGET_STATE);
116     }
117 }