mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-06-05 20:38:27 +02:00
Support fan speed levels (#1541)
* Add fan speed percentage support to the API * Add float fan speed percentage * Add percentage support to automation and configuration * Update Tuya fan * Fix pylint warning * Update API to use speed levels instead of percentage * Use speed levels * Fix type warnings * MQTT component now converts between speed levels and enums * Webserver now supports speed_level * Update prometheus * Remove low/medium/high settings from speed fan * Remove unused enum * Configurable speed levels for speed fan * Remove unused import * Rename speed_level->speed and speed_levels->speed_count * Rename supported_speed_levels -> supported_speed_count in API and FanTraits Field id stays the same in the protocol, so the change is not breaking for aioesphome.
This commit is contained in:
@@ -7,9 +7,7 @@ from esphome.const import (
|
||||
CONF_DIRECTION_OUTPUT,
|
||||
CONF_OUTPUT_ID,
|
||||
CONF_SPEED,
|
||||
CONF_LOW,
|
||||
CONF_MEDIUM,
|
||||
CONF_HIGH,
|
||||
CONF_SPEED_COUNT,
|
||||
)
|
||||
from .. import speed_ns
|
||||
|
||||
@@ -21,13 +19,10 @@ CONFIG_SCHEMA = fan.FAN_SCHEMA.extend(
|
||||
cv.Required(CONF_OUTPUT): cv.use_id(output.FloatOutput),
|
||||
cv.Optional(CONF_OSCILLATION_OUTPUT): cv.use_id(output.BinaryOutput),
|
||||
cv.Optional(CONF_DIRECTION_OUTPUT): cv.use_id(output.BinaryOutput),
|
||||
cv.Optional(CONF_SPEED, default={}): cv.Schema(
|
||||
{
|
||||
cv.Optional(CONF_LOW, default=0.33): cv.percentage,
|
||||
cv.Optional(CONF_MEDIUM, default=0.66): cv.percentage,
|
||||
cv.Optional(CONF_HIGH, default=1.0): cv.percentage,
|
||||
}
|
||||
cv.Optional(CONF_SPEED): cv.invalid(
|
||||
"Configuring individual speeds is deprecated."
|
||||
),
|
||||
cv.Optional(CONF_SPEED_COUNT, default=100): cv.int_range(min=1),
|
||||
}
|
||||
).extend(cv.COMPONENT_SCHEMA)
|
||||
|
||||
@@ -35,10 +30,10 @@ CONFIG_SCHEMA = fan.FAN_SCHEMA.extend(
|
||||
def to_code(config):
|
||||
output_ = yield cg.get_variable(config[CONF_OUTPUT])
|
||||
state = yield fan.create_fan_state(config)
|
||||
var = cg.new_Pvariable(config[CONF_OUTPUT_ID], state, output_)
|
||||
var = cg.new_Pvariable(
|
||||
config[CONF_OUTPUT_ID], state, output_, config[CONF_SPEED_COUNT]
|
||||
)
|
||||
yield cg.register_component(var, config)
|
||||
speeds = config[CONF_SPEED]
|
||||
cg.add(var.set_speeds(speeds[CONF_LOW], speeds[CONF_MEDIUM], speeds[CONF_HIGH]))
|
||||
|
||||
if CONF_OSCILLATION_OUTPUT in config:
|
||||
oscillation_output = yield cg.get_variable(config[CONF_OSCILLATION_OUTPUT])
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "speed_fan.h"
|
||||
#include "esphome/components/fan/fan_helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
@@ -16,7 +17,7 @@ void SpeedFan::dump_config() {
|
||||
}
|
||||
}
|
||||
void SpeedFan::setup() {
|
||||
auto traits = fan::FanTraits(this->oscillating_ != nullptr, true, this->direction_ != nullptr);
|
||||
auto traits = fan::FanTraits(this->oscillating_ != nullptr, true, this->direction_ != nullptr, this->speed_count_);
|
||||
this->fan_->set_traits(traits);
|
||||
this->fan_->add_on_state_callback([this]() { this->next_update_ = true; });
|
||||
}
|
||||
@@ -29,12 +30,7 @@ void SpeedFan::loop() {
|
||||
{
|
||||
float speed = 0.0f;
|
||||
if (this->fan_->state) {
|
||||
if (this->fan_->speed == fan::FAN_SPEED_LOW)
|
||||
speed = this->low_speed_;
|
||||
else if (this->fan_->speed == fan::FAN_SPEED_MEDIUM)
|
||||
speed = this->medium_speed_;
|
||||
else if (this->fan_->speed == fan::FAN_SPEED_HIGH)
|
||||
speed = this->high_speed_;
|
||||
speed = static_cast<float>(this->fan_->speed) / static_cast<float>(this->speed_count_);
|
||||
}
|
||||
ESP_LOGD(TAG, "Setting speed: %.2f", speed);
|
||||
this->output_->set_level(speed);
|
||||
|
||||
@@ -10,28 +10,22 @@ namespace speed {
|
||||
|
||||
class SpeedFan : public Component {
|
||||
public:
|
||||
SpeedFan(fan::FanState *fan, output::FloatOutput *output) : fan_(fan), output_(output) {}
|
||||
SpeedFan(fan::FanState *fan, output::FloatOutput *output, int speed_count)
|
||||
: fan_(fan), output_(output), speed_count_(speed_count) {}
|
||||
void setup() override;
|
||||
void loop() override;
|
||||
void dump_config() override;
|
||||
float get_setup_priority() const override;
|
||||
void set_oscillating(output::BinaryOutput *oscillating) { this->oscillating_ = oscillating; }
|
||||
void set_direction(output::BinaryOutput *direction) { this->direction_ = direction; }
|
||||
void set_speeds(float low, float medium, float high) {
|
||||
this->low_speed_ = low;
|
||||
this->medium_speed_ = medium;
|
||||
this->high_speed_ = high;
|
||||
}
|
||||
|
||||
protected:
|
||||
fan::FanState *fan_;
|
||||
output::FloatOutput *output_;
|
||||
output::BinaryOutput *oscillating_{nullptr};
|
||||
output::BinaryOutput *direction_{nullptr};
|
||||
float low_speed_{};
|
||||
float medium_speed_{};
|
||||
float high_speed_{};
|
||||
bool next_update_{true};
|
||||
int speed_count_{};
|
||||
};
|
||||
|
||||
} // namespace speed
|
||||
|
||||
Reference in New Issue
Block a user