diff --git a/esphome/components/xiaomi_miscale/sensor.py b/esphome/components/xiaomi_miscale/sensor.py index 517870cc01..a2a2f3bdfc 100644 --- a/esphome/components/xiaomi_miscale/sensor.py +++ b/esphome/components/xiaomi_miscale/sensor.py @@ -11,6 +11,7 @@ from esphome.const import ( UNIT_OHM, CONF_IMPEDANCE, ICON_OMEGA, + CONF_CLEAR_IMPEDANCE, ) DEPENDENCIES = ["esp32_ble_tracker"] @@ -25,6 +26,7 @@ CONFIG_SCHEMA = ( { cv.GenerateID(): cv.declare_id(XiaomiMiscale), cv.Required(CONF_MAC_ADDRESS): cv.mac_address, + cv.Optional(CONF_CLEAR_IMPEDANCE, default=False): cv.boolean, cv.Optional(CONF_WEIGHT): sensor.sensor_schema( unit_of_measurement=UNIT_KILOGRAM, icon=ICON_SCALE_BATHROOM, @@ -50,6 +52,7 @@ async def to_code(config): await esp32_ble_tracker.register_ble_device(var, config) cg.add(var.set_address(config[CONF_MAC_ADDRESS].as_hex)) + cg.add(var.set_clear_impedance(config[CONF_CLEAR_IMPEDANCE])) if CONF_WEIGHT in config: sens = await sensor.new_sensor(config[CONF_WEIGHT]) diff --git a/esphome/components/xiaomi_miscale/xiaomi_miscale.cpp b/esphome/components/xiaomi_miscale/xiaomi_miscale.cpp index 6c3bd61cac..4ed5f587de 100644 --- a/esphome/components/xiaomi_miscale/xiaomi_miscale.cpp +++ b/esphome/components/xiaomi_miscale/xiaomi_miscale.cpp @@ -24,25 +24,30 @@ bool XiaomiMiscale::parse_device(const esp32_ble_tracker::ESPBTDevice &device) { bool success = false; for (auto &service_data : device.get_service_datas()) { auto res = parse_header_(service_data); - if (!res.has_value()) { + if (!res.has_value()) continue; - } - if (!(parse_message_(service_data.data, *res))) { + if (!parse_message_(service_data.data, *res)) continue; - } - if (!(report_results_(res, device.address_str()))) { + 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); + if (this->impedance_ != nullptr) { + if (res->version == 1) { + 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_->publish_state(*res->impedance); + } else { + if (clear_impedance_) + this->impedance_->publish_state(NAN); + } + } + } success = true; } diff --git a/esphome/components/xiaomi_miscale/xiaomi_miscale.h b/esphome/components/xiaomi_miscale/xiaomi_miscale.h index 3e51405ddc..cff61f153b 100644 --- a/esphome/components/xiaomi_miscale/xiaomi_miscale.h +++ b/esphome/components/xiaomi_miscale/xiaomi_miscale.h @@ -24,11 +24,13 @@ class XiaomiMiscale : public Component, public esp32_ble_tracker::ESPBTDeviceLis 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; } + void set_clear_impedance(bool clear_impedance) { clear_impedance_ = clear_impedance; } protected: uint64_t address_; sensor::Sensor *weight_{nullptr}; sensor::Sensor *impedance_{nullptr}; + bool clear_impedance_{false}; optional parse_header_(const esp32_ble_tracker::ServiceData &service_data); bool parse_message_(const std::vector &message, ParseResult &result); diff --git a/esphome/const.py b/esphome/const.py index 721f78f26a..dec68e0701 100644 --- a/esphome/const.py +++ b/esphome/const.py @@ -89,6 +89,7 @@ CONF_CHANGE_MODE_EVERY = "change_mode_every" CONF_CHANNEL = "channel" CONF_CHANNELS = "channels" CONF_CHIPSET = "chipset" +CONF_CLEAR_IMPEDANCE = "clear_impedance" CONF_CLIENT_ID = "client_id" CONF_CLK_PIN = "clk_pin" CONF_CLOCK_PIN = "clock_pin"