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