[miscale] Add flag to clear last impedance reading if the newly received reading only contains weight (#3132)

This commit is contained in:
mknjc 2022-02-19 10:59:53 +01:00 committed by GitHub
parent e445d6aada
commit ad2f857e15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 10 deletions

View File

@ -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])

View File

@ -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;
}

View File

@ -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<ParseResult> parse_header_(const esp32_ble_tracker::ServiceData &service_data);
bool parse_message_(const std::vector<uint8_t> &message, ParseResult &result);

View File

@ -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"