]> git.basschouten.com Git - openhab-addons.git/blob
eabf034ab4e698a3a8a298ab1f9acc97400836ec
[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 java.util.HashMap;
18 import java.util.Map;
19
20 import org.openhab.core.library.types.OnOffType;
21 import org.openhab.core.library.types.StringType;
22 import org.openhab.core.thing.Thing;
23
24 import com.google.gson.JsonObject;
25
26 /**
27  * Abstract base class for Xiaomi sensor devices, which provide an alarm status message
28  *
29  * @author Dieter Schmidt - Initial contribution
30  */
31 public abstract class XiaomiSensorBaseAlarmHandler extends XiaomiSensorBaseHandler {
32
33     private static final Map<Integer, String> ALARM_STATUS_MAP = new HashMap<>();
34     private static final String ALARM = "alarm";
35     private static final String UNKNOWN = "unknown";
36
37     public XiaomiSensorBaseAlarmHandler(Thing thing) {
38         super(thing);
39     }
40
41     @Override
42     void parseReport(JsonObject data) {
43         if (data.has(ALARM)) {
44             int alarm = data.get(ALARM).getAsInt();
45             // alarm channel only receives the "real alarm"
46             if (alarm == 1) {
47                 updateState(CHANNEL_ALARM, OnOffType.ON);
48             } else {
49                 updateState(CHANNEL_ALARM, OnOffType.OFF);
50             }
51             // status shows faults
52             String status = ALARM_STATUS_MAP.get(alarm);
53             if (status != null) {
54                 updateState(CHANNEL_STATUS, StringType.valueOf(status));
55             } else {
56                 updateState(CHANNEL_STATUS, StringType.valueOf(UNKNOWN));
57             }
58         }
59     }
60 }