mirror of
https://github.com/esphome/esphome.git
synced 2025-03-15 07:08:20 +00:00
Add support for Xiaomi Mi Scale V1 and V2
https://github.com/esphome/feature-requests/issues/209 @tsunglung have all the rights and effort for this.
This commit is contained in:
parent
11e88019c2
commit
d8c26fc1ed
@ -9,6 +9,31 @@ namespace xiaomi_ble {
|
|||||||
static const char *TAG = "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) {
|
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) {
|
switch (data_type) {
|
||||||
case 0x0D: { // temperature+humidity, 4 bytes, 16-bit signed integer (LE) each, 0.1 °C, 0.1 %
|
case 0x0D: { // temperature+humidity, 4 bytes, 16-bit signed integer (LE) each, 0.1 °C, 0.1 %
|
||||||
if (data_length != 4)
|
if (data_length != 4)
|
||||||
@ -69,26 +94,30 @@ optional<XiaomiParseResult> parse_xiaomi(const esp32_ble_tracker::ESPBTDevice &d
|
|||||||
return {};
|
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");
|
// ESP_LOGVV(TAG, "Xiaomi no service data UUID magic bytes");
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto *raw = reinterpret_cast<const uint8_t *>(device.get_service_data().data());
|
const auto *raw = reinterpret_cast<const uint8_t *>(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!");
|
// ESP_LOGVV(TAG, "Xiaomi service data too short!");
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
bool is_mijia = (raw[1] & 0x20) == 0x20 && raw[2] == 0xAA && raw[3] == 0x01;
|
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;
|
bool is_miflora = (raw[1] & 0x20) == 0x20 && raw[2] == 0x98 && raw[3] == 0x00;
|
||||||
|
bool is_miscale = raw[3] == 0xE3 && raw[4] == 0x07 && raw[5] == 0x08;
|
||||||
if (!is_mijia && !is_miflora) {
|
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");
|
// ESP_LOGVV(TAG, "Xiaomi no magic bytes");
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool success;
|
||||||
|
XiaomiParseResult result;
|
||||||
|
if (is_mijia || is_miflora) {
|
||||||
uint8_t raw_offset = is_mijia ? 11 : 12;
|
uint8_t raw_offset = is_mijia ? 11 : 12;
|
||||||
|
|
||||||
const uint8_t raw_type = raw[raw_offset];
|
const uint8_t raw_type = raw[raw_offset];
|
||||||
@ -97,12 +126,24 @@ optional<XiaomiParseResult> parse_xiaomi(const esp32_ble_tracker::ESPBTDevice &d
|
|||||||
const uint8_t expected_length = data_length + raw_offset + 3;
|
const uint8_t expected_length = data_length + raw_offset + 3;
|
||||||
const uint8_t actual_length = device.get_service_data().size();
|
const uint8_t actual_length = device.get_service_data().size();
|
||||||
if (expected_length != actual_length) {
|
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 {};
|
return {};
|
||||||
}
|
}
|
||||||
XiaomiParseResult result;
|
|
||||||
result.type = is_miflora ? XiaomiParseResult::TYPE_MIFLORA : XiaomiParseResult::TYPE_MIJIA;
|
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)
|
if (!success)
|
||||||
return {};
|
return {};
|
||||||
return result;
|
return result;
|
||||||
@ -113,7 +154,7 @@ bool XiaomiListener::parse_device(const esp32_ble_tracker::ESPBTDevice &device)
|
|||||||
if (!res.has_value())
|
if (!res.has_value())
|
||||||
return false;
|
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());
|
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()) {
|
if (res->moisture.has_value()) {
|
||||||
ESP_LOGD(TAG, " Moisture: %.0f%%", *res->moisture);
|
ESP_LOGD(TAG, " Moisture: %.0f%%", *res->moisture);
|
||||||
}
|
}
|
||||||
|
if (res->weight.has_value()) {
|
||||||
|
ESP_LOGD(TAG, " Weight: %.1fkg", *res->weight);
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user