2 * Copyright (c) 2010-2024 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.HomekitException;
26 import org.openhab.io.homekit.internal.HomekitSettings;
27 import org.openhab.io.homekit.internal.HomekitTaggedItem;
29 import io.github.hapjava.accessories.SecuritySystemAccessory;
30 import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
31 import io.github.hapjava.characteristics.impl.securitysystem.CurrentSecuritySystemStateEnum;
32 import io.github.hapjava.characteristics.impl.securitysystem.TargetSecuritySystemStateEnum;
33 import io.github.hapjava.services.impl.SecuritySystemService;
36 * Implements SecuritySystem as a GroupedAccessory made up of multiple items:
38 * <li>CurrentSecuritySystemState: String type</li>
39 * <li>TargetSecuritySystemState: String type</li>
42 * @author Cody Cutrer - Initial contribution
44 public class HomekitSecuritySystemImpl extends AbstractHomekitAccessoryImpl implements SecuritySystemAccessory {
45 private final Map<CurrentSecuritySystemStateEnum, String> currentStateMapping;
46 private final Map<TargetSecuritySystemStateEnum, String> targetStateMapping;
47 private final List<CurrentSecuritySystemStateEnum> customCurrentStateList = new ArrayList<>();
48 private final List<TargetSecuritySystemStateEnum> customTargetStateList = new ArrayList<>();
50 public HomekitSecuritySystemImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
51 HomekitAccessoryUpdater updater, HomekitSettings settings) {
52 super(taggedItem, mandatoryCharacteristics, updater, settings);
53 currentStateMapping = createMapping(SECURITY_SYSTEM_CURRENT_STATE, CurrentSecuritySystemStateEnum.class,
54 customCurrentStateList);
55 targetStateMapping = createMapping(SECURITY_SYSTEM_TARGET_STATE, TargetSecuritySystemStateEnum.class,
56 customTargetStateList);
60 public void init() throws HomekitException {
62 getServices().add(new SecuritySystemService(this));
66 public CurrentSecuritySystemStateEnum[] getCurrentSecuritySystemStateValidValues() {
67 return customCurrentStateList.isEmpty()
68 ? currentStateMapping.keySet().toArray(new CurrentSecuritySystemStateEnum[0])
69 : customCurrentStateList.toArray(new CurrentSecuritySystemStateEnum[0]);
73 public TargetSecuritySystemStateEnum[] getTargetSecuritySystemStateValidValues() {
74 return customTargetStateList.isEmpty()
75 ? targetStateMapping.keySet().toArray(new TargetSecuritySystemStateEnum[0])
76 : customTargetStateList.toArray(new TargetSecuritySystemStateEnum[0]);
80 public CompletableFuture<CurrentSecuritySystemStateEnum> getCurrentSecuritySystemState() {
81 return CompletableFuture.completedFuture(getKeyFromMapping(SECURITY_SYSTEM_CURRENT_STATE, currentStateMapping,
82 CurrentSecuritySystemStateEnum.DISARMED));
86 public void setTargetSecuritySystemState(TargetSecuritySystemStateEnum state) {
87 HomekitCharacteristicFactory.setValueFromEnum(
88 getCharacteristic(HomekitCharacteristicType.SECURITY_SYSTEM_TARGET_STATE).get(), state,
93 public CompletableFuture<TargetSecuritySystemStateEnum> getTargetSecuritySystemState() {
94 return CompletableFuture.completedFuture(getKeyFromMapping(SECURITY_SYSTEM_TARGET_STATE, targetStateMapping,
95 TargetSecuritySystemStateEnum.DISARM));
99 public void subscribeCurrentSecuritySystemState(HomekitCharacteristicChangeCallback callback) {
100 subscribe(SECURITY_SYSTEM_CURRENT_STATE, callback);
104 public void unsubscribeCurrentSecuritySystemState() {
105 unsubscribe(SECURITY_SYSTEM_CURRENT_STATE);
109 public void subscribeTargetSecuritySystemState(HomekitCharacteristicChangeCallback callback) {
110 subscribe(SECURITY_SYSTEM_TARGET_STATE, callback);
114 public void unsubscribeTargetSecuritySystemState() {
115 unsubscribe(SECURITY_SYSTEM_TARGET_STATE);