ESPHome 2026.1.5
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
5#ifdef USE_ESP32
6
7namespace esphome {
8namespace xiaomi_rtcgq02lm {
9
10static const char *const TAG = "xiaomi_rtcgq02lm";
11
12static constexpr size_t RTCGQ02LM_BINDKEY_SIZE = 16;
13
15 char bindkey_hex[format_hex_pretty_size(RTCGQ02LM_BINDKEY_SIZE)];
16 ESP_LOGCONFIG(TAG, "Xiaomi RTCGQ02LM");
17 ESP_LOGCONFIG(TAG, " Bindkey: %s", format_hex_pretty_to(bindkey_hex, this->bindkey_, RTCGQ02LM_BINDKEY_SIZE, '.'));
18#ifdef USE_BINARY_SENSOR
19 LOG_BINARY_SENSOR(" ", "Motion", this->motion_);
20 LOG_BINARY_SENSOR(" ", "Light", this->light_);
21 LOG_BINARY_SENSOR(" ", "Button", this->button_);
22#endif
23#ifdef USE_SENSOR
24 LOG_SENSOR(" ", "Battery Level", this->battery_level_);
25#endif
26}
27
29 if (device.address_uint64() != this->address_) {
30 ESP_LOGVV(TAG, "parse_device(): unknown MAC address.");
31 return false;
32 }
33 char addr_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
34 const char *addr_str = device.address_str_to(addr_buf);
35 ESP_LOGVV(TAG, "parse_device(): MAC address %s found.", addr_str);
36
37 bool success = false;
38 for (auto &service_data : device.get_service_datas()) {
39 auto res = xiaomi_ble::parse_xiaomi_header(service_data);
40 if (!res.has_value()) {
41 continue;
42 }
43 if (res->is_duplicate) {
44 continue;
45 }
46 if (res->has_encryption &&
47 (!(xiaomi_ble::decrypt_xiaomi_payload(const_cast<std::vector<uint8_t> &>(service_data.data), this->bindkey_,
48 this->address_)))) {
49 continue;
50 }
51 if (!(xiaomi_ble::parse_xiaomi_message(service_data.data, *res))) {
52 continue;
53 }
54
55 if (!(xiaomi_ble::report_xiaomi_results(res, addr_str))) {
56 continue;
57 }
58#ifdef USE_BINARY_SENSOR
59 if (res->has_motion.has_value() && this->motion_ != nullptr) {
60 this->motion_->publish_state(*res->has_motion);
61 this->set_timeout("motion_timeout", this->motion_timeout_,
62 [this, res]() { this->motion_->publish_state(false); });
63 }
64 if (res->is_light.has_value() && this->light_ != nullptr)
65 this->light_->publish_state(*res->is_light);
66 if (res->button_press.has_value() && this->button_ != nullptr) {
67 this->button_->publish_state(*res->button_press);
68 this->set_timeout("button_timeout", this->button_timeout_,
69 [this, res]() { this->button_->publish_state(false); });
70 }
71#endif
72#ifdef USE_SENSOR
73 if (res->battery_level.has_value() && this->battery_level_ != nullptr)
74 this->battery_level_->publish_state(*res->battery_level);
75#endif
76 success = true;
77 }
78
79 return success;
80}
81
82void XiaomiRTCGQ02LM::set_bindkey(const char *bindkey) { parse_hex(bindkey, this->bindkey_, sizeof(this->bindkey_)); }
83
84} // namespace xiaomi_rtcgq02lm
85} // namespace esphome
86
87#endif
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:445
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:76
binary_sensor::BinarySensor * motion_
binary_sensor::BinarySensor * button_
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 char *address)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
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:275
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:334
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:830