ESPHome  2022.5.1
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 
6 namespace esphome {
7 namespace xiaomi_hhccjcy01 {
8 
9 static 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  ESP_LOGVV(TAG, "parse_device(): MAC address %s found.", device.address_str().c_str());
26 
27  bool success = false;
28  for (auto &service_data : device.get_service_datas()) {
29  auto res = xiaomi_ble::parse_xiaomi_header(service_data);
30  if (!res.has_value()) {
31  continue;
32  }
33  if (res->is_duplicate) {
34  continue;
35  }
36  if (res->has_encryption) {
37  ESP_LOGVV(TAG, "parse_device(): payload decryption is currently not supported on this device.");
38  continue;
39  }
40  if (!(xiaomi_ble::parse_xiaomi_message(service_data.data, *res))) {
41  continue;
42  }
43  if (!(xiaomi_ble::report_xiaomi_results(res, device.address_str()))) {
44  continue;
45  }
46  if (res->temperature.has_value() && this->temperature_ != nullptr)
47  this->temperature_->publish_state(*res->temperature);
48  if (res->moisture.has_value() && this->moisture_ != nullptr)
49  this->moisture_->publish_state(*res->moisture);
50  if (res->conductivity.has_value() && this->conductivity_ != nullptr)
51  this->conductivity_->publish_state(*res->conductivity);
52  if (res->illuminance.has_value() && this->illuminance_ != nullptr)
53  this->illuminance_->publish_state(*res->illuminance);
54  if (res->battery_level.has_value() && this->battery_level_ != nullptr)
55  this->battery_level_->publish_state(*res->battery_level);
56  success = true;
57  }
58 
59  return success;
60 }
61 
62 } // namespace xiaomi_hhccjcy01
63 } // namespace esphome
64 
65 #endif
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:70
const std::vector< ServiceData > & get_service_datas() const
bool parse_xiaomi_message(const std::vector< uint8_t > &message, XiaomiParseResult &result)
Definition: xiaomi_ble.cpp:90
optional< XiaomiParseResult > parse_xiaomi_header(const esp32_ble_tracker::ServiceData &service_data)
Definition: xiaomi_ble.cpp:138
bool report_xiaomi_results(const optional< XiaomiParseResult > &result, const std::string &address)
Definition: xiaomi_ble.cpp:322
Definition: a4988.cpp:4
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override