]> git.basschouten.com Git - openhab-addons.git/blob
d62289319fae8edf7286db1dfd2e02923c71077d
[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.OpenClosedType;
20 import org.openhab.core.thing.ChannelUID;
21 import org.openhab.core.thing.Thing;
22 import org.openhab.core.types.Command;
23 import org.openhab.core.types.UnDefType;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import com.google.gson.JsonObject;
28
29 /**
30  * Handles the Xiaomi smart door/window sensor
31  *
32  * @author Patrick Boos - Initial contribution
33  * @author Dieter Schmidt - Refactor
34  * @author Daniel Walters - Add voltage parsing
35  */
36 public class XiaomiSensorMagnetHandler extends XiaomiSensorBaseHandlerWithTimer {
37
38     private static final int DEFAULT_TIMER = 300;
39     private static final int MIN_TIMER = 30;
40     private static final String OPEN = "open";
41     private static final String CLOSED = "close";
42     private static final String STATUS = "status";
43
44     private final Logger logger = LoggerFactory.getLogger(XiaomiSensorMagnetHandler.class);
45
46     public XiaomiSensorMagnetHandler(Thing thing) {
47         super(thing, DEFAULT_TIMER, MIN_TIMER, CHANNEL_OPEN_ALARM_TIMER);
48     }
49
50     @Override
51     void parseReport(JsonObject data) {
52         if (data.has(STATUS)) {
53             String sensorStatus = data.get(STATUS).getAsString();
54             synchronized (this) {
55                 if (OPEN.equals(sensorStatus)) {
56                     updateState(CHANNEL_LAST_OPENED, new DateTimeType());
57                     startTimer();
58                 } else {
59                     cancelRunningTimer();
60                 }
61             }
62         }
63         parseDefault(data);
64     }
65
66     @Override
67     void parseDefault(JsonObject data) {
68         if (data.has(STATUS)) {
69             String sensorStatus = data.get(STATUS).getAsString();
70             if (OPEN.equals(sensorStatus)) {
71                 updateState(CHANNEL_IS_OPEN, OpenClosedType.OPEN);
72             } else if (CLOSED.equals(sensorStatus)) {
73                 updateState(CHANNEL_IS_OPEN, OpenClosedType.CLOSED);
74             } else {
75                 updateState(CHANNEL_IS_OPEN, UnDefType.UNDEF);
76             }
77         }
78         super.parseDefault(data);
79     }
80
81     @Override
82     void execute(ChannelUID channelUID, Command command) {
83         if (CHANNEL_OPEN_ALARM_TIMER.equals(channelUID.getId())) {
84             if (command instanceof DecimalType decimalCommand) {
85                 setTimerFromDecimalType(decimalCommand);
86                 return;
87             }
88             // Only gets here, if no condition was met
89             logger.error("Can't handle command {} on channel {}", command, channelUID);
90         } else {
91             logger.error("Channel {} does not exist", channelUID);
92         }
93     }
94
95     @Override
96     void onTimer() {
97         triggerChannel(CHANNEL_OPEN_ALARM, "ALARM");
98     }
99 }