2 * Copyright (c) 2010-2023 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.binding.hue.internal.handler.sensors;
15 import static org.openhab.binding.hue.internal.HueBindingConstants.*;
16 import static org.openhab.binding.hue.internal.api.dto.clip1.FullSensor.*;
18 import java.math.BigDecimal;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.openhab.binding.hue.internal.api.dto.clip1.FullSensor;
24 import org.openhab.binding.hue.internal.api.dto.clip1.LightLevelConfigUpdate;
25 import org.openhab.binding.hue.internal.api.dto.clip1.SensorConfigUpdate;
26 import org.openhab.binding.hue.internal.handler.HueSensorHandler;
27 import org.openhab.core.config.core.Configuration;
28 import org.openhab.core.library.types.DecimalType;
29 import org.openhab.core.library.types.OnOffType;
30 import org.openhab.core.library.types.QuantityType;
31 import org.openhab.core.library.unit.Units;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.thing.ThingTypeUID;
38 * @author Samuel Leisering - Initial contribution
39 * @author Christoph Weitkamp - Initial contribution
42 public class LightLevelHandler extends HueSensorHandler {
44 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_LIGHT_LEVEL_SENSOR);
46 public LightLevelHandler(Thing thing) {
51 protected SensorConfigUpdate doConfigurationUpdate(Map<String, Object> configurationParameters) {
52 LightLevelConfigUpdate configUpdate = new LightLevelConfigUpdate();
53 if (configurationParameters.containsKey(CONFIG_LED_INDICATION)) {
54 configUpdate.setLedIndication(Boolean.TRUE.equals(configurationParameters.get(CONFIG_LED_INDICATION)));
56 if (configurationParameters.containsKey(CONFIG_LIGHT_LEVEL_THRESHOLD_DARK)) {
57 configUpdate.setThresholdDark(
58 Integer.parseInt(String.valueOf(configurationParameters.get(CONFIG_LIGHT_LEVEL_THRESHOLD_DARK))));
60 if (configurationParameters.containsKey(CONFIG_LIGHT_LEVEL_THRESHOLD_OFFSET)) {
61 configUpdate.setThresholdOffset(
62 Integer.parseInt(String.valueOf(configurationParameters.get(CONFIG_LIGHT_LEVEL_THRESHOLD_OFFSET))));
68 protected void doSensorStateChanged(FullSensor sensor, Configuration config) {
69 Object lightLevel = sensor.getState().get(STATE_LIGHT_LEVEL);
70 if (lightLevel != null) {
71 BigDecimal value = new BigDecimal(String.valueOf(lightLevel));
72 updateState(CHANNEL_LIGHT_LEVEL, new DecimalType(value));
74 // calculate lux, according to
75 // https://developers.meethue.com/documentation/supported-sensors#clip_zll_lightlevel
76 double lux = Math.pow(10, (value.subtract(BigDecimal.ONE).divide(new BigDecimal(10000))).doubleValue());
77 updateState(CHANNEL_ILLUMINANCE, new QuantityType<>(lux, Units.LUX));
80 Object dark = sensor.getState().get(STATE_DARK);
82 boolean value = Boolean.parseBoolean(String.valueOf(dark));
83 updateState(CHANNEL_DARK, value ? OnOffType.ON : OnOffType.OFF);
86 Object daylight = sensor.getState().get(STATE_DAYLIGHT);
87 if (daylight != null) {
88 boolean value = Boolean.parseBoolean(String.valueOf(daylight));
89 updateState(CHANNEL_DAYLIGHT, value ? OnOffType.ON : OnOffType.OFF);
92 if (sensor.getConfig().containsKey(CONFIG_LED_INDICATION)) {
93 config.put(CONFIG_LED_INDICATION, sensor.getConfig().get(CONFIG_LED_INDICATION));
95 if (sensor.getConfig().containsKey(CONFIG_LIGHT_LEVEL_THRESHOLD_DARK)) {
96 config.put(CONFIG_LIGHT_LEVEL_THRESHOLD_DARK, sensor.getConfig().get(CONFIG_LIGHT_LEVEL_THRESHOLD_DARK));
98 if (sensor.getConfig().containsKey(CONFIG_LIGHT_LEVEL_THRESHOLD_OFFSET)) {
99 config.put(CONFIG_LIGHT_LEVEL_THRESHOLD_OFFSET,
100 sensor.getConfig().get(CONFIG_LIGHT_LEVEL_THRESHOLD_OFFSET));