Skip to content

Commit 1436df5

Browse files
committed
Add state.gesture to Xiaomi cube to simplify rules
Some gestures could only be handled by writing multiple rules for all possible button events. The state.gesture is a companion for state.buttonevent to reduce multiple button values into one gesture value. TODO GESTURE_ROTATE_CLOCKWISE and GESTURE_ROTATE_COUNTER_CLOCKWISE.
1 parent 24c7ff9 commit 1436df5

5 files changed

Lines changed: 47 additions & 7 deletions

File tree

database.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2917,6 +2917,11 @@ static int sqliteLoadAllSensorsCallback(void *user, int ncols, char **colval , c
29172917
}
29182918
item = sensor.addItem(DataTypeInt32, RStateButtonEvent);
29192919
item->setValue(0);
2920+
2921+
if (sensor.modelId().startsWith(QLatin1String("lumi.sensor_cube")))
2922+
{
2923+
sensor.addItem(DataTypeInt32, RStateGesture);
2924+
}
29202925
}
29212926
else if (sensor.type().endsWith(QLatin1String("LightLevel")))
29222927
{

de_web_plugin.cpp

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4326,6 +4326,11 @@ void DeRestPluginPrivate::addSensorNode(const deCONZ::Node *node, const SensorFi
43264326
clusterId = MULTISTATE_INPUT_CLUSTER_ID;
43274327
}
43284328
sensorNode.addItem(DataTypeInt32, RStateButtonEvent);
4329+
4330+
if (modelId.startsWith(QLatin1String("lumi.sensor_cube")))
4331+
{
4332+
sensorNode.addItem(DataTypeInt32, RStateGesture);
4333+
}
43294334
}
43304335
else if (sensorNode.type().endsWith(QLatin1String("LightLevel")))
43314336
{
@@ -6196,6 +6201,7 @@ void DeRestPluginPrivate::updateSensorNode(const deCONZ::NodeEvent &event)
61966201
}
61976202

61986203
qint32 buttonevent = -1;
6204+
qint32 gesture = -1; //
61996205
ResourceItem *item = i->item(RStateButtonEvent);
62006206
int rawValue = ia->numericValue().u16;
62016207

@@ -6205,13 +6211,17 @@ void DeRestPluginPrivate::updateSensorNode(const deCONZ::NodeEvent &event)
62056211
static const int sideMap[] = {1, 3, 5, 6, 4, 2};
62066212
int side = sideMap[rawValue & 0x0007];
62076213
int previousSide = sideMap[(rawValue & 0x0038) >> 3];
6208-
if (rawValue == 0x0002) { buttonevent = 7000; } // wakeup
6209-
else if (rawValue == 0x0000) { buttonevent = 7007; } // shake
6210-
else if (rawValue == 0x0003) { buttonevent = 7008; } // drop
6211-
else if (rawValue & 0x0040) { buttonevent = side * 1000 + previousSide; } // flip 90°
6212-
else if (rawValue & 0x0080) { buttonevent = side * 1000 + 7 - side; } // flip 180°
6213-
else if (rawValue & 0x0100) { buttonevent = side * 1000; } // push
6214-
else if (rawValue & 0x0200) { buttonevent = side * 1000 + side; } // double tap
6214+
if (rawValue == 0x0002) { buttonevent = 7000; } // wakeup
6215+
else if (rawValue == 0x0000) { buttonevent = 7007; gesture = GESTURE_SHAKE; } // shake
6216+
else if (rawValue == 0x0003) { buttonevent = 7008; gesture = GESTURE_DROP; } // drop
6217+
else if (rawValue & 0x0040) { buttonevent = side * 1000 + previousSide; // flip 90°
6218+
gesture = GESTURE_FLIP_90; }
6219+
else if (rawValue & 0x0080) { buttonevent = side * 1000 + 7 - side; // flip 180°
6220+
gesture = GESTURE_FLIP_180; }
6221+
else if (rawValue & 0x0100) { buttonevent = side * 1000; // push
6222+
gesture = GESTURE_PUSH; }
6223+
else if (rawValue & 0x0200) { buttonevent = side * 1000 + side; // double tap
6224+
gesture = GESTURE_DOUBLE_TAP; }
62156225
}
62166226
else if (i->modelId() == QLatin1String("lumi.sensor_switch.aq3"))
62176227
{
@@ -6262,13 +6272,28 @@ void DeRestPluginPrivate::updateSensorNode(const deCONZ::NodeEvent &event)
62626272
}
62636273
}
62646274
}
6275+
62656276
if (item && buttonevent != -1)
62666277
{
62676278
item->setValue(buttonevent);
62686279
i->updateStateTimestamp();
62696280
i->setNeedSaveDatabase(true);
62706281
Event e(RSensors, RStateButtonEvent, i->id(), item);
62716282
enqueueEvent(e);
6283+
}
6284+
6285+
item = (gesture != -1) ? i->item(RStateGesture) : nullptr;
6286+
if (item && gesture != -1)
6287+
{
6288+
item->setValue(gesture);
6289+
i->updateStateTimestamp();
6290+
i->setNeedSaveDatabase(true);
6291+
Event e(RSensors, RStateGesture, i->id(), item);
6292+
enqueueEvent(e);
6293+
}
6294+
6295+
if (gesture != -1 || buttonevent != -1) // something was updated
6296+
{
62726297
enqueueEvent(Event(RSensors, RStateLastUpdated, i->id()));
62736298
}
62746299

