9static const char *
const TAG =
"fan";
14 return LOG_STR(
"FORWARD");
16 return LOG_STR(
"REVERSE");
18 return LOG_STR(
"UNKNOWN");
33 if (validated_mode !=
nullptr) {
37 ESP_LOGW(TAG,
"%s: Preset mode '%s' not supported", this->
parent_.
get_name().
c_str(), preset_mode);
50 ESP_LOGD(TAG,
" Oscillating: %s", YESNO(*this->
oscillating_));
53 ESP_LOGD(TAG,
" Speed: %d", *this->
speed_);
68 this->
speed_ = clamp(*this->
speed_, 1, traits.supported_speed_count());
76 if (!this->
parent_.
state && this->binary_state_.has_value() &&
79 && !this->has_preset_mode() &&
80 this->parent_.get_preset_mode() ==
nullptr
82 && traits.supports_speed() && this->parent_.speed == 0 && !this->speed_.has_value()) {
84 this->
speed_ = traits.supported_speed_count();
111 if (traits.supports_preset_modes()) {
113 const auto &preset_modes = traits.supported_preset_modes();
115 call.set_preset_mode(preset_modes[this->
preset_mode]);
127 if (traits.supports_preset_modes()) {
129 const auto &preset_modes = traits.supported_preset_modes();
148 if (this->preset_mode_ ==
nullptr) {
155 if (validated ==
nullptr || this->preset_mode_ == validated) {
158 this->preset_mode_ = validated;
170 ESP_LOGD(TAG,
"'%s' - Sending state:", this->
name_.
c_str());
171 ESP_LOGD(TAG,
" State: %s", ONOFF(this->
state));
172 if (traits.supports_speed()) {
173 ESP_LOGD(TAG,
" Speed: %d", this->
speed);
175 if (traits.supports_oscillation()) {
176 ESP_LOGD(TAG,
" Oscillating: %s", YESNO(this->
oscillating));
178 if (traits.supports_direction()) {
182 if (preset !=
nullptr) {
183 ESP_LOGD(TAG,
" Preset Mode: %s",
preset);
186#if defined(USE_FAN) && defined(USE_CONTROLLER_REGISTRY)
198 bool restored = this->
rtc_.
load(&recovered);
204 recovered.state =
false;
207 recovered.state =
true;
210 recovered.state = restored ? recovered.state :
false;
213 recovered.state = restored ? recovered.state :
true;
216 recovered.state = restored ? !recovered.state :
false;
219 recovered.state = restored ? !recovered.state :
true;
239 if (preset !=
nullptr) {
240 const auto &preset_modes = traits.supported_preset_modes();
242 for (
size_t i = 0; i < preset_modes.size(); i++) {
243 if (preset_modes[i] ==
preset) {
244 state.preset_mode = i;
256 if (traits.supports_speed()) {
259 "%s Speed count: %d",
260 prefix, prefix, traits.supported_speed_count());
262 if (traits.supports_oscillation()) {
263 ESP_LOGCONFIG(tag,
"%s Oscillation: YES", prefix);
265 if (traits.supports_direction()) {
266 ESP_LOGCONFIG(tag,
"%s Direction: YES", prefix);
268 if (traits.supports_preset_modes()) {
269 ESP_LOGCONFIG(tag,
"%s Supported presets:", prefix);
270 for (
const char *s : traits.supported_preset_modes())
271 ESP_LOGCONFIG(tag,
"%s - %s", prefix, s);
static void notify_fan_update(fan::Fan *obj)
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
const StringRef & get_name() const
uint32_t get_preference_hash()
Get a unique hash for storing preferences/settings for this entity.
constexpr const char * c_str() const
FanCall & set_oscillating(bool oscillating)
FanCall & set_direction(FanDirection direction)
optional< bool > binary_state_
optional< FanDirection > direction_
const char * preset_mode_
bool has_preset_mode() const
FanCall & set_speed(int speed)
FanCall & set_state(bool binary_state)
optional< bool > oscillating_
FanCall & set_preset_mode(const std::string &preset_mode)
virtual FanTraits get_traits()=0
bool set_preset_mode_(const char *preset_mode)
Set the preset mode (finds and stores pointer from traits). Returns true if changed.
void clear_preset_mode_()
Clear the preset mode.
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.
const char * find_preset_mode_(const char *preset_mode)
Find and return the matching preset mode pointer from traits, or nullptr if not found.
const char * get_preset_mode() const
Get the current preset mode (returns pointer to string stored in traits, or nullptr if not set)
int speed
The current fan speed level.
void dump_traits_(const char *tag, const char *prefix)
optional< FanRestoreState > restore_state_()
const char * find_preset_mode(const char *preset_mode) const
Find and return the matching preset mode pointer from supported modes, or nullptr if not found.
@ 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
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.