ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
xiaomi_hhccjcy01.cpp
Go to the documentation of this file.
1#include "xiaomi_hhccjcy01.h"
2#include "esphome/core/log.h"
3
4#ifdef USE_ESP32
5
7
8static const char *const TAG = "xiaomi_hhccjcy01";
9
11 ESP_LOGCONFIG(TAG, "Xiaomi HHCCJCY01");
12 LOG_SENSOR(" ", "Temperature", this->temperature_);
13 LOG_SENSOR(" ", "Moisture", this->moisture_);
14 LOG_SENSOR(" ", "Conductivity", this->conductivity_);
15 LOG_SENSOR(" ", "Illuminance", this->illuminance_);
16 LOG_SENSOR(" ", "Battery Level", this->battery_level_);
17}
18
20 if (device.address_uint64() != this->address_) {
21 ESP_LOGVV(TAG, "parse_device(): unknown MAC address.");
22 return false;
23 }
24 char addr_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
25 const char *addr_str = device.address_str_to(addr_buf);
26 ESP_LOGVV(TAG, "parse_device(): MAC address %s found.", addr_str);
27
28 bool success = false;
29 for (auto &service_data : device.get_service_datas()) {
30 auto res = xiaomi_ble::parse_xiaomi_header(service_data);
31 if (!res.has_value()) {
32 continue;
33 }
34 if (res->is_duplicate) {
35 continue;
36 }
37 if (res->has_encryption) {
38 ESP_LOGVV(TAG, "parse_device(): payload decryption is currently not supported on this device.");
39 continue;
40 }
41 if (!(xiaomi_ble::parse_xiaomi_message(service_data.data, *res))) {
42 continue;
43 }
44 if (!(xiaomi_ble::report_xiaomi_results(res, addr_str))) {
45 continue;
46 }
47 if (res->temperature.has_value() && this->temperature_ != nullptr)
48 this->temperature_->publish_state(*res->temperature);
49 if (res->moisture.has_value() && this->moisture_ != nullptr)
50 this->moisture_->publish_state(*res->moisture);
51 if (res->conductivity.has_value() && this->conductivity_ != nullptr)
52 this->conductivity_->publish_state(*res->conductivity);
53 if (res->illuminance.has_value() && this->illuminance_ != nullptr)
54 this->illuminance_->publish_state(*res->illuminance);
55 if (res->battery_level.has_value() && this->battery_level_ != nullptr)
56 this->battery_level_->publish_state(*res->battery_level);
57 success = true;
58 }
59
60 return success;
61}
62
63} // namespace esphome::xiaomi_hhccjcy01
64
65#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)