Improve tuya network status command (#4415)

This commit is contained in:
Andre Borie 2023-02-13 01:14:35 +00:00 committed by GitHub
parent 58eeb6b1b8
commit b8c0f88440
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 2 deletions

View file

@ -5,6 +5,10 @@
#include "esphome/core/util.h" #include "esphome/core/util.h"
#include "esphome/core/gpio.h" #include "esphome/core/gpio.h"
#ifdef USE_CAPTIVE_PORTAL
#include "esphome/components/captive_portal/captive_portal.h"
#endif
namespace esphome { namespace esphome {
namespace tuya { 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"); ESP_LOGE(TAG, "LOCAL_TIME_QUERY is not handled");
#endif #endif
break; 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<uint8_t>{wifi_status}});
ESP_LOGV(TAG, "Network status requested, reported as %i", wifi_status);
break;
}
default: default:
ESP_LOGE(TAG, "Invalid command (0x%02X) received", command); 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); 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; uint8_t status = 0x02;
if (network::is_connected()) { if (network::is_connected()) {
status = 0x03; status = 0x03;
@ -446,7 +459,19 @@ void Tuya::send_wifi_status_() {
if (this->protocol_version_ >= 0x03 && remote_is_connected()) { if (this->protocol_version_ >= 0x03 && remote_is_connected()) {
status = 0x04; 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_) { if (status == this->wifi_status_) {
return; return;

View file

@ -55,6 +55,7 @@ enum class TuyaCommandType : uint8_t {
DATAPOINT_QUERY = 0x08, DATAPOINT_QUERY = 0x08,
WIFI_TEST = 0x0E, WIFI_TEST = 0x0E,
LOCAL_TIME_QUERY = 0x1C, LOCAL_TIME_QUERY = 0x1C,
GET_NETWORK_STATUS = 0x2B,
}; };
enum class TuyaInitState : uint8_t { 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<uint8_t> data); void send_datapoint_command_(uint8_t datapoint_id, TuyaDatapointType datapoint_type, std::vector<uint8_t> data);
void set_status_pin_(); void set_status_pin_();
void send_wifi_status_(); void send_wifi_status_();
uint8_t get_wifi_status_code_();
#ifdef USE_TIME #ifdef USE_TIME
void send_local_time_(); void send_local_time_();