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.CHANNEL_CURTAIN_CONTROL;
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;
26 import com.google.gson.JsonObject;
29 * Manage the Xiaomi Smart Curtain over the API
31 * @author Kuba Wolanin - initial contribution
33 public class XiaomiActorCurtainHandler extends XiaomiActorBaseHandler {
35 private final Logger logger = LoggerFactory.getLogger(XiaomiActorCurtainHandler.class);
36 private String lastDirection;
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";
45 public XiaomiActorCurtainHandler(Thing thing) {
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)) {
61 } else if (command instanceof StopMoveType) {
62 if (command.equals(StopMoveType.STOP)) {
65 status = lastDirection;
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 });
72 logger.warn("Only UpDown or StopMove commands supported - not the command {}", command);
75 if (OPEN.equals(status) | CLOSED.equals(status)) {
76 if (!status.equals(lastDirection)) {
77 lastDirection = status;
80 getXiaomiBridgeHandler().writeToDevice(getItemId(), new String[] { STATUS }, new Object[] { status });
83 logger.warn("Can't handle command {} on channel {}", command, channelUID);
88 void parseHeartbeat(JsonObject data) {
93 void parseReadAck(JsonObject data) {
98 void parseReport(JsonObject data) {
103 void parseWriteAck(JsonObject data) {
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));