]> git.basschouten.com Git - openhab-addons.git/blob
7dc3fb32bae32ff165d9695f54acdfb2207bec52
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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() : getMinCurrentAmbientLightLevel());
51     }
52
53     @Override
54     public double getMinCurrentAmbientLightLevel() {
55         return getAccessoryConfiguration(HomekitCharacteristicType.LIGHT_LEVEL, HomekitTaggedItem.MIN_VALUE,
56                 BigDecimal.valueOf(CurrentAmbientLightLevelCharacteristic.DEFAULT_MIN_VALUE)).doubleValue();
57     }
58
59     @Override
60     public double getMaxCurrentAmbientLightLevel() {
61         return getAccessoryConfiguration(HomekitCharacteristicType.LIGHT_LEVEL, HomekitTaggedItem.MAX_VALUE,
62                 BigDecimal.valueOf(CurrentAmbientLightLevelCharacteristic.DEFAULT_MAX_VALUE)).doubleValue();
63     }
64
65     @Override
66     public void subscribeCurrentAmbientLightLevel(HomekitCharacteristicChangeCallback callback) {
67         subscribe(LIGHT_LEVEL, callback);
68     }
69
70     @Override
71     public void unsubscribeCurrentAmbientLightLevel() {
72         unsubscribe(LIGHT_LEVEL);
73     }
74 }