ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
xiaomi_cgpr1.cpp
Go to the documentation of this file.
1#include "xiaomi_cgpr1.h"
3#include "esphome/core/log.h"
4
5#ifdef USE_ESP32
6
8
9static const char *const TAG = "xiaomi_cgpr1";
10
12 ESP_LOGCONFIG(TAG, "Xiaomi CGPR1");
13 LOG_BINARY_SENSOR(" ", "Motion", this);
14 LOG_SENSOR(" ", "Idle Time", this->idle_time_);
15 LOG_SENSOR(" ", "Battery Level", this->battery_level_);
16 LOG_SENSOR(" ", "Illuminance", this->illuminance_);
17}
18
20 if (device.address_uint64() != this->address_) {
21 ESP_LOGVV(TAG, "parse_device(): unknown MAC address.");
22 return false;
23 }
24 char addr_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
25 const char *addr_str = device.address_str_to(addr_buf);
26 ESP_LOGVV(TAG, "parse_device(): MAC address %s found.", addr_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, addr_str))) {
46 continue;
47 }
48 if (res->idle_time.has_value() && this->idle_time_ != nullptr)
49 this->idle_time_->publish_state(*res->idle_time);
50 if (res->battery_level.has_value() && this->battery_level_ != nullptr)
51 this->battery_level_->publish_state(*res->battery_level);
52 if (res->illuminance.has_value() && this->illuminance_ != nullptr)
53 this->illuminance_->publish_state(*res->illuminance);
54 if (res->has_motion.has_value())
55 this->publish_state(*res->has_motion);
56 success = true;
57 }
58
59 return success;
60}
61
62void XiaomiCGPR1::set_bindkey(const char *bindkey) { parse_hex(bindkey, this->bindkey_, sizeof(this->bindkey_)); }
63
64} // namespace esphome::xiaomi_cgpr1
65
66#endif
void publish_state(bool new_state)
Publish a new state to the front-end.
const char * address_str_to(std::span< char, MAC_ADDRESS_PRETTY_BUFFER_SIZE > buf) const
Format MAC address into provided buffer, returns pointer to buffer for convenience.
const std::vector< ServiceData > & get_service_datas() const
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override
void set_bindkey(const char *bindkey)
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 char *address)
size_t parse_hex(const char *str, size_t length, uint8_t *data, size_t count)
Parse bytes from a hex-encoded string into a byte array.
Definition helpers.cpp:274