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