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.mihome.internal.handler;
15 import static org.openhab.binding.mihome.internal.XiaomiGatewayBindingConstants.*;
17 import org.openhab.core.library.types.DateTimeType;
18 import org.openhab.core.library.types.DecimalType;
19 import org.openhab.core.library.types.OnOffType;
20 import org.openhab.core.thing.ChannelUID;
21 import org.openhab.core.thing.Thing;
22 import org.openhab.core.types.Command;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 import com.google.gson.JsonObject;
29 * Handles the Xiaomi human body motion sensor
31 * @author Patrick Boos - Initial contribution
32 * @author Dieter Schmidt - Refactor
34 public class XiaomiSensorMotionHandler extends XiaomiSensorBaseHandlerWithTimer {
36 private static final int DEFAULT_TIMER = 120;
37 private static final int MIN_TIMER = 5;
38 private static final String STATUS = "status";
39 private static final String MOTION = "motion";
40 private static final String LUX = "lux";
42 private final Logger logger = LoggerFactory.getLogger(XiaomiSensorMotionHandler.class);
44 public XiaomiSensorMotionHandler(Thing thing) {
45 super(thing, DEFAULT_TIMER, MIN_TIMER, CHANNEL_MOTION_OFF_TIMER);
49 void parseReport(JsonObject data) {
50 boolean hasMotion = data.has(STATUS) && MOTION.equals(data.get(STATUS).getAsString());
53 int illu = data.get(LUX).getAsInt();
54 updateState(CHANNEL_ILLUMINATION, new DecimalType(illu));
59 updateState(CHANNEL_MOTION, OnOffType.ON);
60 updateState(CHANNEL_LAST_MOTION, new DateTimeType());
67 void execute(ChannelUID channelUID, Command command) {
68 if (CHANNEL_MOTION_OFF_TIMER.equals(channelUID.getId())) {
69 if (command != null && command instanceof DecimalType) {
70 setTimerFromDecimalType((DecimalType) command);
73 // Only gets here, if no condition was met
74 logger.error("Can't handle command {} on channel {}", command, channelUID);
76 logger.error("Channel {} does not exist", channelUID);
82 updateState(CHANNEL_MOTION, OnOffType.OFF);