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.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;
27 import com.google.gson.JsonObject;
30 * Handles the Xiaomi smart door/window sensor
32 * @author Patrick Boos - Initial contribution
33 * @author Dieter Schmidt - Refactor
34 * @author Daniel Walters - Add voltage parsing
36 public class XiaomiSensorMagnetHandler extends XiaomiSensorBaseHandlerWithTimer {
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";
44 private final Logger logger = LoggerFactory.getLogger(XiaomiSensorMagnetHandler.class);
46 public XiaomiSensorMagnetHandler(Thing thing) {
47 super(thing, DEFAULT_TIMER, MIN_TIMER, CHANNEL_OPEN_ALARM_TIMER);
51 void parseReport(JsonObject data) {
52 if (data.has(STATUS)) {
53 String sensorStatus = data.get(STATUS).getAsString();
55 if (OPEN.equals(sensorStatus)) {
56 updateState(CHANNEL_LAST_OPENED, new DateTimeType());
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);
75 updateState(CHANNEL_IS_OPEN, UnDefType.UNDEF);
78 super.parseDefault(data);
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);
88 // Only gets here, if no condition was met
89 logger.error("Can't handle command {} on channel {}", command, channelUID);
91 logger.error("Channel {} does not exist", channelUID);
97 triggerChannel(CHANNEL_OPEN_ALARM, "ALARM");