resource.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const char *RStateDaylight = "state/daylight";
5151
const char *RStateEffect = "state/effect";
5252
const char *RStateFire = "state/fire";
5353
const char *RStateFlag = "state/flag";
54+
const char *RStateGesture = "state/gesture";
5455
const char *RStateHue = "state/hue";
5556
const char *RStateHumidity = "state/humidity";
5657
const char *RStateLastUpdated = "state/lastupdated";
@@ -172,6 +173,7 @@ void initResourceDescriptors()
172173
rItemDescriptors.emplace_back(ResourceItemDescriptor(DataTypeString, RStateEffect));
173174
rItemDescriptors.emplace_back(ResourceItemDescriptor(DataTypeBool, RStateFire));
174175
rItemDescriptors.emplace_back(ResourceItemDescriptor(DataTypeBool, RStateFlag));
176+
rItemDescriptors.emplace_back(ResourceItemDescriptor(DataTypeInt32, RStateGesture));
175177
rItemDescriptors.emplace_back(ResourceItemDescriptor(DataTypeUInt16, RStateHue));
176178
rItemDescriptors.emplace_back(ResourceItemDescriptor(DataTypeUInt16, RStateHumidity, 0, 10000));
177179
rItemDescriptors.emplace_back(ResourceItemDescriptor(DataTypeTime, RStateLastUpdated));

resource.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ extern const char *RStateDaylight;
6666
extern const char *RStateEffect;
6767
extern const char *RStateFire;
6868
extern const char *RStateFlag;
69+
extern const char *RStateGesture;
6970
extern const char *RStateHue;
7071
extern const char *RStateHumidity;
7172
extern const char *RStateLastUpdated;

sensor.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@
3131
#define S_BUTTON_ACTION_TILT 9
3232
#define S_BUTTON_ACTION_MANY_PRESS 10
3333

34+
#define GESTURE_SHAKE 1
35+
#define GESTURE_DROP 2
36+
#define GESTURE_FLIP_90 3
37+
#define GESTURE_FLIP_180 4
38+
#define GESTURE_PUSH 5
39+
#define GESTURE_DOUBLE_TAP 6
40+
3441
#define S_BUTTON_1 1000
3542
#define S_BUTTON_2 2000
3643
#define S_BUTTON_3 3000

0 commit comments

Comments
 (0)