[BT][BREAKING] Change Switchbot API and add new commands (#2003)

Co-authored-by: Florian <1technophile@users.noreply.github.com>
This commit is contained in:
FlorianandFlorian authored and GitHub committed 2024-08-22 13:04:52 -05:00
1 parent 761e3e34f3
commit b0cf3c1dc7
3 files changed
+85 -14

No files matched your search

+58 -7
View File
@@ -375,7 +375,12 @@ The `ttl` parameter is the number of attempts to connect (defaults to 1), which
## SwitchBot Bot control (available with HA discovery)
SwitchBot Bot devices are automatically discovered and available as a device in the Home Assistant Settings menu.
SwitchBot Bot devices are automatically discovered and available as a device in the Home Assistant Settings menu. The following commands are supported:
* Press
* On
* Off
* Up
* Down
::: tip
If the SwitchBot mode is changed the ESP32 must be restarted.
@@ -383,13 +388,51 @@ If the SwitchBot mode is changed the ESP32 must be restarted.
The device can also be controlled over MQTT with a simplified BLE write command.
### Example command to set the SwitchBot Bot state to ON:
### Set the SwitchBot Bot state to ON:
```
mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT -m '{
"SBS1":"on",
"model_id":"X1",
"cmd":"on",
"id":"AA:BB:CC:DD:EE:FF"
}'
```
### Set the SwitchBot Bot state to OFF:
```
mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT -m '{
"model_id":"X1",
"cmd":"off",
"id":"AA:BB:CC:DD:EE:FF"
}'
```
### Set the SwitchBot Bot to PRESS:
```
mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT -m '{
"model_id":"X1",
"cmd":"press",
"id":"AA:BB:CC:DD:EE:FF"
}'
```
### Set the SwitchBot Bot to go UP:
```
mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT -m '{
"model_id":"X1",
"cmd":"up",
"id":"AA:BB:CC:DD:EE:FF"
}'
```
### Set the SwitchBot Bot to go DOWN:
```
mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT -m '{
"model_id":"X1",
"cmd":"down",
"id":"AA:BB:CC:DD:EE:FF"
}'
```
Response (assuming success):
```
{
@@ -407,33 +450,41 @@ SwitchBot Blind Tilt devices are automatically discovered and available as a dev
The device can also be controlled over MQTT with a simplified BLE write command.
### Example command to set the SwitchBot Blind Tilt state to OPEN:
### Set the SwitchBot Blind Tilt state to OPEN:
```
mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT -m '{
"model_id":"W270160X",
"tilt":"open",
"id":"AA:BB:CC:DD:EE:FF"
}'
```
### Example command to set the SwitchBot Blind Tilt state to CLOSE_DOWN:
### Set the SwitchBot Blind Tilt state to CLOSE_DOWN:
```
mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT -m '{
"model_id":"W270160X",
"tilt":"close_down",
"id":"AA:BB:CC:DD:EE:FF"
}'
```
### Example command to set the SwitchBot Blind Tilt state to CLOSE_UP:
### Set the SwitchBot Blind Tilt state to CLOSE_UP:
```
mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT -m '{
"model_id":"W270160X",
"tilt":"close_up",
"id":"AA:BB:CC:DD:EE:FF"
}'
```
### Example command to set the SwitchBot Blind Tilt state to a percentage (0-100):
### Set the SwitchBot Blind Tilt state to a percentage (0-100):
```
mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT -m '{
"model_id":"W270160X",
"tilt":55,
"id":"AA:BB:CC:DD:EE:FF"
}'
```
### Response (assuming success):
The Switchbot Blind Tilt response :
+11 -2
View File
@@ -453,6 +453,9 @@ bool SBS1_connect::processActions(std::vector<BLEAction>& actions) {
NimBLEUUID notifyCharUUID("cba20003-224d-11e6-9fb8-0002a5d5c51b");
static byte ON[] = {0x57, 0x01, 0x01};
static byte OFF[] = {0x57, 0x01, 0x02};
static byte PRESS[] = {0x57, 0x01, 0x00};
static byte DOWN[] = {0x57, 0x01, 0x03};
static byte UP[] = {0x57, 0x01, 0x04};
bool result = false;
if (actions.size() > 0) {
@@ -470,8 +473,14 @@ bool SBS1_connect::processActions(std::vector<BLEAction>& actions) {
true)) {
if (it.value == "on") {
result = pChar->writeValue(ON, 3, false);
} else {
} else if (it.value == "off") {
result = pChar->writeValue(OFF, 3, false);
} else if (it.value == "press") {
result = pChar->writeValue(PRESS, 3, false);
} else if (it.value == "down") {
result = pChar->writeValue(DOWN, 3, false);
} else if (it.value == "up") {
result = pChar->writeValue(UP, 3, false);
}
if (result) {
@@ -489,7 +498,7 @@ bool SBS1_connect::processActions(std::vector<BLEAction>& actions) {
StaticJsonDocument<JSON_MSG_BUFFER> BLEdataBuffer;
JsonObject BLEdata = BLEdataBuffer.to<JsonObject>();
BLEdata["id"] = it.addr;
BLEdata["state"] = result ? it.value : it.value == "on" ? "off" : "on";
BLEdata["state"] = it.value;
buildTopicFromId(BLEdata, subjectBTtoMQTT);
handleJsonEnqueue(BLEdata, QueueSemaphoreTimeOutTask);
}
+16 -5
View File
@@ -1061,8 +1061,8 @@ void launchBTDiscovery(bool overrideDiscovery) {
String unique_id = macWOdots + "-" + String(prop.key().c_str());
String value_template = "{{ value_json." + String(prop.key().c_str()) + " | is_defined }}";
if (p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::SBS1 && strcmp(prop.key().c_str(), "state") == 0) {
String payload_on = "{\"SBS1\":\"on\",\"id\":\"" + String(p->macAdr) + "\"}";
String payload_off = "{\"SBS1\":\"off\",\"id\":\"" + String(p->macAdr) + "\"}";
String payload_on = "{\"model_id\":\"X1\",\"cmd\":\"on\",\"id\":\"" + String(p->macAdr) + "\"}";
String payload_off = "{\"model_id\":\"X1\",\"cmd\":\"off\",\"id\":\"" + String(p->macAdr) + "\"}";
createDiscovery("switch", //set Type
discovery_topic.c_str(), entity_name.c_str(), unique_id.c_str(),
will_Topic, "switch", value_template.c_str(),
@@ -1070,6 +1070,17 @@ void launchBTDiscovery(bool overrideDiscovery) {
Gateway_AnnouncementMsg, will_Message, false, subjectMQTTtoBT,
model.c_str(), brand.c_str(), model_id.c_str(), macWOdots.c_str(), false,
stateClassNone, "off", "on");
unique_id = macWOdots + "-press";
entity_name = String(model_id.c_str()) + "-press";
String payload_press = "{\"model_id\":\"X1\",\"cmd\":\"press\",\"id\":\"" + String(p->macAdr) + "\"}";
createDiscovery("button", //set Type
discovery_topic.c_str(), entity_name.c_str(), unique_id.c_str(),
will_Topic, "button", "",
payload_press.c_str(), "", "", //set,payload_on,payload_off,unit_of_meas,
0, //set off_delay
Gateway_AnnouncementMsg, will_Message, false, subjectMQTTtoBT,
model.c_str(), brand.c_str(), model_id.c_str(), macWOdots.c_str(), false,
stateClassNone);
} else if (p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::SBBT && strcmp(prop.key().c_str(), "open") == 0) {
value_template = "{% if value_json.direction == \"up\" -%} {{ 100 - value_json.open/2 }}{% elif value_json.direction == \"down\" %}{{ value_json.open/2 }}{% else %} {{ value_json.open/2 }}{%- endif %}";
String command_template = "{\"model_id\":\"W270160X\",\"tilt\":{{ value | int }},\"id\":\"" + String(p->macAdr) + "\"}";
@@ -1400,12 +1411,12 @@ void startBTActionTask() {
void MQTTtoBTAction(JsonObject& BTdata) {
BLEAction action;
memset(&action, 0, sizeof(BLEAction));
if (BTdata.containsKey("SBS1")) {
if (BTdata.containsKey("model_id") && BTdata["model_id"].is<const char*>() && BTdata["model_id"] == "X1") {
strcpy(action.addr, (const char*)BTdata["id"]);
action.write = true;
std::string val = BTdata["SBS1"].as<std::string>(); // Fix #1694
std::string val = BTdata["cmd"].as<std::string>(); // Fix #1694
action.value = val;
action.ttl = 1;
action.ttl = 3;
createOrUpdateDevice(action.addr, device_flags_connect,
TheengsDecoder::BLE_ID_NUM::SBS1, 1);
BLEactions.push_back(action);