]> git.basschouten.com Git - openhab-addons.git/blob
23a3e9414daf087a0cb370b8c8bd4030edc46491
[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.mihome.internal.handler;
14
15 import static org.openhab.binding.mihome.internal.XiaomiGatewayBindingConstants.*;
16
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;
25
26 import com.google.gson.JsonObject;
27
28 /**
29  * Handles the Xiaomi human body motion sensor
30  *
31  * @author Patrick Boos - Initial contribution
32  * @author Dieter Schmidt - Refactor
33  */
34 public class XiaomiSensorMotionHandler extends XiaomiSensorBaseHandlerWithTimer {
35
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";
41
42     private final Logger logger = LoggerFactory.getLogger(XiaomiSensorMotionHandler.class);
43
44     public XiaomiSensorMotionHandler(Thing thing) {
45         super(thing, DEFAULT_TIMER, MIN_TIMER, CHANNEL_MOTION_OFF_TIMER);
46     }
47
48     @Override
49     void parseReport(JsonObject data) {
50         boolean hasMotion = data.has(STATUS) && MOTION.equals(data.get(STATUS).getAsString());
51
52         if (data.has(LUX)) {
53             int illu = data.get(LUX).getAsInt();
54             updateState(CHANNEL_ILLUMINATION, new DecimalType(illu));
55         }
56
57         synchronized (this) {
58             if (hasMotion) {
59                 updateState(CHANNEL_MOTION, OnOffType.ON);
60                 updateState(CHANNEL_LAST_MOTION, new DateTimeType());
61                 startTimer();
62             }
63         }
64     }
65
66     @Override
67     void execute(ChannelUID channelUID, Command command) {
68         if (CHANNEL_MOTION_OFF_TIMER.equals(channelUID.getId())) {
69             if (command instanceof DecimalType decimalCommand) {
70                 setTimerFromDecimalType(decimalCommand);
71                 return;
72             }
73             // Only gets here, if no condition was met
74             logger.error("Can't handle command {} on channel {}", command, channelUID);
75         } else {
76             logger.error("Channel {} does not exist", channelUID);
77         }
78     }
79
80     @Override
81     void onTimer() {
82         updateState(CHANNEL_MOTION, OnOffType.OFF);
83     }
84 }