Add support for controlling Tuya fan direction (#1409)

This commit is contained in:
Ryan Mounce 2021-05-05 13:54:31 +09:30 committed by GitHub
parent 06b8e4df27
commit ffc6fe9ca0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 2 deletions

View file

@ -8,6 +8,7 @@ DEPENDENCIES = ["tuya"]
CONF_SPEED_DATAPOINT = "speed_datapoint" CONF_SPEED_DATAPOINT = "speed_datapoint"
CONF_OSCILLATION_DATAPOINT = "oscillation_datapoint" CONF_OSCILLATION_DATAPOINT = "oscillation_datapoint"
CONF_DIRECTION_DATAPOINT = "direction_datapoint"
TuyaFan = tuya_ns.class_("TuyaFan", cg.Component) TuyaFan = tuya_ns.class_("TuyaFan", cg.Component)
@ -19,6 +20,7 @@ CONFIG_SCHEMA = cv.All(
cv.Optional(CONF_OSCILLATION_DATAPOINT): cv.uint8_t, cv.Optional(CONF_OSCILLATION_DATAPOINT): cv.uint8_t,
cv.Optional(CONF_SPEED_DATAPOINT): cv.uint8_t, cv.Optional(CONF_SPEED_DATAPOINT): cv.uint8_t,
cv.Optional(CONF_SWITCH_DATAPOINT): cv.uint8_t, cv.Optional(CONF_SWITCH_DATAPOINT): cv.uint8_t,
cv.Optional(CONF_DIRECTION_DATAPOINT): cv.uint8_t,
cv.Optional(CONF_SPEED_COUNT, default=3): cv.int_range(min=1, max=256), cv.Optional(CONF_SPEED_COUNT, default=3): cv.int_range(min=1, max=256),
} }
).extend(cv.COMPONENT_SCHEMA), ).extend(cv.COMPONENT_SCHEMA),
@ -41,3 +43,5 @@ def to_code(config):
cg.add(var.set_switch_id(config[CONF_SWITCH_DATAPOINT])) cg.add(var.set_switch_id(config[CONF_SWITCH_DATAPOINT]))
if CONF_OSCILLATION_DATAPOINT in config: if CONF_OSCILLATION_DATAPOINT in config:
cg.add(var.set_oscillation_id(config[CONF_OSCILLATION_DATAPOINT])) cg.add(var.set_oscillation_id(config[CONF_OSCILLATION_DATAPOINT]))
if CONF_DIRECTION_DATAPOINT in config:
cg.add(var.set_direction_id(config[CONF_DIRECTION_DATAPOINT]))

View file

@ -8,8 +8,8 @@ namespace tuya {
static const char *TAG = "tuya.fan"; static const char *TAG = "tuya.fan";
void TuyaFan::setup() { void TuyaFan::setup() {
auto traits = auto traits = fan::FanTraits(this->oscillation_id_.has_value(), this->speed_id_.has_value(),
fan::FanTraits(this->oscillation_id_.has_value(), this->speed_id_.has_value(), false, this->speed_count_); this->direction_id_.has_value(), this->speed_count_);
this->fan_->set_traits(traits); this->fan_->set_traits(traits);
if (this->speed_id_.has_value()) { 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)); 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(); }); 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_); ESP_LOGCONFIG(TAG, " Switch has datapoint ID %u", *this->switch_id_);
if (this->oscillation_id_.has_value()) if (this->oscillation_id_.has_value())
ESP_LOGCONFIG(TAG, " Oscillation has datapoint ID %u", *this->oscillation_id_); 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() { void TuyaFan::write_state() {
@ -70,6 +81,15 @@ void TuyaFan::write_state() {
this->parent_->set_datapoint_value(datapoint); this->parent_->set_datapoint_value(datapoint);
ESP_LOGD(TAG, "Setting oscillating: %s", ONOFF(this->fan_->oscillating)); 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()) { if (this->speed_id_.has_value()) {
TuyaDatapoint datapoint{}; TuyaDatapoint datapoint{};
datapoint.id = *this->speed_id_; datapoint.id = *this->speed_id_;

View file

@ -15,17 +15,20 @@ class TuyaFan : public Component {
void set_speed_id(uint8_t speed_id) { this->speed_id_ = speed_id; } void set_speed_id(uint8_t speed_id) { this->speed_id_ = speed_id; }
void set_switch_id(uint8_t switch_id) { this->switch_id_ = switch_id; } void set_switch_id(uint8_t switch_id) { this->switch_id_ = switch_id; }
void set_oscillation_id(uint8_t oscillation_id) { this->oscillation_id_ = oscillation_id; } void set_oscillation_id(uint8_t oscillation_id) { this->oscillation_id_ = oscillation_id; }
void set_direction_id(uint8_t direction_id) { this->direction_id_ = direction_id; }
void write_state(); void write_state();
protected: protected:
void update_speed_(uint32_t value); void update_speed_(uint32_t value);
void update_switch_(uint32_t value); void update_switch_(uint32_t value);
void update_oscillation_(uint32_t value); void update_oscillation_(uint32_t value);
void update_direction_(uint32_t value);
Tuya *parent_; Tuya *parent_;
optional<uint8_t> speed_id_{}; optional<uint8_t> speed_id_{};
optional<uint8_t> switch_id_{}; optional<uint8_t> switch_id_{};
optional<uint8_t> oscillation_id_{}; optional<uint8_t> oscillation_id_{};
optional<uint8_t> direction_id_{};
fan::FanState *fan_; fan::FanState *fan_;
int speed_count_{}; int speed_count_{};
}; };