ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
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
7
8static const char *const TAG = "xiaomi_hhccpot002";
9
10void XiaomiHHCCPOT002 ::dump_config() {
11 ESP_LOGCONFIG(TAG, "Xiaomi HHCCPOT002");
12 LOG_SENSOR(" ", "Moisture", this->moisture_);
13 LOG_SENSOR(" ", "Conductivity", this->conductivity_);
14}
15
17 if (device.address_uint64() != this->address_) {
18 ESP_LOGVV(TAG, "parse_device(): unknown MAC address.");
19 return false;
20 }
21 char addr_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
22 const char *addr_str = device.address_str_to(addr_buf);
23 ESP_LOGVV(TAG, "parse_device(): MAC address %s found.", addr_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, addr_str))) {
42 continue;
43 }
44 if (res->moisture.has_value() && this->moisture_ != nullptr)
45 this->moisture_->publish_state(*res->moisture);
46 if (res->conductivity.has_value() && this->conductivity_ != nullptr)
47 this->conductivity_->publish_state(*res->conductivity);
48 success = true;
49 }
50
51 return success;
52}
53
54} // namespace esphome::xiaomi_hhccpot002
55
56#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
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)