]> git.basschouten.com Git - openhab-addons.git/blob
8ea96bedc42bf579021aca2c4dc88f644cead9c2
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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 java.util.List;
16 import java.util.concurrent.CompletableFuture;
17
18 import org.openhab.core.library.types.OnOffType;
19 import org.openhab.core.library.types.OpenClosedType;
20 import org.openhab.io.homekit.internal.HomekitAccessoryUpdater;
21 import org.openhab.io.homekit.internal.HomekitCharacteristicType;
22 import org.openhab.io.homekit.internal.HomekitSettings;
23 import org.openhab.io.homekit.internal.HomekitTaggedItem;
24
25 import io.github.hapjava.accessories.MotionSensorAccessory;
26 import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
27 import io.github.hapjava.services.impl.MotionSensorService;
28
29 /**
30  *
31  * @author Tim Harper - Initial contribution
32  */
33 public class HomekitMotionSensorImpl extends AbstractHomekitAccessoryImpl implements MotionSensorAccessory {
34     private final BooleanItemReader motionSensedReader;
35
36     public HomekitMotionSensorImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
37             HomekitAccessoryUpdater updater, HomekitSettings settings) throws IncompleteAccessoryException {
38         super(taggedItem, mandatoryCharacteristics, updater, settings);
39         motionSensedReader = createBooleanReader(HomekitCharacteristicType.MOTION_DETECTED_STATE, OnOffType.ON,
40                 OpenClosedType.OPEN);
41         getServices().add(new MotionSensorService(this));
42     }
43
44     @Override
45     public CompletableFuture<Boolean> getMotionDetected() {
46         return CompletableFuture.completedFuture(motionSensedReader.getValue());
47     }
48
49     @Override
50     public void subscribeMotionDetected(HomekitCharacteristicChangeCallback callback) {
51         subscribe(HomekitCharacteristicType.MOTION_DETECTED_STATE, callback);
52     }
53
54     @Override
55     public void unsubscribeMotionDetected() {
56         unsubscribe(HomekitCharacteristicType.MOTION_DETECTED_STATE);
57     }
58 }