ESPHome  2022.8.0
xiaomi_jqjcy01ym.cpp
Go to the documentation of this file.
1 #include "xiaomi_jqjcy01ym.h"
2 #include "esphome/core/log.h"
3 
4 #ifdef USE_ESP32
5 
6 namespace esphome {
7 namespace xiaomi_jqjcy01ym {
8 
9 static const char *const TAG = "xiaomi_jqjcy01ym";
10 
12  ESP_LOGCONFIG(TAG, "Xiaomi JQJCY01YM");
13  LOG_SENSOR(" ", "Temperature", this->temperature_);
14  LOG_SENSOR(" ", "Humidity", this->humidity_);
15  LOG_SENSOR(" ", "Formaldehyde", this->formaldehyde_);
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  ESP_LOGVV(TAG, "parse_device(): MAC address %s found.", device.address_str().c_str());
25 
26  bool success = false;
27  for (auto &service_data : device.get_service_datas()) {
28  auto res = xiaomi_ble::parse_xiaomi_header(service_data);
29  if (!res.has_value()) {
30  continue;
31  }
32  if (res->is_duplicate) {
33  continue;
34  }
35  if (res->has_encryption) {
36  ESP_LOGVV(TAG, "parse_device(): payload decryption is currently not supported on this device.");
37  continue;
38  }
39  if (!(xiaomi_ble::parse_xiaomi_message(service_data.data, *res))) {
40  continue;
41  }
42  if (!(xiaomi_ble::report_xiaomi_results(res, device.address_str()))) {
43  continue;
44  }
45  if (res->temperature.has_value() && this->temperature_ != nullptr)
46  this->temperature_->publish_state(*res->temperature);
47  if (res->humidity.has_value() && this->humidity_ != nullptr)
48  this->humidity_->publish_state(*res->humidity);
49  if (res->formaldehyde.has_value() && this->formaldehyde_ != nullptr)
50  this->formaldehyde_->publish_state(*res->formaldehyde);
51  if (res->battery_level.has_value() && this->battery_level_ != nullptr)
52  this->battery_level_->publish_state(*res->battery_level);
53  success = true;
54  }
55 
56  return success;
57 }
58 
59 } // namespace xiaomi_jqjcy01ym
60 } // namespace esphome
61 
62 #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:72
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