ESPHome 2026.1.5
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
6namespace esphome {
7namespace xiaomi_wx08zm {
8
9static const char *const TAG = "xiaomi_wx08zm";
10
12 ESP_LOGCONFIG(TAG, "Xiaomi WX08ZM");
13 LOG_BINARY_SENSOR(" ", "Mosquito Repellent", this);
14 LOG_SENSOR(" ", "Tablet Resource", this->tablet_);
15 LOG_SENSOR(" ", "Battery Level", this->battery_level_);
16}
17
19 if (device.address_uint64() != this->address_) {
20 ESP_LOGVV(TAG, "parse_device(): unknown MAC address.");
21 return false;
22 }
23 char addr_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
24 const char *addr_str = device.address_str_to(addr_buf);
25 ESP_LOGVV(TAG, "parse_device(): MAC address %s found.", addr_str);
26
27 bool success = false;
28 for (auto &service_data : device.get_service_datas()) {
29 auto res = xiaomi_ble::parse_xiaomi_header(service_data);
30 if (!res.has_value()) {
31 continue;
32 }
33 if (res->is_duplicate) {
34 continue;
35 }
36 if (res->has_encryption) {
37 ESP_LOGVV(TAG, "parse_device(): payload decryption is currently not supported on this device.");
38 continue;
39 }
40 if (!(xiaomi_ble::parse_xiaomi_message(service_data.data, *res))) {
41 continue;
42 }
43 if (!(xiaomi_ble::report_xiaomi_results(res, addr_str))) {
44 continue;
45 }
46 if (res->is_active.has_value()) {
47 this->publish_state(*res->is_active);
48 }
49 if (res->tablet.has_value() && this->tablet_ != nullptr)
50 this->tablet_->publish_state(*res->tablet);
51 if (res->battery_level.has_value() && this->battery_level_ != nullptr)
52 this->battery_level_->publish_state(*res->battery_level);
53 success = true;
54 }
55
56 return success;
57}
58
59} // namespace xiaomi_wx08zm
60} // namespace esphome
61
62#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:76
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)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7