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