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.CONTACT_SENSOR_STATE;
17 import java.util.List;
19 import java.util.concurrent.CompletableFuture;
21 import org.openhab.io.homekit.internal.HomekitAccessoryUpdater;
22 import org.openhab.io.homekit.internal.HomekitException;
23 import org.openhab.io.homekit.internal.HomekitSettings;
24 import org.openhab.io.homekit.internal.HomekitTaggedItem;
26 import io.github.hapjava.accessories.ContactSensorAccessory;
27 import io.github.hapjava.characteristics.Characteristic;
28 import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
29 import io.github.hapjava.characteristics.impl.contactsensor.ContactStateEnum;
30 import io.github.hapjava.services.impl.ContactSensorService;
34 * @author Philipp Arndt - Initial contribution
36 public class HomekitContactSensorImpl extends AbstractHomekitAccessoryImpl implements ContactSensorAccessory {
37 private final Map<ContactStateEnum, String> mapping;
39 public HomekitContactSensorImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
40 List<Characteristic> mandatoryRawCharacteristics, HomekitAccessoryUpdater updater, HomekitSettings settings)
41 throws IncompleteAccessoryException {
42 super(taggedItem, mandatoryCharacteristics, mandatoryRawCharacteristics, updater, settings);
43 mapping = createMapping(CONTACT_SENSOR_STATE, ContactStateEnum.class);
47 public void init() throws HomekitException {
49 addService(new ContactSensorService(this));
53 public CompletableFuture<ContactStateEnum> getCurrentState() {
54 return CompletableFuture
55 .completedFuture(getKeyFromMapping(CONTACT_SENSOR_STATE, mapping, ContactStateEnum.DETECTED));
59 public void subscribeContactState(HomekitCharacteristicChangeCallback callback) {
60 subscribe(CONTACT_SENSOR_STATE, callback);
64 public void unsubscribeContactState() {
65 unsubscribe(CONTACT_SENSOR_STATE);