7 static const char *
const TAG =
"fan";
12 return LOG_STR(
"FORWARD");
14 return LOG_STR(
"REVERSE");
16 return LOG_STR(
"UNKNOWN");
27 ESP_LOGD(TAG,
" Oscillating: %s", YESNO(*this->
oscillating_));
30 ESP_LOGD(TAG,
" Speed: %d", *this->
speed_);
36 ESP_LOGD(TAG,
" Preset Mode: %s", this->
preset_mode_.c_str());
50 this->
speed_ = traits.supported_speed_count();
55 ESP_LOGW(TAG,
"'%s' - This fan does not support oscillation!", this->
parent_.
get_name().
c_str());
70 const auto &preset_modes = traits.supported_preset_modes();
71 if (preset_modes.find(this->preset_mode_) == preset_modes.end()) {
72 ESP_LOGW(TAG,
"'%s' - This fan does not support preset mode '%s'!", this->
parent_.
get_name().
c_str(),
90 call.set_preset_mode(*std::next(preset_modes.begin(), this->
preset_mode));
118 auto traits = this->get_traits();
120 ESP_LOGD(TAG,
"'%s' - Sending state:", this->name_.c_str());
121 ESP_LOGD(TAG,
" State: %s", ONOFF(this->
state));
122 if (traits.supports_speed()) {
123 ESP_LOGD(TAG,
" Speed: %d", this->
speed);
125 if (traits.supports_oscillation()) {
126 ESP_LOGD(TAG,
" Oscillating: %s", YESNO(this->
oscillating));
128 if (traits.supports_direction()) {
131 if (traits.supports_preset_modes() && !this->
preset_mode.empty()) {
132 ESP_LOGD(TAG,
" Preset Mode: %s", this->
preset_mode.c_str());
134 this->state_callback_.call();
143 bool restored = this->rtc_.load(&recovered);
145 switch (this->restore_mode_) {
149 recovered.state =
false;
152 recovered.state =
true;
155 recovered.state = restored ? recovered.state :
false;
158 recovered.state = restored ? recovered.state :
true;
161 recovered.state = restored ? !recovered.state :
false;
164 recovered.state = restored ? !recovered.state :
true;
177 if (this->get_traits().supports_preset_modes() && !this->
preset_mode.empty()) {
178 const auto &preset_modes = this->get_traits().supported_preset_modes();
180 auto preset_iterator = preset_modes.find(this->
preset_mode);
181 if (preset_iterator != preset_modes.end())
182 state.preset_mode = std::distance(preset_modes.begin(), preset_iterator);
185 this->rtc_.save(&
state);
189 auto traits = this->get_traits();
191 if (traits.supports_speed()) {
192 ESP_LOGCONFIG(tag,
"%s Speed: YES", prefix);
193 ESP_LOGCONFIG(tag,
"%s Speed count: %d", prefix, traits.supported_speed_count());
195 if (traits.supports_oscillation()) {
196 ESP_LOGCONFIG(tag,
"%s Oscillation: YES", prefix);
198 if (traits.supports_direction()) {
199 ESP_LOGCONFIG(tag,
"%s Direction: YES", prefix);
201 if (traits.supports_preset_modes()) {
202 ESP_LOGCONFIG(tag,
"%s Supported presets:", prefix);
203 for (
const std::string &s : traits.supported_preset_modes())
204 ESP_LOGCONFIG(tag,
"%s - %s", prefix, s.c_str());
bool state
The current on/off state of the fan.
bool oscillating
The current oscillation state of the fan.
optional< bool > oscillating_
void apply(Fan &fan)
Apply these settings to the fan.
FanDirection direction
The current direction of the fan.
virtual FanTraits get_traits()=0
optional< FanRestoreState > restore_state_()
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)
constexpr uint32_t RESTORE_STATE_VERSION
bool supports_preset_modes() const
Return if preset modes are supported.
void add_on_state_callback(std::function< void()> &&callback)
Register a callback that will be called each time the state changes.
FanCall & set_speed(int speed)
virtual void control(const FanCall &call)=0
optional< bool > binary_state_
FanDirection
Simple enum to represent the direction of a fan.
ESPPreferences * global_preferences
int speed
The current fan speed level.
FanCall & set_oscillating(bool oscillating)
FanCall to_call(Fan &fan)
Convert this struct to a fan call that can be performed.
constexpr const char * c_str() const
FanCall & set_state(bool binary_state)
std::set< std::string > supported_preset_modes() const
Return the preset modes supported by the fan.
optional< FanDirection > direction_
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
Implementation of SPI Controller mode.
const LogString * fan_direction_to_string(FanDirection direction)
const StringRef & get_name() const
FanCall & set_direction(FanDirection direction)
void dump_traits_(const char *tag, const char *prefix)