[BT][BREAKING] Change Switchbot API and add new commands (#2003)
Co-authored-by: Florian <1technophile@users.noreply.github.com>
This commit is contained in:
1 parent
761e3e34f3
commit
b0cf3c1dc7
3 files changed
+85
-14
No files matched your search
+58
-7
@@ -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 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
|
::: tip
|
||||||
If the SwitchBot mode is changed the ESP32 must be restarted.
|
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.
|
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 '{
|
mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT -m '{
|
||||||
"SBS1":"on",
|
"model_id":"X1",
|
||||||
|
"cmd":"on",
|
||||||
"id":"AA:BB:CC:DD:EE:FF"
|
"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):
|
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.
|
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 '{
|
mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT -m '{
|
||||||
"model_id":"W270160X",
|
"model_id":"W270160X",
|
||||||
"tilt":"open",
|
"tilt":"open",
|
||||||
"id":"AA:BB:CC:DD:EE:FF"
|
"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 '{
|
mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT -m '{
|
||||||
"model_id":"W270160X",
|
"model_id":"W270160X",
|
||||||
"tilt":"close_down",
|
"tilt":"close_down",
|
||||||
"id":"AA:BB:CC:DD:EE:FF"
|
"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 '{
|
mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT -m '{
|
||||||
"model_id":"W270160X",
|
"model_id":"W270160X",
|
||||||
"tilt":"close_up",
|
"tilt":"close_up",
|
||||||
"id":"AA:BB:CC:DD:EE:FF"
|
"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 '{
|
mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT -m '{
|
||||||
"model_id":"W270160X",
|
"model_id":"W270160X",
|
||||||
"tilt":55,
|
"tilt":55,
|
||||||
"id":"AA:BB:CC:DD:EE:FF"
|
"id":"AA:BB:CC:DD:EE:FF"
|
||||||
}'
|
}'
|
||||||
|
```
|
||||||
|
|
||||||
### Response (assuming success):
|
### Response (assuming success):
|
||||||
The Switchbot Blind Tilt response :
|
The Switchbot Blind Tilt response :
|
||||||
|
|||||||
@@ -453,6 +453,9 @@ bool SBS1_connect::processActions(std::vector<BLEAction>& actions) {
|
|||||||
NimBLEUUID notifyCharUUID("cba20003-224d-11e6-9fb8-0002a5d5c51b");
|
NimBLEUUID notifyCharUUID("cba20003-224d-11e6-9fb8-0002a5d5c51b");
|
||||||
static byte ON[] = {0x57, 0x01, 0x01};
|
static byte ON[] = {0x57, 0x01, 0x01};
|
||||||
static byte OFF[] = {0x57, 0x01, 0x02};
|
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;
|
bool result = false;
|
||||||
if (actions.size() > 0) {
|
if (actions.size() > 0) {
|
||||||
@@ -470,8 +473,14 @@ bool SBS1_connect::processActions(std::vector<BLEAction>& actions) {
|
|||||||
true)) {
|
true)) {
|
||||||
if (it.value == "on") {
|
if (it.value == "on") {
|
||||||
result = pChar->writeValue(ON, 3, false);
|
result = pChar->writeValue(ON, 3, false);
|
||||||
} else {
|
} else if (it.value == "off") {
|
||||||
result = pChar->writeValue(OFF, 3, false);
|
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) {
|
if (result) {
|
||||||
@@ -489,7 +498,7 @@ bool SBS1_connect::processActions(std::vector<BLEAction>& actions) {
|
|||||||
StaticJsonDocument<JSON_MSG_BUFFER> BLEdataBuffer;
|
StaticJsonDocument<JSON_MSG_BUFFER> BLEdataBuffer;
|
||||||
JsonObject BLEdata = BLEdataBuffer.to<JsonObject>();
|
JsonObject BLEdata = BLEdataBuffer.to<JsonObject>();
|
||||||
BLEdata["id"] = it.addr;
|
BLEdata["id"] = it.addr;
|
||||||
BLEdata["state"] = result ? it.value : it.value == "on" ? "off" : "on";
|
BLEdata["state"] = it.value;
|
||||||
buildTopicFromId(BLEdata, subjectBTtoMQTT);
|
buildTopicFromId(BLEdata, subjectBTtoMQTT);
|
||||||
handleJsonEnqueue(BLEdata, QueueSemaphoreTimeOutTask);
|
handleJsonEnqueue(BLEdata, QueueSemaphoreTimeOutTask);
|
||||||
}
|
}
|
||||||
|
|||||||
+16
-5
@@ -1061,8 +1061,8 @@ void launchBTDiscovery(bool overrideDiscovery) {
|
|||||||
String unique_id = macWOdots + "-" + String(prop.key().c_str());
|
String unique_id = macWOdots + "-" + String(prop.key().c_str());
|
||||||
String value_template = "{{ value_json." + String(prop.key().c_str()) + " | is_defined }}";
|
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) {
|
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_on = "{\"model_id\":\"X1\",\"cmd\":\"on\",\"id\":\"" + String(p->macAdr) + "\"}";
|
||||||
String payload_off = "{\"SBS1\":\"off\",\"id\":\"" + String(p->macAdr) + "\"}";
|
String payload_off = "{\"model_id\":\"X1\",\"cmd\":\"off\",\"id\":\"" + String(p->macAdr) + "\"}";
|
||||||
createDiscovery("switch", //set Type
|
createDiscovery("switch", //set Type
|
||||||
discovery_topic.c_str(), entity_name.c_str(), unique_id.c_str(),
|
discovery_topic.c_str(), entity_name.c_str(), unique_id.c_str(),
|
||||||
will_Topic, "switch", value_template.c_str(),
|
will_Topic, "switch", value_template.c_str(),
|
||||||
@@ -1070,6 +1070,17 @@ void launchBTDiscovery(bool overrideDiscovery) {
|
|||||||
Gateway_AnnouncementMsg, will_Message, false, subjectMQTTtoBT,
|
Gateway_AnnouncementMsg, will_Message, false, subjectMQTTtoBT,
|
||||||
model.c_str(), brand.c_str(), model_id.c_str(), macWOdots.c_str(), false,
|
model.c_str(), brand.c_str(), model_id.c_str(), macWOdots.c_str(), false,
|
||||||
stateClassNone, "off", "on");
|
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) {
|
} 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 %}";
|
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) + "\"}";
|
String command_template = "{\"model_id\":\"W270160X\",\"tilt\":{{ value | int }},\"id\":\"" + String(p->macAdr) + "\"}";
|
||||||
@@ -1400,12 +1411,12 @@ void startBTActionTask() {
|
|||||||
void MQTTtoBTAction(JsonObject& BTdata) {
|
void MQTTtoBTAction(JsonObject& BTdata) {
|
||||||
BLEAction action;
|
BLEAction action;
|
||||||
memset(&action, 0, sizeof(BLEAction));
|
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"]);
|
strcpy(action.addr, (const char*)BTdata["id"]);
|
||||||
action.write = true;
|
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.value = val;
|
||||||
action.ttl = 1;
|
action.ttl = 3;
|
||||||
createOrUpdateDevice(action.addr, device_flags_connect,
|
createOrUpdateDevice(action.addr, device_flags_connect,
|
||||||
TheengsDecoder::BLE_ID_NUM::SBS1, 1);
|
TheengsDecoder::BLE_ID_NUM::SBS1, 1);
|
||||||
BLEactions.push_back(action);
|
BLEactions.push_back(action);
|
||||||
|
|||||||
Reference in new issue
Block a user