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.*;
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;
40 * @author Samuel Leisering - Initial contribution
41 * @author Christoph Weitkamp - Initial contribution
44 public class PresenceHandler extends HueSensorHandler {
46 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_PRESENCE_SENSOR);
48 private final Logger logger = LoggerFactory.getLogger(PresenceHandler.class);
50 public PresenceHandler(Thing thing) {
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.");
62 final FullSensor sensor = lastFullSensor;
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");
70 SensorConfigUpdate configUpdate = new SensorConfigUpdate();
73 configUpdate.setOn(OnOffType.ON.equals(command));
77 hueBridge.updateSensorConfig(sensor, configUpdate);
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)));
86 if (configurationParameters.containsKey(CONFIG_PRESENCE_SENSITIVITY)) {
87 configUpdate.setSensitivity(
88 Integer.parseInt(String.valueOf(configurationParameters.get(CONFIG_PRESENCE_SENSITIVITY))));
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, OnOffType.from(value));
101 if (sensor.getConfig().containsKey(CONFIG_LED_INDICATION)) {
102 config.put(CONFIG_LED_INDICATION, sensor.getConfig().get(CONFIG_LED_INDICATION));
104 if (sensor.getConfig().containsKey(CONFIG_PRESENCE_SENSITIVITY)) {
105 config.put(CONFIG_PRESENCE_SENSITIVITY, sensor.getConfig().get(CONFIG_PRESENCE_SENSITIVITY));
107 if (sensor.getConfig().containsKey(CONFIG_PRESENCE_SENSITIVITY_MAX)) {
108 config.put(CONFIG_PRESENCE_SENSITIVITY_MAX, sensor.getConfig().get(CONFIG_PRESENCE_SENSITIVITY_MAX));