ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
xiaomi_hhccjcy10.cpp
Go to the documentation of this file.
1#include "xiaomi_hhccjcy10.h"
3#include "esphome/core/log.h"
4
5#ifdef USE_ESP32
6
8
9static const char *const TAG = "xiaomi_hhccjcy10";
10
12 ESP_LOGCONFIG(TAG, "Xiaomi HHCCJCY10");
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 ESP_LOGVV(TAG, "parse_device(): MAC address %s found.", device.address_str_to(addr_buf));
27
28 bool success = false;
29 for (auto &service_data : device.get_service_datas()) {
30 if (!service_data.uuid.contains(0x50, 0xFD)) {
31 ESP_LOGVV(TAG, "no tuya service data UUID.");
32 continue;
33 }
34 if (service_data.data.size() != 9) { // tuya alternate between two service data
35 continue;
36 }
37 const uint8_t *data = service_data.data.data();
38
39 if (this->temperature_ != nullptr) {
40 const int16_t temperature = encode_uint16(data[1], data[2]);
41 this->temperature_->publish_state((float) temperature / 10.0f);
42 }
43
44 if (this->moisture_ != nullptr)
45 this->moisture_->publish_state(data[0]);
46
47 if (this->conductivity_ != nullptr) {
48 const uint16_t conductivity = encode_uint16(data[7], data[8]);
49 this->conductivity_->publish_state((float) conductivity);
50 }
51
52 if (this->illuminance_ != nullptr) {
53 const uint32_t illuminance = encode_uint24(data[3], data[4], data[5]);
54 this->illuminance_->publish_state((float) illuminance);
55 }
56
57 if (this->battery_level_ != nullptr)
58 this->battery_level_->publish_state(data[6]);
59 success = true;
60 }
61
62 return success;
63}
64
65} // namespace esphome::xiaomi_hhccjcy10
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:68
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override
constexpr uint32_t encode_uint24(uint8_t byte1, uint8_t byte2, uint8_t byte3)
Encode a 24-bit value given three bytes in most to least significant byte order.
Definition helpers.h:863
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:859
static void uint32_t
uint16_t temperature
Definition sun_gtil2.cpp:12