ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
xiaomi_mhoc401.cpp
Go to the documentation of this file.
1#include "xiaomi_mhoc401.h"
3#include "esphome/core/log.h"
4
5#ifdef USE_ESP32
6
8
9static const char *const TAG = "xiaomi_mhoc401";
10
11static constexpr size_t MHOC401_BINDKEY_SIZE = 16;
12
14 char bindkey_hex[format_hex_pretty_size(MHOC401_BINDKEY_SIZE)];
15 ESP_LOGCONFIG(TAG,
16 "Xiaomi MHOC401\n"
17 " Bindkey: %s",
18 format_hex_pretty_to(bindkey_hex, this->bindkey_, MHOC401_BINDKEY_SIZE, '.'));
19 LOG_SENSOR(" ", "Temperature", this->temperature_);
20 LOG_SENSOR(" ", "Humidity", this->humidity_);
21 LOG_SENSOR(" ", "Battery Level", this->battery_level_);
22}
23
25 if (device.address_uint64() != this->address_) {
26 ESP_LOGVV(TAG, "parse_device(): unknown MAC address.");
27 return false;
28 }
29 char addr_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
30 const char *addr_str = device.address_str_to(addr_buf);
31 ESP_LOGVV(TAG, "parse_device(): MAC address %s found.", addr_str);
32
33 bool success = false;
34 for (auto &service_data : device.get_service_datas()) {
35 auto res = xiaomi_ble::parse_xiaomi_header(service_data);
36 if (!res.has_value()) {
37 continue;
38 }
39 if (res->is_duplicate) {
40 continue;
41 }
42 if (res->has_encryption &&
43 (!(xiaomi_ble::decrypt_xiaomi_payload(const_cast<std::vector<uint8_t> &>(service_data.data), this->bindkey_,
44 this->address_)))) {
45 continue;
46 }
47 if (!(xiaomi_ble::parse_xiaomi_message(service_data.data, *res))) {
48 continue;
49 }
50 if (res->humidity.has_value() && this->humidity_ != nullptr) {
51 // see https://github.com/custom-components/sensor.mitemp_bt/issues/7#issuecomment-595948254
52 *res->humidity = trunc(*res->humidity);
53 }
54 if (!(xiaomi_ble::report_xiaomi_results(res, addr_str))) {
55 continue;
56 }
57 if (res->temperature.has_value() && this->temperature_ != nullptr)
58 this->temperature_->publish_state(*res->temperature);
59 if (res->humidity.has_value() && this->humidity_ != nullptr)
60 this->humidity_->publish_state(*res->humidity);
61 if (res->battery_level.has_value() && this->battery_level_ != nullptr)
62 this->battery_level_->publish_state(*res->battery_level);
63 success = true;
64 }
65
66 return success;
67}
68
69void XiaomiMHOC401::set_bindkey(const char *bindkey) { parse_hex(bindkey, this->bindkey_, sizeof(this->bindkey_)); }
70
71} // namespace esphome::xiaomi_mhoc401
72
73#endif
const char * address_str_to(std::span< char, MAC_ADDRESS_PRETTY_BUFFER_SIZE > buf) const
Format MAC address into provided buffer, returns pointer to buffer for convenience.
const std::vector< ServiceData > & get_service_datas() const
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override
void set_bindkey(const char *bindkey)
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 char *address)
size_t parse_hex(const char *str, size_t length, uint8_t *data, size_t count)
Parse bytes from a hex-encoded string into a byte array.
Definition helpers.cpp:274
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:340
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:1386