2019-10-16 13:29:56 +02:00
|
|
|
#include "xiaomi_cgg1.h"
|
2019-04-17 12:06:00 +02:00
|
|
|
#include "esphome/core/log.h"
|
|
|
|
|
2021-09-20 11:47:51 +02:00
|
|
|
#ifdef USE_ESP32
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
namespace esphome {
|
2019-10-16 13:29:56 +02:00
|
|
|
namespace xiaomi_cgg1 {
|
2019-04-17 12:06:00 +02:00
|
|
|
|
2021-06-10 22:19:44 +02:00
|
|
|
static const char *const TAG = "xiaomi_cgg1";
|
2019-04-17 12:06:00 +02:00
|
|
|
|
2019-10-16 13:29:56 +02:00
|
|
|
void XiaomiCGG1::dump_config() {
|
|
|
|
ESP_LOGCONFIG(TAG, "Xiaomi CGG1");
|
2021-12-12 21:15:23 +01:00
|
|
|
ESP_LOGCONFIG(TAG, " Bindkey: %s", format_hex_pretty(this->bindkey_, 16).c_str());
|
2019-04-17 12:06:00 +02:00
|
|
|
LOG_SENSOR(" ", "Temperature", this->temperature_);
|
|
|
|
LOG_SENSOR(" ", "Humidity", this->humidity_);
|
|
|
|
LOG_SENSOR(" ", "Battery Level", this->battery_level_);
|
|
|
|
}
|
|
|
|
|
2020-05-27 00:33:28 +02:00
|
|
|
bool XiaomiCGG1::parse_device(const esp32_ble_tracker::ESPBTDevice &device) {
|
|
|
|
if (device.address_uint64() != this->address_) {
|
|
|
|
ESP_LOGVV(TAG, "parse_device(): unknown MAC address.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ESP_LOGVV(TAG, "parse_device(): MAC address %s found.", device.address_str().c_str());
|
|
|
|
|
|
|
|
bool success = false;
|
|
|
|
for (auto &service_data : device.get_service_datas()) {
|
|
|
|
auto res = xiaomi_ble::parse_xiaomi_header(service_data);
|
|
|
|
if (!res.has_value()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (res->is_duplicate) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-03-25 19:33:06 +01:00
|
|
|
if (res->has_encryption &&
|
|
|
|
(!(xiaomi_ble::decrypt_xiaomi_payload(const_cast<std::vector<uint8_t> &>(service_data.data), this->bindkey_,
|
|
|
|
this->address_)))) {
|
2020-05-27 00:33:28 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!(xiaomi_ble::parse_xiaomi_message(service_data.data, *res))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!(xiaomi_ble::report_xiaomi_results(res, device.address_str()))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (res->temperature.has_value() && this->temperature_ != nullptr)
|
|
|
|
this->temperature_->publish_state(*res->temperature);
|
|
|
|
if (res->humidity.has_value() && this->humidity_ != nullptr)
|
|
|
|
this->humidity_->publish_state(*res->humidity);
|
|
|
|
if (res->battery_level.has_value() && this->battery_level_ != nullptr)
|
|
|
|
this->battery_level_->publish_state(*res->battery_level);
|
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
|
2021-09-13 18:11:27 +02:00
|
|
|
return success;
|
2020-05-27 00:33:28 +02:00
|
|
|
}
|
|
|
|
|
2021-03-25 19:33:06 +01:00
|
|
|
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);
|
2021-09-13 18:11:27 +02:00
|
|
|
bindkey_[i] = std::strtoul(temp, nullptr, 16);
|
2021-03-25 19:33:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-16 13:29:56 +02:00
|
|
|
} // namespace xiaomi_cgg1
|
2019-04-17 12:06:00 +02:00
|
|
|
} // namespace esphome
|
|
|
|
|
|
|
|
#endif
|