ESPHome  2022.12.8
xiaomi_rtcgq02lm.cpp
Go to the documentation of this file.
1 #include "xiaomi_rtcgq02lm.h"
2 #include "esphome/core/log.h"
3 
4 #ifdef USE_ESP32
5 
6 namespace esphome {
7 namespace xiaomi_rtcgq02lm {
8 
9 static const char *const TAG = "xiaomi_rtcgq02lm";
10 
12  ESP_LOGCONFIG(TAG, "Xiaomi RTCGQ02LM");
13  ESP_LOGCONFIG(TAG, " Bindkey: %s", format_hex_pretty(this->bindkey_, 16).c_str());
14 #ifdef USE_BINARY_SENSOR
15  LOG_BINARY_SENSOR(" ", "Motion", this->motion_);
16  LOG_BINARY_SENSOR(" ", "Light", this->light_);
17  LOG_BINARY_SENSOR(" ", "Button", this->button_);
18 #endif
19 #ifdef USE_SENSOR
20  LOG_SENSOR(" ", "Battery Level", this->battery_level_);
21 #endif
22 }
23 
25  if (device.address_uint64() != this->address_) {
26  ESP_LOGVV(TAG, "parse_device(): unknown MAC address.");
27  return false;
28  }
29  ESP_LOGVV(TAG, "parse_device(): MAC address %s found.", device.address_str().c_str());
30 
31  bool success = false;
32  for (auto &service_data : device.get_service_datas()) {
33  auto res = xiaomi_ble::parse_xiaomi_header(service_data);
34  if (!res.has_value()) {
35  continue;
36  }
37  if (res->is_duplicate) {
38  continue;
39  }
40  if (res->has_encryption &&
41  (!(xiaomi_ble::decrypt_xiaomi_payload(const_cast<std::vector<uint8_t> &>(service_data.data), this->bindkey_,
42  this->address_)))) {
43  continue;
44  }
45  if (!(xiaomi_ble::parse_xiaomi_message(service_data.data, *res))) {
46  continue;
47  }
48 
49  if (!(xiaomi_ble::report_xiaomi_results(res, device.address_str()))) {
50  continue;
51  }
52 #ifdef USE_BINARY_SENSOR
53  if (res->has_motion.has_value() && this->motion_ != nullptr) {
54  this->motion_->publish_state(*res->has_motion);
55  this->set_timeout("motion_timeout", this->motion_timeout_,
56  [this, res]() { this->motion_->publish_state(false); });
57  }
58  if (res->is_light.has_value() && this->light_ != nullptr)
59  this->light_->publish_state(*res->is_light);
60  if (res->button_press.has_value() && this->button_ != nullptr) {
61  this->button_->publish_state(*res->button_press);
62  this->set_timeout("button_timeout", this->button_timeout_,
63  [this, res]() { this->button_->publish_state(false); });
64  }
65 #endif
66 #ifdef USE_SENSOR
67  if (res->battery_level.has_value() && this->battery_level_ != nullptr)
68  this->battery_level_->publish_state(*res->battery_level);
69 #endif
70  success = true;
71  }
72 
73  return success;
74 }
75 
76 void XiaomiRTCGQ02LM::set_bindkey(const std::string &bindkey) {
77  memset(bindkey_, 0, 16);
78  if (bindkey.size() != 32) {
79  return;
80  }
81  char temp[3] = {0};
82  for (int i = 0; i < 16; i++) {
83  strncpy(temp, &(bindkey.c_str()[i * 2]), 2);
84  bindkey_[i] = std::strtoul(temp, nullptr, 16);
85  }
86 }
87 
88 } // namespace xiaomi_rtcgq02lm
89 } // namespace esphome
90 
91 #endif
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:237
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition: component.cpp:68
binary_sensor::BinarySensor * motion_
binary_sensor::BinarySensor * button_
bool decrypt_xiaomi_payload(std::vector< uint8_t > &raw, const uint8_t *bindkey, const uint64_t &address)
Definition: xiaomi_ble.cpp:234
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:72
void publish_state(bool state)
Publish a new state to the front-end.
const std::vector< ServiceData > & get_service_datas() const
void set_bindkey(const std::string &bindkey)
bool parse_xiaomi_message(const std::vector< uint8_t > &message, XiaomiParseResult &result)
Definition: xiaomi_ble.cpp:90
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override
optional< XiaomiParseResult > parse_xiaomi_header(const esp32_ble_tracker::ServiceData &service_data)
Definition: xiaomi_ble.cpp:138
bool report_xiaomi_results(const optional< XiaomiParseResult > &result, const std::string &address)
Definition: xiaomi_ble.cpp:322
Definition: a4988.cpp:4
binary_sensor::BinarySensor * light_