Browse Source

Fix compatibility with ESPHome 2023.4

Since ESPHome 2023.4 the `stream_server` component doesn't listen on the specified port. I think this was broken by esphome/esphome#4574.
Joshua Spence 2 years ago
parent
commit
4ef23f01d4
1 changed files with 5 additions and 0 deletions
  1. 5 0
      components/stream_server/stream_server.cpp

+ 5 - 0
components/stream_server/stream_server.cpp

@@ -3,6 +3,7 @@
 #include "esphome/core/helpers.h"
 #include "esphome/core/log.h"
 #include "esphome/core/util.h"
+#include "esphome/core/version.h"
 
 #include "esphome/components/network/util.h"
 #include "esphome/components/socket/socket.h"
@@ -18,7 +19,11 @@ void StreamServerComponent::setup() {
     this->buf_ = std::unique_ptr<uint8_t[]>{new uint8_t[this->buf_size_]};
 
     struct sockaddr_storage bind_addr;
+#if ESPHOME_VERSION_CODE >= VERSION_CODE(2023, 4, 0)
+    socklen_t bind_addrlen = socket::set_sockaddr_any(reinterpret_cast<struct sockaddr *>(&bind_addr), sizeof(bind_addr), this->port_);
+#else
     socklen_t bind_addrlen = socket::set_sockaddr_any(reinterpret_cast<struct sockaddr *>(&bind_addr), sizeof(bind_addr), htons(this->port_));
+#endif
 
     this->socket_ = socket::socket_ip(SOCK_STREAM, PF_INET);
     this->socket_->setblocking(false);