Add support for fan preset modes (#5694)

Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
Tucker Kern 2023-12-11 22:13:26 -07:00 committed by GitHub
parent 47665164e8
commit ad79e4fe24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 277 additions and 17 deletions

View File

@ -365,6 +365,7 @@ message ListEntitiesFanResponse {
bool disabled_by_default = 9;
string icon = 10;
EntityCategory entity_category = 11;
repeated string supported_preset_modes = 12;
}
enum FanSpeed {
FAN_SPEED_LOW = 0;
@ -387,6 +388,7 @@ message FanStateResponse {
FanSpeed speed = 4 [deprecated = true];
FanDirection direction = 5;
int32 speed_level = 6;
string preset_mode = 7;
}
message FanCommandRequest {
option (id) = 31;
@ -405,6 +407,8 @@ message FanCommandRequest {
FanDirection direction = 9;
bool has_speed_level = 10;
int32 speed_level = 11;
bool has_preset_mode = 12;
string preset_mode = 13;
}
// ==================== LIGHT ====================

View File

@ -293,6 +293,8 @@ bool APIConnection::send_fan_state(fan::Fan *fan) {
}
if (traits.supports_direction())
resp.direction = static_cast<enums::FanDirection>(fan->direction);
if (traits.supports_preset_modes())
resp.preset_mode = fan->preset_mode;
return this->send_fan_state_response(resp);
}
bool APIConnection::send_fan_info(fan::Fan *fan) {
@ -307,6 +309,8 @@ bool APIConnection::send_fan_info(fan::Fan *fan) {
msg.supports_speed = traits.supports_speed();
msg.supports_direction = traits.supports_direction();
msg.supported_speed_count = traits.supported_speed_count();
for (auto const &preset : traits.supported_preset_modes())
msg.supported_preset_modes.push_back(preset);
msg.disabled_by_default = fan->is_disabled_by_default();
msg.icon = fan->get_icon();
msg.entity_category = static_cast<enums::EntityCategory>(fan->get_entity_category());
@ -328,6 +332,8 @@ void APIConnection::fan_command(const FanCommandRequest &msg) {
}
if (msg.has_direction)
call.set_direction(static_cast<fan::FanDirection>(msg.direction));
if (msg.has_preset_mode)
call.set_preset_mode(msg.preset_mode);
call.perform();
}
#endif

View File

@ -1375,6 +1375,10 @@ bool ListEntitiesFanResponse::decode_length(uint32_t field_id, ProtoLengthDelimi
this->icon = value.as_string();
return true;
}
case 12: {
this->supported_preset_modes.push_back(value.as_string());
return true;
}
default:
return false;
}
@ -1401,6 +1405,9 @@ void ListEntitiesFanResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_bool(9, this->disabled_by_default);
buffer.encode_string(10, this->icon);
buffer.encode_enum<enums::EntityCategory>(11, this->entity_category);
for (auto &it : this->supported_preset_modes) {
buffer.encode_string(12, it, true);
}
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void ListEntitiesFanResponse::dump_to(std::string &out) const {
@ -1451,6 +1458,12 @@ void ListEntitiesFanResponse::dump_to(std::string &out) const {
out.append(" entity_category: ");
out.append(proto_enum_to_string<enums::EntityCategory>(this->entity_category));
out.append("\n");
for (const auto &it : this->supported_preset_modes) {
out.append(" supported_preset_modes: ");
out.append("'").append(it).append("'");
out.append("\n");
}
out.append("}");
}
#endif
@ -1480,6 +1493,16 @@ bool FanStateResponse::decode_varint(uint32_t field_id, ProtoVarInt value) {
return false;
}
}
bool FanStateResponse::decode_length(uint32_t field_id, ProtoLengthDelimited value) {
switch (field_id) {
case 7: {
this->preset_mode = value.as_string();
return true;
}
default:
return false;
}
}
bool FanStateResponse::decode_32bit(uint32_t field_id, Proto32Bit value) {
switch (field_id) {
case 1: {
@ -1497,6 +1520,7 @@ void FanStateResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_enum<enums::FanSpeed>(4, this->speed);
buffer.encode_enum<enums::FanDirection>(5, this->direction);
buffer.encode_int32(6, this->speed_level);
buffer.encode_string(7, this->preset_mode);
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void FanStateResponse::dump_to(std::string &out) const {
@ -1527,6 +1551,10 @@ void FanStateResponse::dump_to(std::string &out) const {
sprintf(buffer, "%" PRId32, this->speed_level);
out.append(buffer);
out.append("\n");
out.append(" preset_mode: ");
out.append("'").append(this->preset_mode).append("'");
out.append("\n");
out.append("}");
}
#endif
@ -1572,6 +1600,20 @@ bool FanCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) {
this->speed_level = value.as_int32();
return true;
}
case 12: {
this->has_preset_mode = value.as_bool();
return true;
}
default:
return false;
}
}
bool FanCommandRequest::decode_length(uint32_t field_id, ProtoLengthDelimited value) {
switch (field_id) {
case 13: {
this->preset_mode = value.as_string();
return true;
}
default:
return false;
}
@ -1598,6 +1640,8 @@ void FanCommandRequest::encode(ProtoWriteBuffer buffer) const {
buffer.encode_enum<enums::FanDirection>(9, this->direction);
buffer.encode_bool(10, this->has_speed_level);
buffer.encode_int32(11, this->speed_level);
buffer.encode_bool(12, this->has_preset_mode);
buffer.encode_string(13, this->preset_mode);
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void FanCommandRequest::dump_to(std::string &out) const {
@ -1648,6 +1692,14 @@ void FanCommandRequest::dump_to(std::string &out) const {
sprintf(buffer, "%" PRId32, this->speed_level);
out.append(buffer);
out.append("\n");
out.append(" has_preset_mode: ");
out.append(YESNO(this->has_preset_mode));
out.append("\n");
out.append(" preset_mode: ");
out.append("'").append(this->preset_mode).append("'");
out.append("\n");
out.append("}");
}
#endif

View File

@ -472,6 +472,7 @@ class ListEntitiesFanResponse : public ProtoMessage {
bool disabled_by_default{false};
std::string icon{};
enums::EntityCategory entity_category{};
std::vector<std::string> supported_preset_modes{};
void encode(ProtoWriteBuffer buffer) const override;
#ifdef HAS_PROTO_MESSAGE_DUMP
void dump_to(std::string &out) const override;
@ -490,6 +491,7 @@ class FanStateResponse : public ProtoMessage {
enums::FanSpeed speed{};
enums::FanDirection direction{};
int32_t speed_level{0};
std::string preset_mode{};
void encode(ProtoWriteBuffer buffer) const override;
#ifdef HAS_PROTO_MESSAGE_DUMP
void dump_to(std::string &out) const override;
@ -497,6 +499,7 @@ class FanStateResponse : public ProtoMessage {
protected:
bool decode_32bit(uint32_t field_id, Proto32Bit value) override;
bool decode_length(uint32_t field_id, ProtoLengthDelimited value) override;
bool decode_varint(uint32_t field_id, ProtoVarInt value) override;
};
class FanCommandRequest : public ProtoMessage {
@ -512,6 +515,8 @@ class FanCommandRequest : public ProtoMessage {
enums::FanDirection direction{};
bool has_speed_level{false};
int32_t speed_level{0};
bool has_preset_mode{false};
std::string preset_mode{};
void encode(ProtoWriteBuffer buffer) const override;
#ifdef HAS_PROTO_MESSAGE_DUMP
void dump_to(std::string &out) const override;
@ -519,6 +524,7 @@ class FanCommandRequest : public ProtoMessage {
protected:
bool decode_32bit(uint32_t field_id, Proto32Bit value) override;
bool decode_length(uint32_t field_id, ProtoLengthDelimited value) override;
bool decode_varint(uint32_t field_id, ProtoVarInt value) override;
};
class ListEntitiesLightResponse : public ProtoMessage {

View File

@ -12,6 +12,7 @@ void CopyFan::setup() {
this->oscillating = source_->oscillating;
this->speed = source_->speed;
this->direction = source_->direction;
this->preset_mode = source_->preset_mode;
this->publish_state();
});
@ -19,6 +20,7 @@ void CopyFan::setup() {
this->oscillating = source_->oscillating;
this->speed = source_->speed;
this->direction = source_->direction;
this->preset_mode = source_->preset_mode;
this->publish_state();
}
@ -33,6 +35,7 @@ fan::FanTraits CopyFan::get_traits() {
traits.set_speed(base.supports_speed());
traits.set_supported_speed_count(base.supported_speed_count());
traits.set_direction(base.supports_direction());
traits.set_supported_preset_modes(base.supported_preset_modes());
return traits;
}
@ -46,6 +49,8 @@ void CopyFan::control(const fan::FanCall &call) {
call2.set_speed(*call.get_speed());
if (call.get_direction().has_value())
call2.set_direction(*call.get_direction());
if (!call.get_preset_mode().empty())
call2.set_preset_mode(call.get_preset_mode());
call2.perform();
}

View File

@ -18,6 +18,7 @@ from esphome.const import (
CONF_ON_SPEED_SET,
CONF_ON_TURN_OFF,
CONF_ON_TURN_ON,
CONF_ON_PRESET_SET,
CONF_TRIGGER_ID,
CONF_DIRECTION,
CONF_RESTORE_MODE,
@ -57,6 +58,9 @@ CycleSpeedAction = fan_ns.class_("CycleSpeedAction", automation.Action)
FanTurnOnTrigger = fan_ns.class_("FanTurnOnTrigger", automation.Trigger.template())
FanTurnOffTrigger = fan_ns.class_("FanTurnOffTrigger", automation.Trigger.template())
FanSpeedSetTrigger = fan_ns.class_("FanSpeedSetTrigger", automation.Trigger.template())
FanPresetSetTrigger = fan_ns.class_(
"FanPresetSetTrigger", automation.Trigger.template()
)
FanIsOnCondition = fan_ns.class_("FanIsOnCondition", automation.Condition.template())
FanIsOffCondition = fan_ns.class_("FanIsOffCondition", automation.Condition.template())
@ -101,9 +105,46 @@ FAN_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA).exte
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(FanSpeedSetTrigger),
}
),
cv.Optional(CONF_ON_PRESET_SET): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(FanPresetSetTrigger),
}
),
}
)
_PRESET_MODES_SCHEMA = cv.All(
cv.ensure_list(cv.string_strict),
cv.Length(min=1),
)
def validate_preset_modes(value):
# Check against defined schema
value = _PRESET_MODES_SCHEMA(value)
# Ensure preset names are unique
errors = []
presets = set()
for i, preset in enumerate(value):
# If name does not exist yet add it
if preset not in presets:
presets.add(preset)
continue
# Otherwise it's an error
errors.append(
cv.Invalid(
f"Found duplicate preset name '{preset}'. Presets must have unique names.",
[i],
)
)
if errors:
raise cv.MultipleInvalid(errors)
return value
async def setup_fan_core_(var, config):
await setup_entity(var, config)
@ -154,6 +195,9 @@ async def setup_fan_core_(var, config):
for conf in config.get(CONF_ON_SPEED_SET, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [], conf)
for conf in config.get(CONF_ON_PRESET_SET, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [], conf)
async def register_fan(var, config):

View File

@ -165,5 +165,23 @@ class FanSpeedSetTrigger : public Trigger<> {
int last_speed_;
};
class FanPresetSetTrigger : public Trigger<> {
public:
FanPresetSetTrigger(Fan *state) {
state->add_on_state_callback([this, state]() {
auto preset_mode = state->preset_mode;
auto should_trigger = preset_mode != this->last_preset_mode_;
this->last_preset_mode_ = preset_mode;
if (should_trigger) {
this->trigger();
}
});
this->last_preset_mode_ = state->preset_mode;
}
protected:
std::string last_preset_mode_;
};
} // namespace fan
} // namespace esphome

View File

@ -32,9 +32,12 @@ void FanCall::perform() {
if (this->direction_.has_value()) {
ESP_LOGD(TAG, " Direction: %s", LOG_STR_ARG(fan_direction_to_string(*this->direction_)));
}
if (!this->preset_mode_.empty()) {
ESP_LOGD(TAG, " Preset Mode: %s", this->preset_mode_.c_str());
}
this->parent_.control(*this);
}
void FanCall::validate_() {
auto traits = this->parent_.get_traits();
@ -62,6 +65,15 @@ void FanCall::validate_() {
ESP_LOGW(TAG, "'%s' - This fan does not support directions!", this->parent_.get_name().c_str());
this->direction_.reset();
}
if (!this->preset_mode_.empty()) {
const auto &preset_modes = traits.supported_preset_modes();
if (preset_modes.find(this->preset_mode_) == preset_modes.end()) {
ESP_LOGW(TAG, "'%s' - This fan does not support preset mode '%s'!", this->parent_.get_name().c_str(),
this->preset_mode_.c_str());
this->preset_mode_.clear();
}
}
}
FanCall FanRestoreState::to_call(Fan &fan) {
@ -70,6 +82,14 @@ FanCall FanRestoreState::to_call(Fan &fan) {
call.set_oscillating(this->oscillating);
call.set_speed(this->speed);
call.set_direction(this->direction);
if (fan.get_traits().supports_preset_modes()) {
// Use stored preset index to get preset name
const auto &preset_modes = fan.get_traits().supported_preset_modes();
if (this->preset_mode < preset_modes.size()) {
call.set_preset_mode(*std::next(preset_modes.begin(), this->preset_mode));
}
}
return call;
}
void FanRestoreState::apply(Fan &fan) {
@ -77,6 +97,14 @@ void FanRestoreState::apply(Fan &fan) {
fan.oscillating = this->oscillating;
fan.speed = this->speed;
fan.direction = this->direction;
if (fan.get_traits().supports_preset_modes()) {
// Use stored preset index to get preset name
const auto &preset_modes = fan.get_traits().supported_preset_modes();
if (this->preset_mode < preset_modes.size()) {
fan.preset_mode = *std::next(preset_modes.begin(), this->preset_mode);
}
}
fan.publish_state();
}
@ -100,7 +128,9 @@ void Fan::publish_state() {
if (traits.supports_direction()) {
ESP_LOGD(TAG, " Direction: %s", LOG_STR_ARG(fan_direction_to_string(this->direction)));
}
if (traits.supports_preset_modes() && !this->preset_mode.empty()) {
ESP_LOGD(TAG, " Preset Mode: %s", this->preset_mode.c_str());
}
this->state_callback_.call();
this->save_state_();
}
@ -143,20 +173,36 @@ void Fan::save_state_() {
state.oscillating = this->oscillating;
state.speed = this->speed;
state.direction = this->direction;
if (this->get_traits().supports_preset_modes() && !this->preset_mode.empty()) {
const auto &preset_modes = this->get_traits().supported_preset_modes();
// Store index of current preset mode
auto preset_iterator = preset_modes.find(this->preset_mode);
if (preset_iterator != preset_modes.end())
state.preset_mode = std::distance(preset_modes.begin(), preset_iterator);
}
this->rtc_.save(&state);
}
void Fan::dump_traits_(const char *tag, const char *prefix) {
if (this->get_traits().supports_speed()) {
auto traits = this->get_traits();
if (traits.supports_speed()) {
ESP_LOGCONFIG(tag, "%s Speed: YES", prefix);
ESP_LOGCONFIG(tag, "%s Speed count: %d", prefix, this->get_traits().supported_speed_count());
ESP_LOGCONFIG(tag, "%s Speed count: %d", prefix, traits.supported_speed_count());
}
if (this->get_traits().supports_oscillation()) {
if (traits.supports_oscillation()) {
ESP_LOGCONFIG(tag, "%s Oscillation: YES", prefix);
}
if (this->get_traits().supports_direction()) {
if (traits.supports_direction()) {
ESP_LOGCONFIG(tag, "%s Direction: YES", prefix);
}
if (traits.supports_preset_modes()) {
ESP_LOGCONFIG(tag, "%s Supported presets:", prefix);
for (const std::string &s : traits.supported_preset_modes())
ESP_LOGCONFIG(tag, "%s - %s", prefix, s.c_str());
}
}
} // namespace fan

View File

@ -72,6 +72,11 @@ class FanCall {
return *this;
}
optional<FanDirection> get_direction() const { return this->direction_; }
FanCall &set_preset_mode(const std::string &preset_mode) {
this->preset_mode_ = preset_mode;
return *this;
}
std::string get_preset_mode() const { return this->preset_mode_; }
void perform();
@ -83,6 +88,7 @@ class FanCall {
optional<bool> oscillating_;
optional<int> speed_;
optional<FanDirection> direction_{};
std::string preset_mode_{};
};
struct FanRestoreState {
@ -90,6 +96,7 @@ struct FanRestoreState {
int speed;
bool oscillating;
FanDirection direction;
uint8_t preset_mode;
/// Convert this struct to a fan call that can be performed.
FanCall to_call(Fan &fan);
@ -107,6 +114,8 @@ class Fan : public EntityBase {
int speed{0};
/// The current direction of the fan
FanDirection direction{FanDirection::FORWARD};
// The current preset mode of the fan
std::string preset_mode{};
FanCall turn_on();
FanCall turn_off();

View File

@ -1,3 +1,6 @@
#include <set>
#include <utility>
#pragma once
namespace esphome {
@ -25,12 +28,19 @@ class FanTraits {
bool supports_direction() const { return this->direction_; }
/// Set whether this fan supports changing direction
void set_direction(bool direction) { this->direction_ = direction; }
/// Return the preset modes supported by the fan.
std::set<std::string> supported_preset_modes() const { return this->preset_modes_; }
/// Set the preset modes supported by the fan.
void set_supported_preset_modes(const std::set<std::string> &preset_modes) { this->preset_modes_ = preset_modes; }
/// Return if preset modes are supported
bool supports_preset_modes() const { return !this->preset_modes_.empty(); }
protected:
bool oscillation_{false};
bool speed_{false};
bool direction_{false};
int speed_count_{};
std::set<std::string> preset_modes_{};
};
} // namespace fan

View File

@ -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]))

View File

@ -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

View File

@ -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_();

View File

@ -1,14 +1,17 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import fan, output
from esphome.components.fan import validate_preset_modes
from esphome.const import (
CONF_PRESET_MODES,
CONF_DIRECTION_OUTPUT,
CONF_OSCILLATION_OUTPUT,
CONF_OUTPUT,
CONF_DIRECTION_OUTPUT,
CONF_OUTPUT_ID,
CONF_SPEED,
CONF_SPEED_COUNT,
)
from .. import speed_ns
SpeedFan = speed_ns.class_("SpeedFan", cg.Component, fan.Fan)
@ -23,6 +26,7 @@ CONFIG_SCHEMA = fan.FAN_SCHEMA.extend(
"Configuring individual speeds is deprecated."
),
cv.Optional(CONF_SPEED_COUNT, default=100): cv.int_range(min=1),
cv.Optional(CONF_PRESET_MODES): validate_preset_modes,
}
).extend(cv.COMPONENT_SCHEMA)
@ -40,3 +44,6 @@ async def to_code(config):
if CONF_DIRECTION_OUTPUT in config:
direction_output = await cg.get_variable(config[CONF_DIRECTION_OUTPUT])
cg.add(var.set_direction(direction_output))
if CONF_PRESET_MODES in config:
cg.add(var.set_preset_modes(config[CONF_PRESET_MODES]))

View File

@ -12,11 +12,14 @@ void SpeedFan::setup() {
restore->apply(*this);
this->write_state_();
}
// Construct traits
this->traits_ = fan::FanTraits(this->oscillating_ != nullptr, true, this->direction_ != nullptr, this->speed_count_);
this->traits_.set_supported_preset_modes(this->preset_modes_);
}
void SpeedFan::dump_config() { LOG_FAN("", "Speed Fan", this); }
fan::FanTraits SpeedFan::get_traits() {
return fan::FanTraits(this->oscillating_ != nullptr, true, this->direction_ != nullptr, this->speed_count_);
}
void SpeedFan::control(const fan::FanCall &call) {
if (call.get_state().has_value())
this->state = *call.get_state();
@ -26,10 +29,12 @@ void SpeedFan::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 SpeedFan::write_state_() {
float speed = this->state ? static_cast<float>(this->speed) / static_cast<float>(this->speed_count_) : 0.0f;
this->output_->set_level(speed);

View File

@ -1,5 +1,7 @@
#pragma once
#include <set>
#include "esphome/core/component.h"
#include "esphome/components/output/binary_output.h"
#include "esphome/components/output/float_output.h"
@ -15,7 +17,8 @@ class SpeedFan : public Component, public fan::Fan {
void dump_config() override;
void set_oscillating(output::BinaryOutput *oscillating) { this->oscillating_ = oscillating; }
void set_direction(output::BinaryOutput *direction) { this->direction_ = direction; }
fan::FanTraits get_traits() override;
void set_preset_modes(const std::set<std::string> &presets) { this->preset_modes_ = presets; }
fan::FanTraits get_traits() override { return this->traits_; }
protected:
void control(const fan::FanCall &call) override;
@ -25,6 +28,8 @@ class SpeedFan : public Component, public fan::Fan {
output::BinaryOutput *oscillating_{nullptr};
output::BinaryOutput *direction_{nullptr};
int speed_count_{};
fan::FanTraits traits_;
std::set<std::string> preset_modes_{};
};
} // namespace speed

View File

@ -504,6 +504,7 @@ CONF_ON_LOOP = "on_loop"
CONF_ON_MESSAGE = "on_message"
CONF_ON_MULTI_CLICK = "on_multi_click"
CONF_ON_OPEN = "on_open"
CONF_ON_PRESET_SET = "on_preset_set"
CONF_ON_PRESS = "on_press"
CONF_ON_RAW_VALUE = "on_raw_value"
CONF_ON_RELEASE = "on_release"
@ -601,6 +602,7 @@ CONF_PRESET = "preset"
CONF_PRESET_BOOST = "preset_boost"
CONF_PRESET_COMMAND_TOPIC = "preset_command_topic"
CONF_PRESET_ECO = "preset_eco"
CONF_PRESET_MODES = "preset_modes"
CONF_PRESET_SLEEP = "preset_sleep"
CONF_PRESET_STATE_TOPIC = "preset_state_topic"
CONF_PRESSURE = "pressure"

View File

@ -2944,6 +2944,33 @@ fan:
on_speed_set:
then:
- logger.log: Fan speed was changed!
- platform: speed
id: fan_speed_presets
icon: mdi:weather-windy
output: pca_6
speed_count: 10
name: Speed Fan w/ Presets
oscillation_output: gpio_19
direction_output: gpio_26
preset_modes:
- Preset 1
- Preset 2
on_preset_set:
then:
- logger.log: Preset mode was changed!
- platform: hbridge
id: fan_hbridge_presets
icon: mdi:weather-windy
speed_count: 4
name: H-bridge Fan w/ Presets
pin_a: pca_6
pin_b: pca_7
preset_modes:
- Preset 1
- Preset 2
on_preset_set:
then:
- logger.log: Preset mode was changed!
- platform: bedjet
name: My Bedjet fan
bedjet_id: my_bedjet_client
@ -4193,4 +4220,3 @@ alarm_control_panel:
then:
- lambda: !lambda |-
ESP_LOGD("TEST", "State change %s", alarm_control_panel_state_to_string(id(alarmcontrolpanel1)->get_state()));