]> git.basschouten.com Git - openhab-addons.git/blob
2fda9a102fc75c96b6631d045ae6be4bda768e67
[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 static org.openhab.io.homekit.internal.HomekitCharacteristicType.LIGHT_LEVEL;
16
17 import java.util.List;
18 import java.util.concurrent.CompletableFuture;
19
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.core.library.types.DecimalType;
22 import org.openhab.io.homekit.internal.HomekitAccessoryUpdater;
23 import org.openhab.io.homekit.internal.HomekitSettings;
24 import org.openhab.io.homekit.internal.HomekitTaggedItem;
25
26 import io.github.hapjava.accessories.LightSensorAccessory;
27 import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
28 import io.github.hapjava.services.impl.LightSensorService;
29
30 /**
31  * HomeKit light sensor implementation.
32  *
33  * @author Eugen Freiter - Initial contribution
34  */
35 public class HomekitLightSensorImpl extends AbstractHomekitAccessoryImpl implements LightSensorAccessory {
36
37     public HomekitLightSensorImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
38             HomekitAccessoryUpdater updater, HomekitSettings settings) {
39         super(taggedItem, mandatoryCharacteristics, updater, settings);
40         getServices().add(new LightSensorService(this));
41     }
42
43     @Override
44     public CompletableFuture<Double> getCurrentAmbientLightLevel() {
45         final @Nullable DecimalType state = getStateAs(LIGHT_LEVEL, DecimalType.class);
46         return CompletableFuture.completedFuture(state != null ? state.doubleValue() : 0.0);
47     }
48
49     @Override
50     public void subscribeCurrentAmbientLightLevel(HomekitCharacteristicChangeCallback callback) {
51         subscribe(LIGHT_LEVEL, callback);
52     }
53
54     @Override
55     public void unsubscribeCurrentAmbientLightLevel() {
56         unsubscribe(LIGHT_LEVEL);
57     }
58 }