]> git.basschouten.com Git - openhab-addons.git/blob
3ff7c9a19169fda93e2363fccc606610b742caf4
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.io.homekit.internal.accessories;
14
15 import static org.openhab.io.homekit.internal.HomekitCharacteristicType.CONTACT_SENSOR_STATE;
16
17 import java.util.List;
18 import java.util.Map;
19 import java.util.concurrent.CompletableFuture;
20
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;
25
26 import io.github.hapjava.accessories.ContactSensorAccessory;
27 import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
28 import io.github.hapjava.characteristics.impl.contactsensor.ContactStateEnum;
29 import io.github.hapjava.services.impl.ContactSensorService;
30
31 /**
32  *
33  * @author Philipp Arndt - Initial contribution
34  */
35 public class HomekitContactSensorImpl extends AbstractHomekitAccessoryImpl implements ContactSensorAccessory {
36     private final Map<ContactStateEnum, String> mapping;
37
38     public HomekitContactSensorImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
39             HomekitAccessoryUpdater updater, HomekitSettings settings) throws IncompleteAccessoryException {
40         super(taggedItem, mandatoryCharacteristics, updater, settings);
41         mapping = createMapping(CONTACT_SENSOR_STATE, ContactStateEnum.class);
42     }
43
44     @Override
45     public void init() throws HomekitException {
46         super.init();
47         getServices().add(new ContactSensorService(this));
48     }
49
50     @Override
51     public CompletableFuture<ContactStateEnum> getCurrentState() {
52         return CompletableFuture
53                 .completedFuture(getKeyFromMapping(CONTACT_SENSOR_STATE, mapping, ContactStateEnum.DETECTED));
54     }
55
56     @Override
57     public void subscribeContactState(HomekitCharacteristicChangeCallback callback) {
58         subscribe(CONTACT_SENSOR_STATE, callback);
59     }
60
61     @Override
62     public void unsubscribeContactState() {
63         unsubscribe(CONTACT_SENSOR_STATE);
64     }
65 }