From 12647d648fb5c73ade5ec277aa86ac9c6a80b5cc Mon Sep 17 00:00:00 2001
From: Jan Grewe <jan@faked.org>
Date: Wed, 26 Oct 2022 18:04:36 +0200
Subject: [PATCH] Yieryi3178: fix double publish

---
 include/yieryi_3178.h | 23 ++++++++++-------------
 1 file changed, 10 insertions(+), 13 deletions(-)

diff --git a/include/yieryi_3178.h b/include/yieryi_3178.h
index 52e3a09..edf7d19 100644
--- a/include/yieryi_3178.h
+++ b/include/yieryi_3178.h
@@ -2,7 +2,6 @@
 #include "esphome/core/helpers.h"
 
 class Yieryi3178 : public UARTDevice, public PollingComponent {
-
   public:
 
     Sensor *ec   = new Sensor();
@@ -14,6 +13,7 @@ class Yieryi3178 : public UARTDevice, public PollingComponent {
     Yieryi3178(UARTComponent *parent, uint32_t update_interval) : UARTDevice(parent), PollingComponent(update_interval) {}
 
     std::vector<uint8_t> bytes;
+    byte request[8] = { 0x01, 0x03, 0x00, 0x00, 0x00, 0x04, 0x44, 0x09 };
 
     typedef union {
       unsigned char Byte[2];
@@ -21,56 +21,53 @@ class Yieryi3178 : public UARTDevice, public PollingComponent {
     } TwoByte;
 
     void update() override {
-
-      byte request[8] = { 0x01, 0x03, 0x00, 0x00, 0x00, 0x04, 0x44, 0x09 };
-
       write_array(request, sizeof(request));
       flush();
       delay(10);
       
       while(available() > 0) {
-
-        bytes.push_back(read());      
+        bytes.push_back(read());
 
         if(bytes.size() < 16)
           continue;  
 
         if(bytes.size() == 16) {
-
-          ESP_LOGV("yieryi3178", "Bytes received: %s", format_hex_pretty(bytes).c_str());
+          ESP_LOGVV("yieryi3178", "Bytes received: %s", format_hex_pretty(bytes).c_str());
 
           TwoByte data_ec;
           data_ec.Byte[0] = bytes[5];
           data_ec.Byte[1] = bytes[4];  
           ec->publish_state(data_ec.UInt16 * 0.001);
-          ESP_LOGD("yieryi3178", "EC: %.3f", data_ec.UInt16 * 0.001);
+          ESP_LOGV("yieryi3178", "EC: %.3f", data_ec.UInt16 * 0.001);
 
           TwoByte data_ph;
           data_ph.Byte[0] = bytes[7];
           data_ph.Byte[1] = bytes[6];  
           ph->publish_state(data_ph.UInt16 * 0.01);
-          ESP_LOGD("yieryi3178", "pH: %.2f", data_ph.UInt16 * 0.01);
+          ESP_LOGV("yieryi3178", "pH: %.2f", data_ph.UInt16 * 0.01);
 
           TwoByte data_rh;
           data_rh.Byte[0] = bytes[9];
           data_rh.Byte[1] = bytes[8];  
           rh->publish_state(data_rh.UInt16);
-          ESP_LOGD("yieryi3178", "RH: %i", data_rh.UInt16);
+          ESP_LOGV("yieryi3178", "RH: %i", data_rh.UInt16);
 
           TwoByte data_temp;
           data_temp.Byte[0] = bytes[11];
           data_temp.Byte[1] = bytes[10];  
           temp->publish_state(data_temp.UInt16 * 0.1);
-          ESP_LOGD("yieryi3178", "Temp: %.1f", data_temp.UInt16 * 0.1);
+          ESP_LOGV("yieryi3178", "Temp: %.1f", data_temp.UInt16 * 0.1);
 
           TwoByte data_bat;
           data_bat.Byte[0] = bytes[13];
           data_bat.Byte[1] = bytes[12];  
           bat->publish_state(data_bat.UInt16 / 100 / 1.5);
-          ESP_LOGD("yieryi3178", "Bat: %.1f", data_bat.UInt16 / 100 / 1.5);
+          ESP_LOGV("yieryi3178", "Bat: %.1f", data_bat.UInt16 / 100 / 1.5);
 
           bytes.clear();
 
+          while(available() > 0)
+            read();
         }
       }
     }
-- 
GitLab