]> git.basschouten.com Git - openhab-addons.git/blob
b8763b2f03a83f3dbdaaaa36ce2348a619dbe44c
[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.CHANNEL_SWITCH_CH0;
16
17 import org.openhab.core.library.types.OnOffType;
18 import org.openhab.core.thing.ChannelUID;
19 import org.openhab.core.thing.Thing;
20 import org.openhab.core.types.Command;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import com.google.gson.JsonObject;
25
26 /**
27  * Handles the Xiaomi aqara wall switch with one button
28  *
29  * @author Dieter Schmidt - Initial contribution
30  */
31 public class XiaomiAqaraActorSwitch1Handler extends XiaomiActorBaseHandler {
32
33     private final Logger logger = LoggerFactory.getLogger(XiaomiAqaraActorSwitch1Handler.class);
34
35     private static final String CHANNEL_0 = "channel_0";
36     private static final String ON = "on";
37
38     public XiaomiAqaraActorSwitch1Handler(Thing thing) {
39         super(thing);
40     }
41
42     @Override
43     void execute(ChannelUID channelUID, Command command) {
44         if (CHANNEL_SWITCH_CH0.equals(channelUID.getId())) {
45             String status = command.toString().toLowerCase();
46             getXiaomiBridgeHandler().writeToDevice(getItemId(), new String[] { CHANNEL_0 }, new Object[] { status });
47             return;
48         }
49         // Only gets here, if no condition was met
50         logger.error("Can't handle command {} on channel {}", command, channelUID);
51     }
52
53     @Override
54     void parseReport(JsonObject data) {
55         parseDefault(data);
56     }
57
58     @Override
59     void parseHeartbeat(JsonObject data) {
60         parseDefault(data);
61     }
62
63     @Override
64     void parseReadAck(JsonObject data) {
65         parseDefault(data);
66     }
67
68     @Override
69     void parseWriteAck(JsonObject data) {
70         logger.debug("Got write ack message but ignoring it to prevent item state toggling");
71     }
72
73     @Override
74     void parseDefault(JsonObject data) {
75         if (data.has(CHANNEL_0)) {
76             boolean isOn = ON.equals(data.get(CHANNEL_0).getAsString().toLowerCase());
77             updateState(CHANNEL_SWITCH_CH0, isOn ? OnOffType.ON : OnOffType.OFF);
78         }
79     }
80 }