ESPHome 2026.10.0-dev
Loading...
Searching...
No Matches
xiaomi_rtcgq02lm.cpp
Go to the documentation of this file.
1#include "xiaomi_rtcgq02lm.h"
3#include "esphome/core/log.h"
4
6
7static const char *const TAG = "xiaomi_rtcgq02lm";
8
9static constexpr size_t RTCGQ02LM_BINDKEY_SIZE = 16;
10
12 char bindkey_hex[format_hex_pretty_size(RTCGQ02LM_BINDKEY_SIZE)];
13 ESP_LOGCONFIG(TAG, "Xiaomi RTCGQ02LM");
14 ESP_LOGCONFIG(TAG, " Bindkey: %s", format_hex_pretty_to(bindkey_hex, this->bindkey_, RTCGQ02LM_BINDKEY_SIZE, '.'));
15#ifdef USE_BINARY_SENSOR
16 LOG_BINARY_SENSOR(" ", "Motion", this->motion_);
17 LOG_BINARY_SENSOR(" ", "Light", this->light_);
18 LOG_BINARY_SENSOR(" ", "Button", this->button_);
19#endif
20#ifdef USE_SENSOR
21 LOG_SENSOR(" ", "Battery Level", this->battery_level_);
22#endif
23}
24
26 if (device.address_uint64() != this->address_) {
27 ESP_LOGVV(TAG, "parse_device(): unknown MAC address.");
28 return false;
29 }
30 char addr_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
31 const char *addr_str = device.address_str_to(addr_buf);
32 ESP_LOGVV(TAG, "parse_device(): MAC address %s found.", addr_str);
33
34 bool success = false;
35 for (auto &service_data : device.get_service_datas()) {
36 auto res = xiaomi_ble::parse_xiaomi_header(service_data);
37 if (!res.has_value()) {
38 continue;
39 }
40 if (res->is_duplicate) {
41 continue;
42 }
43 if (res->has_encryption &&
44 (!(xiaomi_ble::decrypt_xiaomi_payload(const_cast<std::vector<uint8_t> &>(service_data.data), this->bindkey_,
45 this->address_)))) {
46 continue;
47 }
48 if (!(xiaomi_ble::parse_xiaomi_message(service_data.data, *res))) {
49 continue;
50 }
51
52 if (!(xiaomi_ble::report_xiaomi_results(res, addr_str))) {
53 continue;
54 }
55#ifdef USE_BINARY_SENSOR
56 if (res->has_motion.has_value() && this->motion_ != nullptr) {
57 this->motion_->publish_state(*res->has_motion);
58 this->set_timeout("motion_timeout", this->motion_timeout_, [this]() { this->motion_->publish_state(false); });
59 }
60 if (res->is_light.has_value() && this->light_ != nullptr)
61 this->light_->publish_state(*res->is_light);
62 if (res->button_press.has_value() && this->button_ != nullptr) {
63 this->button_->publish_state(*res->button_press);
64 this->set_timeout("button_timeout", this->button_timeout_, [this]() { this->button_->publish_state(false); });
65 }
66#endif
67#ifdef USE_SENSOR
68 if (res->battery_level.has_value() && this->battery_level_ != nullptr)
69 this->battery_level_->publish_state(*res->battery_level);
70#endif
71 success = true;
72 }
73
74 return success;
75}
76
77void XiaomiRTCGQ02LM::set_bindkey(const char *bindkey) { parse_hex(bindkey, this->bindkey_, sizeof(this->bindkey_)); }
78
79} // namespace esphome::xiaomi_rtcgq02lm
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
void publish_state(bool new_state)
Publish a new state to the front-end.
ESPDEPRECATED("Use address_str_to() instead. Removed in 2027.2.0.", "2026.8.0") std const char * address_str_to(char *buf) const
Return MAC as "XX:XX:XX:XX:XX:XX" string.
uint64_t address_uint64() const
Return MAC as packed uint64 (byte 0 in LSB — matches esp32's address_uint64).
const std::vector< ServiceData > & get_service_datas() const
Definition ble_device.h:225
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
binary_sensor::BinarySensor * motion_
binary_sensor::BinarySensor * button_
bool parse_device(const ble_device_base::ESPBTDevice &device) override
bool decrypt_xiaomi_payload(std::vector< uint8_t > &raw, const uint8_t *bindkey, const uint64_t &address)
bool parse_xiaomi_message(const std::vector< uint8_t > &message, XiaomiParseResult &result)
optional< XiaomiParseResult > parse_xiaomi_header(const ble_device_base::ServiceData &service_data)
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:293
char * format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator)
Format byte array as uppercase hex to buffer (base implementation).
Definition helpers.cpp:425
constexpr size_t format_hex_pretty_size(size_t byte_count)
Calculate buffer size needed for format_hex_pretty_to with separator: "XX:XX:...:XX\0".
Definition helpers.h:1438