]> git.basschouten.com Git - openhab-addons.git/blob
933fb05c604ece3f4820a06b76f427e713e17705
[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.binding.hue.internal.handler.sensors;
14
15 import static org.openhab.binding.hue.internal.HueBindingConstants.*;
16 import static org.openhab.binding.hue.internal.dto.FullSensor.*;
17
18 import java.math.BigDecimal;
19 import java.util.Map;
20 import java.util.Set;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.openhab.binding.hue.internal.dto.FullSensor;
24 import org.openhab.binding.hue.internal.dto.LightLevelConfigUpdate;
25 import org.openhab.binding.hue.internal.dto.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;
34
35 /**
36  * Light Level Sensor
37  *
38  * @author Samuel Leisering - Initial contribution
39  * @author Christoph Weitkamp - Initial contribution
40  */
41 @NonNullByDefault
42 public class LightLevelHandler extends HueSensorHandler {
43
44     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_LIGHT_LEVEL_SENSOR);
45
46     public LightLevelHandler(Thing thing) {
47         super(thing);
48     }
49
50     @Override
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)));
55         }
56         if (configurationParameters.containsKey(CONFIG_LIGHT_LEVEL_THRESHOLD_DARK)) {
57             configUpdate.setThresholdDark(
58                     Integer.parseInt(String.valueOf(configurationParameters.get(CONFIG_LIGHT_LEVEL_THRESHOLD_DARK))));
59         }
60         if (configurationParameters.containsKey(CONFIG_LIGHT_LEVEL_THRESHOLD_OFFSET)) {
61             configUpdate.setThresholdOffset(
62                     Integer.parseInt(String.valueOf(configurationParameters.get(CONFIG_LIGHT_LEVEL_THRESHOLD_OFFSET))));
63         }
64         return configUpdate;
65     }
66
67     @Override
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));
73
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));
78         }
79
80         Object dark = sensor.getState().get(STATE_DARK);
81         if (dark != null) {
82             boolean value = Boolean.parseBoolean(String.valueOf(dark));
83             updateState(CHANNEL_DARK, value ? OnOffType.ON : OnOffType.OFF);
84         }
85
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);
90         }
91
92         if (sensor.getConfig().containsKey(CONFIG_LED_INDICATION)) {
93             config.put(CONFIG_LED_INDICATION, sensor.getConfig().get(CONFIG_LED_INDICATION));
94         }
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));
97         }
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));
101         }
102     }
103 }