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());
53 const auto &preset_modes = traits.supported_preset_modes();
54 if (preset_modes.find(this->preset_mode_) == preset_modes.end()) {
55 ESP_LOGW(TAG,
"%s: Preset mode '%s' not supported", this->
parent_.
get_name().
c_str(), this->preset_mode_.c_str());
61 if (!this->
parent_.
state && this->binary_state_.has_value() &&
64 && this->preset_mode_.empty() &&
65 this->parent_.preset_mode.empty()
67 && traits.supports_speed() && this->parent_.speed == 0 && !this->speed_.has_value()) {
69 this->
speed_ = traits.supported_speed_count();
99 call.set_preset_mode(*std::next(preset_modes.begin(), this->preset_mode));
114 fan.
preset_mode = *std::next(preset_modes.begin(), this->preset_mode);
129 ESP_LOGD(TAG,
"'%s' - Sending state:", this->
name_.
c_str());
130 ESP_LOGD(TAG,
" State: %s", ONOFF(this->
state));
131 if (traits.supports_speed()) {
132 ESP_LOGD(TAG,
" Speed: %d", this->
speed);
134 if (traits.supports_oscillation()) {
135 ESP_LOGD(TAG,
" Oscillating: %s", YESNO(this->
oscillating));
137 if (traits.supports_direction()) {
140 if (traits.supports_preset_modes() && !this->preset_mode.empty()) {
141 ESP_LOGD(TAG,
" Preset Mode: %s", this->
preset_mode.c_str());
152 bool restored = this->
rtc_.
load(&recovered);
158 recovered.state =
false;
161 recovered.state =
true;
164 recovered.state = restored ? recovered.state :
false;
167 recovered.state = restored ? recovered.state :
true;
170 recovered.state = restored ? !recovered.state :
false;
173 recovered.state = restored ? !recovered.state :
true;
189 auto preset_iterator = preset_modes.find(this->
preset_mode);
190 if (preset_iterator != preset_modes.end())
191 state.preset_mode = std::distance(preset_modes.begin(), preset_iterator);
200 if (traits.supports_speed()) {
203 "%s Speed count: %d",
204 prefix, prefix, traits.supported_speed_count());
206 if (traits.supports_oscillation()) {
207 ESP_LOGCONFIG(tag,
"%s Oscillation: YES", prefix);
209 if (traits.supports_direction()) {
210 ESP_LOGCONFIG(tag,
"%s Direction: YES", prefix);
212 if (traits.supports_preset_modes()) {
213 ESP_LOGCONFIG(tag,
"%s Supported presets:", prefix);
214 for (
const std::string &s : traits.supported_preset_modes())
215 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.