Skip to content

Commit

Permalink
Revert "[miio] Improve yeelight RGB with brightness, introduce substi…
Browse files Browse the repository at this point in the history
…tutions (openhab#10984)"

This reverts commit afe9142.

Signed-off-by: dw-8 <davy.wong.on+github@gmail.com>
  • Loading branch information
dw-8 committed Jul 25, 2021
1 parent c728bfc commit 05f44d1
Show file tree
Hide file tree
Showing 13 changed files with 179 additions and 541 deletions.
164 changes: 71 additions & 93 deletions bundles/org.openhab.binding.miio/README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
package org.openhab.binding.miio.internal.basic;

import java.awt.Color;
import java.util.Map;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.library.types.HSBType;
import org.openhab.core.library.types.PercentType;
import org.slf4j.Logger;
Expand Down Expand Up @@ -51,31 +49,6 @@ public static JsonElement bRGBtoHSV(JsonElement bRGB) throws ClassCastException
return bRGB;
}

/**
* Adds the brightness info (from separate channel) to a HSV value.
* *
*
* @param RGB
* @param map with device variables containing the brightness info
* @return HSV
*/
public static JsonElement addBrightToHSV(JsonElement rgbValue, @Nullable Map<String, Object> deviceVariables)
throws ClassCastException, IllegalStateException {
int bright = 100;
if (deviceVariables != null) {
JsonElement lastBright = (JsonElement) deviceVariables.getOrDefault("bright", new JsonPrimitive(100));
bright = lastBright.getAsInt();
}
if (rgbValue.isJsonPrimitive()
&& (rgbValue.getAsJsonPrimitive().isNumber() || rgbValue.getAsString().matches("^[0-9]+$"))) {
Color rgb = new Color(rgbValue.getAsInt());
HSBType hsb = HSBType.fromRGB(rgb.getRed(), rgb.getGreen(), rgb.getBlue());
hsb = new HSBType(hsb.getHue(), hsb.getSaturation(), new PercentType(bright));
return new JsonPrimitive(hsb.toFullString());
}
return rgbValue;
}

public static JsonElement secondsToHours(JsonElement seconds) throws ClassCastException {
double value = seconds.getAsDouble() / 3600;
return new JsonPrimitive(value);
Expand Down Expand Up @@ -121,10 +94,9 @@ public static JsonElement tankLevel(JsonElement value12) throws ClassCastExcepti
}
}

