From da3abf07c37e90506ce059e29528fe33d46f6cd1 Mon Sep 17 00:00:00 2001 From: Piotr Szulc Date: Mon, 1 Jul 2024 20:54:54 +0200 Subject: [PATCH] Tuya integration no longer waits for wifi --- esphome/components/tuya/tuya.cpp | 46 ++++++++------------------------ esphome/components/tuya/tuya.h | 3 +-- 2 files changed, 12 insertions(+), 37 deletions(-) diff --git a/esphome/components/tuya/tuya.cpp b/esphome/components/tuya/tuya.cpp index 1443d10254..de3d5e97e7 100644 --- a/esphome/components/tuya/tuya.cpp +++ b/esphome/components/tuya/tuya.cpp @@ -1,18 +1,9 @@ #include "tuya.h" -#include "esphome/components/network/util.h" #include "esphome/core/gpio.h" #include "esphome/core/helpers.h" #include "esphome/core/log.h" #include "esphome/core/util.h" -#ifdef USE_WIFI -#include "esphome/components/wifi/wifi_component.h" -#endif - -#ifdef USE_CAPTIVE_PORTAL -#include "esphome/components/captive_portal/captive_portal.h" -#endif - namespace esphome { namespace tuya { @@ -21,6 +12,10 @@ static const int COMMAND_DELAY = 10; static const int RECEIVE_TIMEOUT = 300; static const int MAX_RETRIES = 5; +static const uint8_t NET_STATUS_WIFI_CONNECTED = 0x03; +static const uint8_t NET_STATUS_CLOUD_CONNECTED = 0x04; +static const uint8_t FAKE_WIFI_RSSI = 100; + void Tuya::setup() { this->set_interval("heartbeat", 15000, [this] { this->send_empty_command_(TuyaCommandType::HEARTBEAT); }); if (this->status_pin_ != nullptr) { @@ -244,7 +239,7 @@ void Tuya::handle_command_(uint8_t command, uint8_t version, const uint8_t *buff break; case TuyaCommandType::WIFI_RSSI: this->send_command_( - TuyaCommand{.cmd = TuyaCommandType::WIFI_RSSI, .payload = std::vector{get_wifi_rssi_()}}); + TuyaCommand{.cmd = TuyaCommandType::WIFI_RSSI, .payload = std::vector{FAKE_WIFI_RSSI}}); break; case TuyaCommandType::LOCAL_TIME_QUERY: #ifdef USE_TIME @@ -494,37 +489,18 @@ void Tuya::set_status_pin_() { } uint8_t Tuya::get_wifi_status_code_() { - uint8_t status = 0x02; + uint8_t status = NET_STATUS_WIFI_CONNECTED; - if (network::is_connected()) { - status = 0x03; - - // Protocol version 3 also supports specifying when connected to "the cloud" - if (this->protocol_version_ >= 0x03 && remote_is_connected()) { - status = 0x04; - } - } else { -#ifdef USE_CAPTIVE_PORTAL - if (captive_portal::global_captive_portal != nullptr && captive_portal::global_captive_portal->is_active()) { - status = 0x01; - } -#endif - }; + // Protocol version 3 also supports specifying when connected to "the cloud" + if (this->protocol_version_ >= 0x03 && remote_is_connected()) { + status = NET_STATUS_CLOUD_CONNECTED; + } return status; } -uint8_t Tuya::get_wifi_rssi_() { -#ifdef USE_WIFI - if (wifi::global_wifi_component != nullptr) - return wifi::global_wifi_component->wifi_rssi(); -#endif - - return 0; -} - void Tuya::send_wifi_status_() { - uint8_t status = this->get_wifi_status_code_(); + const uint8_t status = get_wifi_status_code_(); if (status == this->wifi_status_) { return; diff --git a/esphome/components/tuya/tuya.h b/esphome/components/tuya/tuya.h index 76431ddfe4..4a6ffffb3b 100644 --- a/esphome/components/tuya/tuya.h +++ b/esphome/components/tuya/tuya.h @@ -87,7 +87,7 @@ struct TuyaCommand { class Tuya : public Component, public uart::UARTDevice { public: - float get_setup_priority() const override { return setup_priority::LATE; } + float get_setup_priority() const override { return setup_priority::DATA; } void setup() override; void loop() override; void dump_config() override; @@ -135,7 +135,6 @@ class Tuya : public Component, public uart::UARTDevice { void set_status_pin_(); void send_wifi_status_(); uint8_t get_wifi_status_code_(); - uint8_t get_wifi_rssi_(); #ifdef USE_TIME void send_local_time_();