]> git.basschouten.com Git - openhab-addons.git/blob
7f7591600a11c10890dca0336b965d6322ef7079
[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.myq.internal.handler;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.myq.internal.MyQBindingConstants;
18 import org.openhab.binding.myq.internal.config.MyQDeviceConfiguration;
19 import org.openhab.binding.myq.internal.dto.DeviceDTO;
20 import org.openhab.core.library.types.OnOffType;
21 import org.openhab.core.library.types.PercentType;
22 import org.openhab.core.library.types.StringType;
23 import org.openhab.core.library.types.UpDownType;
24 import org.openhab.core.thing.Bridge;
25 import org.openhab.core.thing.ChannelUID;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingStatus;
28 import org.openhab.core.thing.ThingStatusDetail;
29 import org.openhab.core.thing.binding.BaseThingHandler;
30 import org.openhab.core.thing.binding.BridgeHandler;
31 import org.openhab.core.types.Command;
32 import org.openhab.core.types.RefreshType;
33 import org.openhab.core.types.UnDefType;
34
35 /**
36  * The {@link MyQGarageDoorHandler} is responsible for handling commands for a garage door thing, which are
37  * sent to one of the channels.
38  *
39  * @author Dan Cunningham - Initial contribution
40  */
41 @NonNullByDefault
42 public class MyQGarageDoorHandler extends BaseThingHandler implements MyQDeviceHandler {
43     private @Nullable DeviceDTO device;
44     private String serialNumber;
45
46     public MyQGarageDoorHandler(Thing thing) {
47         super(thing);
48         serialNumber = getConfigAs(MyQDeviceConfiguration.class).serialNumber;
49     }
50
51     @Override
52     public void initialize() {
53         updateStatus(ThingStatus.UNKNOWN);
54     }
55
56     @Override
57     public void handleCommand(ChannelUID channelUID, Command command) {
58         if (command instanceof RefreshType) {
59             updateState();
60             return;
61         }
62         Bridge bridge = getBridge();
63         final DeviceDTO localDevice = device;
64         if (bridge != null && localDevice != null) {
65             BridgeHandler handler = bridge.getHandler();
66             if (handler != null) {
67                 String cmd = null;
68                 if (command instanceof OnOffType) {
69                     cmd = command == OnOffType.ON ? "open" : "close";
70                 }
71                 if (command instanceof UpDownType) {
72                     cmd = command == UpDownType.UP ? "open" : "close";
73                 }
74                 if (command instanceof PercentType percentage) {
75                     cmd = percentage.as(UpDownType.class) == UpDownType.UP ? "open" : "close";
76                 }
77                 if (command instanceof StringType) {
78                     cmd = command.toString();
79                 }
80                 if (cmd != null) {
81                     ((MyQAccountHandler) handler).sendDoorAction(localDevice, cmd);
82                 }
83             }
84         }
85     }
86
87     @Override
88     public String getSerialNumber() {
89         return serialNumber;
90     }
91
92     protected void updateState() {
93         final DeviceDTO localDevice = device;
94         if (localDevice != null) {
95             String doorState = localDevice.state.doorState;
96             updateState("status", new StringType(doorState));
97             switch (doorState) {
98                 case "open":
99                 case "opening":
100                 case "closing":
101                 case "stopped":
102                 case "transition":
103                     updateState("switch", OnOffType.ON);
104                     updateState("rollershutter", UpDownType.UP);
105                     break;
106                 case "closed":
107                     updateState("switch", OnOffType.OFF);
108                     updateState("rollershutter", UpDownType.DOWN);
109                     break;
110                 default:
111                     updateState("switch", UnDefType.UNDEF);
112                     updateState("rollershutter", UnDefType.UNDEF);
113                     break;
114             }
115             updateState("closeerror", localDevice.state.isUnattendedCloseAllowed ? OnOffType.OFF : OnOffType.ON);
116             updateState("openerror", localDevice.state.isUnattendedOpenAllowed ? OnOffType.OFF : OnOffType.ON);
117         }
118     }
119
120     @Override
121     public void handleDeviceUpdate(DeviceDTO device) {
122         if (!MyQBindingConstants.THING_TYPE_GARAGEDOOR.getId().equals(device.deviceFamily)) {
123             return;
124         }
125         this.device = device;
126         if (device.state.online) {
127             updateStatus(ThingStatus.ONLINE);
128             updateState();
129         } else {
130             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Device reports as offline");
131         }
132     }
133 }