mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-06-04 03:48:29 +02:00
Refactor fan platform to resemble climate/cover platforms (#2848)
Co-authored-by: Oxan van Leeuwen <oxan@oxanvanleeuwen.nl> Co-authored-by: rob-deutsch <robzyb+altgithub@gmail.com> Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
@@ -10,7 +10,7 @@ CONF_SPEED_DATAPOINT = "speed_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, fan.Fan)
|
||||
|
||||
CONFIG_SCHEMA = cv.All(
|
||||
fan.FAN_SCHEMA.extend(
|
||||
@@ -30,12 +30,10 @@ CONFIG_SCHEMA = cv.All(
|
||||
|
||||
async def to_code(config):
|
||||
parent = await cg.get_variable(config[CONF_TUYA_ID])
|
||||
state = await fan.create_fan_state(config)
|
||||
|
||||
var = cg.new_Pvariable(
|
||||
config[CONF_OUTPUT_ID], parent, state, config[CONF_SPEED_COUNT]
|
||||
)
|
||||
var = cg.new_Pvariable(config[CONF_OUTPUT_ID], parent, config[CONF_SPEED_COUNT])
|
||||
await cg.register_component(var, config)
|
||||
await fan.register_fan(var, config)
|
||||
|
||||
if CONF_SPEED_DATAPOINT in config:
|
||||
cg.add(var.set_speed_id(config[CONF_SPEED_DATAPOINT]))
|
||||
|
||||
@@ -8,52 +8,48 @@ namespace tuya {
|
||||
static const char *const TAG = "tuya.fan";
|
||||
|
||||
void TuyaFan::setup() {
|
||||
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()) {
|
||||
this->parent_->register_listener(*this->speed_id_, [this](const TuyaDatapoint &datapoint) {
|
||||
ESP_LOGV(TAG, "MCU reported speed of: %d", datapoint.value_enum);
|
||||
auto call = this->fan_->make_call();
|
||||
if (datapoint.value_enum < this->speed_count_)
|
||||
call.set_speed(datapoint.value_enum + 1);
|
||||
else
|
||||
ESP_LOGCONFIG(TAG, "Speed has invalid value %d", datapoint.value_enum);
|
||||
call.perform();
|
||||
if (datapoint.value_enum >= this->speed_count_) {
|
||||
ESP_LOGE(TAG, "Speed has invalid value %d", datapoint.value_enum);
|
||||
} else {
|
||||
this->speed = datapoint.value_enum + 1;
|
||||
this->publish_state();
|
||||
}
|
||||
});
|
||||
}
|
||||
if (this->switch_id_.has_value()) {
|
||||
this->parent_->register_listener(*this->switch_id_, [this](const TuyaDatapoint &datapoint) {
|
||||
ESP_LOGV(TAG, "MCU reported switch is: %s", ONOFF(datapoint.value_bool));
|
||||
auto call = this->fan_->make_call();
|
||||
call.set_state(datapoint.value_bool);
|
||||
call.perform();
|
||||
this->state = datapoint.value_bool;
|
||||
this->publish_state();
|
||||
});
|
||||
}
|
||||
if (this->oscillation_id_.has_value()) {
|
||||
this->parent_->register_listener(*this->oscillation_id_, [this](const TuyaDatapoint &datapoint) {
|
||||
ESP_LOGV(TAG, "MCU reported oscillation is: %s", ONOFF(datapoint.value_bool));
|
||||
auto call = this->fan_->make_call();
|
||||
call.set_oscillating(datapoint.value_bool);
|
||||
call.perform();
|
||||
this->oscillating = datapoint.value_bool;
|
||||
this->publish_state();
|
||||
});
|
||||
}
|
||||
if (this->direction_id_.has_value()) {
|
||||
this->parent_->register_listener(*this->direction_id_, [this](const 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->direction = datapoint.value_bool ? fan::FanDirection::REVERSE : fan::FanDirection::FORWARD;
|
||||
this->publish_state();
|
||||
});
|
||||
}
|
||||
|
||||
this->fan_->add_on_state_callback([this]() { this->write_state(); });
|
||||
this->parent_->add_on_initialized_callback([this]() {
|
||||
auto restored = this->restore_state_();
|
||||
if (restored)
|
||||
restored->to_call(*this).perform();
|
||||
});
|
||||
}
|
||||
|
||||
void TuyaFan::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "Tuya Fan:");
|
||||
ESP_LOGCONFIG(TAG, " Speed count %d", this->speed_count_);
|
||||
LOG_FAN("", "Tuya Fan", this);
|
||||
if (this->speed_id_.has_value())
|
||||
ESP_LOGCONFIG(TAG, " Speed has datapoint ID %u", *this->speed_id_);
|
||||
if (this->switch_id_.has_value())
|
||||
@@ -64,29 +60,26 @@ void TuyaFan::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, " Direction has datapoint ID %u", *this->direction_id_);
|
||||
}
|
||||
|
||||
void TuyaFan::write_state() {
|
||||
if (this->switch_id_.has_value()) {
|
||||
ESP_LOGV(TAG, "Setting switch: %s", ONOFF(this->fan_->state));
|
||||
this->parent_->set_boolean_datapoint_value(*this->switch_id_, this->fan_->state);
|
||||
fan::FanTraits TuyaFan::get_traits() {
|
||||
return fan::FanTraits(this->oscillation_id_.has_value(), this->speed_id_.has_value(), this->direction_id_.has_value(),
|
||||
this->speed_count_);
|
||||
}
|
||||
|
||||
void TuyaFan::control(const fan::FanCall &call) {
|
||||
if (this->switch_id_.has_value() && call.get_state().has_value()) {
|
||||
this->parent_->set_boolean_datapoint_value(*this->switch_id_, *call.get_state());
|
||||
}
|
||||
if (this->oscillation_id_.has_value()) {
|
||||
ESP_LOGV(TAG, "Setting oscillating: %s", ONOFF(this->fan_->oscillating));
|
||||
this->parent_->set_boolean_datapoint_value(*this->oscillation_id_, this->fan_->oscillating);
|
||||
if (this->oscillation_id_.has_value() && call.get_oscillating().has_value()) {
|
||||
this->parent_->set_boolean_datapoint_value(*this->oscillation_id_, *call.get_oscillating());
|
||||
}
|
||||
if (this->direction_id_.has_value()) {
|
||||
bool enable = this->fan_->direction == fan::FAN_DIRECTION_REVERSE;
|
||||
ESP_LOGV(TAG, "Setting reverse direction: %s", ONOFF(enable));
|
||||
if (this->direction_id_.has_value() && call.get_direction().has_value()) {
|
||||
bool enable = *call.get_direction() == fan::FanDirection::REVERSE;
|
||||
this->parent_->set_enum_datapoint_value(*this->direction_id_, enable);
|
||||
}
|
||||
if (this->speed_id_.has_value()) {
|
||||
ESP_LOGV(TAG, "Setting speed: %d", this->fan_->speed);
|
||||
this->parent_->set_enum_datapoint_value(*this->speed_id_, this->fan_->speed - 1);
|
||||
if (this->speed_id_.has_value() && call.get_speed().has_value()) {
|
||||
this->parent_->set_enum_datapoint_value(*this->speed_id_, *call.get_speed() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
// We need a higher priority than the FanState component to make sure that the traits are set
|
||||
// when that component sets itself up.
|
||||
float TuyaFan::get_setup_priority() const { return fan_->get_setup_priority() + 1.0f; }
|
||||
|
||||
} // namespace tuya
|
||||
} // namespace esphome
|
||||
|
||||
@@ -2,35 +2,31 @@
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/tuya/tuya.h"
|
||||
#include "esphome/components/fan/fan_state.h"
|
||||
#include "esphome/components/fan/fan.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace tuya {
|
||||
|
||||
class TuyaFan : public Component {
|
||||
class TuyaFan : public Component, public fan::Fan {
|
||||
public:
|
||||
TuyaFan(Tuya *parent, fan::FanState *fan, int speed_count) : parent_(parent), fan_(fan), speed_count_(speed_count) {}
|
||||
TuyaFan(Tuya *parent, int speed_count) : parent_(parent), speed_count_(speed_count) {}
|
||||
void setup() override;
|
||||
float get_setup_priority() const override;
|
||||
void dump_config() override;
|
||||
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_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();
|
||||
|
||||
fan::FanTraits get_traits() override;
|
||||
|
||||
protected:
|
||||
void update_speed_(uint32_t value);
|
||||
void update_switch_(uint32_t value);
|
||||
void update_oscillation_(uint32_t value);
|
||||
void update_direction_(uint32_t value);
|
||||
void control(const fan::FanCall &call) override;
|
||||
|
||||
Tuya *parent_;
|
||||
optional<uint8_t> speed_id_{};
|
||||
optional<uint8_t> switch_id_{};
|
||||
optional<uint8_t> oscillation_id_{};
|
||||
optional<uint8_t> direction_id_{};
|
||||
fan::FanState *fan_;
|
||||
int speed_count_{};
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user