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