2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.io.homekit.internal.accessories;
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;
18 import java.util.ArrayList;
19 import java.util.EnumMap;
20 import java.util.List;
22 import java.util.concurrent.CompletableFuture;
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;
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;
38 * Implements SecuritySystem as a GroupedAccessory made up of multiple items:
40 * <li>CurrentSecuritySystemState: String type</li>
41 * <li>TargetSecuritySystemState: String type</li>
44 * @author Cody Cutrer - Initial contribution
46 public class HomekitSecuritySystemImpl extends AbstractHomekitAccessoryImpl implements SecuritySystemAccessory {
47 private final Map<CurrentSecuritySystemStateEnum, String> currentStateMapping = new EnumMap<CurrentSecuritySystemStateEnum, String>(
48 CurrentSecuritySystemStateEnum.class) {
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");
57 private final Map<TargetSecuritySystemStateEnum, String> targetStateMapping = new EnumMap<TargetSecuritySystemStateEnum, String>(
58 TargetSecuritySystemStateEnum.class) {
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");
66 private final List<CurrentSecuritySystemStateEnum> customCurrentStateList;
67 private final List<TargetSecuritySystemStateEnum> customTargetStateList;
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));
80 public CurrentSecuritySystemStateEnum[] getCurrentSecuritySystemStateValidValues() {
81 return customCurrentStateList.isEmpty()
82 ? currentStateMapping.keySet().toArray(new CurrentSecuritySystemStateEnum[0])
83 : customCurrentStateList.toArray(new CurrentSecuritySystemStateEnum[0]);
87 public TargetSecuritySystemStateEnum[] getTargetSecuritySystemStateValidValues() {
88 return customTargetStateList.isEmpty()
89 ? targetStateMapping.keySet().toArray(new TargetSecuritySystemStateEnum[0])
90 : customTargetStateList.toArray(new TargetSecuritySystemStateEnum[0]);
94 public CompletableFuture<CurrentSecuritySystemStateEnum> getCurrentSecuritySystemState() {
95 return CompletableFuture.completedFuture(getKeyFromMapping(SECURITY_SYSTEM_CURRENT_STATE, currentStateMapping,
96 CurrentSecuritySystemStateEnum.DISARMED));
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))));
106 public CompletableFuture<TargetSecuritySystemStateEnum> getTargetSecuritySystemState() {
107 return CompletableFuture.completedFuture(getKeyFromMapping(SECURITY_SYSTEM_TARGET_STATE, targetStateMapping,
108 TargetSecuritySystemStateEnum.DISARM));
112 public void subscribeCurrentSecuritySystemState(HomekitCharacteristicChangeCallback callback) {
113 subscribe(SECURITY_SYSTEM_CURRENT_STATE, callback);
117 public void unsubscribeCurrentSecuritySystemState() {
118 unsubscribe(SECURITY_SYSTEM_CURRENT_STATE);
122 public void subscribeTargetSecuritySystemState(HomekitCharacteristicChangeCallback callback) {
123 subscribe(SECURITY_SYSTEM_TARGET_STATE, callback);
127 public void unsubscribeTargetSecuritySystemState() {
128 unsubscribe(SECURITY_SYSTEM_TARGET_STATE);