mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-06-05 12:30:28 +02:00
Add support for controlling Tuya fan direction (#1409)
This commit is contained in:
@@ -8,8 +8,8 @@ namespace tuya {
|
||||
static const char *TAG = "tuya.fan";
|
||||
|
||||
void TuyaFan::setup() {
|
||||
auto traits =
|
||||
fan::FanTraits(this->oscillation_id_.has_value(), this->speed_id_.has_value(), false, this->speed_count_);
|
||||
auto traits = fan::FanTraits(this->oscillation_id_.has_value(), this->speed_id_.has_value(),
|
||||
this->direction_id_.has_value(), this->speed_count_);
|
||||
this->fan_->set_traits(traits);
|
||||
|
||||
if (this->speed_id_.has_value()) {
|
||||
@@ -39,6 +39,15 @@ void TuyaFan::setup() {
|
||||
ESP_LOGD(TAG, "MCU reported oscillation is: %s", ONOFF(datapoint.value_bool));
|
||||
});
|
||||
}
|
||||
if (this->direction_id_.has_value()) {
|
||||
this->parent_->register_listener(*this->direction_id_, [this](TuyaDatapoint datapoint) {
|
||||
auto call = this->fan_->make_call();
|
||||
call.set_direction(datapoint.value_bool ? fan::FAN_DIRECTION_REVERSE : fan::FAN_DIRECTION_FORWARD);
|
||||
call.perform();
|
||||
ESP_LOGD(TAG, "MCU reported reverse direction is: %s", ONOFF(datapoint.value_bool));
|
||||
});
|
||||
}
|
||||
|
||||
this->fan_->add_on_state_callback([this]() { this->write_state(); });
|
||||
}
|
||||
|
||||
@@ -51,6 +60,8 @@ void TuyaFan::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, " Switch has datapoint ID %u", *this->switch_id_);
|
||||
if (this->oscillation_id_.has_value())
|
||||
ESP_LOGCONFIG(TAG, " Oscillation has datapoint ID %u", *this->oscillation_id_);
|
||||
if (this->direction_id_.has_value())
|
||||
ESP_LOGCONFIG(TAG, " Direction has datapoint ID %u", *this->direction_id_);
|
||||
}
|
||||
|
||||
void TuyaFan::write_state() {
|
||||
@@ -70,6 +81,15 @@ void TuyaFan::write_state() {
|
||||
this->parent_->set_datapoint_value(datapoint);
|
||||
ESP_LOGD(TAG, "Setting oscillating: %s", ONOFF(this->fan_->oscillating));
|
||||
}
|
||||
if (this->direction_id_.has_value()) {
|
||||
TuyaDatapoint datapoint{};
|
||||
datapoint.id = *this->direction_id_;
|
||||
datapoint.type = TuyaDatapointType::BOOLEAN;
|
||||
bool enable = this->fan_->direction == fan::FAN_DIRECTION_REVERSE;
|
||||
datapoint.value_bool = enable;
|
||||
this->parent_->set_datapoint_value(datapoint);
|
||||
ESP_LOGD(TAG, "Setting reverse direction: %s", ONOFF(enable));
|
||||
}
|
||||
if (this->speed_id_.has_value()) {
|
||||
TuyaDatapoint datapoint{};
|
||||
datapoint.id = *this->speed_id_;
|
||||
|
||||
Reference in New Issue
Block a user