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