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.tplinksmarthome.internal.device;
15 import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.*;
17 import java.io.IOException;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.tplinksmarthome.internal.Commands;
22 import org.openhab.binding.tplinksmarthome.internal.model.HasErrorResponse;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.types.Command;
26 import org.openhab.core.types.State;
27 import org.openhab.core.types.UnDefType;
30 * TP-Link Smart Home device with a switch, like Smart Plugs and Switches.
32 * @author Hilbrand Bouwkamp - Initial contribution
35 public class SwitchDevice extends SmartHomeDevice {
38 public String getUpdateCommand() {
39 return Commands.getSysinfo();
43 public boolean handleCommand(ChannelUID channelUid, Command command) throws IOException {
44 return command instanceof OnOffType && handleOnOffType(channelUid, (OnOffType) command);
48 * Returns the switch state.
50 * @param deviceState data object containing the state
51 * @return the switch state
53 protected State getOnOffState(DeviceState deviceState) {
54 return deviceState.getSysinfo().getRelayState();
57 private boolean handleOnOffType(ChannelUID channelUid, OnOffType onOff) throws IOException {
58 final HasErrorResponse response;
59 final String baseChannelId = getBaseChannel(channelUid);
61 if (CHANNEL_SWITCH.contentEquals(baseChannelId)) {
62 response = setOnOffState(channelUid, onOff);
63 } else if (CHANNEL_LED.contentEquals(baseChannelId)) {
65 .setLedOnResponse(connection.sendCommand(commands.setLedOn(onOff, getChildId(channelUid))));
69 checkErrors(response);
70 return response != null;
74 * Sends the {@link OnOffType} command to the device and returns the returned answer.
76 * @param channelUid channel Id to use to determine child id
77 * @param onOff command to the send
78 * @return state returned by the device
79 * @throws IOException exception in case device not reachable
81 protected @Nullable HasErrorResponse setOnOffState(ChannelUID channelUid, OnOffType onOff) throws IOException {
83 .setRelayStateResponse(connection.sendCommand(commands.setRelayState(onOff, getChildId(channelUid))));
87 public State updateChannel(ChannelUID channelUid, DeviceState deviceState) {
88 final String baseChannelId = getBaseChannel(channelUid);
90 if (CHANNEL_SWITCH.equals(baseChannelId)) {
91 return getOnOffState(deviceState);
92 } else if (CHANNEL_LED.equals(baseChannelId)) {
93 return deviceState.getSysinfo().getLedOff();
95 return UnDefType.UNDEF;
99 * Returns the child Id for the given channel if the device supports children and it's a channel for a specific
102 * @param channelUid channel Id to get the child id for
103 * @return null or child id
105 protected @Nullable String getChildId(ChannelUID channelUid) {
109 private String getBaseChannel(ChannelUID channelUid) {
110 return channelUid.isInGroup() ? channelUid.getIdWithoutGroup() : channelUid.getId();