2 * Copyright (c) 2010-2020 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.*;
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.Commands;
22 import org.openhab.binding.tplinksmarthome.internal.model.HasErrorResponse;
23 import org.openhab.binding.tplinksmarthome.internal.model.LightState;
24 import org.openhab.binding.tplinksmarthome.internal.model.TransitionLightStateResponse;
25 import org.openhab.core.library.types.DecimalType;
26 import org.openhab.core.library.types.HSBType;
27 import org.openhab.core.library.types.OnOffType;
28 import org.openhab.core.library.types.PercentType;
29 import org.openhab.core.thing.ChannelUID;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.types.Command;
32 import org.openhab.core.types.State;
33 import org.openhab.core.types.UnDefType;
36 * TP-Link Smart Home Light Bulb.
38 * @author Hilbrand Bouwkamp - Initial contribution
41 public class BulbDevice extends SmartHomeDevice {
43 protected Commands commands = new Commands();
45 private final int colorTempMin;
46 private final int colorTempMax;
47 private final int colorTempRangeFactor;
49 public BulbDevice(ThingTypeUID thingTypeUID) {
50 this(thingTypeUID, 0, 0);
53 public BulbDevice(ThingTypeUID thingTypeUID, int colorTempMin, int colorTempMax) {
54 this.colorTempMin = colorTempMin;
55 this.colorTempMax = colorTempMax;
56 colorTempRangeFactor = (colorTempMax - colorTempMin) / 100;
60 public String getUpdateCommand() {
61 return Commands.getRealtimeBulbAndSysinfo();
65 public boolean handleCommand(ChannelUID channelUid, Command command) throws IOException {
66 final String channelId = channelUid.getId();
67 final int transitionPeriod = configuration.transitionPeriod;
68 final HasErrorResponse response;
70 if (command instanceof OnOffType) {
71 response = handleOnOffType(channelId, (OnOffType) command, transitionPeriod);
72 } else if (command instanceof HSBType) {
73 response = handleHSBType(channelId, (HSBType) command, transitionPeriod);
74 } else if (command instanceof DecimalType) {
75 response = handleDecimalType(channelId, (DecimalType) command, transitionPeriod);
79 checkErrors(response);
80 return response != null;
83 private @Nullable HasErrorResponse handleOnOffType(String channelID, OnOffType onOff, int transitionPeriod)
85 if (CHANNELS_BULB_SWITCH.contains(channelID)) {
86 return commands.setTransitionLightStateResponse(
87 connection.sendCommand(commands.setLightState(onOff, transitionPeriod)));
92 private @Nullable HasErrorResponse handleDecimalType(String channelID, DecimalType command, int transitionPeriod)
94 if (CHANNEL_COLOR.equals(channelID) || CHANNEL_BRIGHTNESS.equals(channelID)) {
95 return commands.setTransitionLightStateResponse(
96 connection.sendCommand(commands.setBrightness(command.intValue(), transitionPeriod)));
97 } else if (CHANNEL_COLOR_TEMPERATURE.equals(channelID)) {
98 return handleColorTemperature(convertPercentageToKelvin(command.intValue()), transitionPeriod);
99 } else if (CHANNEL_COLOR_TEMPERATURE_ABS.equals(channelID)) {
100 return handleColorTemperature(guardColorTemperature(command.intValue()), transitionPeriod);
105 private @Nullable TransitionLightStateResponse handleColorTemperature(int colorTemperature, int transitionPeriod)
107 return commands.setTransitionLightStateResponse(
108 connection.sendCommand(commands.setColorTemperature(colorTemperature, transitionPeriod)));
112 private HasErrorResponse handleHSBType(String channelID, HSBType command, int transitionPeriod) throws IOException {
113 if (CHANNEL_COLOR.equals(channelID)) {
114 return commands.setTransitionLightStateResponse(
115 connection.sendCommand(commands.setColor(command, transitionPeriod)));
121 public State updateChannel(ChannelUID channelUid, DeviceState deviceState) {
122 final LightState lightState = deviceState.getSysinfo().getLightState();
125 switch (channelUid.getId()) {
126 case CHANNEL_BRIGHTNESS:
127 state = lightState.getBrightness();
130 state = new HSBType(lightState.getHue(), lightState.getSaturation(), lightState.getBrightness());
132 case CHANNEL_COLOR_TEMPERATURE:
133 state = new PercentType(convertKelvinToPercentage(lightState.getColorTemp()));
135 case CHANNEL_COLOR_TEMPERATURE_ABS:
136 state = new DecimalType(guardColorTemperature(lightState.getColorTemp()));
139 state = lightState.getOnOff();
141 case CHANNEL_ENERGY_POWER:
142 state = new DecimalType(deviceState.getRealtime().getPower());
145 state = UnDefType.UNDEF;
151 private int convertPercentageToKelvin(int percentage) {
152 return guardColorTemperature(colorTempMin + colorTempRangeFactor * percentage);
155 private int convertKelvinToPercentage(int colorTemperature) {
156 return (guardColorTemperature(colorTemperature) - colorTempMin) / colorTempRangeFactor;
159 private int guardColorTemperature(int colorTemperature) {
160 return Math.max(colorTempMin, Math.min(colorTempMax, colorTemperature));