]> git.basschouten.com Git - openhab-addons.git/blob
39caa20297866acf568892439346087896f7544e
[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.api.dto.clip1.FullSensor.*;
17
18 import java.util.Map;
19 import java.util.Set;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.hue.internal.api.dto.clip1.FullSensor;
23 import org.openhab.binding.hue.internal.api.dto.clip1.PresenceConfigUpdate;
24 import org.openhab.binding.hue.internal.api.dto.clip1.SensorConfigUpdate;
25 import org.openhab.binding.hue.internal.handler.HueClient;
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.ThingStatus;
31 import org.openhab.core.thing.ThingStatusDetail;
32 import org.openhab.core.thing.ThingTypeUID;
33 import org.openhab.core.types.Command;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * Presence Sensor
39  *
40  * @author Samuel Leisering - Initial contribution
41  * @author Christoph Weitkamp - Initial contribution
42  */
43 @NonNullByDefault
44 public class PresenceHandler extends HueSensorHandler {
45
46     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_PRESENCE_SENSOR);
47
48     private final Logger logger = LoggerFactory.getLogger(PresenceHandler.class);
49
50     public PresenceHandler(Thing thing) {
51         super(thing);
52     }
53
54     @Override
55     public void handleCommand(String channel, Command command) {
56         HueClient hueBridge = getHueClient();
57         if (hueBridge == null) {
58             logger.warn("Hue Bridge handler not found. Cannot handle command without bridge.");
59             return;
60         }
61
62         final FullSensor sensor = lastFullSensor;
63         if (sensor == null) {
64             logger.debug("Hue sensor not known on bridge. Cannot handle command.");
65             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
66                     "@text/offline.conf-error-wrong-sensor-id");
67             return;
68         }
69
70         SensorConfigUpdate configUpdate = new SensorConfigUpdate();
71         switch (channel) {
72             case CHANNEL_ENABLED:
73                 configUpdate.setOn(OnOffType.ON.equals(command));
74                 break;
75         }
76
77         hueBridge.updateSensorConfig(sensor, configUpdate);
78     }
79
80     @Override
81     protected SensorConfigUpdate doConfigurationUpdate(Map<String, Object> configurationParameters) {
82         PresenceConfigUpdate configUpdate = new PresenceConfigUpdate();
83         if (configurationParameters.containsKey(CONFIG_LED_INDICATION)) {
84             configUpdate.setLedIndication(Boolean.TRUE.equals(configurationParameters.get(CONFIG_LED_INDICATION)));
85         }
86         if (configurationParameters.containsKey(CONFIG_PRESENCE_SENSITIVITY)) {
87             configUpdate.setSensitivity(
88                     Integer.parseInt(String.valueOf(configurationParameters.get(CONFIG_PRESENCE_SENSITIVITY))));
89         }
90         return configUpdate;
91     }
92
93     @Override
94     protected void doSensorStateChanged(FullSensor sensor, Configuration config) {
95         Object presence = sensor.getState().get(STATE_PRESENCE);
96         if (presence != null) {
97             boolean value = Boolean.parseBoolean(String.valueOf(presence));
98             updateState(CHANNEL_PRESENCE, value ? OnOffType.ON : OnOffType.OFF);
99         }
100
101         if (sensor.getConfig().containsKey(CONFIG_LED_INDICATION)) {
102             config.put(CONFIG_LED_INDICATION, sensor.getConfig().get(CONFIG_LED_INDICATION));
103         }
104         if (sensor.getConfig().containsKey(CONFIG_PRESENCE_SENSITIVITY)) {
105             config.put(CONFIG_PRESENCE_SENSITIVITY, sensor.getConfig().get(CONFIG_PRESENCE_SENSITIVITY));
106         }
107         if (sensor.getConfig().containsKey(CONFIG_PRESENCE_SENSITIVITY_MAX)) {
108             config.put(CONFIG_PRESENCE_SENSITIVITY_MAX, sensor.getConfig().get(CONFIG_PRESENCE_SENSITIVITY_MAX));
109         }
110     }
111 }