]> git.basschouten.com Git - openhab-addons.git/blob
29f1a9c59e0bc309b59b6d1f607082070912b7e4
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.tapocontrol.internal.device;
14
15 import static org.openhab.binding.tapocontrol.internal.constants.TapoThingConstants.*;
16 import static org.openhab.binding.tapocontrol.internal.helpers.TapoUtils.*;
17
18 import java.util.HashMap;
19
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;
34
35 import com.google.gson.JsonObject;
36
37 /**
38  * TAPO Smart-Plug-Device.
39  *
40  * @author Christian Wild - Initial contribution
41  */
42 @NonNullByDefault
43 public class TapoLightStrip extends TapoDevice {
44     private final Logger logger = LoggerFactory.getLogger(TapoLightStrip.class);
45
46     /**
47      * Constructor
48      * 
49      * @param thing Thing object representing device
50      */
51     public TapoLightStrip(Thing thing) {
52         super(thing);
53     }
54
55     /**
56      * handle command sent to device
57      * 
58      * @param channelUID channelUID command is sent to
59      * @param command command to be sent
60      */
61     @Override
62     public void handleCommand(ChannelUID channelUID, Command command) {
63         Boolean refreshInfo = false;
64
65         String channel = channelUID.getIdWithoutGroup();
66         String group = channelUID.getGroupId();
67         if (command instanceof RefreshType) {
68             refreshInfo = true;
69         } else if (group == CHANNEL_GROUP_EFFECTS) {
70             setLightEffect(channel, command);
71             refreshInfo = true;
72         } else {
73             switch (channel) {
74                 case CHANNEL_OUTPUT:
75                     connector.sendDeviceCommand(JSON_KEY_ON, command == OnOffType.ON);
76                     refreshInfo = true;
77                     break;
78                 case CHANNEL_BRIGHTNESS:
79                     if (command instanceof PercentType) {
80                         Float percent = ((PercentType) command).floatValue();
81                         setBrightness(percent.intValue()); // 0..100% = 0..100
82                         refreshInfo = true;
83                     } else if (command instanceof DecimalType) {
84                         setBrightness(((DecimalType) command).intValue());
85                         refreshInfo = true;
86                     }
87                     break;
88                 case CHANNEL_COLOR_TEMP:
89                     if (command instanceof DecimalType) {
90                         setColorTemp(((DecimalType) command).intValue());
91                         refreshInfo = true;
92                     }
93                     break;
94                 case CHANNEL_COLOR:
95                     if (command instanceof HSBType) {
96                         setColor((HSBType) command);
97                         refreshInfo = true;
98                     }
99                     break;
100                 default:
101                     logger.warn("({}) command type '{}' not supported for channel '{}'", uid, command.toString(),
102                             channelUID.getId());
103             }
104         }
105
106         /* refreshInfo */
107         if (refreshInfo) {
108             queryDeviceInfo(true);
109         }
110     }
111
112     /**
113      * SET BRIGHTNESS
114      * 
115      * @param newBrightness percentage 0-100 of new brightness
116      */
117     protected void setBrightness(Integer newBrightness) {
118         /* switch off if 0 */
119         if (newBrightness == 0) {
120             connector.sendDeviceCommand(JSON_KEY_ON, false);
121         } else {
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);
126         }
127     }
128
129     /**
130      * SET COLOR
131      * 
132      * @param command
133      */
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);
141     }
142
143     /**
144      * SET COLORTEMP
145      * 
146      * @param colorTemp (Integer) in Kelvin
147      */
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);
154     }
155
156     /**
157      * set Light Effect from channel/command
158      * 
159      * @param channel channel (effect) to set
160      * @param command command (value) to set
161      */
162     protected void setLightEffect(String channel, Command command) {
163         TapoLightEffect lightEffect = deviceInfo.getLightEffect();
164         switch (channel) {
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());
171                 }
172                 break;
173             case CHANNEL_FX_COLORS:
174                 // comming soon
175                 break;
176             case CHANNEL_FX_NAME:
177                 lightEffect.setName(command.toString());
178                 break;
179         }
180         setLightEffects(lightEffect);
181     }
182
183     /**
184      * SET LIGHTNING EFFECTS
185      * 
186      * @param lightEffect new lightEffect
187      */
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());
196
197         connector.sendDeviceCommand(JSON_KEY_LIGHTNING_EFFECT, newEffect.toString());
198     }
199
200     /**
201      * UPDATE PROPERTIES
202      * 
203      * @param TapoDeviceInfo
204      */
205     @Override
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()));
220         // light effect
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()));
224     }
225 }