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