]> git.basschouten.com Git - openhab-addons.git/blob
e91395db5e40eda1c90f1f8ae7c3b4982947e59d
[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) {
60             response = setOnOffState(channelUid, (OnOffType) command);
61         } else if (command instanceof PercentType) {
62             PercentType percentCommand = (PercentType) command;
63
64             // Don't send value 0 as brightness value as it will give an error from the device.
65             if (percentCommand.intValue() > 0) {
66                 response = commands.setDimmerBrightnessResponse(
67                         connection.sendCommand(commands.setDimmerBrightness(percentCommand.intValue())));
68                 checkErrors(response);
69                 if (response == null) {
70                     return false;
71                 }
72                 response = setOnOffState(channelUid, OnOffType.ON);
73             } else {
74                 response = setOnOffState(channelUid, OnOffType.OFF);
75             }
76         }
77         checkErrors(response);
78         return response != null;
79     }
80
81     @Override
82     public State updateChannel(ChannelUID channelUid, DeviceState deviceState) {
83         if (CHANNEL_BRIGHTNESS.equals(channelUid.getId())) {
84             return deviceState.getSysinfo().getRelayState() == OnOffType.OFF ? PercentType.ZERO
85                     : new PercentType(deviceState.getSysinfo().getBrightness());
86         } else {
87             return super.updateChannel(channelUid, deviceState);
88         }
89     }
90 }