ESPHome  2024.11.2
xiaomi_lywsd03mmc.cpp
Go to the documentation of this file.
1 #include "xiaomi_lywsd03mmc.h"
2 #include "esphome/core/log.h"
3 
4 #ifdef USE_ESP32
5 
6 namespace esphome {
7 namespace xiaomi_lywsd03mmc {
8 
9 static const char *const TAG = "xiaomi_lywsd03mmc";
10 
12  ESP_LOGCONFIG(TAG, "Xiaomi LYWSD03MMC");
13  ESP_LOGCONFIG(TAG, " Bindkey: %s", format_hex_pretty(this->bindkey_, 16).c_str());
14  LOG_SENSOR(" ", "Temperature", this->temperature_);
15  LOG_SENSOR(" ", "Humidity", this->humidity_);
16  LOG_SENSOR(" ", "Battery Level", this->battery_level_);
17 }
18 
20  if (device.address_uint64() != this->address_) {
21  ESP_LOGVV(TAG, "parse_device(): unknown MAC address.");
22  return false;
23  }
24  ESP_LOGVV(TAG, "parse_device(): MAC address %s found.", device.address_str().c_str());
25 
26  bool success = false;
27  for (auto &service_data : device.get_service_datas()) {
28  auto res = xiaomi_ble::parse_xiaomi_header(service_data);
29  if (!res.has_value()) {
30  continue;
31  }
32  if (res->is_duplicate) {
33  continue;
34  }
35  if (res->has_encryption &&
36  (!(xiaomi_ble::decrypt_xiaomi_payload(const_cast<std::vector<uint8_t> &>(service_data.data), this->bindkey_,
37  this->address_)))) {
38  continue;
39  }
40  if (!(xiaomi_ble::parse_xiaomi_message(service_data.data, *res))) {
41  continue;
42  }
43  if (res->humidity.has_value() && this->humidity_ != nullptr) {
44  // see https://github.com/custom-components/sensor.mitemp_bt/issues/7#issuecomment-595948254
45  *res->humidity = trunc(*res->humidity);
46  }
47  if (!(xiaomi_ble::report_xiaomi_results(res, device.address_str()))) {
48  continue;
49  }
50  if (res->temperature.has_value() && this->temperature_ != nullptr)
51  this->temperature_->publish_state(*res->temperature);
52  if (res->humidity.has_value() && this->humidity_ != nullptr)
53  this->humidity_->publish_state(*res->humidity);
54  if (res->battery_level.has_value() && this->battery_level_ != nullptr)
55  this->battery_level_->publish_state(*res->battery_level);
56  success = true;
57  }
58 
59  return success;
60 }
61 
62 void XiaomiLYWSD03MMC::set_bindkey(const std::string &bindkey) {
63  memset(bindkey_, 0, 16);
64  if (bindkey.size() != 32) {
65  return;
66  }
67  char temp[3] = {0};
68  for (int i = 0; i < 16; i++) {
69  strncpy(temp, &(bindkey.c_str()[i * 2]), 2);
70  bindkey_[i] = std::strtoul(temp, nullptr, 16);
71  }
72 }
73 
74 } // namespace xiaomi_lywsd03mmc
75 } // namespace esphome
76 
77 #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:364
bool decrypt_xiaomi_payload(std::vector< uint8_t > &raw, const uint8_t *bindkey, const uint64_t &address)
Definition: xiaomi_ble.cpp:251
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
const std::vector< ServiceData > & get_service_datas() const
bool parse_xiaomi_message(const std::vector< uint8_t > &message, XiaomiParseResult &result)
Definition: xiaomi_ble.cpp:101
optional< XiaomiParseResult > parse_xiaomi_header(const esp32_ble_tracker::ServiceData &service_data)
Definition: xiaomi_ble.cpp:150
bool report_xiaomi_results(const optional< XiaomiParseResult > &result, const std::string &address)
Definition: xiaomi_ble.cpp:339
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void set_bindkey(const std::string &bindkey)