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.tapocontrol.internal.device;
15 import static org.openhab.binding.tapocontrol.internal.constants.TapoThingConstants.*;
16 import static org.openhab.binding.tapocontrol.internal.helpers.TapoUtils.*;
18 import java.util.HashMap;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.tapocontrol.internal.structures.TapoDeviceInfo;
22 import org.openhab.binding.tapocontrol.internal.structures.TapoLightEffect;
23 import org.openhab.core.library.types.DecimalType;
24 import org.openhab.core.library.types.HSBType;
25 import org.openhab.core.library.types.OnOffType;
26 import org.openhab.core.library.types.PercentType;
27 import org.openhab.core.library.unit.Units;
28 import org.openhab.core.thing.ChannelUID;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.types.Command;
31 import org.openhab.core.types.RefreshType;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import com.google.gson.JsonObject;
38 * TAPO Smart-Plug-Device.
40 * @author Christian Wild - Initial contribution
43 public class TapoLightStrip extends TapoDevice {
44 private final Logger logger = LoggerFactory.getLogger(TapoLightStrip.class);
49 * @param thing Thing object representing device
51 public TapoLightStrip(Thing thing) {
56 * handle command sent to device
58 * @param channelUID channelUID command is sent to
59 * @param command command to be sent
62 public void handleCommand(ChannelUID channelUID, Command command) {
63 Boolean refreshInfo = false;
65 String channel = channelUID.getIdWithoutGroup();
66 String group = channelUID.getGroupId();
67 if (command instanceof RefreshType) {
69 } else if (group == CHANNEL_GROUP_EFFECTS) {
70 setLightEffect(channel, command);
75 connector.sendDeviceCommand(JSON_KEY_ON, command == OnOffType.ON);
78 case CHANNEL_BRIGHTNESS:
79 if (command instanceof PercentType percentCommand) {
80 Float percent = percentCommand.floatValue();
81 setBrightness(percent.intValue()); // 0..100% = 0..100
83 } else if (command instanceof DecimalType decimalCommand) {
84 setBrightness(decimalCommand.intValue());
88 case CHANNEL_COLOR_TEMP:
89 if (command instanceof DecimalType decimalCommand) {
90 setColorTemp(decimalCommand.intValue());
95 if (command instanceof HSBType hsbCommand) {
101 logger.warn("({}) command type '{}' not supported for channel '{}'", uid, command.toString(),
108 queryDeviceInfo(true);
115 * @param newBrightness percentage 0-100 of new brightness
117 protected void setBrightness(Integer newBrightness) {
118 /* switch off if 0 */
119 if (newBrightness == 0) {
120 connector.sendDeviceCommand(JSON_KEY_ON, false);
122 HashMap<String, Object> newState = new HashMap<>();
123 newState.put(JSON_KEY_ON, true);
124 newState.put(JSON_KEY_BRIGHTNESS, newBrightness);
125 connector.sendDeviceCommands(newState);
134 protected void setColor(HSBType command) {
135 HashMap<String, Object> newState = new HashMap<>();
136 newState.put(JSON_KEY_ON, true);
137 newState.put(JSON_KEY_HUE, command.getHue().intValue());
138 newState.put(JSON_KEY_SATURATION, command.getSaturation().intValue());
139 newState.put(JSON_KEY_BRIGHTNESS, command.getBrightness().intValue());
140 connector.sendDeviceCommands(newState);
146 * @param colorTemp (Integer) in Kelvin
148 protected void setColorTemp(Integer colorTemp) {
149 HashMap<String, Object> newState = new HashMap<>();
150 colorTemp = limitVal(colorTemp, BULB_MIN_COLORTEMP, BULB_MAX_COLORTEMP);
151 newState.put(JSON_KEY_ON, true);
152 newState.put(JSON_KEY_COLORTEMP, colorTemp);
153 connector.sendDeviceCommands(newState);
157 * set Light Effect from channel/command
159 * @param channel channel (effect) to set
160 * @param command command (value) to set
162 protected void setLightEffect(String channel, Command command) {
163 TapoLightEffect lightEffect = deviceInfo.getLightEffect();
165 case CHANNEL_FX_BRIGHTNESS:
166 if (command instanceof PercentType percentCommand) {
167 Float percent = percentCommand.floatValue();
168 lightEffect.setBrightness(percent.intValue()); // 0..100% = 0..100
169 } else if (command instanceof DecimalType decimalCommand) {
170 lightEffect.setBrightness(decimalCommand.intValue());
173 case CHANNEL_FX_COLORS:
176 case CHANNEL_FX_NAME:
177 lightEffect.setName(command.toString());
180 setLightEffects(lightEffect);
184 * SET LIGHTNING EFFECTS
186 * @param lightEffect new lightEffect
188 protected void setLightEffects(TapoLightEffect lightEffect) {
189 JsonObject newEffect = new JsonObject();
190 newEffect.addProperty(JSON_KEY_LIGHTNING_EFFECT_ENABLE, lightEffect.getEnable());
191 newEffect.addProperty(JSON_KEY_LIGHTNING_EFFECT_NAME, lightEffect.getName());
192 newEffect.addProperty(JSON_KEY_LIGHTNING_EFFECT_BRIGHNTESS, lightEffect.getBrightness());
193 newEffect.addProperty(JSON_KEY_LIGHTNING_EFFECT_COLORTEMPRANGE, lightEffect.getColorTempRange().toString());
194 newEffect.addProperty(JSON_KEY_LIGHTNING_EFFECT_DISPLAYCOLORS, lightEffect.getDisplayColors().toString());
195 newEffect.addProperty(JSON_KEY_LIGHTNING_EFFECT_CUSTOM, lightEffect.getCustom());
197 connector.sendDeviceCommand(JSON_KEY_LIGHTNING_EFFECT, newEffect.toString());
203 * @param deviceInfo TapoDeviceInfo
206 protected void devicePropertiesChanged(TapoDeviceInfo deviceInfo) {
207 TapoLightEffect lightEffect = deviceInfo.getLightEffect();
208 super.devicePropertiesChanged(deviceInfo);
209 publishState(getChannelID(CHANNEL_GROUP_ACTUATOR, CHANNEL_OUTPUT), getOnOffType(deviceInfo.isOn()));
210 publishState(getChannelID(CHANNEL_GROUP_ACTUATOR, CHANNEL_BRIGHTNESS),
211 getPercentType(deviceInfo.getBrightness()));
212 publishState(getChannelID(CHANNEL_GROUP_ACTUATOR, CHANNEL_COLOR_TEMP),
213 getDecimalType(deviceInfo.getColorTemp()));
214 publishState(getChannelID(CHANNEL_GROUP_ACTUATOR, CHANNEL_COLOR), deviceInfo.getHSB());
215 publishState(getChannelID(CHANNEL_GROUP_DEVICE, CHANNEL_WIFI_STRENGTH),
216 getDecimalType(deviceInfo.getSignalLevel()));
217 publishState(getChannelID(CHANNEL_GROUP_DEVICE, CHANNEL_ONTIME),
218 getTimeType(deviceInfo.getOnTime(), Units.SECOND));
219 publishState(getChannelID(CHANNEL_GROUP_DEVICE, CHANNEL_OVERHEAT), getOnOffType(deviceInfo.isOverheated()));
221 publishState(getChannelID(CHANNEL_GROUP_EFFECTS, CHANNEL_FX_BRIGHTNESS),
222 getPercentType(lightEffect.getBrightness()));
223 publishState(getChannelID(CHANNEL_GROUP_EFFECTS, CHANNEL_FX_NAME), getStringType(lightEffect.getName()));