ESPHome 2025.6.0
Loading...
Searching...
No Matches
xiaomi_cgd1.cpp
Go to the documentation of this file.
1#include "xiaomi_cgd1.h"
2#include "esphome/core/log.h"
3
4#ifdef USE_ESP32
5
6namespace esphome {
7namespace xiaomi_cgd1 {
8
9static const char *const TAG = "xiaomi_cgd1";
10
12 ESP_LOGCONFIG(TAG,
13 "Xiaomi CGD1\n"
14 " Bindkey: %s",
15 format_hex_pretty(this->bindkey_, 16).c_str());
16 LOG_SENSOR(" ", "Temperature", this->temperature_);
17 LOG_SENSOR(" ", "Humidity", this->humidity_);
18 LOG_SENSOR(" ", "Battery Level", this->battery_level_);
19}
20
22 if (device.address_uint64() != this->address_) {
23 ESP_LOGVV(TAG, "parse_device(): unknown MAC address.");
24 return false;
25 }
26 ESP_LOGVV(TAG, "parse_device(): MAC address %s found.", device.address_str().c_str());
27
28 bool success = false;
29 for (auto &service_data : device.get_service_datas()) {
30 auto res = xiaomi_ble::parse_xiaomi_header(service_data);
31 if (!res.has_value()) {
32 continue;
33 }
34 if (res->is_duplicate) {
35 continue;
36 }
37 if (res->has_encryption &&
38 (!(xiaomi_ble::decrypt_xiaomi_payload(const_cast<std::vector<uint8_t> &>(service_data.data), this->bindkey_,
39 this->address_)))) {
40 continue;
41 }
42 if (!(xiaomi_ble::parse_xiaomi_message(service_data.data, *res))) {
43 continue;
44 }
45 if (!(xiaomi_ble::report_xiaomi_results(res, device.address_str()))) {
46 continue;
47 }
48 if (res->temperature.has_value() && this->temperature_ != nullptr)
49 this->temperature_->publish_state(*res->temperature);
50 if (res->humidity.has_value() && this->humidity_ != nullptr)
51 this->humidity_->publish_state(*res->humidity);
52 if (res->battery_level.has_value() && this->battery_level_ != nullptr)
53 this->battery_level_->publish_state(*res->battery_level);
54 success = true;
55 }
56
57 return success;
58}
59
60void XiaomiCGD1::set_bindkey(const std::string &bindkey) {
61 memset(bindkey_, 0, 16);
62 if (bindkey.size() != 32) {
63 return;
64 }
65 char temp[3] = {0};
66 for (int i = 0; i < 16; i++) {
67 strncpy(temp, &(bindkey.c_str()[i * 2]), 2);
68 bindkey_[i] = std::strtoul(temp, nullptr, 16);
69 }
70}
71
72} // namespace xiaomi_cgd1
73} // namespace esphome
74
75#endif
const std::vector< ServiceData > & get_service_datas() const
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:39
void set_bindkey(const std::string &bindkey)
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override
bool decrypt_xiaomi_payload(std::vector< uint8_t > &raw, const uint8_t *bindkey, const uint64_t &address)
optional< XiaomiParseResult > parse_xiaomi_header(const esp32_ble_tracker::ServiceData &service_data)
bool parse_xiaomi_message(const std::vector< uint8_t > &message, XiaomiParseResult &result)
bool report_xiaomi_results(const optional< XiaomiParseResult > &result, const std::string &address)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string format_hex_pretty(const uint8_t *data, size_t length)
Format the byte array data of length len in pretty-printed, human-readable hex.
Definition helpers.cpp:372