7 namespace xiaomi_miscale {
9 static const char *
const TAG =
"xiaomi_miscale";
12 ESP_LOGCONFIG(TAG,
"Xiaomi Miscale");
13 LOG_SENSOR(
" ",
"Weight", this->
weight_);
14 LOG_SENSOR(
" ",
"Impedance", this->
impedance_);
19 ESP_LOGVV(TAG,
"parse_device(): unknown MAC address.");
22 ESP_LOGVV(TAG,
"parse_device(): MAC address %s found.", device.
address_str().c_str());
36 if (res->weight.has_value() && this->
weight_ !=
nullptr)
40 if (res->version == 1) {
41 ESP_LOGW(TAG,
"Impedance is only supported on version 2. Your scale was identified as version 1.");
43 if (res->impedance.has_value()) {
65 "parse_header(): Couldn't identify scale version or data size was not correct. UUID: %s, data_size: %d",
91 const uint8_t *data = message.data();
94 const int16_t weight = uint16_t(data[1]) | (uint16_t(data[2]) << 8);
95 if (data[0] == 0x22 || data[0] == 0xa2) {
96 result.
weight = weight * 0.01f / 2.0f;
97 }
else if (data[0] == 0x12 || data[0] == 0xb2) {
98 result.
weight = weight * 0.01f * 0.6f;
99 }
else if (data[0] == 0x03 || data[0] == 0xb3) {
100 result.
weight = weight * 0.01f * 0.453592f;
117 const uint8_t *data = message.data();
119 bool has_impedance = ((data[1] & (1 << 1)) != 0);
120 bool is_stabilized = ((data[1] & (1 << 5)) != 0);
121 bool load_removed = ((data[1] & (1 << 7)) != 0);
123 if (!is_stabilized || load_removed) {
128 const int16_t weight = uint16_t(data[11]) | (uint16_t(data[12]) << 8);
129 if (data[0] == 0x02) {
130 result.
weight = weight * 0.01f / 2.0f;
131 }
else if (data[0] == 0x03) {
132 result.
weight = weight * 0.01f * 0.453592f;
137 const int16_t impedance = uint16_t(data[9]) | (uint16_t(data[10]) << 8);
140 if (impedance == 0 || impedance >= 3000) {
150 ESP_LOGVV(TAG,
"report_results(): no results available.");
154 ESP_LOGD(TAG,
"Got Xiaomi Miscale v%d (%s):", result->version, address.c_str());
157 ESP_LOGD(TAG,
" Weight: %.2fkg", *result->weight);
160 ESP_LOGD(TAG,
" Impedance: %.0fohm", *result->impedance);
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override
optional< float > impedance
optional< ParseResult > parse_header_(const esp32_ble_tracker::ServiceData &service_data)
std::string address_str() const
bool parse_message_v2_(const std::vector< uint8_t > &message, ParseResult &result)
static ESPBTUUID from_uint16(uint16_t uuid)
bool parse_message_(const std::vector< uint8_t > &message, ParseResult &result)
void publish_state(float state)
Publish a new state to the front-end.
sensor::Sensor * impedance_
const std::vector< ServiceData > & get_service_datas() const
void dump_config() override
bool parse_message_v1_(const std::vector< uint8_t > &message, ParseResult &result)
uint64_t address_uint64() const
std::string to_string() const
bool report_results_(const optional< ParseResult > &result, const std::string &address)
Implementation of SPI Controller mode.