Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[miio] Add support for BT Gateway switch on chuangmi.plug.212a01 #11657

Merged
merged 6 commits into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion bundles/org.openhab.binding.miio/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ Currently the miio binding supports more than 300 different models.
| Mi Multifunction Air Monitor | miio:basic | [cgllc.airmonitor.b1](#cgllc-airmonitor-b1) | Yes | |
| Qingping Air Monitor | miio:basic | [cgllc.airmonitor.s1](#cgllc-airmonitor-s1) | Yes | |
| Mi Universal Remote | miio:unsupported | chuangmi.ir.v2 | No | |
| Mi Smart Power Plug 2 (Wi-Fi and Bluetooth Gateway) | miio:basic | [chuangmi.plug.212a01](#chuangmi-plug-212a01) | Yes | Experimental support. Please report back if all channels are functional. Preferably share the debug log of property refresh and command responses |
| Mi Smart Power Plug 2 (Wi-Fi and Bluetooth Gateway) | miio:basic | [chuangmi.plug.212a01](#chuangmi-plug-212a01) | Yes | |
| Mi Smart Plug WiFi | miio:basic | [chuangmi.plug.hmi205](#chuangmi-plug-hmi205) | Yes | |
| Mi Smart Plug (WiFi) | miio:basic | [chuangmi.plug.hmi206](#chuangmi-plug-hmi206) | Yes | |
| Mi Smart Wi-Fi Plug (Bluetooth Gateway) | miio:basic | [chuangmi.plug.hmi208](#chuangmi-plug-hmi208) | Yes | |
Expand Down Expand Up @@ -693,6 +693,7 @@ Note, not all the values need to be in the json file, e.g. a subset of the param
| countdown | Number:Time | Imilab Timer - Countdown | |
| task-switch | Switch | Imilab Timer - Task Switch | |
| countdown-info | Switch | Imilab Timer - Countdown Info | |
| bt-gw | String | BT Gateway | Value mapping `["disable"="Disable","enable"="Enable"]` |

### Mi Smart Plug WiFi (<a name="chuangmi-plug-hmi205">chuangmi.plug.hmi205</a>) Channels

Expand Down Expand Up @@ -5469,6 +5470,7 @@ Number:Time off_duration "Imilab Timer - Off Duration" (G_plug) {channel="miio:b
Number:Time countdown "Imilab Timer - Countdown" (G_plug) {channel="miio:basic:plug:countdown"}
Switch task_switch "Imilab Timer - Task Switch" (G_plug) {channel="miio:basic:plug:task-switch"}
Switch countdown_info "Imilab Timer - Countdown Info" (G_plug) {channel="miio:basic:plug:countdown-info"}
String bt_gw "BT Gateway" (G_plug) {channel="miio:basic:plug:bt-gw"}
```

### Mi Smart Plug WiFi (chuangmi.plug.hmi205) item file lines
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.slf4j.LoggerFactory;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;

/**
Expand Down Expand Up @@ -121,9 +123,28 @@ public static JsonElement tankLevel(JsonElement value12) throws ClassCastExcepti
}
}

public static JsonElement getJsonElement(String element, JsonElement responseValue)
throws ClassCastException, IllegalStateException {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'd better know before accessing it if it's the right type in the right state. Catching these is bad.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe I'm overly causious here.. as the responsevalue is something that comes from the device and can have rather unexpected values I normally rather have a friendly message than an ugly stacktrace.

That said, I think the code indeed has little chance to generate these errors even in case of strange inputs so i'll remove it

if (responseValue.isJsonPrimitive()) {
JsonElement jsonElement = JsonParser.parseString(responseValue.getAsString());
if (jsonElement.isJsonObject()) {
JsonObject value = jsonElement.getAsJsonObject();
if (value.has(element)) {
return value.get(element);
}
}
} else {
LOGGER.debug("JsonElement '{}' not found in '{}'", element, responseValue);
}
return responseValue;
}

public static JsonElement execute(String transformation, JsonElement value,
@Nullable Map<String, Object> deviceVariables) {
try {
if (transformation.toUpperCase().startsWith("GETJSONELEMENT")) {
return getJsonElement(transformation.length() > 15 ? transformation.substring(15) : "", value);
}
switch (transformation.toUpperCase()) {
case "YEELIGHTSCENEID":
return yeelightSceneConversion(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,57 @@
},
"refresh": true,
"actions": []
},
{
"property": "",
"friendlyName": "BT Gateway",
"channel": "bt-gw",
"type": "String",
"stateDescription": {
"readOnly": false,
"options": [
{
"value": "disable",
"label": "Disable"
},
{
"value": "enable",
"label": "Enable"
}
]
},
"refresh": true,
"customRefreshCommand": "bt_gateway_status",
"transformation": "getJsonElement-gateway_status",
"actions": [
{
"command": "bt_gateway_enable",
"parameterType": "EMPTY",
"condition": {
"name": "matchValue",
"parameters": [
{
"matchValue": "enable"
}
]
}
},
{
"command": "bt_gateway_disable",
"parameterType": "EMPTY",
"condition": {
"name": "matchValue",
"parameters": [
{
"matchValue": "disable"
}
]
}
}
],
"readmeComment": "Value mapping `[\"disable\"\u003d\"Disable\",\"enable\"\u003d\"Enable\"]`"
}
],
"experimental": true
"experimental": false
}
}