ESPHome  2022.12.8
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 
6 namespace esphome {
7 namespace xiaomi_wx08zm {
8 
9 static 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  ESP_LOGVV(TAG, "parse_device(): MAC address %s found.", device.address_str().c_str());
24 
25  bool success = false;
26  for (auto &service_data : device.get_service_datas()) {
27  auto res = xiaomi_ble::parse_xiaomi_header(service_data);
28  if (!res.has_value()) {
29  continue;
30  }
31  if (res->is_duplicate) {
32  continue;
33  }
34  if (res->has_encryption) {
35  ESP_LOGVV(TAG, "parse_device(): payload decryption is currently not supported on this device.");
36  continue;
37  }
38  if (!(xiaomi_ble::parse_xiaomi_message(service_data.data, *res))) {
39  continue;
40  }
41  if (!(xiaomi_ble::report_xiaomi_results(res, device.address_str()))) {
42  continue;
43  }
44  if (res->is_active.has_value()) {
45  this->publish_state(*res->is_active);
46  }
47  if (res->tablet.has_value() && this->tablet_ != nullptr)
48  this->tablet_->publish_state(*res->tablet);
49  if (res->battery_level.has_value() && this->battery_level_ != nullptr)
50  this->battery_level_->publish_state(*res->battery_level);
51  success = true;
52  }
53 
54  return success;
55 }
56 
57 } // namespace xiaomi_wx08zm
58 } // namespace esphome
59 
60 #endif
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:72
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override
void publish_state(bool state)
Publish a new state to the front-end.
const std::vector< ServiceData > & get_service_datas() const
bool parse_xiaomi_message(const std::vector< uint8_t > &message, XiaomiParseResult &result)
Definition: xiaomi_ble.cpp:90
optional< XiaomiParseResult > parse_xiaomi_header(const esp32_ble_tracker::ServiceData &service_data)
Definition: xiaomi_ble.cpp:138
bool report_xiaomi_results(const optional< XiaomiParseResult > &result, const std::string &address)
Definition: xiaomi_ble.cpp:322
Definition: a4988.cpp:4