ESPHome 2026.1.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
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 ESP_LOGVV(TAG, "parse_device(): MAC address %s found.", device.address_str().c_str());
34
35 bool success = false;
36 for (auto &service_data : device.get_service_datas()) {
37 auto res = xiaomi_ble::parse_xiaomi_header(service_data);
38 if (!res.has_value()) {
39 continue;
40 }
41 if (res->is_duplicate) {
42 continue;
43 }
44 if (res->has_encryption &&
45 (!(xiaomi_ble::decrypt_xiaomi_payload(const_cast<std::vector<uint8_t> &>(service_data.data), this->bindkey_,
46 this->address_)))) {
47 continue;
48 }
49 if (!(xiaomi_ble::parse_xiaomi_message(service_data.data, *res))) {
50 continue;
51 }
52
53 if (!(xiaomi_ble::report_xiaomi_results(res, device.address_str()))) {
54 continue;
55 }
56#ifdef USE_BINARY_SENSOR
57 if (res->has_motion.has_value() && this->motion_ != nullptr) {
58 this->motion_->publish_state(*res->has_motion);
59 this->set_timeout("motion_timeout", this->motion_timeout_,
60 [this, res]() { this->motion_->publish_state(false); });
61 }
62 if (res->is_light.has_value() && this->light_ != nullptr)
63 this->light_->publish_state(*res->is_light);
64 if (res->button_press.has_value() && this->button_ != nullptr) {
65 this->button_->publish_state(*res->button_press);
66 this->set_timeout("button_timeout", this->button_timeout_,
67 [this, res]() { this->button_->publish_state(false); });
68 }
69#endif
70#ifdef USE_SENSOR
71 if (res->battery_level.has_value() && this->battery_level_ != nullptr)
72 this->battery_level_->publish_state(*res->battery_level);
73#endif
74 success = true;
75 }
76
77 return success;
78}
79
80void XiaomiRTCGQ02LM::set_bindkey(const std::string &bindkey) {
81 memset(bindkey_, 0, 16);
82 if (bindkey.size() != 32) {
83 return;
84 }
85 char temp[3] = {0};
86 for (int i = 0; i < 16; i++) {
87 strncpy(temp, &(bindkey.c_str()[i * 2]), 2);
88 bindkey_[i] = std::strtoul(temp, nullptr, 16);
89 }
90}
91
92} // namespace xiaomi_rtcgq02lm
93} // namespace esphome
94
95#endif
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
void publish_state(bool new_state)
Publish a new state to the front-end.
const std::vector< ServiceData > & get_service_datas() const
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:77
binary_sensor::BinarySensor * motion_
binary_sensor::BinarySensor * button_
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
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:331
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:735