2 * Copyright (c) 2010-2021 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.EnumMap;
19 import java.util.List;
21 import java.util.concurrent.CompletableFuture;
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;
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;
37 * Implements SecuritySystem as a GroupedAccessory made up of multiple items:
39 * <li>CurrentSecuritySystemState: String type</li>
40 * <li>TargetSecuritySystemState: String type</li>
43 * @author Cody Cutrer - Initial contribution
45 public class HomekitSecuritySystemImpl extends AbstractHomekitAccessoryImpl implements SecuritySystemAccessory {
46 private final Map<CurrentSecuritySystemStateEnum, String> currentStateMapping = new EnumMap<CurrentSecuritySystemStateEnum, String>(
47 CurrentSecuritySystemStateEnum.class) {
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");
56 private final Map<TargetSecuritySystemStateEnum, String> targetStateMapping = new EnumMap<TargetSecuritySystemStateEnum, String>(
57 TargetSecuritySystemStateEnum.class) {
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");
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));
75 public CompletableFuture<CurrentSecuritySystemStateEnum> getCurrentSecuritySystemState() {
76 return CompletableFuture.completedFuture(getKeyFromMapping(SECURITY_SYSTEM_CURRENT_STATE, currentStateMapping,
77 CurrentSecuritySystemStateEnum.DISARMED));
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))));
87 public CompletableFuture<TargetSecuritySystemStateEnum> getTargetSecuritySystemState() {
88 return CompletableFuture.completedFuture(getKeyFromMapping(SECURITY_SYSTEM_TARGET_STATE, targetStateMapping,
89 TargetSecuritySystemStateEnum.DISARM));
93 public void subscribeCurrentSecuritySystemState(HomekitCharacteristicChangeCallback callback) {
94 subscribe(SECURITY_SYSTEM_CURRENT_STATE, callback);
98 public void unsubscribeCurrentSecuritySystemState() {
99 unsubscribe(SECURITY_SYSTEM_CURRENT_STATE);
103 public void subscribeTargetSecuritySystemState(HomekitCharacteristicChangeCallback callback) {
104 subscribe(SECURITY_SYSTEM_TARGET_STATE, callback);
108 public void unsubscribeTargetSecuritySystemState() {
109 unsubscribe(SECURITY_SYSTEM_TARGET_STATE);