]> git.basschouten.com Git - openhab-addons.git/blob
d096a1298e54391f84eaa12ddc6ea50f63067e42
[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.tplinksmarthome.internal.device;
14
15 import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.*;
16
17 import java.io.IOException;
18
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;
28
29 /**
30  * TP-Link Smart Home device with a switch, like Smart Plugs and Switches.
31  *
32  * @author Hilbrand Bouwkamp - Initial contribution
33  */
34 @NonNullByDefault
35 public class SwitchDevice extends SmartHomeDevice {
36
37     @Override
38     public String getUpdateCommand() {
39         return Commands.getSysinfo();
40     }
41
42     @Override
43     public boolean handleCommand(ChannelUID channelUid, Command command) throws IOException {
44         return command instanceof OnOffType onOffCommand && handleOnOffType(channelUid, onOffCommand);
45     }
46
47     /**
48      * Returns the switch state.
49      *
50      * @param deviceState data object containing the state
51      * @return the switch state
52      */
53     protected State getOnOffState(DeviceState deviceState) {
54         return deviceState.getSysinfo().getRelayState();
55     }
56
57     private boolean handleOnOffType(ChannelUID channelUid, OnOffType onOff) throws IOException {
58         final HasErrorResponse response;
59         final String baseChannelId = getBaseChannel(channelUid);
60
61         if (CHANNEL_SWITCH.contentEquals(baseChannelId)) {
62             response = setOnOffState(channelUid, onOff);
63         } else if (CHANNEL_LED.contentEquals(baseChannelId)) {
64             response = commands
65                     .setLedOnResponse(connection.sendCommand(commands.setLedOn(onOff, getChildId(channelUid))));
66         } else {
67             response = null;
68         }
69         checkErrors(response);
70         return response != null;
71     }
72
73     /**
74      * Sends the {@link OnOffType} command to the device and returns the returned answer.
75      *
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
80      */
81     protected @Nullable HasErrorResponse setOnOffState(ChannelUID channelUid, OnOffType onOff) throws IOException {
82         return commands
83                 .setRelayStateResponse(connection.sendCommand(commands.setRelayState(onOff, getChildId(channelUid))));
84     }
85
86     @Override
87     public State updateChannel(ChannelUID channelUid, DeviceState deviceState) {
88         final String baseChannelId = getBaseChannel(channelUid);
89
90         if (CHANNEL_SWITCH.equals(baseChannelId)) {
91             return getOnOffState(deviceState);
92         } else if (CHANNEL_LED.equals(baseChannelId)) {
93             return deviceState.getSysinfo().getLedOff();
94         }
95         return UnDefType.UNDEF;
96     }
97
98     /**
99      * Returns the child Id for the given channel if the device supports children and it's a channel for a specific
100      * child.
101      *
102      * @param channelUid channel Id to get the child id for
103      * @return null or child id
104      */
105     protected @Nullable String getChildId(ChannelUID channelUid) {
106         return null;
107     }
108
109     private String getBaseChannel(ChannelUID channelUid) {
110         return channelUid.isInGroup() ? channelUid.getIdWithoutGroup() : channelUid.getId();
111     }
112 }