2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.io.homekit.internal.accessories;
15 import static org.openhab.io.homekit.internal.HomekitCharacteristicType.LIGHT_LEVEL;
17 import java.math.BigDecimal;
18 import java.util.List;
19 import java.util.concurrent.CompletableFuture;
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;
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;
36 * HomeKit light sensor implementation.
38 * @author Eugen Freiter - Initial contribution
40 public class HomekitLightSensorImpl extends AbstractHomekitAccessoryImpl implements LightSensorAccessory {
42 public HomekitLightSensorImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
43 List<Characteristic> mandatoryRawCharacteristics, HomekitAccessoryUpdater updater,
44 HomekitSettings settings) {
45 super(taggedItem, mandatoryCharacteristics, mandatoryRawCharacteristics, updater, settings);
49 public void init() throws HomekitException {
51 addService(new LightSensorService(this));
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());
62 public double getMinCurrentAmbientLightLevel() {
63 return getAccessoryConfiguration(HomekitCharacteristicType.LIGHT_LEVEL, HomekitTaggedItem.MIN_VALUE,
64 BigDecimal.valueOf(CurrentAmbientLightLevelCharacteristic.DEFAULT_MIN_VALUE)).doubleValue();
68 public double getMaxCurrentAmbientLightLevel() {
69 return getAccessoryConfiguration(HomekitCharacteristicType.LIGHT_LEVEL, HomekitTaggedItem.MAX_VALUE,
70 BigDecimal.valueOf(CurrentAmbientLightLevelCharacteristic.DEFAULT_MAX_VALUE)).doubleValue();
74 public void subscribeCurrentAmbientLightLevel(HomekitCharacteristicChangeCallback callback) {
75 subscribe(LIGHT_LEVEL, callback);
79 public void unsubscribeCurrentAmbientLightLevel() {
80 unsubscribe(LIGHT_LEVEL);