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.CHANNEL_BRIGHTNESS;
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.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;
29 * TP-Link Smart Home device with a dimmer (HS220).
31 * @author Hilbrand Bouwkamp - Initial contribution
34 public class DimmerDevice extends SwitchDevice {
37 protected @Nullable HasErrorResponse setOnOffState(ChannelUID channelUid, OnOffType onOff) throws IOException {
38 return commands.setSwitchStateResponse(connection.sendCommand(commands.setSwitchState(onOff)));
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);
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.
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
56 private boolean handleBrightnessChannel(ChannelUID channelUid, Command command) throws IOException {
57 HasErrorResponse response = null;
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) {
70 response = setOnOffState(channelUid, OnOffType.ON);
72 response = setOnOffState(channelUid, OnOffType.OFF);
75 checkErrors(response);
76 return response != null;
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());
85 return super.updateChannel(channelUid, deviceState);