2 * Copyright (c) 2010-2022 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.CHANNELS_BULB_SWITCH;
16 import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.CHANNEL_BRIGHTNESS;
17 import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.CHANNEL_COLOR;
18 import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.CHANNEL_COLOR_TEMPERATURE;
19 import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.CHANNEL_COLOR_TEMPERATURE_ABS;
20 import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.CHANNEL_ENERGY_POWER;
21 import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.CHANNEL_SWITCH;
23 import java.io.IOException;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.binding.tplinksmarthome.internal.Commands;
28 import org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeThingType;
29 import org.openhab.binding.tplinksmarthome.internal.model.HasErrorResponse;
30 import org.openhab.binding.tplinksmarthome.internal.model.LightState;
31 import org.openhab.binding.tplinksmarthome.internal.model.TransitionLightStateResponse;
32 import org.openhab.core.library.types.DecimalType;
33 import org.openhab.core.library.types.HSBType;
34 import org.openhab.core.library.types.OnOffType;
35 import org.openhab.core.library.types.PercentType;
36 import org.openhab.core.thing.ChannelUID;
37 import org.openhab.core.types.Command;
38 import org.openhab.core.types.State;
39 import org.openhab.core.types.UnDefType;
42 * TP-Link Smart Home Light Bulb.
44 * @author Hilbrand Bouwkamp - Initial contribution
47 public class BulbDevice extends SmartHomeDevice {
49 protected Commands commands = new Commands();
51 private final int colorTempMin;
52 private final int colorTempMax;
53 private final int colorTempRangeFactor;
55 public BulbDevice(final TPLinkSmartHomeThingType type) {
56 this.colorTempMin = type.getColorScales().getWarm();
57 this.colorTempMax = type.getColorScales().getCool();
58 colorTempRangeFactor = (colorTempMax - colorTempMin) / 100;
62 public String getUpdateCommand() {
63 return Commands.getRealtimeBulbAndSysinfo();
67 public boolean handleCommand(final ChannelUID channelUid, final Command command) throws IOException {
68 final String channelId = channelUid.getId();
69 final int transitionPeriod = configuration.transitionPeriod;
70 final HasErrorResponse response;
72 if (command instanceof OnOffType) {
73 response = handleOnOffType(channelId, (OnOffType) command, transitionPeriod);
74 } else if (command instanceof HSBType) {
75 response = handleHSBType(channelId, (HSBType) command, transitionPeriod);
76 } else if (command instanceof DecimalType) {
77 response = handleDecimalType(channelId, (DecimalType) command, transitionPeriod);
81 checkErrors(response);
82 return response != null;
85 private @Nullable HasErrorResponse handleOnOffType(final String channelID, final OnOffType onOff,
86 final int transitionPeriod) throws IOException {
87 if (CHANNELS_BULB_SWITCH.contains(channelID)) {
88 return commands.setTransitionLightStateResponse(
89 connection.sendCommand(commands.setLightState(onOff, transitionPeriod)));
94 private @Nullable HasErrorResponse handleDecimalType(final String channelID, final DecimalType command,
95 final int transitionPeriod) throws IOException {
96 if (CHANNEL_COLOR.equals(channelID) || CHANNEL_BRIGHTNESS.equals(channelID)) {
97 return commands.setTransitionLightStateResponse(
98 connection.sendCommand(commands.setBrightness(command.intValue(), transitionPeriod)));
99 } else if (CHANNEL_COLOR_TEMPERATURE.equals(channelID)) {
100 return handleColorTemperature(convertPercentageToKelvin(command.intValue()), transitionPeriod);
101 } else if (CHANNEL_COLOR_TEMPERATURE_ABS.equals(channelID)) {
102 return handleColorTemperature(guardColorTemperature(command.intValue()), transitionPeriod);
107 private @Nullable TransitionLightStateResponse handleColorTemperature(final int colorTemperature,
108 final int transitionPeriod) throws IOException {
109 return commands.setTransitionLightStateResponse(
110 connection.sendCommand(commands.setColorTemperature(colorTemperature, transitionPeriod)));
114 private HasErrorResponse handleHSBType(final String channelID, final HSBType command, final int transitionPeriod)
116 if (CHANNEL_COLOR.equals(channelID)) {
117 return commands.setTransitionLightStateResponse(
118 connection.sendCommand(commands.setColor(command, transitionPeriod)));
124 public State updateChannel(final ChannelUID channelUid, final DeviceState deviceState) {
125 final LightState lightState = deviceState.getSysinfo().getLightState();
128 switch (channelUid.getId()) {
129 case CHANNEL_BRIGHTNESS:
130 state = lightState.getBrightness();
133 state = new HSBType(lightState.getHue(), lightState.getSaturation(), lightState.getBrightness());
135 case CHANNEL_COLOR_TEMPERATURE:
136 state = new PercentType(convertKelvinToPercentage(lightState.getColorTemp()));
138 case CHANNEL_COLOR_TEMPERATURE_ABS:
139 state = new DecimalType(guardColorTemperature(lightState.getColorTemp()));
142 state = lightState.getOnOff();
144 case CHANNEL_ENERGY_POWER:
145 state = new DecimalType(deviceState.getRealtime().getPower());
148 state = UnDefType.UNDEF;
154 private int convertPercentageToKelvin(final int percentage) {
155 return guardColorTemperature(colorTempMin + colorTempRangeFactor * percentage);
158 private int convertKelvinToPercentage(final int colorTemperature) {
159 return (guardColorTemperature(colorTemperature) - colorTempMin) / colorTempRangeFactor;
162 private int guardColorTemperature(final int colorTemperature) {
163 return Math.max(colorTempMin, Math.min(colorTempMax, colorTemperature));