mirror of
https://github.com/esphome/esphome.git
synced 2024-12-12 08:24:55 +01:00
7708b81ef5
* 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.
83 lines
2.8 KiB
C++
83 lines
2.8 KiB
C++
#include "fan_state.h"
|
|
#include "fan_helpers.h"
|
|
#include "esphome/core/log.h"
|
|
|
|
namespace esphome {
|
|
namespace fan {
|
|
|
|
static const char *TAG = "fan";
|
|
|
|
const FanTraits &FanState::get_traits() const { return this->traits_; }
|
|
void FanState::set_traits(const FanTraits &traits) { this->traits_ = traits; }
|
|
void FanState::add_on_state_callback(std::function<void()> &&callback) {
|
|
this->state_callback_.add(std::move(callback));
|
|
}
|
|
FanState::FanState(const std::string &name) : Nameable(name) {}
|
|
|
|
FanStateCall FanState::turn_on() { return this->make_call().set_state(true); }
|
|
FanStateCall FanState::turn_off() { return this->make_call().set_state(false); }
|
|
FanStateCall FanState::toggle() { return this->make_call().set_state(!this->state); }
|
|
FanStateCall FanState::make_call() { return FanStateCall(this); }
|
|
|
|
struct FanStateRTCState {
|
|
bool state;
|
|
int speed;
|
|
bool oscillating;
|
|
FanDirection direction;
|
|
};
|
|
|
|
void FanState::setup() {
|
|
this->rtc_ = global_preferences.make_preference<FanStateRTCState>(this->get_object_id_hash());
|
|
FanStateRTCState recovered{};
|
|
if (!this->rtc_.load(&recovered))
|
|
return;
|
|
|
|
auto call = this->make_call();
|
|
call.set_state(recovered.state);
|
|
call.set_speed(recovered.speed);
|
|
call.set_oscillating(recovered.oscillating);
|
|
call.set_direction(recovered.direction);
|
|
call.perform();
|
|
}
|
|
float FanState::get_setup_priority() const { return setup_priority::HARDWARE - 1.0f; }
|
|
uint32_t FanState::hash_base() { return 418001110UL; }
|
|
|
|
void FanStateCall::perform() const {
|
|
if (this->binary_state_.has_value()) {
|
|
this->state_->state = *this->binary_state_;
|
|
}
|
|
if (this->oscillating_.has_value()) {
|
|
this->state_->oscillating = *this->oscillating_;
|
|
}
|
|
if (this->direction_.has_value()) {
|
|
this->state_->direction = *this->direction_;
|
|
}
|
|
if (this->speed_.has_value()) {
|
|
const int speed_count = this->state_->get_traits().supported_speed_count();
|
|
this->state_->speed = static_cast<int>(clamp(*this->speed_, 1, speed_count));
|
|
}
|
|
|
|
FanStateRTCState saved{};
|
|
saved.state = this->state_->state;
|
|
saved.speed = this->state_->speed;
|
|
saved.oscillating = this->state_->oscillating;
|
|
saved.direction = this->state_->direction;
|
|
this->state_->rtc_.save(&saved);
|
|
|
|
this->state_->state_callback_.call();
|
|
}
|
|
|
|
FanStateCall &FanStateCall::set_speed(const char *legacy_speed) {
|
|
const auto supported_speed_count = this->state_->get_traits().supported_speed_count();
|
|
if (strcasecmp(legacy_speed, "low") == 0) {
|
|
this->set_speed(fan::speed_enum_to_level(FAN_SPEED_LOW, supported_speed_count));
|
|
} else if (strcasecmp(legacy_speed, "medium") == 0) {
|
|
this->set_speed(fan::speed_enum_to_level(FAN_SPEED_MEDIUM, supported_speed_count));
|
|
} else if (strcasecmp(legacy_speed, "high") == 0) {
|
|
this->set_speed(fan::speed_enum_to_level(FAN_SPEED_HIGH, supported_speed_count));
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
} // namespace fan
|
|
} // namespace esphome
|