10static const char *
const TAG =
"fan";
16 return FanDirectionStrings::get_log_str(
static_cast<uint8_t
>(
direction), FanDirectionStrings::LAST_INDEX);
36 if (validated_mode !=
nullptr) {
53 ESP_LOGD(TAG,
" Oscillating: %s", YESNO(*this->
oscillating_));
56 ESP_LOGD(TAG,
" Speed: %d", *this->
speed_);
71 this->
speed_ = clamp(*this->
speed_, 1, traits.supported_speed_count());
79 if (!this->
parent_.
state && this->binary_state_.has_value() &&
82 && !this->has_preset_mode() &&
83 !this->parent_.has_preset_mode()
85 && traits.supports_speed() && this->parent_.speed == 0 && !this->speed_.has_value()) {
87 this->
speed_ = traits.supported_speed_count();
114 if (traits.supports_preset_modes()) {
116 const auto &preset_modes = traits.supported_preset_modes();
118 call.set_preset_mode(preset_modes[this->
preset_mode]);
130 if (traits.supports_preset_modes()) {
132 const auto &preset_modes = traits.supported_preset_modes();
157 if (this->preset_mode_ ==
nullptr) {
164 if (validated ==
nullptr || this->preset_mode_ == validated) {
167 this->preset_mode_ = validated;
204 if (traits.supports_speed()) {
205 ESP_LOGD(TAG,
" Speed: %d", this->
speed);
207 if (traits.supports_oscillation()) {
208 ESP_LOGD(TAG,
" Oscillating: %s", YESNO(this->
oscillating));
210 if (traits.supports_direction()) {
213 if (this->preset_mode_ !=
nullptr) {
214 ESP_LOGD(TAG,
" Preset Mode: %s", this->preset_mode_);
217#if defined(USE_FAN) && defined(USE_CONTROLLER_REGISTRY)
228 bool restored = this->
rtc_.
load(&recovered);
239 recovered.state =
false;
242 recovered.state =
true;
245 recovered.state = restored ? recovered.state :
false;
248 recovered.state = restored ? recovered.state :
true;
251 recovered.state = restored ? !recovered.state :
false;
254 recovered.state = restored ? !recovered.state :
true;
275 const auto &preset_modes = traits.supported_preset_modes();
277 for (
size_t i = 0; i < preset_modes.size(); i++) {
278 if (preset_modes[i] == this->preset_mode_) {
279 state.preset_mode = i;
291 if (traits.supports_speed()) {
294 "%s Speed count: %d",
295 prefix, prefix, traits.supported_speed_count());
297 if (traits.supports_oscillation()) {
298 ESP_LOGCONFIG(tag,
"%s Oscillation: YES", prefix);
300 if (traits.supports_direction()) {
301 ESP_LOGCONFIG(tag,
"%s Direction: YES", prefix);
303 if (traits.supports_preset_modes()) {
304 ESP_LOGCONFIG(tag,
"%s Supported presets:", prefix);
305 for (
const char *s : traits.supported_preset_modes())
306 ESP_LOGCONFIG(tag,
"%s - %s", prefix, s);
static void notify_fan_update(fan::Fan *obj)
const StringRef & get_name() const
ESPPreferenceObject make_entity_preference(uint32_t version=0)
Create a preference object for storing this entity's state/settings.
StringRef is a reference to a string owned by something else.
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_
const char * get_preset_mode() const
bool has_preset_mode() const
FanCall & set_speed(int speed)
FanCall & set_state(bool binary_state)
optional< int > get_speed() const
optional< bool > oscillating_
FanCall & set_preset_mode(const std::string &preset_mode)
virtual FanTraits get_traits()=0
LazyCallbackManager< void()> state_callback_
void apply_preset_mode_(const FanCall &call)
Apply preset mode from a FanCall (handles speed-clears-preset convention)
void clear_preset_mode_()
Clear the preset mode.
bool set_preset_mode_(const char *preset_mode, size_t len)
Set the preset mode (finds and stores pointer from traits).
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_
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.
bool has_preset_mode() const
Check if a preset mode is currently active.
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.
PROGMEM_STRING_TABLE(FanDirectionStrings, "FORWARD", "REVERSE", "UNKNOWN")
Providing packet encoding functions for exchanging data with a remote host.
static constexpr uint8_t NO_PRESET
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.