ESPHome  2024.11.0
xiaomi_hhccpot002.cpp
Go to the documentation of this file.
1 #include "xiaomi_hhccpot002.h"
2 #include "esphome/core/log.h"
3 
4 #ifdef USE_ESP32
5 
6 namespace esphome {
7 namespace xiaomi_hhccpot002 {
8 
9 static const char *const TAG = "xiaomi_hhccpot002";
10 
12  ESP_LOGCONFIG(TAG, "Xiaomi HHCCPOT002");
13  LOG_SENSOR(" ", "Moisture", this->moisture_);
14  LOG_SENSOR(" ", "Conductivity", this->conductivity_);
15 }
16 
18  if (device.address_uint64() != this->address_) {
19  ESP_LOGVV(TAG, "parse_device(): unknown MAC address.");
20  return false;
21  }
22  ESP_LOGVV(TAG, "parse_device(): MAC address %s found.", device.address_str().c_str());
23 
24  bool success = false;
25  for (auto &service_data : device.get_service_datas()) {
26  auto res = xiaomi_ble::parse_xiaomi_header(service_data);
27  if (!res.has_value()) {
28  continue;
29  }
30  if (res->is_duplicate) {
31  continue;
32  }
33  if (res->has_encryption) {
34  ESP_LOGVV(TAG, "parse_device(): payload decryption is currently not supported on this device.");
35  continue;
36  }
37  if (!(xiaomi_ble::parse_xiaomi_message(service_data.data, *res))) {
38  continue;
39  }
40  if (!(xiaomi_ble::report_xiaomi_results(res, device.address_str()))) {
41  continue;
42  }
43  if (res->moisture.has_value() && this->moisture_ != nullptr)
44  this->moisture_->publish_state(*res->moisture);
45  if (res->conductivity.has_value() && this->conductivity_ != nullptr)
46  this->conductivity_->publish_state(*res->conductivity);
47  success = true;
48  }
49 
50  return success;
51 }
52 
53 } // namespace xiaomi_hhccpot002
54 } // namespace esphome
55 
56 #endif
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
const std::vector< ServiceData > & get_service_datas() const
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override
bool parse_xiaomi_message(const std::vector< uint8_t > &message, XiaomiParseResult &result)
Definition: xiaomi_ble.cpp:101
optional< XiaomiParseResult > parse_xiaomi_header(const esp32_ble_tracker::ServiceData &service_data)
Definition: xiaomi_ble.cpp:150
bool report_xiaomi_results(const optional< XiaomiParseResult > &result, const std::string &address)
Definition: xiaomi_ble.cpp:339
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7