ESPHome  2024.10.2
xiaomi_mjyd02yla.cpp
Go to the documentation of this file.
1 #include "xiaomi_mjyd02yla.h"
2 #include "esphome/core/log.h"
3 
4 #ifdef USE_ESP32
5 
6 namespace esphome {
7 namespace xiaomi_mjyd02yla {
8 
9 static const char *const TAG = "xiaomi_mjyd02yla";
10 
12  ESP_LOGCONFIG(TAG, "Xiaomi MJYD02YL-A");
13  LOG_BINARY_SENSOR(" ", "Motion", this);
14  LOG_BINARY_SENSOR(" ", "Light", this->is_light_);
15  LOG_SENSOR(" ", "Idle Time", this->idle_time_);
16  LOG_SENSOR(" ", "Battery Level", this->battery_level_);
17  LOG_SENSOR(" ", "Illuminance", this->illuminance_);
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  (!(xiaomi_ble::decrypt_xiaomi_payload(const_cast<std::vector<uint8_t> &>(service_data.data), this->bindkey_,
38  this->address_)))) {
39  continue;
40  }
41  if (!(xiaomi_ble::parse_xiaomi_message(service_data.data, *res))) {
42  continue;
43  }
44  if (!(xiaomi_ble::report_xiaomi_results(res, device.address_str()))) {
45  continue;
46  }
47  if (res->idle_time.has_value() && this->idle_time_ != nullptr)
48  this->idle_time_->publish_state(*res->idle_time);
49  if (res->battery_level.has_value() && this->battery_level_ != nullptr)
50  this->battery_level_->publish_state(*res->battery_level);
51  if (res->illuminance.has_value() && this->illuminance_ != nullptr)
52  this->illuminance_->publish_state(*res->illuminance);
53  if (res->is_light.has_value() && this->is_light_ != nullptr)
54  this->is_light_->publish_state(*res->is_light);
55  if (res->has_motion.has_value())
56  this->publish_state(*res->has_motion);
57  success = true;
58  }
59 
60  return success;
61 }
62 
63 void XiaomiMJYD02YLA::set_bindkey(const std::string &bindkey) {
64  memset(bindkey_, 0, 16);
65  if (bindkey.size() != 32) {
66  return;
67  }
68  char temp[3] = {0};
69  for (int i = 0; i < 16; i++) {
70  strncpy(temp, &(bindkey.c_str()[i * 2]), 2);
71  bindkey_[i] = std::strtoul(temp, nullptr, 16);
72  }
73 }
74 
75 } // namespace xiaomi_mjyd02yla
76 } // namespace esphome
77 
78 #endif
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override
void set_bindkey(const std::string &bindkey)
bool decrypt_xiaomi_payload(std::vector< uint8_t > &raw, const uint8_t *bindkey, const uint64_t &address)
Definition: xiaomi_ble.cpp:251
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
void publish_state(bool state)
Publish a new state to the front-end.
const std::vector< ServiceData > & get_service_datas() const
binary_sensor::BinarySensor * is_light_
bool parse_xiaomi_message(const std::vector< uint8_t > &message, XiaomiParseResult &result)
Definition: xiaomi_ble.cpp:101
optional< XiaomiParseResult > parse_xiaomi_header(const esp32_ble_tracker::ServiceData &service_data)
Definition: xiaomi_ble.cpp:150
bool report_xiaomi_results(const optional< XiaomiParseResult > &result, const std::string &address)
Definition: xiaomi_ble.cpp:339
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7