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.Characteristic;
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;
47 private final Map<TargetSecuritySystemStateEnum, String> targetStateMapping;
48 private final List<CurrentSecuritySystemStateEnum> customCurrentStateList = new ArrayList<>();
49 private final List<TargetSecuritySystemStateEnum> customTargetStateList = new ArrayList<>();
51 public HomekitSecuritySystemImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
52 List<Characteristic> mandatoryRawCharacteristics, HomekitAccessoryUpdater updater,
53 HomekitSettings settings) {
54 super(taggedItem, mandatoryCharacteristics, mandatoryRawCharacteristics, updater, settings);
55 currentStateMapping = createMapping(SECURITY_SYSTEM_CURRENT_STATE, CurrentSecuritySystemStateEnum.class,
56 customCurrentStateList);
57 targetStateMapping = createMapping(SECURITY_SYSTEM_TARGET_STATE, TargetSecuritySystemStateEnum.class,
58 customTargetStateList);
62 public void init() throws HomekitException {
64 addService(new SecuritySystemService(this));
68 public CurrentSecuritySystemStateEnum[] getCurrentSecuritySystemStateValidValues() {
69 return customCurrentStateList.isEmpty()
70 ? currentStateMapping.keySet().toArray(new CurrentSecuritySystemStateEnum[0])
71 : customCurrentStateList.toArray(new CurrentSecuritySystemStateEnum[0]);
75 public TargetSecuritySystemStateEnum[] getTargetSecuritySystemStateValidValues() {
76 return customTargetStateList.isEmpty()
77 ? targetStateMapping.keySet().toArray(new TargetSecuritySystemStateEnum[0])
78 : customTargetStateList.toArray(new TargetSecuritySystemStateEnum[0]);
82 public CompletableFuture<CurrentSecuritySystemStateEnum> getCurrentSecuritySystemState() {
83 return CompletableFuture.completedFuture(getKeyFromMapping(SECURITY_SYSTEM_CURRENT_STATE, currentStateMapping,
84 CurrentSecuritySystemStateEnum.DISARMED));
88 public void setTargetSecuritySystemState(TargetSecuritySystemStateEnum state) {
89 HomekitCharacteristicFactory.setValueFromEnum(
90 getCharacteristic(HomekitCharacteristicType.SECURITY_SYSTEM_TARGET_STATE).get(), state,
95 public CompletableFuture<TargetSecuritySystemStateEnum> getTargetSecuritySystemState() {
96 return CompletableFuture.completedFuture(getKeyFromMapping(SECURITY_SYSTEM_TARGET_STATE, targetStateMapping,
97 TargetSecuritySystemStateEnum.DISARM));
101 public void subscribeCurrentSecuritySystemState(HomekitCharacteristicChangeCallback callback) {
102 subscribe(SECURITY_SYSTEM_CURRENT_STATE, callback);
106 public void unsubscribeCurrentSecuritySystemState() {
107 unsubscribe(SECURITY_SYSTEM_CURRENT_STATE);
111 public void subscribeTargetSecuritySystemState(HomekitCharacteristicChangeCallback callback) {
112 subscribe(SECURITY_SYSTEM_TARGET_STATE, callback);
116 public void unsubscribeTargetSecuritySystemState() {
117 unsubscribe(SECURITY_SYSTEM_TARGET_STATE);