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.List;
21 import java.util.concurrent.CompletableFuture;
23 import org.openhab.io.homekit.internal.HomekitAccessoryUpdater;
24 import org.openhab.io.homekit.internal.HomekitCharacteristicType;
25 import org.openhab.io.homekit.internal.HomekitSettings;
26 import org.openhab.io.homekit.internal.HomekitTaggedItem;
28 import io.github.hapjava.accessories.SecuritySystemAccessory;
29 import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
30 import io.github.hapjava.characteristics.impl.securitysystem.CurrentSecuritySystemStateEnum;
31 import io.github.hapjava.characteristics.impl.securitysystem.TargetSecuritySystemStateEnum;
32 import io.github.hapjava.services.impl.SecuritySystemService;
35 * Implements SecuritySystem as a GroupedAccessory made up of multiple items:
37 * <li>CurrentSecuritySystemState: String type</li>
38 * <li>TargetSecuritySystemState: String type</li>
41 * @author Cody Cutrer - Initial contribution
43 public class HomekitSecuritySystemImpl extends AbstractHomekitAccessoryImpl implements SecuritySystemAccessory {
44 private final Map<CurrentSecuritySystemStateEnum, String> currentStateMapping;
45 private final Map<TargetSecuritySystemStateEnum, String> targetStateMapping;
46 private final List<CurrentSecuritySystemStateEnum> customCurrentStateList = new ArrayList<>();
47 private final List<TargetSecuritySystemStateEnum> customTargetStateList = new ArrayList<>();
49 public HomekitSecuritySystemImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
50 HomekitAccessoryUpdater updater, HomekitSettings settings) {
51 super(taggedItem, mandatoryCharacteristics, updater, settings);
52 currentStateMapping = createMapping(SECURITY_SYSTEM_CURRENT_STATE, CurrentSecuritySystemStateEnum.class,
53 customCurrentStateList);
54 targetStateMapping = createMapping(SECURITY_SYSTEM_TARGET_STATE, TargetSecuritySystemStateEnum.class,
55 customTargetStateList);
56 getServices().add(new SecuritySystemService(this));
60 public CurrentSecuritySystemStateEnum[] getCurrentSecuritySystemStateValidValues() {
61 return customCurrentStateList.isEmpty()
62 ? currentStateMapping.keySet().toArray(new CurrentSecuritySystemStateEnum[0])
63 : customCurrentStateList.toArray(new CurrentSecuritySystemStateEnum[0]);
67 public TargetSecuritySystemStateEnum[] getTargetSecuritySystemStateValidValues() {
68 return customTargetStateList.isEmpty()
69 ? targetStateMapping.keySet().toArray(new TargetSecuritySystemStateEnum[0])
70 : customTargetStateList.toArray(new TargetSecuritySystemStateEnum[0]);
74 public CompletableFuture<CurrentSecuritySystemStateEnum> getCurrentSecuritySystemState() {
75 return CompletableFuture.completedFuture(getKeyFromMapping(SECURITY_SYSTEM_CURRENT_STATE, currentStateMapping,
76 CurrentSecuritySystemStateEnum.DISARMED));
80 public void setTargetSecuritySystemState(TargetSecuritySystemStateEnum state) {
81 HomekitCharacteristicFactory.setValueFromEnum(
82 getCharacteristic(HomekitCharacteristicType.SECURITY_SYSTEM_TARGET_STATE).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);