diff --git a/esphome/components/xiaomi_ble/xiaomi_ble.cpp b/esphome/components/xiaomi_ble/xiaomi_ble.cpp index 4367b5fd1e..675a10ca0b 100644 --- a/esphome/components/xiaomi_ble/xiaomi_ble.cpp +++ b/esphome/components/xiaomi_ble/xiaomi_ble.cpp @@ -9,6 +9,31 @@ namespace xiaomi_ble { static const char *TAG = "xiaomi_ble"; bool parse_xiaomi_data_byte(uint8_t data_type, const uint8_t *data, uint8_t data_length, XiaomiParseResult &result) { +if (result.type == XiaomiParseResult::TYPE_MISCALE) { + switch(data_type) { + case 0x00:{ + const uint16_t weight = uint16_t(data[1]) | (uint16_t(data[2]) << 8); + if (data[0] == 0x22 || data[0] == 0xa2) + result.weight = weight * 0.01f / 2.0f; + else if (data[0] == 0x03 || data[0] == 0xb2) + result.weight = weight * 0.01f * 0.453592; + else + return false; + return true; + } + case 0x01: { + const uint16_t weight = uint16_t(data[11]) | (uint16_t(data[12]) << 8); + if (data[0] == 0x02) + result.weight = weight * 0.01f / 2.0f; + else if (data[0] == 0x03) + result.weight = weight * 0.01f * 0.453592; + else + return false; + return true; + } + } +} + switch (data_type) { case 0x0D: { // temperature+humidity, 4 bytes, 16-bit signed integer (LE) each, 0.1 °C, 0.1 % if (data_length != 4) @@ -69,26 +94,30 @@ optional parse_xiaomi(const esp32_ble_tracker::ESPBTDevice &d return {}; } - if (!device.get_service_data_uuid()->contains(0x95, 0xFE)) { + if (!device.get_service_data_uuid()->contains(0x95, 0xFE)) and !device.get_service_data_uuid()->contains(0x1D, 0x18) and !device.get_service_data_uuid()->contains(0x1B, 0x18)) { // ESP_LOGVV(TAG, "Xiaomi no service data UUID magic bytes"); return {}; } const auto *raw = reinterpret_cast(device.get_service_data().data()); - if (device.get_service_data().size() < 14) { + if (device.get_service_data().size() < 9) { // ESP_LOGVV(TAG, "Xiaomi service data too short!"); return {}; } bool is_mijia = (raw[1] & 0x20) == 0x20 && raw[2] == 0xAA && raw[3] == 0x01; bool is_miflora = (raw[1] & 0x20) == 0x20 && raw[2] == 0x98 && raw[3] == 0x00; - - if (!is_mijia && !is_miflora) { + bool is_miscale = raw[3] == 0xE3 && raw[4] == 0x07 && raw[5] == 0x08; + bool is_mibfs = raw[2] == 0xE3 && raw[3] == 0x07 && raw[4] == 0x08; + if (!is_mijia && !is_miflora && !is_miscale && !is_mibfs) { // ESP_LOGVV(TAG, "Xiaomi no magic bytes"); return {}; } + bool success; + XiaomiParseResult result; +if (is_mijia || is_miflora) { uint8_t raw_offset = is_mijia ? 11 : 12; const uint8_t raw_type = raw[raw_offset]; @@ -97,12 +126,24 @@ optional parse_xiaomi(const esp32_ble_tracker::ESPBTDevice &d const uint8_t expected_length = data_length + raw_offset + 3; const uint8_t actual_length = device.get_service_data().size(); if (expected_length != actual_length) { - // ESP_LOGV(TAG, "Xiaomi %s data length mismatch (%u != %d)", type, expected_length, actual_length); + // ESP_LOGV(TAG, "Xiaomi %s data length mismatch (%u != %d)", raw_type, expected_length, actual_length); return {}; } - XiaomiParseResult result; result.type = is_miflora ? XiaomiParseResult::TYPE_MIFLORA : XiaomiParseResult::TYPE_MIJIA; - bool success = parse_xiaomi_data_byte(raw_type, data, data_length, result); + success = parse_xiaomi_data_byte(raw_type, data, data_length, result); +} else { + const uint8_t *data = &raw[0]; + const uint8_t data_length = 2; + + result.type = XiaomiParseResult::TYPE_MISCALE; + if (device.get_service_data_uuid()->contains(0x1B, 0x18) && !is_mibfs) { + const uint8_t raw_type = 0x0; + success = parse_xiaomi_data_byte(raw_type, data, data_length, result); + } else { + const uint8_t raw_type = 0x1; + success = parse_xiaomi_data_byte(raw_type, data, data_length, result); + } +} if (!success) return {}; return result; @@ -113,7 +154,7 @@ bool XiaomiListener::parse_device(const esp32_ble_tracker::ESPBTDevice &device) if (!res.has_value()) return false; - const char *name = res->type == XiaomiParseResult::TYPE_MIFLORA ? "Mi Flora" : "Mi Jia"; + const char *name = res->type == XiaomiParseResult::TYPE_MIFLORA ? "Mi Flora" : (res->type == XiaomiParseResult::TYPE_MIJIA ? "Mi Jia": "Mi Scale"); ESP_LOGD(TAG, "Got Xiaomi %s (%s):", name, device.address_str().c_str()); @@ -135,7 +176,9 @@ bool XiaomiListener::parse_device(const esp32_ble_tracker::ESPBTDevice &device) if (res->moisture.has_value()) { ESP_LOGD(TAG, " Moisture: %.0f%%", *res->moisture); } - + if (res->weight.has_value()) { + ESP_LOGD(TAG, " Weight: %.1fkg", *res->weight); + } return true; }