mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-05-26 15:48:28 +02:00
Tuya status gpio support (#3466)
This commit is contained in:
committed by
GitHub
parent
fea05e9d33
commit
0665acd190
@@ -3,6 +3,7 @@
|
||||
#include "esphome/components/network/util.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/util.h"
|
||||
#include "esphome/core/gpio.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace tuya {
|
||||
@@ -13,6 +14,9 @@ static const int RECEIVE_TIMEOUT = 300;
|
||||
|
||||
void Tuya::setup() {
|
||||
this->set_interval("heartbeat", 15000, [this] { this->send_empty_command_(TuyaCommandType::HEARTBEAT); });
|
||||
if (this->status_pin_.has_value()) {
|
||||
this->status_pin_.value()->digital_write(false);
|
||||
}
|
||||
}
|
||||
|
||||
void Tuya::loop() {
|
||||
@@ -49,9 +53,12 @@ void Tuya::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, " Datapoint %u: unknown", info.id);
|
||||
}
|
||||
}
|
||||
if ((this->gpio_status_ != -1) || (this->gpio_reset_ != -1)) {
|
||||
ESP_LOGCONFIG(TAG, " GPIO Configuration: status: pin %d, reset: pin %d (not supported)", this->gpio_status_,
|
||||
this->gpio_reset_);
|
||||
if ((this->status_pin_reported_ != -1) || (this->reset_pin_reported_ != -1)) {
|
||||
ESP_LOGCONFIG(TAG, " GPIO Configuration: status: pin %d, reset: pin %d (not supported)",
|
||||
this->status_pin_reported_, this->reset_pin_reported_);
|
||||
}
|
||||
if (this->status_pin_.has_value()) {
|
||||
LOG_PIN(" Status Pin: ", this->status_pin_.value());
|
||||
}
|
||||
ESP_LOGCONFIG(TAG, " Product: '%s'", this->product_.c_str());
|
||||
this->check_uart_settings(9600);
|
||||
@@ -164,16 +171,27 @@ void Tuya::handle_command_(uint8_t command, uint8_t version, const uint8_t *buff
|
||||
}
|
||||
case TuyaCommandType::CONF_QUERY: {
|
||||
if (len >= 2) {
|
||||
this->gpio_status_ = buffer[0];
|
||||
this->gpio_reset_ = buffer[1];
|
||||
this->status_pin_reported_ = buffer[0];
|
||||
this->reset_pin_reported_ = buffer[1];
|
||||
}
|
||||
if (this->init_state_ == TuyaInitState::INIT_CONF) {
|
||||
// If mcu returned status gpio, then we can omit sending wifi state
|
||||
if (this->gpio_status_ != -1) {
|
||||
if (this->status_pin_reported_ != -1) {
|
||||
this->init_state_ = TuyaInitState::INIT_DATAPOINT;
|
||||
this->send_empty_command_(TuyaCommandType::DATAPOINT_QUERY);
|
||||
bool is_pin_equals =
|
||||
this->status_pin_.has_value() && this->status_pin_.value()->get_pin() == this->status_pin_reported_;
|
||||
// Configure status pin toggling (if reported and configured) or WIFI_STATE periodic send
|
||||
if (is_pin_equals) {
|
||||
ESP_LOGV(TAG, "Configured status pin %i", this->status_pin_reported_);
|
||||
this->set_interval("wifi", 1000, [this] { this->set_status_pin_(); });
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Supplied status_pin does not equals the reported pin %i. TuyaMcu will work in limited mode.",
|
||||
this->status_pin_reported_);
|
||||
}
|
||||
} else {
|
||||
this->init_state_ = TuyaInitState::INIT_WIFI;
|
||||
ESP_LOGV(TAG, "Configured WIFI_STATE periodic send");
|
||||
this->set_interval("wifi", 1000, [this] { this->send_wifi_status_(); });
|
||||
}
|
||||
}
|
||||
@@ -397,16 +415,19 @@ void Tuya::send_empty_command_(TuyaCommandType command) {
|
||||
send_command_(TuyaCommand{.cmd = command, .payload = std::vector<uint8_t>{}});
|
||||
}
|
||||
|
||||
void Tuya::set_status_pin_() {
|
||||
bool is_network_ready = network::is_connected() && remote_is_connected();
|
||||
this->status_pin_.value()->digital_write(is_network_ready);
|
||||
}
|
||||
|
||||
void Tuya::send_wifi_status_() {
|
||||
uint8_t status = 0x02;
|
||||
if (network::is_connected()) {
|
||||
status = 0x03;
|
||||
|
||||
// Protocol version 3 also supports specifying when connected to "the cloud"
|
||||
if (this->protocol_version_ >= 0x03) {
|
||||
if (remote_is_connected()) {
|
||||
status = 0x04;
|
||||
}
|
||||
if (this->protocol_version_ >= 0x03 && remote_is_connected()) {
|
||||
status = 0x04;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user