Tuya integration no longer waits for wifi

This commit is contained in:
Piotr Szulc 2024-07-01 20:54:54 +02:00
parent d0ab2a16a6
commit da3abf07c3
2 changed files with 12 additions and 37 deletions

View file

@ -1,18 +1,9 @@
#include "tuya.h" #include "tuya.h"
#include "esphome/components/network/util.h"
#include "esphome/core/gpio.h" #include "esphome/core/gpio.h"
#include "esphome/core/helpers.h" #include "esphome/core/helpers.h"
#include "esphome/core/log.h" #include "esphome/core/log.h"
#include "esphome/core/util.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 esphome {
namespace tuya { namespace tuya {
@ -21,6 +12,10 @@ static const int COMMAND_DELAY = 10;
static const int RECEIVE_TIMEOUT = 300; static const int RECEIVE_TIMEOUT = 300;
static const int MAX_RETRIES = 5; 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() { void Tuya::setup() {
this->set_interval("heartbeat", 15000, [this] { this->send_empty_command_(TuyaCommandType::HEARTBEAT); }); this->set_interval("heartbeat", 15000, [this] { this->send_empty_command_(TuyaCommandType::HEARTBEAT); });
if (this->status_pin_ != nullptr) { if (this->status_pin_ != nullptr) {
@ -244,7 +239,7 @@ void Tuya::handle_command_(uint8_t command, uint8_t version, const uint8_t *buff
break; break;
case TuyaCommandType::WIFI_RSSI: case TuyaCommandType::WIFI_RSSI:
this->send_command_( this->send_command_(
TuyaCommand{.cmd = TuyaCommandType::WIFI_RSSI, .payload = std::vector<uint8_t>{get_wifi_rssi_()}}); TuyaCommand{.cmd = TuyaCommandType::WIFI_RSSI, .payload = std::vector<uint8_t>{FAKE_WIFI_RSSI}});
break; break;
case TuyaCommandType::LOCAL_TIME_QUERY: case TuyaCommandType::LOCAL_TIME_QUERY:
#ifdef USE_TIME #ifdef USE_TIME
@ -494,37 +489,18 @@ void Tuya::set_status_pin_() {
} }
uint8_t Tuya::get_wifi_status_code_() { uint8_t Tuya::get_wifi_status_code_() {
uint8_t status = 0x02; uint8_t status = NET_STATUS_WIFI_CONNECTED;
if (network::is_connected()) { // Protocol version 3 also supports specifying when connected to "the cloud"
status = 0x03; if (this->protocol_version_ >= 0x03 && remote_is_connected()) {
status = NET_STATUS_CLOUD_CONNECTED;
// 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
};
return status; 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_() { 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_) { if (status == this->wifi_status_) {
return; return;

View file

@ -87,7 +87,7 @@ struct TuyaCommand {
class Tuya : public Component, public uart::UARTDevice { class Tuya : public Component, public uart::UARTDevice {
public: 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 setup() override;
void loop() override; void loop() override;
void dump_config() override; void dump_config() override;
@ -135,7 +135,6 @@ class Tuya : public Component, public uart::UARTDevice {
void set_status_pin_(); void set_status_pin_();
void send_wifi_status_(); void send_wifi_status_();
uint8_t get_wifi_status_code_(); uint8_t get_wifi_status_code_();
uint8_t get_wifi_rssi_();
#ifdef USE_TIME #ifdef USE_TIME
void send_local_time_(); void send_local_time_();