mirror of
https://github.com/esphome/esphome.git
synced 2025-10-29 22:24:26 +00:00
Add support for fan preset modes (#5694)
Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
@@ -3,6 +3,7 @@ import esphome.config_validation as cv
|
||||
from esphome import automation
|
||||
from esphome.automation import maybe_simple_id
|
||||
from esphome.components import fan, output
|
||||
from esphome.components.fan import validate_preset_modes
|
||||
from esphome.const import (
|
||||
CONF_ID,
|
||||
CONF_DECAY_MODE,
|
||||
@@ -10,6 +11,7 @@ from esphome.const import (
|
||||
CONF_PIN_A,
|
||||
CONF_PIN_B,
|
||||
CONF_ENABLE_PIN,
|
||||
CONF_PRESET_MODES,
|
||||
)
|
||||
from .. import hbridge_ns
|
||||
|
||||
@@ -28,7 +30,6 @@ DECAY_MODE_OPTIONS = {
|
||||
# Actions
|
||||
BrakeAction = hbridge_ns.class_("BrakeAction", automation.Action)
|
||||
|
||||
|
||||
CONFIG_SCHEMA = fan.FAN_SCHEMA.extend(
|
||||
{
|
||||
cv.GenerateID(CONF_ID): cv.declare_id(HBridgeFan),
|
||||
@@ -39,6 +40,7 @@ CONFIG_SCHEMA = fan.FAN_SCHEMA.extend(
|
||||
),
|
||||
cv.Optional(CONF_SPEED_COUNT, default=100): cv.int_range(min=1),
|
||||
cv.Optional(CONF_ENABLE_PIN): cv.use_id(output.FloatOutput),
|
||||
cv.Optional(CONF_PRESET_MODES): validate_preset_modes,
|
||||
}
|
||||
).extend(cv.COMPONENT_SCHEMA)
|
||||
|
||||
@@ -69,3 +71,6 @@ async def to_code(config):
|
||||
if CONF_ENABLE_PIN in config:
|
||||
enable_pin = await cg.get_variable(config[CONF_ENABLE_PIN])
|
||||
cg.add(var.set_enable_pin(enable_pin))
|
||||
|
||||
if CONF_PRESET_MODES in config:
|
||||
cg.add(var.set_preset_modes(config[CONF_PRESET_MODES]))
|
||||
|
||||
@@ -33,7 +33,12 @@ void HBridgeFan::setup() {
|
||||
restore->apply(*this);
|
||||
this->write_state_();
|
||||
}
|
||||
|
||||
// Construct traits
|
||||
this->traits_ = fan::FanTraits(this->oscillating_ != nullptr, true, true, this->speed_count_);
|
||||
this->traits_.set_supported_preset_modes(this->preset_modes_);
|
||||
}
|
||||
|
||||
void HBridgeFan::dump_config() {
|
||||
LOG_FAN("", "H-Bridge Fan", this);
|
||||
if (this->decay_mode_ == DECAY_MODE_SLOW) {
|
||||
@@ -42,9 +47,7 @@ void HBridgeFan::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, " Decay Mode: Fast");
|
||||
}
|
||||
}
|
||||
fan::FanTraits HBridgeFan::get_traits() {
|
||||
return fan::FanTraits(this->oscillating_ != nullptr, true, true, this->speed_count_);
|
||||
}
|
||||
|
||||
void HBridgeFan::control(const fan::FanCall &call) {
|
||||
if (call.get_state().has_value())
|
||||
this->state = *call.get_state();
|
||||
@@ -54,10 +57,12 @@ void HBridgeFan::control(const fan::FanCall &call) {
|
||||
this->oscillating = *call.get_oscillating();
|
||||
if (call.get_direction().has_value())
|
||||
this->direction = *call.get_direction();
|
||||
this->preset_mode = call.get_preset_mode();
|
||||
|
||||
this->write_state_();
|
||||
this->publish_state();
|
||||
}
|
||||
|
||||
void HBridgeFan::write_state_() {
|
||||
float speed = this->state ? static_cast<float>(this->speed) / static_cast<float>(this->speed_count_) : 0.0f;
|
||||
if (speed == 0.0f) { // off means idle
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <set>
|
||||
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/components/output/binary_output.h"
|
||||
#include "esphome/components/output/float_output.h"
|
||||
@@ -20,10 +22,11 @@ class HBridgeFan : public Component, public fan::Fan {
|
||||
void set_pin_a(output::FloatOutput *pin_a) { pin_a_ = pin_a; }
|
||||
void set_pin_b(output::FloatOutput *pin_b) { pin_b_ = pin_b; }
|
||||
void set_enable_pin(output::FloatOutput *enable) { enable_ = enable; }
|
||||
void set_preset_modes(const std::set<std::string> &presets) { preset_modes_ = presets; }
|
||||
|
||||
void setup() override;
|
||||
void dump_config() override;
|
||||
fan::FanTraits get_traits() override;
|
||||
fan::FanTraits get_traits() override { return this->traits_; }
|
||||
|
||||
fan::FanCall brake();
|
||||
|
||||
@@ -34,6 +37,8 @@ class HBridgeFan : public Component, public fan::Fan {
|
||||
output::BinaryOutput *oscillating_{nullptr};
|
||||
int speed_count_{};
|
||||
DecayMode decay_mode_{DECAY_MODE_SLOW};
|
||||
fan::FanTraits traits_;
|
||||
std::set<std::string> preset_modes_{};
|
||||
|
||||
void control(const fan::FanCall &call) override;
|
||||
void write_state_();
|
||||
|
||||
Reference in New Issue
Block a user