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:
Jim Ekman
2021-03-17 14:40:02 +01:00
committed by GitHub
parent 08998caabc
commit 7708b81ef5
22 changed files with 182 additions and 99 deletions
+3 -7
View File
@@ -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);