]> git.basschouten.com Git - openhab-addons.git/blob
12e7771726c258f46796ee41001d70a8de2be128
[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.core.library.types.DecimalType;
23 import org.openhab.core.library.types.HSBType;
24 import org.openhab.core.library.types.OnOffType;
25 import org.openhab.core.library.types.PercentType;
26 import org.openhab.core.library.unit.Units;
27 import org.openhab.core.thing.ChannelUID;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.types.Command;
30 import org.openhab.core.types.RefreshType;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * TAPO Universal-Device
36  * universal device for testing pruposes
37  *
38  * @author Christian Wild - Initial contribution
39  */
40 @NonNullByDefault
41 public class TapoUniversalDevice extends TapoDevice {
42     private final Logger logger = LoggerFactory.getLogger(TapoUniversalDevice.class);
43
44     // CHANNEL LIST
45     public static final String CHANNEL_GROUP_DEBUG = "debug";
46     public static final String CHANNEL_RESPONSE = "deviceResponse";
47     public static final String CHANNEL_COMMAND = "deviceCommand";
48
49     /**
50      * Constructor
51      *
52      * @param thing Thing object representing device
53      */
54     public TapoUniversalDevice(Thing thing) {
55         super(thing);
56     }
57
58     @Override
59     public void handleCommand(ChannelUID channelUID, Command command) {
60         logger.debug("({}) handleCommand '{}' for channelUID {}", uid, command.toString(), channelUID.getId());
61         Boolean refreshInfo = false;
62
63         String channel = channelUID.getIdWithoutGroup();
64         if (command instanceof RefreshType) {
65             refreshInfo = true;
66         } else {
67             switch (channel) {
68                 case CHANNEL_OUTPUT:
69                     connector.sendDeviceCommand(JSON_KEY_ON, command == OnOffType.ON);
70                     refreshInfo = true;
71                     break;
72                 case CHANNEL_BRIGHTNESS:
73                     if (command instanceof PercentType) {
74                         Float percent = ((PercentType) command).floatValue();
75                         setBrightness(percent.intValue()); // 0..100% = 0..100
76                         refreshInfo = true;
77                     } else if (command instanceof DecimalType) {
78                         setBrightness(((DecimalType) command).intValue());
79                         refreshInfo = true;
80                     }
81                     break;
82                 case CHANNEL_COLOR_TEMP:
83                     if (command instanceof DecimalType) {
84                         setColorTemp(((DecimalType) command).intValue());
85                         refreshInfo = true;
86                     }
87                     break;
88                 case CHANNEL_COLOR:
89                     if (command instanceof HSBType) {
90                         setColor((HSBType) command);
91                         refreshInfo = true;
92                     }
93                     break;
94                 case CHANNEL_COMMAND:
95                     String[] cmd = command.toString().split(":");
96                     if (cmd.length == 1) {
97                         connector.sendCustomQuery(cmd[0]);
98                     } else if (cmd.length == 2) {
99                         connector.sendDeviceCommand(cmd[0], cmd[1]);
100                     } else {
101                         logger.warn("({}) wrong command format '{}'", uid, command.toString());
102                     }
103                     break;
104                 default:
105                     logger.warn("({}) command type '{}' not supported for channel '{}'", uid, command.toString(),
106                             channelUID.getId());
107             }
108         }
109
110         /* refreshInfo */
111         if (refreshInfo) {
112             queryDeviceInfo();
113         }
114     }
115
116     /**
117      * SET BRIGHTNESS
118      * 
119      * @param newBrightness percentage 0-100 of new brightness
120      */
121     protected void setBrightness(Integer newBrightness) {
122         /* switch off if 0 */
123         if (newBrightness == 0) {
124             connector.sendDeviceCommand(JSON_KEY_ON, false);
125         } else {
126             HashMap<String, Object> newState = new HashMap<>();
127             newState.put(JSON_KEY_ON, true);
128             newState.put(JSON_KEY_BRIGHTNES, newBrightness);
129             connector.sendDeviceCommands(newState);
130         }
131     }
132
133     /**
134      * SET COLOR
135      * 
136      * @param command
137      */
138     protected void setColor(HSBType command) {
139         HashMap<String, Object> newState = new HashMap<>();
140         newState.put(JSON_KEY_ON, true);
141         newState.put(JSON_KEY_HUE, command.getHue());
142         newState.put(JSON_KEY_SATURATION, command.getSaturation());
143         newState.put(JSON_KEY_BRIGHTNES, command.getBrightness());
144         connector.sendDeviceCommands(newState);
145     }
146
147     /**
148      * SET COLORTEMP
149      * 
150      * @param colorTemp (Integer) in Kelvin
151      */
152     protected void setColorTemp(Integer colorTemp) {
153         HashMap<String, Object> newState = new HashMap<>();
154         colorTemp = limitVal(colorTemp, BULB_MIN_COLORTEMP, BULB_MAX_COLORTEMP);
155         newState.put(JSON_KEY_ON, true);
156         newState.put(JSON_KEY_COLORTEMP, colorTemp);
157         connector.sendDeviceCommands(newState);
158     }
159
160     /**
161      * SET DEVICE INFOs to device
162      * 
163      * @param deviceInfo
164      */
165     @Override
166     public void setDeviceInfo(TapoDeviceInfo deviceInfo) {
167         devicePropertiesChanged(deviceInfo);
168         handleConnectionState();
169     }
170
171     /**
172      * Handle full responsebody received from connector
173      * 
174      * @param responseBody
175      */
176     public void responsePasstrough(String responseBody) {
177         logger.debug("({}) received response {}", uid, responseBody);
178         publishState(getChannelID(CHANNEL_GROUP_DEBUG, CHANNEL_RESPONSE), getStringType(responseBody));
179     }
180
181     /**
182      * UPDATE PROPERTIES
183      * 
184      * @param TapoDeviceInfo
185      */
186     @Override
187     protected void devicePropertiesChanged(TapoDeviceInfo deviceInfo) {
188         super.devicePropertiesChanged(deviceInfo);
189         publishState(getChannelID(CHANNEL_GROUP_ACTUATOR, CHANNEL_OUTPUT), getOnOffType(deviceInfo.isOn()));
190         publishState(getChannelID(CHANNEL_GROUP_ACTUATOR, CHANNEL_BRIGHTNESS),
191                 getPercentType(deviceInfo.getBrightness()));
192         publishState(getChannelID(CHANNEL_GROUP_ACTUATOR, CHANNEL_COLOR_TEMP),
193                 getDecimalType(deviceInfo.getColorTemp()));
194         publishState(getChannelID(CHANNEL_GROUP_ACTUATOR, CHANNEL_COLOR), deviceInfo.getHSB());
195
196         publishState(getChannelID(CHANNEL_GROUP_DEVICE, CHANNEL_WIFI_STRENGTH),
197                 getDecimalType(deviceInfo.getSignalLevel()));
198         publishState(getChannelID(CHANNEL_GROUP_DEVICE, CHANNEL_ONTIME),
199                 getTimeType(deviceInfo.getOnTime(), Units.SECOND));
200         publishState(getChannelID(CHANNEL_GROUP_DEVICE, CHANNEL_OVERHEAT),
201                 getDecimalType(deviceInfo.isOverheated() ? 1 : 0));
202     }
203
204     /***********************************
205      *
206      * CHANNELS
207      *
208      ************************************/
209     /**
210      * Get ChannelID including group
211      * 
212      * @param group String channel-group
213      * @param channel String channel-name
214      * @return String channelID
215      */
216     @Override
217     protected String getChannelID(String group, String channel) {
218         return group + "#" + channel;
219     }
220
221     /**
222      * Get Channel from ChannelID
223      * 
224      * @param channelID String channelID
225      * @return String channel-name
226      */
227     protected String getChannelFromID(ChannelUID channelID) {
228         String channel = channelID.getIdWithoutGroup();
229         channel = channel.replace(CHANNEL_GROUP_ACTUATOR + "#", "");
230         channel = channel.replace(CHANNEL_GROUP_DEVICE + "#", "");
231         channel = channel.replace(CHANNEL_GROUP_DEBUG + "#", "");
232         return channel;
233     }
234 }