ESPHome  2024.12.2
xiaomi_hhccjcy10.cpp
Go to the documentation of this file.
1 #include "xiaomi_hhccjcy10.h"
2 #include "esphome/core/log.h"
3 #include "esphome/core/helpers.h"
4 
5 #ifdef USE_ESP32
6 
7 namespace esphome {
8 namespace xiaomi_hhccjcy10 {
9 
10 static const char *const TAG = "xiaomi_hhccjcy10";
11 
13  ESP_LOGCONFIG(TAG, "Xiaomi HHCCJCY10");
14  LOG_SENSOR(" ", "Temperature", this->temperature_);
15  LOG_SENSOR(" ", "Moisture", this->moisture_);
16  LOG_SENSOR(" ", "Conductivity", this->conductivity_);
17  LOG_SENSOR(" ", "Illuminance", this->illuminance_);
18  LOG_SENSOR(" ", "Battery Level", this->battery_level_);
19 }
20 
22  if (device.address_uint64() != this->address_) {
23  ESP_LOGVV(TAG, "parse_device(): unknown MAC address.");
24  return false;
25  }
26  ESP_LOGVV(TAG, "parse_device(): MAC address %s found.", device.address_str().c_str());
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 xiaomi_hhccjcy10
66 } // namespace esphome
67 
68 #endif
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
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:192
uint16_t temperature
Definition: sun_gtil2.cpp:26
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:183
const std::vector< ServiceData > & get_service_datas() const
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7