mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-06-01 18:48:28 +02:00
Refactor fan platform to resemble climate/cover platforms (#2848)
Co-authored-by: Oxan van Leeuwen <oxan@oxanvanleeuwen.nl> Co-authored-by: rob-deutsch <robzyb+altgithub@gmail.com> Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
@@ -1,121 +1,16 @@
|
||||
#include "fan_state.h"
|
||||
#include "fan_helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace fan {
|
||||
|
||||
static const char *const 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) : EntityBase(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() {
|
||||
auto call = this->make_call();
|
||||
FanStateRTCState recovered{};
|
||||
|
||||
switch (this->restore_mode_) {
|
||||
case FAN_RESTORE_DEFAULT_OFF:
|
||||
case FAN_RESTORE_DEFAULT_ON:
|
||||
case FAN_RESTORE_INVERTED_DEFAULT_OFF:
|
||||
case FAN_RESTORE_INVERTED_DEFAULT_ON:
|
||||
this->rtc_ = global_preferences->make_preference<FanStateRTCState>(this->get_object_id_hash());
|
||||
if (!this->rtc_.load(&recovered)) {
|
||||
if (this->restore_mode_ == FAN_RESTORE_DEFAULT_ON || this->restore_mode_ == FAN_RESTORE_INVERTED_DEFAULT_ON) {
|
||||
call.set_state(true);
|
||||
} else {
|
||||
call.set_state(false);
|
||||
}
|
||||
} else {
|
||||
if (this->restore_mode_ == FAN_RESTORE_INVERTED_DEFAULT_OFF ||
|
||||
this->restore_mode_ == FAN_RESTORE_INVERTED_DEFAULT_ON) {
|
||||
call.set_state(!recovered.state);
|
||||
} else {
|
||||
call.set_state(recovered.state);
|
||||
}
|
||||
|
||||
call.set_speed(recovered.speed);
|
||||
call.set_oscillating(recovered.oscillating);
|
||||
call.set_direction(recovered.direction);
|
||||
}
|
||||
break;
|
||||
case FAN_ALWAYS_OFF:
|
||||
case FAN_ALWAYS_ON:
|
||||
if (this->restore_mode_ == FAN_ALWAYS_OFF) {
|
||||
call.set_state(false);
|
||||
} else if (this->restore_mode_ == FAN_ALWAYS_ON) {
|
||||
call.set_state(true);
|
||||
}
|
||||
|
||||
this->rtc_ = global_preferences->make_preference<FanStateRTCState>(this->get_object_id_hash());
|
||||
if (this->rtc_.load(&recovered)) {
|
||||
call.set_speed(recovered.speed);
|
||||
call.set_oscillating(recovered.oscillating);
|
||||
call.set_direction(recovered.direction);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
call.perform();
|
||||
auto restore = this->restore_state_();
|
||||
if (restore)
|
||||
restore->to_call(*this).perform();
|
||||
}
|
||||
float FanState::get_setup_priority() const { return setup_priority::DATA - 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 = 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();
|
||||
}
|
||||
|
||||
// This whole method is deprecated, don't warn about usage of deprecated methods inside of it.
|
||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user