Skip to content

Commit

Permalink
[TapoControl] Adding P300 to the list of supported equipments (openha…
Browse files Browse the repository at this point in the history
…b#14364)

* Adding P300 to the list of supported equipments

Signed-off-by: clinique <gael@lhopital.org>
  • Loading branch information
clinique authored and nemerdaud committed Feb 28, 2023
1 parent 50bdca4 commit 7db8e0b
Show file tree
Hide file tree
Showing 14 changed files with 490 additions and 97 deletions.
6 changes: 5 additions & 1 deletion bundles/org.openhab.binding.tapocontrol/README.md
Expand Up @@ -12,6 +12,7 @@ The following Tapo-Devices are supported. For precise channel-description look a
| | P105 | Smart Mini Socket |
| EnergyMonitoring SmartPlug (Wi-Fi) | P110 | Energy Monitoring Smart Socket |
| | P115 | Energy Monitoring Mini Smart Socket |
| Power Strip (Wi-Fi) | P300 | Smart Wi-Fi Power Strip - 3 sockets |
| Dimmable SmartBulb (Wi-Fi) | L510 | Dimmable White-Light Smart-Bulb (E27) |
| | L610 | Dimmable White-Light Smart-Spot (GU10) |
| MultiColor SmartBulb (Wi-Fi) | L530 | Multicolor Smart-Bulb (E27) |
Expand Down Expand Up @@ -66,6 +67,9 @@ All devices support some of the following channels:
| group | channel | type | description | things supporting this channel |
|-----------|----------------- |------------------------|------------------------------|------------------------------------------------------------------|
| actuator | output | Switch | Power device on or off | P100, P105, P110, P115, L510, L530, L610, L630, L900, L920, L930 |
| | output1 | Switch | Power socket 1 on or off | P300 |
| | output2 | Switch | Power socket 2 on or off | P300 |
| | output3 | Switch | Power socket 3 on or off | P300 |
| | brightness | Dimmer | Brightness 0-100% | L510, L530, L610, L630, L900 |
| | colorTemperature | Number | White-Color-Temp 2500-6500K | L510, L530, L610, L630, L900 |
| | color | Color | Color | L530, L630, L900 |
Expand Down Expand Up @@ -94,7 +98,7 @@ tapocontrol:L530:myTapoBridge:colorBulb "color-light" (tapocontrol:bri
tapocontrol:L900:myTapoBridge:myLightStrip "light-strip" (tapocontrol:bridge:myTapoBridge) [ ipAddress="192.168.178.153", pollingInterval=30 ]
Bridge tapocontrol:bridge:secondBridgeExample "Cloud-Login" [ username="youtoo@anyprovider.com", password="verysecret" ] {
Thing tapocontrol:P110:secondBridgeExample:mySocket "My-Socket" [ ipAddress="192.168.101.51", pollingInterval=30 ]
Thing P110 mySocket "My-Socket" [ ipAddress="192.168.101.51", pollingInterval=30 ]
}
```

Expand Down
Expand Up @@ -15,22 +15,26 @@
import static org.openhab.binding.tapocontrol.internal.constants.TapoBindingSettings.*;
import static org.openhab.binding.tapocontrol.internal.constants.TapoErrorConstants.*;
import static org.openhab.binding.tapocontrol.internal.constants.TapoThingConstants.*;
import static org.openhab.binding.tapocontrol.internal.helpers.TapoUtils.*;
import static org.openhab.binding.tapocontrol.internal.helpers.TapoUtils.jsonObjectToInt;

import java.net.InetAddress;
import java.util.HashMap;
import java.util.Objects;
import java.util.Optional;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.tapocontrol.internal.device.TapoBridgeHandler;
import org.openhab.binding.tapocontrol.internal.device.TapoDevice;
import org.openhab.binding.tapocontrol.internal.helpers.PayloadBuilder;
import org.openhab.binding.tapocontrol.internal.helpers.TapoErrorHandler;
import org.openhab.binding.tapocontrol.internal.structures.TapoChild;
import org.openhab.binding.tapocontrol.internal.structures.TapoChildData;
import org.openhab.binding.tapocontrol.internal.structures.TapoDeviceInfo;
import org.openhab.binding.tapocontrol.internal.structures.TapoEnergyData;
import org.openhab.binding.tapocontrol.internal.structures.TapoSubRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

/**
Expand All @@ -41,12 +45,12 @@
*/
@NonNullByDefault
public class TapoDeviceConnector extends TapoDeviceHttpApi {

private final Logger logger = LoggerFactory.getLogger(TapoDeviceConnector.class);
private final String uid;
private final TapoDevice device;
private TapoDeviceInfo deviceInfo;
private TapoEnergyData energyData;
private Gson gson;

private TapoDeviceInfo deviceInfo = new TapoDeviceInfo();
private TapoEnergyData energyData = new TapoEnergyData();
private TapoChildData childData = new TapoChildData();
private long lastQuery = 0L;
private long lastSent = 0L;
private long lastLogin = 0L;
Expand All @@ -58,11 +62,6 @@ public class TapoDeviceConnector extends TapoDeviceHttpApi {
*/
public TapoDeviceConnector(TapoDevice device, TapoBridgeHandler bridgeThingHandler) {
super(device, bridgeThingHandler);
this.device = device;
this.gson = new Gson();
this.deviceInfo = new TapoDeviceInfo();
this.energyData = new TapoEnergyData();
this.uid = device.getThingUID().getAsString();
}

/***********************************
Expand Down Expand Up @@ -111,7 +110,7 @@ public boolean login() {

/**
* send custom command to device
*
*
* @param plBuilder Payloadbuilder with unencrypted payload
*/
public void sendCustomQuery(String queryMethod) {
Expand All @@ -123,7 +122,7 @@ public void sendCustomQuery(String queryMethod) {

/**
* send custom command to device
*
*
* @param plBuilder Payloadbuilder with unencrypted payload
*/
public void sendCustomPayload(PayloadBuilder plBuilder) {
Expand Down Expand Up @@ -159,6 +158,27 @@ public void sendDeviceCommand(String name, Object value) {
}
}

/**
* send "set_device_info" command to child's device
*
* @param index of the child
* @param childProperty to modify
* @param value for the property
*/
public void sendChildCommand(Integer index, String childProperty, Object value) {
long now = System.currentTimeMillis();
if (now > this.lastSent + TAPO_SEND_MIN_GAP_MS) {
this.lastSent = now;
getChild(index).ifPresent(child -> {
child.setDeviceOn(Boolean.valueOf((Boolean) value));
TapoSubRequest request = new TapoSubRequest(child.getDeviceId(), DEVICE_CMD_SETINFO, child);
sendSecurePasstrhroug(GSON.toJson(request), request.method());
});
} else {
logger.debug("({}) command not sent because of min_gap: {}", uid, now + " <- " + lastSent);
}
}

/**
* send multiple "set_device_info" commands to device
*
Expand All @@ -184,15 +204,16 @@ public void sendDeviceCommands(HashMap<String, Object> map) {
}

/**
* Query Info from Device adn refresh deviceInfo
* Query Info from Device and refresh deviceInfo
*/
public void queryInfo() {
queryInfo(false);
queryChildDevices();
}

/**
* Query Info from Device adn refresh deviceInfo
*
* Query Info from Device and refresh deviceInfo
*
* @param ignoreGap ignore gap to last query. query anyway
*/
public void queryInfo(boolean ignoreGap) {
Expand All @@ -212,6 +233,21 @@ public void queryInfo(boolean ignoreGap) {
}
}

/**
* Query Info from Child Devices and refresh deviceInfo
*/
@Override
public void queryChildDevices() {
logger.trace("({}) DeviceConnetor_queryChildDevices from '{}'", uid, deviceURL);

/* create payload */
PayloadBuilder plBuilder = new PayloadBuilder();
plBuilder.method = DEVICE_CMD_CHILD_DEVICE_LIST;
String payload = plBuilder.getPayload();

sendSecurePasstrhroug(payload, DEVICE_CMD_CHILD_DEVICE_LIST);
}

/**
* Get energy usage from device
*/
Expand All @@ -229,7 +265,7 @@ public void getEnergyUsage() {
/**
* SEND SECUREPASSTHROUGH
* encprypt payload and send to device
*
*
* @param payload payload sent to device
* @param command command executed - this will handle result
*/
Expand All @@ -255,7 +291,7 @@ protected void sendSecurePasstrhroug(String payload, String command) {

/**
* Handle SuccessResponse (setDeviceInfo)
*
*
* @param responseBody String with responseBody from device
*/
@Override
Expand All @@ -270,9 +306,9 @@ protected void handleSuccessResponse(String responseBody) {
}

/**
*
*
* handle JsonResponse (getDeviceInfo)
*
*
* @param responseBody String with responseBody from device
*/
@Override
Expand All @@ -290,7 +326,7 @@ protected void handleDeviceResult(String responseBody) {

/**
* handle JsonResponse (getEnergyData)
*
*
* @param responseBody String with responseBody from device
*/
@Override
Expand All @@ -305,9 +341,26 @@ protected void handleEnergyResult(String responseBody) {
this.device.responsePasstrough(responseBody);
}

/**
* handle JsonResponse (getChildDeviceList)
*
* @param responseBody String with responseBody from device
*/
@Override
protected void handleChildDevices(String responseBody) {
JsonObject jsnResult = getJsonFromResponse(responseBody);
if (jsnResult.has(CHILD_PROPERTY_START_INDEX)) {
this.childData = Objects.requireNonNull(GSON.fromJson(jsnResult, TapoChildData.class));
this.device.setChildData(childData);
} else {
this.childData = new TapoChildData();
}
this.device.responsePasstrough(responseBody);
}

/**
* handle custom response
*
*
* @param responseBody String with responseBody from device
*/
@Override
Expand All @@ -317,7 +370,7 @@ protected void handleCustomResponse(String responseBody) {

/**
* handle error
*
*
* @param te TapoErrorHandler
*/
@Override
Expand All @@ -327,18 +380,18 @@ protected void handleError(TapoErrorHandler tapoError) {

/**
* get Json from response
*
*
* @param responseBody
* @return JsonObject with result
*/
private JsonObject getJsonFromResponse(String responseBody) {
JsonObject jsonObject = gson.fromJson(responseBody, JsonObject.class);
JsonObject jsonObject = GSON.fromJson(responseBody, JsonObject.class);
/* get errocode (0=success) */
if (jsonObject != null) {
Integer errorCode = jsonObjectToInt(jsonObject, "error_code");
if (errorCode == 0) {
/* decrypt response */
jsonObject = gson.fromJson(responseBody, JsonObject.class);
jsonObject = GSON.fromJson(responseBody, JsonObject.class);
logger.trace("({}) received result: {}", uid, responseBody);
if (jsonObject != null) {
/* return result if set / else request was successful */
Expand Down Expand Up @@ -369,7 +422,7 @@ private JsonObject getJsonFromResponse(String responseBody) {

/**
* Check if device is online
*
*
* @return true if device is online
*/
public Boolean isOnline() {
Expand All @@ -378,7 +431,7 @@ public Boolean isOnline() {

/**
* Check if device is online
*
*
* @param raiseError if true
* @return true if device is online
*/
Expand All @@ -397,7 +450,7 @@ public Boolean isOnline(Boolean raiseError) {

/**
* IP-Adress
*
*
* @return String ipAdress
*/
public String getIP() {
Expand All @@ -406,7 +459,7 @@ public String getIP() {

/**
* PING IP Adress
*
*
* @return true if ping successfull
*/
public Boolean pingDevice() {
Expand All @@ -418,4 +471,8 @@ public Boolean pingDevice() {
return false;
}
}

private Optional<TapoChild> getChild(int position) {
return childData.getChildDeviceList().stream().filter(child -> child.getPosition() == position).findFirst();
}
}

0 comments on commit 7db8e0b

Please sign in to comment.