]> git.basschouten.com Git - openhab-addons.git/blob
491733e78d39b85e52bf4cb7c6c035bdb49a50c1
[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 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.HomekitException;
26 import org.openhab.io.homekit.internal.HomekitSettings;
27 import org.openhab.io.homekit.internal.HomekitTaggedItem;
28
29 import io.github.hapjava.accessories.LightSensorAccessory;
30 import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
31 import io.github.hapjava.characteristics.impl.lightsensor.CurrentAmbientLightLevelCharacteristic;
32 import io.github.hapjava.services.impl.LightSensorService;
33
34 /**
35  * HomeKit light sensor implementation.
36  *
37  * @author Eugen Freiter - Initial contribution
38  */
39 public class HomekitLightSensorImpl extends AbstractHomekitAccessoryImpl implements LightSensorAccessory {
40
41     public HomekitLightSensorImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
42             HomekitAccessoryUpdater updater, HomekitSettings settings) {
43         super(taggedItem, mandatoryCharacteristics, updater, settings);
44     }
45
46     @Override
47     public void init() throws HomekitException {
48         super.init();
49         getServices().add(new LightSensorService(this));
50     }
51
52     @Override
53     public CompletableFuture<Double> getCurrentAmbientLightLevel() {
54         final @Nullable DecimalType state = getStateAs(LIGHT_LEVEL, DecimalType.class);
55         return CompletableFuture
56                 .completedFuture(state != null ? state.doubleValue() : getMinCurrentAmbientLightLevel());
57     }
58
59     @Override
60     public double getMinCurrentAmbientLightLevel() {
61         return getAccessoryConfiguration(HomekitCharacteristicType.LIGHT_LEVEL, HomekitTaggedItem.MIN_VALUE,
62                 BigDecimal.valueOf(CurrentAmbientLightLevelCharacteristic.DEFAULT_MIN_VALUE)).doubleValue();
63     }
64
65     @Override
66     public double getMaxCurrentAmbientLightLevel() {
67         return getAccessoryConfiguration(HomekitCharacteristicType.LIGHT_LEVEL, HomekitTaggedItem.MAX_VALUE,
68                 BigDecimal.valueOf(CurrentAmbientLightLevelCharacteristic.DEFAULT_MAX_VALUE)).doubleValue();
69     }
70
71     @Override
72     public void subscribeCurrentAmbientLightLevel(HomekitCharacteristicChangeCallback callback) {
73         subscribe(LIGHT_LEVEL, callback);
74     }
75
76     @Override
77     public void unsubscribeCurrentAmbientLightLevel() {
78         unsubscribe(LIGHT_LEVEL);
79     }
80 }