ESPHome 2026.1.0-dev
Loading...
Searching...
No Matches
xiaomi_lywsd03mmc.cpp
Go to the documentation of this file.
1#include "xiaomi_lywsd03mmc.h"
3#include "esphome/core/log.h"
4
5#ifdef USE_ESP32
6
7namespace esphome {
8namespace xiaomi_lywsd03mmc {
9
10static const char *const TAG = "xiaomi_lywsd03mmc";
11
12static constexpr size_t LYWSD03MMC_BINDKEY_SIZE = 16;
13
15 char bindkey_hex[format_hex_pretty_size(LYWSD03MMC_BINDKEY_SIZE)];
16 ESP_LOGCONFIG(TAG,
17 "Xiaomi LYWSD03MMC\n"
18 " Bindkey: %s",
19 format_hex_pretty_to(bindkey_hex, this->bindkey_, LYWSD03MMC_BINDKEY_SIZE, '.'));
20 LOG_SENSOR(" ", "Temperature", this->temperature_);
21 LOG_SENSOR(" ", "Humidity", this->humidity_);
22 LOG_SENSOR(" ", "Battery Level", this->battery_level_);
23}
24
26 if (device.address_uint64() != this->address_) {
27 ESP_LOGVV(TAG, "parse_device(): unknown MAC address.");
28 return false;
29 }
30 ESP_LOGVV(TAG, "parse_device(): MAC address %s found.", device.address_str().c_str());
31
32 bool success = false;
33 for (auto &service_data : device.get_service_datas()) {
34 auto res = xiaomi_ble::parse_xiaomi_header(service_data);
35 if (!res.has_value()) {
36 continue;
37 }
38 if (res->is_duplicate) {
39 continue;
40 }
41 if (res->has_encryption &&
42 (!(xiaomi_ble::decrypt_xiaomi_payload(const_cast<std::vector<uint8_t> &>(service_data.data), this->bindkey_,
43 this->address_)))) {
44 continue;
45 }
46 if (!(xiaomi_ble::parse_xiaomi_message(service_data.data, *res))) {
47 continue;
48 }
49 if (res->humidity.has_value() && this->humidity_ != nullptr) {
50 // see https://github.com/custom-components/sensor.mitemp_bt/issues/7#issuecomment-595948254
51 *res->humidity = trunc(*res->humidity);
52 }
53 if (!(xiaomi_ble::report_xiaomi_results(res, device.address_str()))) {
54 continue;
55 }
56 if (res->temperature.has_value() && this->temperature_ != nullptr)
57 this->temperature_->publish_state(*res->temperature);
58 if (res->humidity.has_value() && this->humidity_ != nullptr)
59 this->humidity_->publish_state(*res->humidity);
60 if (res->battery_level.has_value() && this->battery_level_ != nullptr)
61 this->battery_level_->publish_state(*res->battery_level);
62 success = true;
63 }
64
65 return success;
66}
67
68void XiaomiLYWSD03MMC::set_bindkey(const std::string &bindkey) {
69 memset(bindkey_, 0, 16);
70 if (bindkey.size() != 32) {
71 return;
72 }
73 char temp[3] = {0};
74 for (int i = 0; i < 16; i++) {
75 strncpy(temp, &(bindkey.c_str()[i * 2]), 2);
76 bindkey_[i] = std::strtoul(temp, nullptr, 16);
77 }
78}
79
80} // namespace xiaomi_lywsd03mmc
81} // namespace esphome
82
83#endif
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
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