7static 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());
49 if (traits.supports_speed() && !this->parent_.state && this->parent_.speed == 0 && !this->speed_.has_value()) {
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(),
73 this->preset_mode_.c_str());
90 call.set_preset_mode(*std::next(preset_modes.begin(), this->preset_mode));
105 fan.
preset_mode = *std::next(preset_modes.begin(), this->preset_mode);
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());
143 bool restored = this->
rtc_.
load(&recovered);
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;
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);
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());
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
uint32_t get_object_id_hash()
const StringRef & get_name() const
constexpr const char * c_str() const
FanCall & set_oscillating(bool oscillating)
FanCall & set_direction(FanDirection direction)
optional< bool > binary_state_
optional< FanDirection > direction_
FanCall & set_speed(int speed)
FanCall & set_state(bool binary_state)
optional< bool > oscillating_
virtual FanTraits get_traits()=0
void add_on_state_callback(std::function< void()> &&callback)
Register a callback that will be called each time the state changes.
FanDirection direction
The current direction of the fan.
FanRestoreMode restore_mode_
CallbackManager< void()> state_callback_
bool oscillating
The current oscillation state of the fan.
virtual void control(const FanCall &call)=0
bool state
The current on/off state of the fan.
int speed
The current fan speed level.
void dump_traits_(const char *tag, const char *prefix)
optional< FanRestoreState > restore_state_()
bool supports_preset_modes() const
Return if preset modes are supported.
std::set< std::string > supported_preset_modes() const
Return the preset modes supported by the fan.
@ RESTORE_INVERTED_DEFAULT_OFF
@ RESTORE_INVERTED_DEFAULT_ON
const LogString * fan_direction_to_string(FanDirection direction)
constexpr uint32_t RESTORE_STATE_VERSION
FanDirection
Simple enum to represent the direction of a fan.
Providing packet encoding functions for exchanging data with a remote host.
ESPPreferences * global_preferences
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)
void apply(Fan &fan)
Apply these settings to the fan.
FanCall to_call(Fan &fan)
Convert this struct to a fan call that can be performed.