]> git.basschouten.com Git - openhab-addons.git/blob
7c2ec8532a07c5d355e3ed57a7056c1587d95cfb
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.EnumMap;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.concurrent.CompletableFuture;
22
23 import org.openhab.core.library.items.StringItem;
24 import org.openhab.core.library.types.StringType;
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.SecuritySystemAccessory;
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 = new EnumMap<CurrentSecuritySystemStateEnum, String>(
47             CurrentSecuritySystemStateEnum.class) {
48         {
49             put(CurrentSecuritySystemStateEnum.DISARMED, "DISARMED");
50             put(CurrentSecuritySystemStateEnum.AWAY_ARM, "AWAY_ARM");
51             put(CurrentSecuritySystemStateEnum.STAY_ARM, "STAY_ARM");
52             put(CurrentSecuritySystemStateEnum.NIGHT_ARM, "NIGHT_ARM");
53             put(CurrentSecuritySystemStateEnum.TRIGGERED, "TRIGGERED");
54         }
55     };
56     private final Map<TargetSecuritySystemStateEnum, String> targetStateMapping = new EnumMap<TargetSecuritySystemStateEnum, String>(
57             TargetSecuritySystemStateEnum.class) {
58         {
59             put(TargetSecuritySystemStateEnum.DISARM, "DISARM");
60             put(TargetSecuritySystemStateEnum.AWAY_ARM, "AWAY_ARM");
61             put(TargetSecuritySystemStateEnum.STAY_ARM, "STAY_ARM");
62             put(TargetSecuritySystemStateEnum.NIGHT_ARM, "NIGHT_ARM");
63         }
64     };
65
66     public HomekitSecuritySystemImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
67             HomekitAccessoryUpdater updater, HomekitSettings settings) {
68         super(taggedItem, mandatoryCharacteristics, updater, settings);
69         updateMapping(SECURITY_SYSTEM_CURRENT_STATE, currentStateMapping);
70         updateMapping(SECURITY_SYSTEM_TARGET_STATE, targetStateMapping);
71         getServices().add(new SecuritySystemService(this));
72     }
73
74     @Override
75     public CompletableFuture<CurrentSecuritySystemStateEnum> getCurrentSecuritySystemState() {
76         return CompletableFuture.completedFuture(getKeyFromMapping(SECURITY_SYSTEM_CURRENT_STATE, currentStateMapping,
77                 CurrentSecuritySystemStateEnum.DISARMED));
78     }
79
80     @Override
81     public void setTargetSecuritySystemState(TargetSecuritySystemStateEnum state) {
82         getItem(HomekitCharacteristicType.SECURITY_SYSTEM_TARGET_STATE, StringItem.class)
83                 .ifPresent(item -> item.send(new StringType(targetStateMapping.get(state))));
84     }
85
86     @Override
87     public CompletableFuture<TargetSecuritySystemStateEnum> getTargetSecuritySystemState() {
88         return CompletableFuture.completedFuture(getKeyFromMapping(SECURITY_SYSTEM_TARGET_STATE, targetStateMapping,
89                 TargetSecuritySystemStateEnum.DISARM));
90     }
91
92     @Override
93     public void subscribeCurrentSecuritySystemState(HomekitCharacteristicChangeCallback callback) {
94         subscribe(SECURITY_SYSTEM_CURRENT_STATE, callback);
95     }
96
97     @Override
98     public void unsubscribeCurrentSecuritySystemState() {
99         unsubscribe(SECURITY_SYSTEM_CURRENT_STATE);
100     }
101
102     @Override
103     public void subscribeTargetSecuritySystemState(HomekitCharacteristicChangeCallback callback) {
104         subscribe(SECURITY_SYSTEM_TARGET_STATE, callback);
105     }
106
107     @Override
108     public void unsubscribeTargetSecuritySystemState() {
109         unsubscribe(SECURITY_SYSTEM_TARGET_STATE);
110     }
111 }