Kaynağa Gözat

ESPHome ≥2021.10.0 compatibility

Co-authored-by: Oxan van Leeuwen <oxan@oxanvanleeuwen.nl>
Bojan Potočnik 4 yıl önce
ebeveyn
işleme
19204e000d

+ 3 - 1
components/stream_server/__init__.py

@@ -20,7 +20,9 @@ from esphome.const import CONF_ID, CONF_PORT
 
 # ESPHome doesn't know the Stream abstraction yet, so hardcode to use a UART for now.
 
-DEPENDENCIES = ["uart"]
+AUTO_LOAD = ["async_tcp"]
+
+DEPENDENCIES = ["uart", "network"]
 
 MULTI_CONF = True
 

+ 24 - 3
components/stream_server/stream_server.cpp

@@ -19,6 +19,11 @@
 #include "esphome/core/log.h"
 #include "esphome/core/util.h"
 
+#if ESPHOME_VERSION_CODE >= VERSION_CODE(2021, 10, 0)
+#include "esphome/components/network/util.h"
+#endif
+
+
 static const char *TAG = "streamserver";
 
 using namespace esphome;
@@ -56,23 +61,39 @@ void StreamServerComponent::read() {
     int len;
     while ((len = this->stream_->available()) > 0) {
         char buf[128];
-        size_t read = this->stream_->readBytes(buf, min(len, 128));
+        len = std::min(len, 128);
+#if ESPHOME_VERSION_CODE >= VERSION_CODE(2021, 10, 0)
+        this->stream_->read_array(reinterpret_cast<uint8_t*>(buf), len);
+#else
+        this->stream_->readBytes(buf, len);
+#endif
         for (auto const& client : this->clients_)
-            client->tcp_client->write(buf, read);
+            client->tcp_client->write(buf, len);
     }
 }
 
 void StreamServerComponent::write() {
+#if ESPHOME_VERSION_CODE >= VERSION_CODE(2021, 10, 0)
+    this->stream_->write_array(this->recv_buf_);
+    this->recv_buf_.clear();
+#else
     size_t len;
     while ((len = this->recv_buf_.size()) > 0) {
         this->stream_->write(this->recv_buf_.data(), len);
         this->recv_buf_.erase(this->recv_buf_.begin(), this->recv_buf_.begin() + len);
     }
+#endif
 }
 
 void StreamServerComponent::dump_config() {
     ESP_LOGCONFIG(TAG, "Stream Server:");
-    ESP_LOGCONFIG(TAG, "  Address: %s:%u", network_get_address().c_str(), this->port_);
+    ESP_LOGCONFIG(TAG, "  Address: %s:%u",
+#if ESPHOME_VERSION_CODE >= VERSION_CODE(2021, 10, 0)
+                  esphome::network::get_ip_address().str().c_str(),
+#else
+                  network_get_address().c_str(),
+#endif
+                  this->port_);
 }
 
 void StreamServerComponent::on_shutdown() {

+ 16 - 2
components/stream_server/stream_server.h

@@ -16,9 +16,15 @@
 
 #pragma once
 
+#include "esphome/core/version.h"
 #include "esphome/core/component.h"
 #include "esphome/components/uart/uart.h"
 
+// Provide VERSION_CODE for ESPHome versions lacking it, as existence checking doesn't work for function-like macros
+#ifndef VERSION_CODE
+#define VERSION_CODE(major, minor, patch) ((major) << 16 | (minor) << 8 | (patch))
+#endif
+
 #include <memory>
 #include <string>
 #include <vector>
@@ -27,13 +33,21 @@
 #ifdef ARDUINO_ARCH_ESP8266
 #include <ESPAsyncTCP.h>
 #else
+// AsyncTCP.h includes parts of freertos, which require FreeRTOS.h header to be included first
+#include <freertos/FreeRTOS.h>
 #include <AsyncTCP.h>
 #endif
 
+#if ESPHOME_VERSION_CODE >= VERSION_CODE(2021, 10, 0)
+using SSStream = esphome::uart::UARTComponent;
+#else
+using SSStream = Stream;
+#endif
+
 class StreamServerComponent : public esphome::Component {
 public:
     StreamServerComponent() = default;
-    explicit StreamServerComponent(Stream *stream) : stream_{stream} {}
+    explicit StreamServerComponent(SSStream *stream) : stream_{stream} {}
     void set_uart_parent(esphome::uart::UARTComponent *parent) { this->stream_ = parent; }
 
     void setup() override;
@@ -59,7 +73,7 @@ protected:
         bool disconnected{false};
     };
 
-    Stream *stream_{nullptr};
+    SSStream *stream_{nullptr};
     AsyncServer server_{0};
     uint16_t port_{6638};
     std::vector<uint8_t> recv_buf_{};