public static JsonElement execute(String transformation, JsonElement value,
@Nullable Map<String, Object> deviceVariables) {
public static JsonElement execute(String transfortmation, JsonElement value) {
try {
switch (transformation.toUpperCase()) {
switch (transfortmation.toUpperCase()) {
case "YEELIGHTSCENEID":
return yeelightSceneConversion(value);
case "SECONDSTOHOURS":
Expand All @@ -135,16 +107,14 @@ public static JsonElement execute(String transformation, JsonElement value,
return divideHundred(value);
case "TANKLEVEL":
return tankLevel(value);
case "ADDBRIGHTTOHSV":
return addBrightToHSV(value, deviceVariables);
case "BRGBTOHSV":
return bRGBtoHSV(value);
default:
LOGGER.debug("Transformation {} not found. Returning '{}'", transformation, value.toString());
LOGGER.debug("Transformation {} not found. Returning '{}'", transfortmation, value.toString());
return value;
}
} catch (ClassCastException | IllegalStateException e) {
LOGGER.debug("Transformation {} failed. Returning '{}'", transformation, value.toString());
LOGGER.debug("Transformation {} failed. Returning '{}'", transfortmation, value.toString());
return value;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public void setStateDescription(@Nullable StateDescriptionDTO stateDescription)

public Boolean getRefresh() {
final @Nullable Boolean rf = refresh;
return rf != null && rf.booleanValue();
return rf != null && rf.booleanValue() && !getProperty().isEmpty();
}

public void setRefresh(@Nullable Boolean refresh) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@
import static org.openhab.binding.miio.internal.MiIoBindingConstants.*;

import java.io.IOException;
import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
Expand Down Expand Up @@ -62,7 +59,6 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSyntaxException;

/**
Expand All @@ -75,7 +71,6 @@
public abstract class MiIoAbstractHandler extends BaseThingHandler implements MiIoMessageListener {
protected static final int MAX_QUEUE = 5;
protected static final Gson GSON = new GsonBuilder().create();
protected static final String TIMESTAMP = "timestamp";

protected ScheduledExecutorService miIoScheduler = scheduler;
protected @Nullable ScheduledFuture<?> pollingJob;
Expand Down Expand Up @@ -116,13 +111,12 @@ public MiIoAbstractHandler(Thing thing, MiIoDatabaseWatchService miIoDatabaseWat
public abstract void handleCommand(ChannelUID channelUID, Command command);

protected boolean handleCommandsChannels(ChannelUID channelUID, Command command) {
String cmd = processSubstitutions(command.toString(), deviceVariables);
if (channelUID.getId().equals(CHANNEL_COMMAND)) {
cmds.put(sendCommand(cmd), channelUID.getId());
cmds.put(sendCommand(command.toString()), channelUID.getId());
return true;
}
if (channelUID.getId().equals(CHANNEL_RPC)) {
cmds.put(sendCommand(cmd, cloudServer), channelUID.getId());
cmds.put(sendCommand(command.toString(), cloudServer), channelUID.getId());
return true;
}
return false;
Expand Down Expand Up @@ -152,8 +146,6 @@ public void initialize() {
}
this.cloudServer = configuration.cloudServer;
isIdentified = false;
deviceVariables.put(TIMESTAMP, Instant.now().getEpochSecond());
deviceVariables.put(PROPERTY_DID, configuration.deviceId);
miIoScheduler.schedule(this::initializeData, 1, TimeUnit.SECONDS);
int pollingPeriod = configuration.refreshInterval;
if (pollingPeriod > 0) {
Expand Down Expand Up @@ -224,7 +216,14 @@ protected int sendCommand(MiIoCommand command) {
}

protected int sendCommand(MiIoCommand command, String params) {
return sendCommand(command.getCommand(), processSubstitutions(params, deviceVariables), getCloudServer(), "");
try {
final MiIoAsyncCommunication connection = getConnection();
return (connection != null) ? connection.queueCommand(command, params, getCloudServer()) : 0;
} catch (MiIoCryptoException | IOException e) {
logger.debug("Command {} for {} failed (type: {}): {}", command.toString(), getThing().getUID(),
getThing().getThingTypeUID(), e.getLocalizedMessage());
}
return 0;
}

protected int sendCommand(String commandString) {
Expand All @@ -242,39 +241,19 @@ protected int sendCommand(String commandString) {
* @return vacuum response
*/
protected int sendCommand(String commandString, String cloudServer) {
String command = commandString.trim();
command = processSubstitutions(commandString.trim(), deviceVariables);
String param = "[]";
int sb = command.indexOf("[");
int cb = command.indexOf("{");
if (Math.max(sb, cb) > 0) {
int loc = (Math.min(sb, cb) > 0 ? Math.min(sb, cb) : Math.max(sb, cb));
param = command.substring(loc).trim();
command = command.substring(0, loc).trim();
}
return sendCommand(command, param, cloudServer, "");
}

protected int sendCommand(String command, String params, String cloudServer) {
return sendCommand(command, processSubstitutions(params, deviceVariables), cloudServer, "");
}

/**
* Sends commands to the {@link MiIoAsyncCommunication} for transmission to the Mi devices or cloud
*
* @param command (method) to be queued for execution
* @param parameters to be send with the command
* @param cloud server to be used or empty string for direct sending to the device
* @param sending subdevice or empty string for regular device
* @return message id
*/
protected int sendCommand(String command, String params, String cloudServer, String sender) {
final MiIoAsyncCommunication connection = getConnection();
try {
final MiIoAsyncCommunication connection = getConnection();
return (connection != null) ? connection.queueCommand(command, params, cloudServer, sender) : 0;
String command = commandString.trim();
String param = "[]";
int sb = command.indexOf("[");
int cb = command.indexOf("{");
if (Math.max(sb, cb) > 0) {
int loc = (Math.min(sb, cb) > 0 ? Math.min(sb, cb) : Math.max(sb, cb));
param = command.substring(loc).trim();
command = command.substring(0, loc).trim();
}
return (connection != null) ? connection.queueCommand(command, param, cloudServer) : 0;
} catch (MiIoCryptoException | IOException e) {
logger.debug("Command {} for {} failed (type: {}): {}", command.toString(), getThing().getUID(),
getThing().getThingTypeUID(), e.getLocalizedMessage());
disconnected(e.getMessage());
}
return 0;
Expand Down Expand Up @@ -435,7 +414,6 @@ private void updateDeviceIdConfig(String deviceId) {
Configuration config = editConfiguration();
config.put(PROPERTY_DID, deviceId);
updateConfiguration(config);
deviceVariables.put(PROPERTY_DID, deviceId);
} else {
logger.debug("Could not update config with deviceId: {}", deviceId);
}
Expand Down Expand Up @@ -480,42 +458,6 @@ private void updateProperties(JsonObject miioInfo) {
updateProperties(properties);
}

protected String processSubstitutions(String cmd, Map<String, Object> deviceVariables) {
if (!cmd.contains("$")) {
return cmd;
}
String returnCmd = cmd.replace("\"$", "$").replace("$\"", "$");
String cmdParts[] = cmd.split("\\$");
if (logger.isTraceEnabled()) {
logger.debug("processSubstitutions {} ", cmd);
for (Entry<String, Object> e : deviceVariables.entrySet()) {
logger.debug("key, value: {} -> {}", e.getKey(), e.getValue());
}
}
for (String substitute : cmdParts) {
if (deviceVariables.containsKey(substitute)) {
String replacementString = "";
Object replacement = deviceVariables.get(substitute);
if (replacement == null) {
logger.debug("Replacement for '{}' is null. skipping replacement", substitute);
continue;
}
if (replacement instanceof Integer || replacement instanceof Long || replacement instanceof Double
|| replacement instanceof BigDecimal || replacement instanceof Boolean) {
replacementString = replacement.toString();
} else if (replacement instanceof JsonPrimitive) {
replacementString = ((JsonPrimitive) replacement).getAsString();
} else if (replacement instanceof String) {
replacementString = "\"" + (String) replacement + "\"";
} else {
replacementString = String.valueOf(replacement);
}
returnCmd = returnCmd.replace("$" + substitute + "$", replacementString);
}
}
return returnCmd;
}

protected boolean updateThingType(JsonObject miioInfo) {
MiIoBindingConfiguration configuration = getConfigAs(MiIoBindingConfiguration.class);
String model = miioInfo.get("model").getAsString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.awt.Color;
import java.io.IOException;
import java.net.URL;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -123,7 +122,6 @@ public void initialize() {
@Override
public void handleCommand(ChannelUID channelUID, Command receivedCommand) {
Command command = receivedCommand;
deviceVariables.put(TIMESTAMP, Instant.now().getEpochSecond());
if (command == RefreshType.REFRESH) {
if (updateDataCache.isExpired()) {
logger.debug("Refreshing {}", channelUID);
Expand Down Expand Up @@ -314,7 +312,6 @@ protected synchronized void updateData() {
}
final MiIoBasicDevice midevice = miioDevice;
if (midevice != null) {
deviceVariables.put(TIMESTAMP, Instant.now().getEpochSecond());
refreshProperties(midevice);
refreshCustomProperties(midevice);
refreshNetwork();
Expand Down Expand Up @@ -584,14 +581,13 @@ private void updatePropsFromJsonObject(MiIoSendCommand response) {

private void updateChannel(@Nullable MiIoBasicChannel basicChannel, String param, JsonElement value) {
JsonElement val = value;
deviceVariables.put(param, val);
if (basicChannel == null) {
logger.debug("Channel not found for {}", param);
return;
}
final String transformation = basicChannel.getTransformation();
if (transformation != null) {
JsonElement transformed = Conversions.execute(transformation, val, deviceVariables);
JsonElement transformed = Conversions.execute(transformation, val);
logger.debug("Transformed with '{}': {} {} -> {} ", transformation, basicChannel.getFriendlyName(), val,
transformed);
val = transformed;
Expand All @@ -618,8 +614,7 @@ private void updateChannel(@Nullable MiIoBasicChannel basicChannel, String param
} else {
String strVal = val.getAsString().toLowerCase();
updateState(basicChannel.getChannel(),
"on".equals(strVal) || "true".equals(strVal) || "1".equals(strVal) ? OnOffType.ON
: OnOffType.OFF);
"on".equals(strVal) || "true".equals(strVal) ? OnOffType.ON : OnOffType.OFF);
}
break;
case "color":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,16 @@ public synchronized void unregisterListener(MiIoMessageListener listener) {
}
}

public int queueCommand(String command, String params, String cloudServer, String sender)
public int queueCommand(MiIoCommand command, String cloudServer) throws MiIoCryptoException, IOException {
return queueCommand(command, "[]", cloudServer);
}

public int queueCommand(MiIoCommand command, String params, String cloudServer)
throws MiIoCryptoException, IOException {
return queueCommand(command.getCommand(), params, cloudServer);
}

public int queueCommand(String command, String params, String cloudServer)
throws MiIoCryptoException, IOException, JsonSyntaxException {
try {
JsonObject fullCommand = new JsonObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@
"friendlyName": "Shutdown Timer",
"channel": "delayoff",
"type": "Number:Time",
"unit": "minutes",
"stateDescription": {
"pattern": "%.0f %unit%"
},
"unit": "seconds",
"refresh": true,
"actions": [
{
Expand Down
Loading

0 comments on commit 05f44d1

Please sign in to comment.