From b8c0f884404f3c5950d36d8715050186948e37fc Mon Sep 17 00:00:00 2001 From: Andre Borie Date: Mon, 13 Feb 2023 01:14:35 +0000 Subject: [PATCH] Improve tuya network status command (#4415) --- esphome/components/tuya/tuya.cpp | 29 +++++++++++++++++++++++++++-- esphome/components/tuya/tuya.h | 2 ++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/esphome/components/tuya/tuya.cpp b/esphome/components/tuya/tuya.cpp index 89a687e5a6..efe214f6a3 100644 --- a/esphome/components/tuya/tuya.cpp +++ b/esphome/components/tuya/tuya.cpp @@ -5,6 +5,10 @@ #include "esphome/core/util.h" #include "esphome/core/gpio.h" +#ifdef USE_CAPTIVE_PORTAL +#include "esphome/components/captive_portal/captive_portal.h" +#endif + namespace esphome { namespace tuya { @@ -243,6 +247,14 @@ void Tuya::handle_command_(uint8_t command, uint8_t version, const uint8_t *buff ESP_LOGE(TAG, "LOCAL_TIME_QUERY is not handled"); #endif break; + case TuyaCommandType::GET_NETWORK_STATUS: { + uint8_t wifi_status = this->get_wifi_status_code_(); + + this->send_command_( + TuyaCommand{.cmd = TuyaCommandType::GET_NETWORK_STATUS, .payload = std::vector{wifi_status}}); + ESP_LOGV(TAG, "Network status requested, reported as %i", wifi_status); + break; + } default: ESP_LOGE(TAG, "Invalid command (0x%02X) received", command); } @@ -437,8 +449,9 @@ void Tuya::set_status_pin_() { this->status_pin_.value()->digital_write(is_network_ready); } -void Tuya::send_wifi_status_() { +uint8_t Tuya::get_wifi_status_code_() { uint8_t status = 0x02; + if (network::is_connected()) { status = 0x03; @@ -446,7 +459,19 @@ void Tuya::send_wifi_status_() { 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; +} + +void Tuya::send_wifi_status_() { + uint8_t status = this->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 5839dfbec0..cc13a64bdb 100644 --- a/esphome/components/tuya/tuya.h +++ b/esphome/components/tuya/tuya.h @@ -55,6 +55,7 @@ enum class TuyaCommandType : uint8_t { DATAPOINT_QUERY = 0x08, WIFI_TEST = 0x0E, LOCAL_TIME_QUERY = 0x1C, + GET_NETWORK_STATUS = 0x2B, }; enum class TuyaInitState : uint8_t { @@ -120,6 +121,7 @@ class Tuya : public Component, public uart::UARTDevice { void send_datapoint_command_(uint8_t datapoint_id, TuyaDatapointType datapoint_type, std::vector data); void set_status_pin_(); void send_wifi_status_(); + uint8_t get_wifi_status_code_(); #ifdef USE_TIME void send_local_time_();