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