[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

+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);