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.myq.internal.handler;
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;
36 * The {@link MyQGarageDoorHandler} is responsible for handling commands for a garage door thing, which are
37 * sent to one of the channels.
39 * @author Dan Cunningham - Initial contribution
42 public class MyQGarageDoorHandler extends BaseThingHandler implements MyQDeviceHandler {
43 private @Nullable DeviceDTO device;
44 private String serialNumber;
46 public MyQGarageDoorHandler(Thing thing) {
48 serialNumber = getConfigAs(MyQDeviceConfiguration.class).serialNumber;
52 public void initialize() {
53 updateStatus(ThingStatus.UNKNOWN);
57 public void handleCommand(ChannelUID channelUID, Command command) {
58 if (command instanceof RefreshType) {
62 Bridge bridge = getBridge();
63 final DeviceDTO localDevice = device;
64 if (bridge != null && localDevice != null) {
65 BridgeHandler handler = bridge.getHandler();
66 if (handler != null) {
68 if (command instanceof OnOffType) {
69 cmd = command == OnOffType.ON ? "open" : "close";
71 if (command instanceof UpDownType) {
72 cmd = command == UpDownType.UP ? "open" : "close";
74 if (command instanceof PercentType) {
75 cmd = ((PercentType) command).as(UpDownType.class) == UpDownType.UP ? "open" : "close";
77 if (command instanceof StringType) {
78 cmd = command.toString();
81 ((MyQAccountHandler) handler).sendDoorAction(localDevice, cmd);
88 public String getSerialNumber() {
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));
103 updateState("switch", OnOffType.ON);
104 updateState("rollershutter", UpDownType.UP);
107 updateState("switch", OnOffType.OFF);
108 updateState("rollershutter", UpDownType.DOWN);
111 updateState("switch", UnDefType.UNDEF);
112 updateState("rollershutter", UnDefType.UNDEF);
115 updateState("closeerror", localDevice.state.isUnattendedCloseAllowed ? OnOffType.OFF : OnOffType.ON);
116 updateState("openerror", localDevice.state.isUnattendedOpenAllowed ? OnOffType.OFF : OnOffType.ON);
121 public void handleDeviceUpdate(DeviceDTO device) {
122 if (!MyQBindingConstants.THING_TYPE_GARAGEDOOR.getId().equals(device.deviceFamily)) {
125 this.device = device;
126 if (device.state.online) {
127 updateStatus(ThingStatus.ONLINE);
130 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Device reports as offline");