ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
xiaomi_wx08zm.cpp
Go to the documentation of this file.
1#include "xiaomi_wx08zm.h"
2#include "esphome/core/log.h"
3
4#ifdef USE_ESP32
5
7
8static const char *const TAG = "xiaomi_wx08zm";
9
11 ESP_LOGCONFIG(TAG, "Xiaomi WX08ZM");
12 LOG_BINARY_SENSOR(" ", "Mosquito Repellent", this);
13 LOG_SENSOR(" ", "Tablet Resource", this->tablet_);
14 LOG_SENSOR(" ", "Battery Level", this->battery_level_);
15}
16
18 if (device.address_uint64() != this->address_) {
19 ESP_LOGVV(TAG, "parse_device(): unknown MAC address.");
20 return false;
21 }
22 char addr_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
23 const char *addr_str = device.address_str_to(addr_buf);
24 ESP_LOGVV(TAG, "parse_device(): MAC address %s found.", addr_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 ESP_LOGVV(TAG, "parse_device(): payload decryption is currently not supported on this device.");
37 continue;
38 }
39 if (!(xiaomi_ble::parse_xiaomi_message(service_data.data, *res))) {
40 continue;
41 }
42 if (!(xiaomi_ble::report_xiaomi_results(res, addr_str))) {
43 continue;
44 }
45 if (res->is_active.has_value()) {
46 this->publish_state(*res->is_active);
47 }
48 if (res->tablet.has_value() && this->tablet_ != nullptr)
49 this->tablet_->publish_state(*res->tablet);
50 if (res->battery_level.has_value() && this->battery_level_ != nullptr)
51 this->battery_level_->publish_state(*res->battery_level);
52 success = true;
53 }
54
55 return success;
56}
57
58} // namespace esphome::xiaomi_wx08zm
59
60#endif
void publish_state(bool new_state)
Publish a new state to the front-end.
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
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)