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(DEVICE_PROPERTY_ON, command == OnOffType.ON);
78 case CHANNEL_BRIGHTNESS:
79 if (command instanceof PercentType) {
80 Float percent = ((PercentType) command).floatValue();
81 setBrightness(percent.intValue()); // 0..100% = 0..100
83 } else if (command instanceof DecimalType) {
84 setBrightness(((DecimalType) command).intValue());
88 case CHANNEL_COLOR_TEMP:
89 if (command instanceof DecimalType) {
90 setColorTemp(((DecimalType) command).intValue());
95 if (command instanceof HSBType) {
96 setColor((HSBType) command);
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(DEVICE_PROPERTY_ON, false);
122 HashMap<String, Object> newState = new HashMap<>();
123 newState.put(DEVICE_PROPERTY_ON, true);
124 newState.put(DEVICE_PROPERTY_BRIGHTNES, newBrightness);
125 connector.sendDeviceCommands(newState);
134 protected void setColor(HSBType command) {
135 HashMap<String, Object> newState = new HashMap<>();
136 newState.put(DEVICE_PROPERTY_ON, true);
137 newState.put(DEVICE_PROPERTY_HUE, command.getHue());
138 newState.put(DEVICE_PROPERTY_SATURATION, command.getSaturation());
139 newState.put(DEVICE_PROPERTY_BRIGHTNES, command.getBrightness());
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(DEVICE_PROPERTY_ON, true);
152 newState.put(DEVICE_PROPERTY_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) {
167 Float percent = ((PercentType) command).floatValue();
168 lightEffect.setBrightness(percent.intValue()); // 0..100% = 0..100
169 } else if (command instanceof DecimalType) {
170 lightEffect.setBrightness(((DecimalType) command).intValue());
173 case CHANNEL_FX_COLORS:
176 case CHANNEL_FX_NAME:
177 lightEffect.setName(command.toString());
179 case CHANNEL_FX_ENABLE:
180 lightEffect.setEnable(command == OnOffType.ON);
183 setLightEffects(lightEffect);
187 * SET LIGHTNING EFFECTS
189 * @param lightEffect new lightEffect
191 protected void setLightEffects(TapoLightEffect lightEffect) {
192 JsonObject newEffect = new JsonObject();
193 newEffect.addProperty(PROPERTY_LIGHTNING_EFFECT_ENABLE, lightEffect.getEnable());
194 newEffect.addProperty(PROPERTY_LIGHTNING_EFFECT_NAME, lightEffect.getName());
195 newEffect.addProperty(PROPERTY_LIGHTNING_EFFECT_BRIGHNTESS, lightEffect.getBrightness());
196 newEffect.addProperty(PROPERTY_LIGHTNING_EFFECT_COLORTEMPRANGE, lightEffect.getColorTempRange().toString());
197 newEffect.addProperty(PROPERTY_LIGHTNING_EFFECT_DISPLAYCOLORS, lightEffect.getDisplayColors().toString());
198 newEffect.addProperty(PROPERTY_LIGHTNING_EFFECT_CUSTOM, lightEffect.getCustom());
200 connector.sendDeviceCommand(DEVICE_PROPERTY_EFFECT, newEffect.toString());
206 * @param TapoDeviceInfo
209 protected void devicePropertiesChanged(TapoDeviceInfo deviceInfo) {
210 TapoLightEffect lightEffect = deviceInfo.getLightEffect();
211 super.devicePropertiesChanged(deviceInfo);
212 publishState(getChannelID(CHANNEL_GROUP_ACTUATOR, CHANNEL_OUTPUT), getOnOffType(deviceInfo.isOn()));
213 publishState(getChannelID(CHANNEL_GROUP_ACTUATOR, CHANNEL_BRIGHTNESS),
214 getPercentType(deviceInfo.getBrightness()));
215 publishState(getChannelID(CHANNEL_GROUP_ACTUATOR, CHANNEL_COLOR_TEMP),
216 getDecimalType(deviceInfo.getColorTemp()));
217 publishState(getChannelID(CHANNEL_GROUP_ACTUATOR, CHANNEL_COLOR), deviceInfo.getHSB());
218 publishState(getChannelID(CHANNEL_GROUP_DEVICE, CHANNEL_WIFI_STRENGTH),
219 getDecimalType(deviceInfo.getSignalLevel()));
220 publishState(getChannelID(CHANNEL_GROUP_DEVICE, CHANNEL_ONTIME),
221 getTimeType(deviceInfo.getOnTime(), Units.SECOND));
222 publishState(getChannelID(CHANNEL_GROUP_DEVICE, CHANNEL_OVERHEAT), getOnOffType(deviceInfo.isOverheated()));
224 publishState(getChannelID(CHANNEL_GROUP_EFFECTS, CHANNEL_FX_BRIGHTNESS),
225 getPercentType(lightEffect.getBrightness()));
226 publishState(getChannelID(CHANNEL_GROUP_EFFECTS, CHANNEL_FX_NAME), getStringType(lightEffect.getName()));
227 publishState(getChannelID(CHANNEL_GROUP_EFFECTS, CHANNEL_FX_ENABLE), getOnOffType(lightEffect.getEnable()));
228 publishState(getChannelID(CHANNEL_GROUP_EFFECTS, CHANNEL_FX_CUSTOM), getOnOffType(lightEffect.getCustom()));