mirror of
https://github.com/esphome/esphome.git
synced 2024-12-23 16:47:57 +01:00
Combine code of xiaomi_miscale and xiaomi_miscale2 (#2266)
* Combine xiaomi_miscale and xiaomi_miscale2 * check if message contains impedance * auto detect scale version * remove xiaomi_miscale2 * fix lint errors * Apply suggestions from code review Co-authored-by: Oxan van Leeuwen <oxan@oxanvanleeuwen.nl> * Apply suggestions from code review on old code * Fix clang-tidy warnings Co-authored-by: Oxan van Leeuwen <oxan@oxanvanleeuwen.nl>
This commit is contained in:
parent
ed593544d8
commit
f1364d4af4
@ -380,8 +380,8 @@ bool ESPBTUUID::operator==(const ESPBTUUID &uuid) const {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
esp_bt_uuid_t ESPBTUUID::get_uuid() { return this->uuid_; }
|
||||
std::string ESPBTUUID::to_string() {
|
||||
esp_bt_uuid_t ESPBTUUID::get_uuid() const { return this->uuid_; }
|
||||
std::string ESPBTUUID::to_string() const {
|
||||
char sbuf[64];
|
||||
switch (this->uuid_.len) {
|
||||
case ESP_UUID_LEN_16:
|
||||
|
@ -34,9 +34,9 @@ class ESPBTUUID {
|
||||
bool operator==(const ESPBTUUID &uuid) const;
|
||||
bool operator!=(const ESPBTUUID &uuid) const { return !(*this == uuid); }
|
||||
|
||||
esp_bt_uuid_t get_uuid();
|
||||
esp_bt_uuid_t get_uuid() const;
|
||||
|
||||
std::string to_string();
|
||||
std::string to_string() const;
|
||||
|
||||
protected:
|
||||
esp_bt_uuid_t uuid_;
|
||||
|
@ -8,6 +8,9 @@ from esphome.const import (
|
||||
STATE_CLASS_MEASUREMENT,
|
||||
UNIT_KILOGRAM,
|
||||
ICON_SCALE_BATHROOM,
|
||||
UNIT_OHM,
|
||||
CONF_IMPEDANCE,
|
||||
ICON_OMEGA,
|
||||
)
|
||||
|
||||
DEPENDENCIES = ["esp32_ble_tracker"]
|
||||
@ -28,6 +31,12 @@ CONFIG_SCHEMA = (
|
||||
accuracy_decimals=2,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
cv.Optional(CONF_IMPEDANCE): sensor.sensor_schema(
|
||||
unit_of_measurement=UNIT_OHM,
|
||||
icon=ICON_OMEGA,
|
||||
accuracy_decimals=0,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
}
|
||||
)
|
||||
.extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA)
|
||||
@ -45,3 +54,6 @@ async def to_code(config):
|
||||
if CONF_WEIGHT in config:
|
||||
sens = await sensor.new_sensor(config[CONF_WEIGHT])
|
||||
cg.add(var.set_weight(sens))
|
||||
if CONF_IMPEDANCE in config:
|
||||
sens = await sensor.new_sensor(config[CONF_IMPEDANCE])
|
||||
cg.add(var.set_impedance(sens))
|
||||
|
@ -11,6 +11,7 @@ static const char *const TAG = "xiaomi_miscale";
|
||||
void XiaomiMiscale::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "Xiaomi Miscale");
|
||||
LOG_SENSOR(" ", "Weight", this->weight_);
|
||||
LOG_SENSOR(" ", "Impedance", this->impedance_);
|
||||
}
|
||||
|
||||
bool XiaomiMiscale::parse_device(const esp32_ble_tracker::ESPBTDevice &device) {
|
||||
@ -26,14 +27,22 @@ bool XiaomiMiscale::parse_device(const esp32_ble_tracker::ESPBTDevice &device) {
|
||||
if (!res.has_value()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!(parse_message(service_data.data, *res))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!(report_results(res, device.address_str()))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (res->weight.has_value() && this->weight_ != nullptr)
|
||||
this->weight_->publish_state(*res->weight);
|
||||
|
||||
if (res->version == 1 && this->impedance_ != nullptr) {
|
||||
ESP_LOGW(TAG, "Impedance is only supported on version 2. Your scale was identified as verison 1.");
|
||||
} else if (res->impedance.has_value() && this->impedance_ != nullptr)
|
||||
this->impedance_->publish_state(*res->impedance);
|
||||
success = true;
|
||||
}
|
||||
|
||||
@ -42,8 +51,14 @@ bool XiaomiMiscale::parse_device(const esp32_ble_tracker::ESPBTDevice &device) {
|
||||
|
||||
optional<ParseResult> XiaomiMiscale::parse_header(const esp32_ble_tracker::ServiceData &service_data) {
|
||||
ParseResult result;
|
||||
if (!service_data.uuid.contains(0x1D, 0x18)) {
|
||||
ESP_LOGVV(TAG, "parse_header(): no service data UUID magic bytes.");
|
||||
if (service_data.uuid == esp32_ble_tracker::ESPBTUUID::from_uint16(0x181D) && service_data.data.size() == 10) {
|
||||
result.version = 1;
|
||||
} else if (service_data.uuid == esp32_ble_tracker::ESPBTUUID::from_uint16(0x181B) && service_data.data.size() == 13) {
|
||||
result.version = 2;
|
||||
} else {
|
||||
ESP_LOGVV(TAG,
|
||||
"parse_header(): Couldn't identify scale version or data size was not correct. UUID: %s, data_size: %d",
|
||||
service_data.uuid.to_string().c_str(), service_data.data.size());
|
||||
return {};
|
||||
}
|
||||
|
||||
@ -51,7 +66,15 @@ optional<ParseResult> XiaomiMiscale::parse_header(const esp32_ble_tracker::Servi
|
||||
}
|
||||
|
||||
bool XiaomiMiscale::parse_message(const std::vector<uint8_t> &message, ParseResult &result) {
|
||||
// example 1d18 a2 6036 e307 07 11 0f1f11
|
||||
if (result.version == 1) {
|
||||
return parse_message_V1(message, result);
|
||||
} else {
|
||||
return parse_message_V2(message, result);
|
||||
}
|
||||
}
|
||||
|
||||
bool XiaomiMiscale::parse_message_V1(const std::vector<uint8_t> &message, ParseResult &result) {
|
||||
// message size is checked in parse_header
|
||||
// 1-2 Weight (MISCALE 181D)
|
||||
// 3-4 Years (MISCALE 181D)
|
||||
// 5 month (MISCALE 181D)
|
||||
@ -61,21 +84,56 @@ bool XiaomiMiscale::parse_message(const std::vector<uint8_t> &message, ParseResu
|
||||
// 9 second (MISCALE 181D)
|
||||
|
||||
const uint8_t *data = message.data();
|
||||
const int data_length = 10;
|
||||
|
||||
if (message.size() != data_length) {
|
||||
ESP_LOGVV(TAG, "parse_message(): payload has wrong size (%d)!", message.size());
|
||||
return false;
|
||||
}
|
||||
|
||||
// weight, 2 bytes, 16-bit unsigned integer, 1 kg
|
||||
const int16_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; // unit 'kg'
|
||||
else if (data[0] == 0x12 || data[0] == 0xb2)
|
||||
result.weight = weight * 0.01f * 0.6; // unit 'jin'
|
||||
result.weight = weight * 0.01f * 0.6f; // unit 'jin'
|
||||
else if (data[0] == 0x03 || data[0] == 0xb3)
|
||||
result.weight = weight * 0.01f * 0.453592; // unit 'lbs'
|
||||
result.weight = weight * 0.01f * 0.453592f; // unit 'lbs'
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool XiaomiMiscale::parse_message_V2(const std::vector<uint8_t> &message, ParseResult &result) {
|
||||
// message size is checked in parse_header
|
||||
// 2-3 Years (MISCALE 2 181B)
|
||||
// 4 month (MISCALE 2 181B)
|
||||
// 5 day (MISCALE 2 181B)
|
||||
// 6 hour (MISCALE 2 181B)
|
||||
// 7 minute (MISCALE 2 181B)
|
||||
// 8 second (MISCALE 2 181B)
|
||||
// 9-10 impedance (MISCALE 2 181B)
|
||||
// 11-12 weight (MISCALE 2 181B)
|
||||
|
||||
const uint8_t *data = message.data();
|
||||
|
||||
bool has_impedance = ((data[1] & (1 << 1)) != 0);
|
||||
bool is_stabilized = ((data[1] & (1 << 5)) != 0);
|
||||
bool load_removed = ((data[1] & (1 << 7)) != 0);
|
||||
|
||||
if (!is_stabilized || load_removed) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// weight, 2 bytes, 16-bit unsigned integer, 1 kg
|
||||
const int16_t weight = uint16_t(data[11]) | (uint16_t(data[12]) << 8);
|
||||
if (data[0] == 0x02)
|
||||
result.weight = weight * 0.01f / 2.0f; // unit 'kg'
|
||||
else if (data[0] == 0x03)
|
||||
result.weight = weight * 0.01f * 0.453592f; // unit 'lbs'
|
||||
|
||||
if (has_impedance) {
|
||||
// impedance, 2 bytes, 16-bit
|
||||
const int16_t impedance = uint16_t(data[9]) | (uint16_t(data[10]) << 8);
|
||||
result.impedance = impedance;
|
||||
|
||||
if (impedance == 0 || impedance >= 3000) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -86,11 +144,14 @@ bool XiaomiMiscale::report_results(const optional<ParseResult> &result, const st
|
||||
return false;
|
||||
}
|
||||
|
||||
ESP_LOGD(TAG, "Got Xiaomi Miscale (%s):", address.c_str());
|
||||
ESP_LOGD(TAG, "Got Xiaomi Miscale v%d (%s):", result->version, address.c_str());
|
||||
|
||||
if (result->weight.has_value()) {
|
||||
ESP_LOGD(TAG, " Weight: %.2fkg", *result->weight);
|
||||
}
|
||||
if (result->impedance.has_value()) {
|
||||
ESP_LOGD(TAG, " Impedance: %.0fohm", *result->impedance);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -10,7 +10,9 @@ namespace esphome {
|
||||
namespace xiaomi_miscale {
|
||||
|
||||
struct ParseResult {
|
||||
int version;
|
||||
optional<float> weight;
|
||||
optional<float> impedance;
|
||||
};
|
||||
|
||||
class XiaomiMiscale : public Component, public esp32_ble_tracker::ESPBTDeviceListener {
|
||||
@ -21,13 +23,17 @@ class XiaomiMiscale : public Component, public esp32_ble_tracker::ESPBTDeviceLis
|
||||
void dump_config() override;
|
||||
float get_setup_priority() const override { return setup_priority::DATA; }
|
||||
void set_weight(sensor::Sensor *weight) { weight_ = weight; }
|
||||
void set_impedance(sensor::Sensor *impedance) { impedance_ = impedance; }
|
||||
|
||||
protected:
|
||||
uint64_t address_;
|
||||
sensor::Sensor *weight_{nullptr};
|
||||
sensor::Sensor *impedance_{nullptr};
|
||||
|
||||
optional<ParseResult> parse_header(const esp32_ble_tracker::ServiceData &service_data);
|
||||
bool parse_message(const std::vector<uint8_t> &message, ParseResult &result);
|
||||
bool parse_message_V1(const std::vector<uint8_t> &message, ParseResult &result);
|
||||
bool parse_message_V2(const std::vector<uint8_t> &message, ParseResult &result);
|
||||
bool report_results(const optional<ParseResult> &result, const std::string &address);
|
||||
};
|
||||
|
||||
|
@ -1,59 +1,5 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import sensor, esp32_ble_tracker
|
||||
from esphome.const import (
|
||||
CONF_MAC_ADDRESS,
|
||||
CONF_ID,
|
||||
CONF_WEIGHT,
|
||||
STATE_CLASS_MEASUREMENT,
|
||||
UNIT_KILOGRAM,
|
||||
ICON_SCALE_BATHROOM,
|
||||
UNIT_OHM,
|
||||
CONF_IMPEDANCE,
|
||||
ICON_OMEGA,
|
||||
|
||||
CONFIG_SCHEMA = cv.invalid(
|
||||
"This platform has been combined into xiaomi_miscale. Use xiaomi_miscale instead."
|
||||
)
|
||||
|
||||
DEPENDENCIES = ["esp32_ble_tracker"]
|
||||
|
||||
xiaomi_miscale2_ns = cg.esphome_ns.namespace("xiaomi_miscale2")
|
||||
XiaomiMiscale2 = xiaomi_miscale2_ns.class_(
|
||||
"XiaomiMiscale2", esp32_ble_tracker.ESPBTDeviceListener, cg.Component
|
||||
)
|
||||
|
||||
CONFIG_SCHEMA = (
|
||||
cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(XiaomiMiscale2),
|
||||
cv.Required(CONF_MAC_ADDRESS): cv.mac_address,
|
||||
cv.Optional(CONF_WEIGHT): sensor.sensor_schema(
|
||||
unit_of_measurement=UNIT_KILOGRAM,
|
||||
icon=ICON_SCALE_BATHROOM,
|
||||
accuracy_decimals=2,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
cv.Optional(CONF_IMPEDANCE): sensor.sensor_schema(
|
||||
unit_of_measurement=UNIT_OHM,
|
||||
icon=ICON_OMEGA,
|
||||
accuracy_decimals=0,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
}
|
||||
)
|
||||
.extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA)
|
||||
.extend(cv.COMPONENT_SCHEMA)
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(var, config)
|
||||
await esp32_ble_tracker.register_ble_device(var, config)
|
||||
|
||||
cg.add(var.set_address(config[CONF_MAC_ADDRESS].as_hex))
|
||||
|
||||
if CONF_WEIGHT in config:
|
||||
sens = await sensor.new_sensor(config[CONF_WEIGHT])
|
||||
cg.add(var.set_weight(sens))
|
||||
if CONF_IMPEDANCE in config:
|
||||
sens = await sensor.new_sensor(config[CONF_IMPEDANCE])
|
||||
cg.add(var.set_impedance(sens))
|
||||
|
@ -1,112 +0,0 @@
|
||||
#include "xiaomi_miscale2.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
#ifdef USE_ESP32
|
||||
|
||||
namespace esphome {
|
||||
namespace xiaomi_miscale2 {
|
||||
|
||||
static const char *const TAG = "xiaomi_miscale2";
|
||||
|
||||
void XiaomiMiscale2::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "Xiaomi Miscale2");
|
||||
LOG_SENSOR(" ", "Weight", this->weight_);
|
||||
LOG_SENSOR(" ", "Impedance", this->impedance_);
|
||||
}
|
||||
|
||||
bool XiaomiMiscale2::parse_device(const esp32_ble_tracker::ESPBTDevice &device) {
|
||||
if (device.address_uint64() != this->address_) {
|
||||
ESP_LOGVV(TAG, "parse_device(): unknown MAC address.");
|
||||
return false;
|
||||
}
|
||||
ESP_LOGVV(TAG, "parse_device(): MAC address %s found.", device.address_str().c_str());
|
||||
|
||||
bool success = false;
|
||||
for (auto &service_data : device.get_service_datas()) {
|
||||
auto res = parse_header(service_data);
|
||||
if (!res.has_value()) {
|
||||
continue;
|
||||
}
|
||||
if (!(parse_message(service_data.data, *res))) {
|
||||
continue;
|
||||
}
|
||||
if (!(report_results(res, device.address_str()))) {
|
||||
continue;
|
||||
}
|
||||
if (res->weight.has_value() && this->weight_ != nullptr)
|
||||
this->weight_->publish_state(*res->weight);
|
||||
if (res->impedance.has_value() && this->impedance_ != nullptr)
|
||||
this->impedance_->publish_state(*res->impedance);
|
||||
success = true;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
optional<ParseResult> XiaomiMiscale2::parse_header(const esp32_ble_tracker::ServiceData &service_data) {
|
||||
ParseResult result;
|
||||
if (!service_data.uuid.contains(0x1B, 0x18)) {
|
||||
ESP_LOGVV(TAG, "parse_header(): no service data UUID magic bytes.");
|
||||
return {};
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool XiaomiMiscale2::parse_message(const std::vector<uint8_t> &message, ParseResult &result) {
|
||||
// 2-3 Years (MISCALE 2 181B)
|
||||
// 4 month (MISCALE 2 181B)
|
||||
// 5 day (MISCALE 2 181B)
|
||||
// 6 hour (MISCALE 2 181B)
|
||||
// 7 minute (MISCALE 2 181B)
|
||||
// 8 second (MISCALE 2 181B)
|
||||
// 9-10 impedance (MISCALE 2 181B)
|
||||
// 11-12 weight (MISCALE 2 181B)
|
||||
|
||||
const uint8_t *data = message.data();
|
||||
const int data_length = 13;
|
||||
|
||||
if (message.size() != data_length) {
|
||||
ESP_LOGVV(TAG, "parse_message(): payload has wrong size (%d)!", message.size());
|
||||
return false;
|
||||
}
|
||||
|
||||
bool is_stabilized = ((data[1] & (1 << 5)) != 0);
|
||||
bool load_removed = ((data[1] & (1 << 7)) != 0);
|
||||
|
||||
// weight, 2 bytes, 16-bit unsigned integer, 1 kg
|
||||
const int16_t weight = uint16_t(data[11]) | (uint16_t(data[12]) << 8);
|
||||
if (data[0] == 0x02)
|
||||
result.weight = weight * 0.01f / 2.0f; // unit 'kg'
|
||||
else if (data[0] == 0x03)
|
||||
result.weight = weight * 0.01f * 0.453592; // unit 'lbs'
|
||||
|
||||
// impedance, 2 bytes, 16-bit
|
||||
const int16_t impedance = uint16_t(data[9]) | (uint16_t(data[10]) << 8);
|
||||
result.impedance = impedance;
|
||||
|
||||
return is_stabilized && !load_removed && impedance != 0 && impedance < 3000;
|
||||
}
|
||||
|
||||
bool XiaomiMiscale2::report_results(const optional<ParseResult> &result, const std::string &address) {
|
||||
if (!result.has_value()) {
|
||||
ESP_LOGVV(TAG, "report_results(): no results available.");
|
||||
return false;
|
||||
}
|
||||
|
||||
ESP_LOGD(TAG, "Got Xiaomi Miscale2 (%s):", address.c_str());
|
||||
|
||||
if (result->weight.has_value()) {
|
||||
ESP_LOGD(TAG, " Weight: %.2fkg", *result->weight);
|
||||
}
|
||||
if (result->impedance.has_value()) {
|
||||
ESP_LOGD(TAG, " Impedance: %.0fohm", *result->impedance);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace xiaomi_miscale2
|
||||
} // namespace esphome
|
||||
|
||||
#endif
|
@ -1,40 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
#include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h"
|
||||
|
||||
#ifdef USE_ESP32
|
||||
|
||||
namespace esphome {
|
||||
namespace xiaomi_miscale2 {
|
||||
|
||||
struct ParseResult {
|
||||
optional<float> weight;
|
||||
optional<float> impedance;
|
||||
};
|
||||
|
||||
class XiaomiMiscale2 : public Component, public esp32_ble_tracker::ESPBTDeviceListener {
|
||||
public:
|
||||
void set_address(uint64_t address) { address_ = address; };
|
||||
|
||||
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override;
|
||||
void dump_config() override;
|
||||
float get_setup_priority() const override { return setup_priority::DATA; }
|
||||
void set_weight(sensor::Sensor *weight) { weight_ = weight; }
|
||||
void set_impedance(sensor::Sensor *impedance) { impedance_ = impedance; }
|
||||
|
||||
protected:
|
||||
uint64_t address_;
|
||||
sensor::Sensor *weight_{nullptr};
|
||||
sensor::Sensor *impedance_{nullptr};
|
||||
|
||||
optional<ParseResult> parse_header(const esp32_ble_tracker::ServiceData &service_data);
|
||||
bool parse_message(const std::vector<uint8_t> &message, ParseResult &result);
|
||||
bool report_results(const optional<ParseResult> &result, const std::string &address);
|
||||
};
|
||||
|
||||
} // namespace xiaomi_miscale2
|
||||
} // namespace esphome
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user