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 java.util.HashMap;
20 import org.openhab.core.library.types.OnOffType;
21 import org.openhab.core.library.types.StringType;
22 import org.openhab.core.thing.Thing;
24 import com.google.gson.JsonObject;
27 * Abstract base class for Xiaomi sensor devices, which provide an alarm status message
29 * @author Dieter Schmidt - Initial contribution
31 public abstract class XiaomiSensorBaseAlarmHandler extends XiaomiSensorBaseHandler {
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";
37 public XiaomiSensorBaseAlarmHandler(Thing thing) {
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"
47 updateState(CHANNEL_ALARM, OnOffType.ON);
49 updateState(CHANNEL_ALARM, OnOffType.OFF);
51 // status shows faults
52 String status = ALARM_STATUS_MAP.get(alarm);
54 updateState(CHANNEL_STATUS, StringType.valueOf(status));
56 updateState(CHANNEL_STATUS, StringType.valueOf(UNKNOWN));