]> git.basschouten.com Git - openhab-addons.git/blob
5316c0bb7e0247e6992c2adb9c6687caa0801c3a
[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_CURTAIN_CONTROL;
16
17 import org.openhab.core.library.types.PercentType;
18 import org.openhab.core.library.types.StopMoveType;
19 import org.openhab.core.library.types.UpDownType;
20 import org.openhab.core.thing.ChannelUID;
21 import org.openhab.core.thing.Thing;
22 import org.openhab.core.types.Command;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import com.google.gson.JsonObject;
27
28 /**
29  * Manage the Xiaomi Smart Curtain over the API
30  *
31  * @author Kuba Wolanin - initial contribution
32  */
33 public class XiaomiActorCurtainHandler extends XiaomiActorBaseHandler {
34
35     private final Logger logger = LoggerFactory.getLogger(XiaomiActorCurtainHandler.class);
36     private String lastDirection;
37
38     private static final String STATUS = "status";
39     private static final String OPEN = "open";
40     private static final String CLOSED = "close";
41     private static final String STOP = "stop";
42     private static final String CURTAIN_LEVEL = "curtain_level";
43     private static final String AUTO = "auto";
44
45     public XiaomiActorCurtainHandler(Thing thing) {
46         super(thing);
47         lastDirection = OPEN;
48     }
49
50     @Override
51     void execute(ChannelUID channelUID, Command command) {
52         String status = command.toString().toLowerCase();
53         switch (channelUID.getId()) {
54             case CHANNEL_CURTAIN_CONTROL:
55                 if (command instanceof UpDownType) {
56                     if (command.equals(UpDownType.UP)) {
57                         status = OPEN;
58                     } else {
59                         status = CLOSED;
60                     }
61                 } else if (command instanceof StopMoveType) {
62                     if (command.equals(StopMoveType.STOP)) {
63                         status = STOP;
64                     } else {
65                         status = lastDirection;
66                     }
67                 } else if (command instanceof PercentType) {
68                     getXiaomiBridgeHandler().writeToDevice(getItemId(), new String[] { STATUS }, new Object[] { AUTO });
69                     getXiaomiBridgeHandler().writeToDevice(getItemId(), new String[] { CURTAIN_LEVEL },
70                             new Object[] { status });
71                 } else {
72                     logger.warn("Only UpDown or StopMove commands supported - not the command {}", command);
73                     return;
74                 }
75                 if (OPEN.equals(status) | CLOSED.equals(status)) {
76                     if (!status.equals(lastDirection)) {
77                         lastDirection = status;
78                     }
79                 }
80                 getXiaomiBridgeHandler().writeToDevice(getItemId(), new String[] { STATUS }, new Object[] { status });
81                 break;
82             default:
83                 logger.warn("Can't handle command {} on channel {}", command, channelUID);
84         }
85     }
86
87     @Override
88     void parseHeartbeat(JsonObject data) {
89         parseDefault(data);
90     }
91
92     @Override
93     void parseReadAck(JsonObject data) {
94         parseDefault(data);
95     }
96
97     @Override
98     void parseReport(JsonObject data) {
99         parseDefault(data);
100     }
101
102     @Override
103     void parseWriteAck(JsonObject data) {
104         parseDefault(data);
105     }
106
107     @Override
108     void parseDefault(JsonObject data) {
109         if (data.has(CURTAIN_LEVEL)) {
110             int level = data.get(CURTAIN_LEVEL).getAsInt();
111             if (level >= 0 | level <= 100) {
112                 updateState(CHANNEL_CURTAIN_CONTROL, new PercentType(level));
113             }
114         }
115     }
116 }