ESPHome 2026.10.0-dev
Loading...
Searching...
No Matches
xiaomi_mjyd02yla.cpp
Go to the documentation of this file.
1#include "xiaomi_mjyd02yla.h"
3#include "esphome/core/log.h"
4
6
7static const char *const TAG = "xiaomi_mjyd02yla";
8
10 ESP_LOGCONFIG(TAG, "Xiaomi MJYD02YL-A");
11 LOG_BINARY_SENSOR(" ", "Motion", this);
12 LOG_BINARY_SENSOR(" ", "Light", this->is_light_);
13 LOG_SENSOR(" ", "Idle Time", this->idle_time_);
14 LOG_SENSOR(" ", "Battery Level", this->battery_level_);
15 LOG_SENSOR(" ", "Illuminance", this->illuminance_);
16}
17
19 if (device.address_uint64() != this->address_) {
20 ESP_LOGVV(TAG, "parse_device(): unknown MAC address.");
21 return false;
22 }
23 char addr_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
24 const char *addr_str = device.address_str_to(addr_buf);
25 ESP_LOGVV(TAG, "parse_device(): MAC address %s found.", addr_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, addr_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
63void XiaomiMJYD02YLA::set_bindkey(const char *bindkey) { parse_hex(bindkey, this->bindkey_, sizeof(this->bindkey_)); }
64
65} // namespace esphome::xiaomi_mjyd02yla
void publish_state(bool new_state)
Publish a new state to the front-end.
ESPDEPRECATED("Use address_str_to() instead. Removed in 2027.2.0.", "2026.8.0") std const char * address_str_to(char *buf) const
Return MAC as "XX:XX:XX:XX:XX:XX" string.
uint64_t address_uint64() const
Return MAC as packed uint64 (byte 0 in LSB — matches esp32's address_uint64).
const std::vector< ServiceData > & get_service_datas() const
Definition ble_device.h:225
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
binary_sensor::BinarySensor * is_light_
bool parse_device(const ble_device_base::ESPBTDevice &device) override
bool decrypt_xiaomi_payload(std::vector< uint8_t > &raw, const uint8_t *bindkey, const uint64_t &address)
bool parse_xiaomi_message(const std::vector< uint8_t > &message, XiaomiParseResult &result)
optional< XiaomiParseResult > parse_xiaomi_header(const ble_device_base::ServiceData &service_data)
bool report_xiaomi_results(const optional< XiaomiParseResult > &result, const char *address)
size_t parse_hex(const char *str, size_t length, uint8_t *data, size_t count)
Parse bytes from a hex-encoded string into a byte array.
Definition helpers.cpp:293