Skip to content

Commit 98bdc2f

Browse files
author
PeÄ?ta Ond??ej
committed
Added support for the magic cube rotate event
1 parent e4092ac commit 98bdc2f

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# org.openhab.binding.xiaomigateway
22

33
This binding brings Xiaomi Gateway Smart Home devices (Aqara) integration with OpenHAB1.x
4-
Currently only one gateway is supported and only getting sub device state/reading events confirmed to be working.
4+
Currently only one gateway is supported and only getting sub device state/reading events confirmed to be working including the magic cube.
55
Controlling of gateway light works too including color and brightness change within OpenHAB.
66
Unfortunately I have no switch to test controlling its state using write command, but write key generation based on gateway token seems to be implemented OK.
77

@@ -32,7 +32,10 @@ The binding detects a Xiaomi gateway on local network and lists all sub devices.
3232
```
3333

3434
sid is a sub device identificator used in item configuration file.
35-
possible values are: magnet, temperature, humidity, virtual_switch (button simulates ON/OFF switch), click, long_click, double_click
35+
possible sensor values are: magnet, motion, temperature, humidity
36+
possible button values are: virtual_switch (button simulates ON/OFF switch), click, long_click, double_click
37+
possible switch values are: click, double_click, both_click
38+
possible cube values are: flip90, flip180, move, tap_twice, shake_air, swing, alert, free_fall, rotate_left, rotate_right (ON command is received when an event fired)
3639

3740
#openhab.cfg
3841
If you want to control devices please supply a developer key (you can see it in Mi Home app when you enable developer mode)

src/main/java/org/openhab/binding/xiaomigateway/internal/XiaomiGatewayBinding.java

+17-4
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ private void processCubeEvent(String itemName, String type, JsonObject jobject)
308308
String event = getStatusEvent(jobject);
309309
boolean publish = false;
310310
if (isRotateCubeEvent(jobject)) {
311-
event = "rotate";
311+
event = isLeftRotate(jobject) ? "rotate_left" : "rotate_right";
312312
}
313313
logger.debug("XiaomiGateway: processing cube event " + event);
314314
switch (event) {
@@ -336,8 +336,11 @@ private void processCubeEvent(String itemName, String type, JsonObject jobject)
336336
case "free_fall":
337337
publish = type.endsWith(".free_fall");
338338
break;
339-
case "rotate":
340-
logger.info("rotate is not supported yet!");
339+
case "rotate_left":
340+
publish = type.endsWith(".rotate_left");
341+
break;
342+
case "rotate_right":
343+
publish = type.endsWith(".rotate_right");
341344
break;
342345
default:
343346
logger.error("Unknown cube event: " + event);
@@ -347,6 +350,17 @@ private void processCubeEvent(String itemName, String type, JsonObject jobject)
347350
eventPublisher.sendCommand(itemName, OnOffType.ON);
348351
}
349352

353+
private boolean isLeftRotate(JsonObject jobject) {
354+
try {
355+
String data = jobject.get("data").getAsString();
356+
JsonObject jo = parser.parse(data).getAsJsonObject();
357+
return jobject.get("model").getAsString().equals("cube") && jo != null && jo.get("rotate") != null && jo.get("rotate").getAsString().startsWith("-");
358+
} catch (Exception ex) {
359+
logger.error(ex.toString());
360+
return false;
361+
}
362+
}
363+
350364
private boolean isRotateCubeEvent(JsonObject jobject) {
351365
try {
352366
String data = jobject.get("data").getAsString();
@@ -702,7 +716,6 @@ protected void internalReceiveCommand(String itemName, Command command) {
702716
long color = rgb - (currentBrightness * 65536 * 256) + brightness.longValue() * 65536 * 256;
703717
changeGatewayColor(color);
704718
}
705-
706719
return;
707720
}
708721

0 commit comments

Comments
 (0)