Add optional bindkey support for CGG1. (#1407)

This commit is contained in:
Diego Elio Pettenò
2021-03-25 18:33:06 +00:00
committed by GitHub
parent 46356cbc4a
commit a6255c31fe
4 changed files with 27 additions and 4 deletions
+16 -2
View File
@@ -10,6 +10,7 @@ static const char *TAG = "xiaomi_cgg1";
void XiaomiCGG1::dump_config() {
ESP_LOGCONFIG(TAG, "Xiaomi CGG1");
ESP_LOGCONFIG(TAG, " Bindkey: %s", hexencode(this->bindkey_, 16).c_str());
LOG_SENSOR(" ", "Temperature", this->temperature_);
LOG_SENSOR(" ", "Humidity", this->humidity_);
LOG_SENSOR(" ", "Battery Level", this->battery_level_);
@@ -31,8 +32,9 @@ bool XiaomiCGG1::parse_device(const esp32_ble_tracker::ESPBTDevice &device) {
if (res->is_duplicate) {
continue;
}
if (res->has_encryption) {
ESP_LOGVV(TAG, "parse_device(): payload decryption is currently not supported on this device.");
if (res->has_encryption &&
(!(xiaomi_ble::decrypt_xiaomi_payload(const_cast<std::vector<uint8_t> &>(service_data.data), this->bindkey_,
this->address_)))) {
continue;
}
if (!(xiaomi_ble::parse_xiaomi_message(service_data.data, *res))) {
@@ -57,6 +59,18 @@ bool XiaomiCGG1::parse_device(const esp32_ble_tracker::ESPBTDevice &device) {
return true;
}
void XiaomiCGG1::set_bindkey(const std::string &bindkey) {
memset(bindkey_, 0, 16);
if (bindkey.size() != 32) {
return;
}
char temp[3] = {0};
for (int i = 0; i < 16; i++) {
strncpy(temp, &(bindkey.c_str()[i * 2]), 2);
bindkey_[i] = std::strtoul(temp, NULL, 16);
}
}
} // namespace xiaomi_cgg1
} // namespace esphome