]> git.basschouten.com Git - openhab-addons.git/blob
ae77d6a15037cf3d4ef2d8a01430f48ee62713fe
[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.QuantityType;
18 import org.openhab.core.thing.Thing;
19
20 import com.google.gson.JsonObject;
21
22 /**
23  * Handles the Xiaomi temperature & humidity sensor
24  *
25  * @author Patrick Boos - Initial contribution
26  * @author Daniel Walters - Add pressure support
27  */
28 public class XiaomiSensorHtHandler extends XiaomiSensorBaseHandler {
29
30     private static final String HUMIDITY = "humidity";
31     private static final String TEMPERATURE = "temperature";
32     private static final String VOLTAGE = "voltage";
33     private static final String PRESSURE = "pressure";
34
35     public XiaomiSensorHtHandler(Thing thing) {
36         super(thing);
37     }
38
39     @Override
40     void parseReport(JsonObject data) {
41         parseDefault(data);
42     }
43
44     @Override
45     void parseHeartbeat(JsonObject data) {
46         parseDefault(data);
47     }
48
49     @Override
50     void parseReadAck(JsonObject data) {
51         parseDefault(data);
52     }
53
54     @Override
55     void parseDefault(JsonObject data) {
56         if (data.has(HUMIDITY)) {
57             float humidity = data.get(HUMIDITY).getAsFloat() / 100;
58             updateState(CHANNEL_HUMIDITY, new QuantityType<>(humidity, PERCENT_UNIT));
59         }
60         if (data.has(TEMPERATURE)) {
61             float temperature = data.get(TEMPERATURE).getAsFloat() / 100;
62             updateState(CHANNEL_TEMPERATURE, new QuantityType<>(temperature, TEMPERATURE_UNIT));
63         }
64         if (data.has(VOLTAGE)) {
65             Integer voltage = data.get(VOLTAGE).getAsInt();
66             calculateBatteryLevelFromVoltage(voltage);
67         }
68         if (data.has(PRESSURE)) {
69             float pressure = (float) (data.get(PRESSURE).getAsFloat() / 1000.0);
70             updateState(CHANNEL_PRESSURE, new QuantityType<>(pressure, PRESSURE_UNIT));
71         }
72     }
73 }