ESPHome 2025.7.0
Loading...
Searching...
No Matches
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
6namespace esphome {
7namespace xiaomi_lywsd03mmc {
8
9static const char *const TAG = "xiaomi_lywsd03mmc";
10
12 ESP_LOGCONFIG(TAG,
13 "Xiaomi LYWSD03MMC\n"
14 " Bindkey: %s",
15 format_hex_pretty(this->bindkey_, 16).c_str());
16 LOG_SENSOR(" ", "Temperature", this->temperature_);
17 LOG_SENSOR(" ", "Humidity", this->humidity_);
18 LOG_SENSOR(" ", "Battery Level", this->battery_level_);
19}
20
22 if (device.address_uint64() != this->address_) {
23 ESP_LOGVV(TAG, "parse_device(): unknown MAC address.");
24 return false;
25 }
26 ESP_LOGVV(TAG, "parse_device(): MAC address %s found.", device.address_str().c_str());
27
28 bool success = false;
29 for (auto &service_data : device.get_service_datas()) {
30 auto res = xiaomi_ble::parse_xiaomi_header(service_data);
31 if (!res.has_value()) {
32 continue;
33 }
34 if (res->is_duplicate) {
35 continue;
36 }
37 if (res->has_encryption &&
38 (!(xiaomi_ble::decrypt_xiaomi_payload(const_cast<std::vector<uint8_t> &>(service_data.data), this->bindkey_,
39 this->address_)))) {
40 continue;
41 }
42 if (!(xiaomi_ble::parse_xiaomi_message(service_data.data, *res))) {
43 continue;
44 }
45 if (res->humidity.has_value() && this->humidity_ != nullptr) {
46 // see https://github.com/custom-components/sensor.mitemp_bt/issues/7#issuecomment-595948254
47 *res->humidity = trunc(*res->humidity);
48 }
49 if (!(xiaomi_ble::report_xiaomi_results(res, device.address_str()))) {
50 continue;
51 }
52 if (res->temperature.has_value() && this->temperature_ != nullptr)
53 this->temperature_->publish_state(*res->temperature);
54 if (res->humidity.has_value() && this->humidity_ != nullptr)
55 this->humidity_->publish_state(*res->humidity);
56 if (res->battery_level.has_value() && this->battery_level_ != nullptr)
57 this->battery_level_->publish_state(*res->battery_level);
58 success = true;
59 }
60
61 return success;
62}
63
64void XiaomiLYWSD03MMC::set_bindkey(const std::string &bindkey) {
65 memset(bindkey_, 0, 16);
66 if (bindkey.size() != 32) {
67 return;
68 }
69 char temp[3] = {0};
70 for (int i = 0; i < 16; i++) {
71 strncpy(temp, &(bindkey.c_str()[i * 2]), 2);
72 bindkey_[i] = std::strtoul(temp, nullptr, 16);
73 }
74}
75
76} // namespace xiaomi_lywsd03mmc
77} // namespace esphome
78
79#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:45
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
std::string format_hex_pretty(const uint8_t *data, size_t length, char separator, bool show_length)
Format a byte array in pretty-printed, human-readable hex format.
Definition helpers.cpp:261