make output optional for speed fan (#6274)

Co-authored-by: Samuel Sieb <samuel@sieb.net>
This commit is contained in:
Samuel Sieb 2024-02-25 10:23:01 -08:00 committed by Jesse Hills
parent d814ed1d4a
commit e66e135a63
No known key found for this signature in database
GPG Key ID: BEAAE804EFD8E83A
3 changed files with 13 additions and 8 deletions

View File

@ -19,7 +19,7 @@ SpeedFan = speed_ns.class_("SpeedFan", cg.Component, fan.Fan)
CONFIG_SCHEMA = fan.FAN_SCHEMA.extend( CONFIG_SCHEMA = fan.FAN_SCHEMA.extend(
{ {
cv.GenerateID(CONF_OUTPUT_ID): cv.declare_id(SpeedFan), cv.GenerateID(CONF_OUTPUT_ID): cv.declare_id(SpeedFan),
cv.Required(CONF_OUTPUT): cv.use_id(output.FloatOutput), cv.Optional(CONF_OUTPUT): cv.use_id(output.FloatOutput),
cv.Optional(CONF_OSCILLATION_OUTPUT): cv.use_id(output.BinaryOutput), cv.Optional(CONF_OSCILLATION_OUTPUT): cv.use_id(output.BinaryOutput),
cv.Optional(CONF_DIRECTION_OUTPUT): cv.use_id(output.BinaryOutput), cv.Optional(CONF_DIRECTION_OUTPUT): cv.use_id(output.BinaryOutput),
cv.Optional(CONF_SPEED): cv.invalid( cv.Optional(CONF_SPEED): cv.invalid(
@ -32,11 +32,14 @@ CONFIG_SCHEMA = fan.FAN_SCHEMA.extend(
async def to_code(config): async def to_code(config):
output_ = await cg.get_variable(config[CONF_OUTPUT]) var = cg.new_Pvariable(config[CONF_OUTPUT_ID], config[CONF_SPEED_COUNT])
var = cg.new_Pvariable(config[CONF_OUTPUT_ID], output_, config[CONF_SPEED_COUNT])
await cg.register_component(var, config) await cg.register_component(var, config)
await fan.register_fan(var, config) await fan.register_fan(var, config)
if CONF_OUTPUT in config:
output_ = await cg.get_variable(config[CONF_OUTPUT])
cg.add(var.set_output(output_))
if CONF_OSCILLATION_OUTPUT in config: if CONF_OSCILLATION_OUTPUT in config:
oscillation_output = await cg.get_variable(config[CONF_OSCILLATION_OUTPUT]) oscillation_output = await cg.get_variable(config[CONF_OSCILLATION_OUTPUT])
cg.add(var.set_oscillating(oscillation_output)) cg.add(var.set_oscillating(oscillation_output))

View File

@ -36,9 +36,10 @@ void SpeedFan::control(const fan::FanCall &call) {
} }
void SpeedFan::write_state_() { void SpeedFan::write_state_() {
if (this->output_ != nullptr) {
float speed = this->state ? static_cast<float>(this->speed) / static_cast<float>(this->speed_count_) : 0.0f; float speed = this->state ? static_cast<float>(this->speed) / static_cast<float>(this->speed_count_) : 0.0f;
this->output_->set_level(speed); this->output_->set_level(speed);
}
if (this->oscillating_ != nullptr) if (this->oscillating_ != nullptr)
this->oscillating_->set_state(this->oscillating); this->oscillating_->set_state(this->oscillating);
if (this->direction_ != nullptr) if (this->direction_ != nullptr)

View File

@ -12,9 +12,10 @@ namespace speed {
class SpeedFan : public Component, public fan::Fan { class SpeedFan : public Component, public fan::Fan {
public: public:
SpeedFan(output::FloatOutput *output, int speed_count) : output_(output), speed_count_(speed_count) {} SpeedFan(int speed_count) : speed_count_(speed_count) {}
void setup() override; void setup() override;
void dump_config() override; void dump_config() override;
void set_output(output::FloatOutput *output) { this->output_ = output; }
void set_oscillating(output::BinaryOutput *oscillating) { this->oscillating_ = oscillating; } void set_oscillating(output::BinaryOutput *oscillating) { this->oscillating_ = oscillating; }
void set_direction(output::BinaryOutput *direction) { this->direction_ = direction; } void set_direction(output::BinaryOutput *direction) { this->direction_ = direction; }
void set_preset_modes(const std::set<std::string> &presets) { this->preset_modes_ = presets; } void set_preset_modes(const std::set<std::string> &presets) { this->preset_modes_ = presets; }
@ -24,7 +25,7 @@ class SpeedFan : public Component, public fan::Fan {
void control(const fan::FanCall &call) override; void control(const fan::FanCall &call) override;
void write_state_(); void write_state_();
output::FloatOutput *output_; output::FloatOutput *output_{nullptr};
output::BinaryOutput *oscillating_{nullptr}; output::BinaryOutput *oscillating_{nullptr};
output::BinaryOutput *direction_{nullptr}; output::BinaryOutput *direction_{nullptr};
int speed_count_{}; int speed_count_{};