From b5dac00dcb55c1911f2d6e0bd6266da5580a8934 Mon Sep 17 00:00:00 2001 From: Alfredo <alfredo@pironti.eu> Date: Wed, 10 May 2023 01:16:14 +0200 Subject: [PATCH] Fix ezo parsing (#4792) --- esphome/components/ezo/ezo.cpp | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/esphome/components/ezo/ezo.cpp b/esphome/components/ezo/ezo.cpp index 9d4343e004..8e4486dbf2 100644 --- a/esphome/components/ezo/ezo.cpp +++ b/esphome/components/ezo/ezo.cpp @@ -106,20 +106,18 @@ void EZOSensor::loop() { break; } - ESP_LOGV(TAG, "Received buffer \"%s\" for command type %s", buf, EZO_COMMAND_TYPE_STRINGS[to_run->command_type]); + ESP_LOGV(TAG, "Received buffer \"%s\" for command type %s", &buf[1], EZO_COMMAND_TYPE_STRINGS[to_run->command_type]); - if ((buf[0] == 1) || (to_run->command_type == EzoCommandType::EZO_CALIBRATION)) { // EZO_CALIBRATION returns 0-3 - // some sensors return multiple comma-separated values, terminate string after first one - for (size_t i = 1; i < sizeof(buf) - 1; i++) { - if (buf[i] == ',') { - buf[i] = '\0'; - break; - } - } + if (buf[0] == 1) { std::string payload = reinterpret_cast<char *>(&buf[1]); if (!payload.empty()) { switch (to_run->command_type) { case EzoCommandType::EZO_READ: { + // some sensors return multiple comma-separated values, terminate string after first one + int start_location = 0; + if ((start_location = payload.find(',')) != std::string::npos) { + payload.erase(start_location); + } auto val = parse_number<float>(payload); if (!val.has_value()) { ESP_LOGW(TAG, "Can't convert '%s' to number!", payload.c_str()); @@ -154,7 +152,10 @@ void EZOSensor::loop() { break; } case EzoCommandType::EZO_T: { - this->t_callback_.call(payload); + int start_location = 0; + if ((start_location = payload.find(',')) != std::string::npos) { + this->t_callback_.call(payload.substr(start_location + 1)); + } break; } case EzoCommandType::EZO_CUSTOM: {