13static constexpr size_t DEVICE_ID_POS = 2;
14static constexpr size_t FRAME_COUNT_POS = 4;
15static constexpr size_t CIPHER_POS = 5;
16static constexpr size_t CIPHER_SIZE = 12;
17static constexpr size_t COUNTER_POS = CIPHER_POS + CIPHER_SIZE;
18static constexpr size_t TAG_POS = COUNTER_POS + 3;
19static constexpr size_t TAG_SIZE = 4;
20static constexpr size_t FRAME_SIZE = TAG_POS + TAG_SIZE;
22static constexpr uint8_t FRAME_ENCRYPTED_DATA = 0x48;
23static constexpr uint8_t OBJECT_SIZE = 9;
24static constexpr uint16_t OBJECT_S200_MEASUREMENT = 0x4E16;
25static constexpr uint16_t OBJECT_S400_MEASUREMENT = 0x6E16;
26static constexpr uint32_t STABILIZED_RESET_ID = 0;
27static constexpr uint32_t STABILIZED_RESET_MS = 1000;
29XiaomiBodyScale::XiaomiBodyScale(uint64_t
address,
const char *bindkey) : address_(
address) {
30 parse_hex(bindkey, this->bindkey_,
sizeof(this->bindkey_));
33void XiaomiBodyScale::dump_config() {
34 uint8_t mac[MAC_ADDRESS_SIZE];
36 char mac_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
41 LOG_SENSOR(
" ",
"Weight", this->weight_sensor_);
42 LOG_SENSOR(
" ",
"Impedance Low (50 kHz)", this->impedance_low_sensor_);
43 LOG_SENSOR(
" ",
"Impedance High (250 kHz)", this->impedance_high_sensor_);
44 LOG_SENSOR(
" ",
"Heart Rate", this->heart_rate_sensor_);
45 LOG_SENSOR(
" ",
"Profile ID", this->profile_id_sensor_);
46 LOG_BINARY_SENSOR(
" ",
"Stabilized", this->stabilized_binary_sensor_);
49bool XiaomiBodyScale::decrypt_(
const uint8_t *frame, uint8_t *plaintext)
const {
50 uint8_t nonce[MAC_ADDRESS_SIZE + 6];
51 for (
size_t i = 0; i < MAC_ADDRESS_SIZE; i++)
52 nonce[i] =
static_cast<uint8_t
>(this->address_ >> (i * 8));
53 memcpy(nonce + MAC_ADDRESS_SIZE, frame + DEVICE_ID_POS, 3);
54 memcpy(nonce + MAC_ADDRESS_SIZE + 3, frame + COUNTER_POS, 3);
55 static constexpr uint8_t AUTH_DATA[1] = {0x11};
57 frame + CIPHER_POS, CIPHER_SIZE, plaintext, frame + TAG_POS, TAG_SIZE);
60void XiaomiBodyScale::publish_stabilized_(
bool stabilized) {
61 if (this->stabilized_binary_sensor_ ==
nullptr)
63 this->stabilized_binary_sensor_->publish_state(stabilized);
66 this->set_timeout(STABILIZED_RESET_ID, STABILIZED_RESET_MS,
67 [
this]() { this->stabilized_binary_sensor_->publish_state(
false); });
71void XiaomiBodyScale::publish_s400_(
uint32_t packed) {
74 const uint16_t raw_weight = packed & 0x7FF;
76 const uint16_t weight = raw_weight == 0x7FF ? 0 : raw_weight;
77 const uint8_t heart_rate = (packed >> 11) & 0x7F;
78 const uint16_t impedance = packed >> 18;
79 ESP_LOGD(TAG,
"weight=%u heart_rate=%u impedance=%u", raw_weight, heart_rate, impedance);
81 if (weight != 0 && this->weight_sensor_ !=
nullptr)
82 this->weight_sensor_->publish_state(weight / 10.0f);
83 if (heart_rate > 0 && heart_rate < 127 && this->heart_rate_sensor_ !=
nullptr)
84 this->heart_rate_sensor_->publish_state(heart_rate + 50.0f);
86 if (weight == 0 && heart_rate == 0) {
88 if (impedance != 0 && this->impedance_high_sensor_ !=
nullptr)
89 this->impedance_high_sensor_->publish_state(impedance / 10.0f);
90 this->publish_stabilized_(impedance != 0);
91 }
else if (impedance != 0) {
92 PUBLISH_STATE(this->impedance_low_sensor_, impedance / 10.0f);
93 this->publish_stabilized_(
false);
96 this->publish_stabilized_(
true);
105 if (service_data.data.size() != FRAME_SIZE || !service_data.uuid.contains(0x95, 0xFE))
107 const uint8_t *frame = service_data.data.data();
109 if ((frame[0] & FRAME_ENCRYPTED_DATA) != FRAME_ENCRYPTED_DATA)
112 if (frame[FRAME_COUNT_POS] == this->last_frame_count_)
114 uint8_t
object[CIPHER_SIZE];
115 if (!this->decrypt_(frame,
object)) {
116 ESP_LOGW(TAG,
"Decryption failed, wrong bindkey?");
120 this->last_frame_count_ = frame[FRAME_COUNT_POS];
123 const uint16_t value_type =
encode_uint16(
object[1],
object[0]);
124 const uint8_t *data =
object + 3;
126 const bool sized =
object[2] == OBJECT_SIZE;
127 if (sized && value_type == OBJECT_S400_MEASUREMENT) {
128 this->publish_s400_(packed);
129 }
else if (sized && value_type == OBJECT_S200_MEASUREMENT) {
131 if (packed != 0 && this->weight_sensor_ !=
nullptr)
132 this->weight_sensor_->publish_state(packed / 100.0f);
134 ESP_LOGVV(TAG,
"Unknown object 0x%04X, length %u", value_type,
object[2]);
137 PUBLISH_STATE(this->profile_id_sensor_, data[0]);
bool aes_ccm_auth_decrypt(const uint8_t key[16], const uint8_t *nonce, size_t nonce_len, const uint8_t *aad, size_t aad_len, const uint8_t *ciphertext, size_t ct_len, uint8_t *plaintext, const uint8_t *tag, size_t tag_len)
ESPHOME_LOG_TAG(TAG, "xiaomi_body_scale")
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.
constexpr uint32_t encode_uint32(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4)
Encode a 32-bit value given four bytes in most to least significant byte order.