]> git.basschouten.com Git - openhab-addons.git/blob
9c17ff20229e8fd39ef316c143e0cee650702fbd
[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.CHANNEL_BRIGHTNESS;
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.model.HasErrorResponse;
22 import org.openhab.core.library.types.OnOffType;
23 import org.openhab.core.library.types.PercentType;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.types.Command;
26 import org.openhab.core.types.State;
27
28 /**
29  * TP-Link Smart Home device with a dimmer (HS220).
30  *
31  * @author Hilbrand Bouwkamp - Initial contribution
32  */
33 @NonNullByDefault
34 public class DimmerDevice extends SwitchDevice {
35
36     @Override
37     protected @Nullable HasErrorResponse setOnOffState(ChannelUID channelUid, OnOffType onOff) throws IOException {
38         return commands.setSwitchStateResponse(connection.sendCommand(commands.setSwitchState(onOff)));
39     }
40
41     @Override
42     public boolean handleCommand(ChannelUID channelUid, Command command) throws IOException {
43         return CHANNEL_BRIGHTNESS.equals(channelUid.getId()) ? handleBrightnessChannel(channelUid, command)
44                 : super.handleCommand(channelUid, command);
45     }
46
47     /**
48      * Handle the brightness channel. Because the device has different commands for setting the device on/off and
49      * setting the brightness the on/off command must be send to the device as well when the brightness.
50      *
51      * @param channelUid uid of the channel to handle
52      * @param command command to the send
53      * @return returns true if the command was handled
54      * @throws IOException throws an {@link IOException} if the command handling failed
55      */
56     private boolean handleBrightnessChannel(ChannelUID channelUid, Command command) throws IOException {
57         HasErrorResponse response = null;
58
59         if (command instanceof OnOffType onOffCommand) {
60             response = setOnOffState(channelUid, onOffCommand);
61         } else if (command instanceof PercentType percentCommand) {
62             // Don't send value 0 as brightness value as it will give an error from the device.
63             if (percentCommand.intValue() > 0) {
64                 response = commands.setDimmerBrightnessResponse(
65                         connection.sendCommand(commands.setDimmerBrightness(percentCommand.intValue())));
66                 checkErrors(response);
67                 if (response == null) {
68                     return false;
69                 }
70                 response = setOnOffState(channelUid, OnOffType.ON);
71             } else {
72                 response = setOnOffState(channelUid, OnOffType.OFF);
73             }
74         }
75         checkErrors(response);
76         return response != null;
77     }
78
79     @Override
80     public State updateChannel(ChannelUID channelUid, DeviceState deviceState) {
81         if (CHANNEL_BRIGHTNESS.equals(channelUid.getId())) {
82             return deviceState.getSysinfo().getRelayState() == OnOffType.OFF ? PercentType.ZERO
83                     : new PercentType(deviceState.getSysinfo().getBrightness());
84         } else {
85             return super.updateChannel(channelUid, deviceState);
86         }
87     }
88 }