]> git.basschouten.com Git - openhab-addons.git/blob
87b4b0f55c249246ee4f4c6d8332e89eaf9e394e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.FullSensor.*;
16 import static org.openhab.binding.hue.internal.HueBindingConstants.*;
17
18 import java.util.Collections;
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.FullSensor;
24 import org.openhab.binding.hue.internal.PresenceConfigUpdate;
25 import org.openhab.binding.hue.internal.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.OnOffType;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingTypeUID;
31
32 /**
33  * Presence Sensor
34  *
35  * @author Samuel Leisering - Initial contribution
36  * @author Christoph Weitkamp - Initial contribution
37  */
38 @NonNullByDefault
39 public class PresenceHandler extends HueSensorHandler {
40     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_PRESENCE_SENSOR);
41
42     public PresenceHandler(Thing thing) {
43         super(thing);
44     }
45
46     @Override
47     protected SensorConfigUpdate doConfigurationUpdate(Map<String, Object> configurationParameters) {
48         PresenceConfigUpdate configUpdate = new PresenceConfigUpdate();
49         if (configurationParameters.containsKey(CONFIG_LED_INDICATION)) {
50             configUpdate.setLedIndication(Boolean.TRUE.equals(configurationParameters.get(CONFIG_LED_INDICATION)));
51         }
52         if (configurationParameters.containsKey(CONFIG_PRESENCE_SENSITIVITY)) {
53             configUpdate.setSensitivity(
54                     Integer.parseInt(String.valueOf(configurationParameters.get(CONFIG_PRESENCE_SENSITIVITY))));
55         }
56         return configUpdate;
57     }
58
59     @Override
60     protected void doSensorStateChanged(FullSensor sensor, Configuration config) {
61         Object presence = sensor.getState().get(STATE_PRESENCE);
62         if (presence != null) {
63             boolean value = Boolean.parseBoolean(String.valueOf(presence));
64             updateState(CHANNEL_PRESENCE, value ? OnOffType.ON : OnOffType.OFF);
65         }
66
67         if (sensor.getConfig().containsKey(CONFIG_LED_INDICATION)) {
68             config.put(CONFIG_LED_INDICATION, sensor.getConfig().get(CONFIG_LIGHT_LEVEL_THRESHOLD_DARK));
69         }
70         if (sensor.getConfig().containsKey(CONFIG_PRESENCE_SENSITIVITY)) {
71             config.put(CONFIG_PRESENCE_SENSITIVITY, sensor.getConfig().get(CONFIG_PRESENCE_SENSITIVITY));
72         }
73         if (sensor.getConfig().containsKey(CONFIG_PRESENCE_SENSITIVITY_MAX)) {
74             config.put(CONFIG_PRESENCE_SENSITIVITY_MAX, sensor.getConfig().get(CONFIG_PRESENCE_SENSITIVITY_MAX));
75         }
76     }
77 }