diff --git a/esphome/components/dfrobot_sen0395/commands.cpp b/esphome/components/dfrobot_sen0395/commands.cpp index 2c60fb3449..b0eb297287 100644 --- a/esphome/components/dfrobot_sen0395/commands.cpp +++ b/esphome/components/dfrobot_sen0395/commands.cpp @@ -66,13 +66,41 @@ uint8_t ReadStateCommand::execute(DfrobotSen0395Component *parent) { this->parent_ = parent; if (this->parent_->read_message_()) { std::string message(this->parent_->read_buffer_); - if (message.rfind("$JYBSS,0, , , *") != std::string::npos) { - this->parent_->set_detected_(false); + std::vector params; + size_t pos = 0; + std::string token; + while ((pos = message.find(",")) != std::string::npos) { + token = message.substr(0, pos); + params.push_back(token); + message.erase(0, pos + 1); + } + params.push_back(message); + + if (params.size() == 5 && params[0] == "$JYBSS") { + this->parent_->set_detected_(params[1] == "1"); + if (params[1] != "1") { + for (int ti = 1; ti <= TARGET_COUNT; ++ti) { + this->parent_->set_detected_target_distance_(ti, NAN); + this->parent_->set_detected_target_snr_(ti, NAN); + } + } this->parent_->set_active(true); return 1; // Command done - } else if (message.rfind("$JYBSS,1, , , *") != std::string::npos) { - this->parent_->set_detected_(true); - this->parent_->set_active(true); + } else if (params.size() == 8 && params[0] == "$JYRPO") { + const auto target_count = std::atoi(params[1].c_str()); + const auto target = std::atoi(params[2].c_str()); + const auto dist = std::atof(params[3].c_str()); + const auto snr = std::atof(params[5].c_str()); + this->parent_->set_detected_target_distance_(target, dist); + this->parent_->set_detected_target_snr_(target, snr); + + if (target == target_count) { + for (int ti = target + 1; ti <= TARGET_COUNT; ++ti) { + this->parent_->set_detected_target_distance_(ti, NAN); + this->parent_->set_detected_target_snr_(ti, NAN); + } + } + return 1; // Command done } }