ESPHome 2026.2.1
Loading...
Searching...
No Matches
esphome::fan::Fan Class Referenceabstract

#include <fan.h>

Inheritance diagram for esphome::fan::Fan:
esphome::EntityBase esphome::bedjet::BedJetFan esphome::binary::BinaryFan esphome::copy::CopyFan esphome::demo::DemoFan esphome::hbridge::HBridgeFan esphome::speed::SpeedFan esphome::template_::TemplateFan esphome::tuya::TuyaFan

Public Member Functions

FanCall turn_on ()
 
FanCall turn_off ()
 
FanCall toggle ()
 
FanCall make_call ()
 
void add_on_state_callback (std::function< void()> &&callback)
 Register a callback that will be called each time the state changes.
 
void publish_state ()
 
virtual FanTraits get_traits ()=0
 
void set_restore_mode (FanRestoreMode restore_mode)
 Set the restore mode of this fan.
 
StringRef get_preset_mode () const
 Get the current preset mode.
 
bool has_preset_mode () const
 Check if a preset mode is currently active.
 
- Public Member Functions inherited from esphome::EntityBase
const StringRefget_name () const
 
void set_name (const char *name)
 
void set_name (const char *name, uint32_t object_id_hash)
 Set name with pre-computed object_id hash (avoids runtime hash calculation) Use hash=0 for dynamic names that need runtime calculation.
 
bool has_own_name () const
 
ESPDEPRECATED("object_id mangles names and all object_id methods are planned for removal " "(see https://github.com/esphome/backlog/issues/76). " "Now is the time to stop using object_id. If still needed, use get_object_id_to() " "which will remain available longer. get_object_id() will be removed in 2026.7.0", "2025.12.0") std uint32_t get_object_id_hash ()
 
StringRef get_object_id_to (std::span< char, OBJECT_ID_MAX_LEN > buf) const
 Get object_id with zero heap allocation For static case: returns StringRef to internal storage (buffer unused) For dynamic case: formats into buffer and returns StringRef to buffer.
 
size_t write_object_id_to (char *buf, size_t buf_size) const
 Write object_id directly to buffer, returns length written (excluding null) Useful for building compound strings without intermediate buffer.
 
bool is_internal () const
 
void set_internal (bool internal)
 
bool is_disabled_by_default () const
 
void set_disabled_by_default (bool disabled_by_default)
 
EntityCategory get_entity_category () const
 
void set_entity_category (EntityCategory entity_category)
 
ESPDEPRECATED("Use get_icon_ref() instead for better performance (avoids string copy). Will be removed in ESPHome 2026.5.0", "2025.11.0") std void set_icon (const char *icon)
 
StringRef get_icon_ref () const
 
uint32_t get_device_id () const
 
void set_device (Device *device)
 
Deviceget_device () const
 
bool has_state () const
 
void set_has_state (bool state)
 
 ESPDEPRECATED ("Use make_entity_preference<T>() instead, or preferences won't be migrated. " "See https://github.com/esphome/backlog/issues/85. Will be removed in 2027.1.0.", "2026.7.0") uint32_t get_preference_hash()
 Get a unique hash for storing preferences/settings for this entity.
 
template<typename T >
ESPPreferenceObject make_entity_preference (uint32_t version=0)
 Create a preference object for storing this entity's state/settings.
 

Data Fields

bool state {false}
 The current on/off state of the fan.
 
bool oscillating {false}
 The current oscillation state of the fan.
 
int speed {0}
 The current fan speed level.
 
FanDirection direction {FanDirection::FORWARD}
 The current direction of the fan.
 

Protected Member Functions

virtual void control (const FanCall &call)=0
 
optional< FanRestoreStaterestore_state_ ()
 
void save_state_ ()
 
void dump_traits_ (const char *tag, const char *prefix)
 
bool set_preset_mode_ (const char *preset_mode, size_t len)
 Set the preset mode (finds and stores pointer from traits).
 
bool set_preset_mode_ (const char *preset_mode)
 
bool set_preset_mode_ (const std::string &preset_mode)
 
bool set_preset_mode_ (StringRef preset_mode)
 
void clear_preset_mode_ ()
 Clear the preset mode.
 
void apply_preset_mode_ (const FanCall &call)
 Apply preset mode from a FanCall (handles speed-clears-preset convention)
 
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 * find_preset_mode_ (const char *preset_mode, size_t len)
 
- Protected Member Functions inherited from esphome::EntityBase
ESPPreferenceObject make_entity_preference_ (size_t size, uint32_t version)
 Non-template helper for make_entity_preference() to avoid code bloat.
 
void calc_object_id_ ()
 

Protected Attributes

friend FanCall
 
LazyCallbackManager< void()> state_callback_ {}
 
ESPPreferenceObject rtc_
 
FanRestoreMode restore_mode_
 
- Protected Attributes inherited from esphome::EntityBase
StringRef name_
 
const char * icon_c_str_ {nullptr}
 
uint32_t object_id_hash_ {}
 
Devicedevice_ {}
 
struct esphome::EntityBase::EntityFlags flags_
 

Detailed Description

Definition at line 108 of file fan.h.

Member Function Documentation

◆ add_on_state_callback()

void esphome::fan::Fan::add_on_state_callback ( std::function< void()> && callback)

Register a callback that will be called each time the state changes.

Definition at line 196 of file fan.cpp.

◆ apply_preset_mode_()

void esphome::fan::Fan::apply_preset_mode_ ( const FanCall & call)
protected

Apply preset mode from a FanCall (handles speed-clears-preset convention)

Definition at line 187 of file fan.cpp.

◆ clear_preset_mode_()

void esphome::fan::Fan::clear_preset_mode_ ( )
protected

Clear the preset mode.

Definition at line 185 of file fan.cpp.

◆ control()

◆ dump_traits_()

void esphome::fan::Fan::dump_traits_ ( const char * tag,
const char * prefix )
protected

Definition at line 288 of file fan.cpp.

◆ find_preset_mode_() [1/2]

const char * esphome::fan::Fan::find_preset_mode_ ( const char * preset_mode)
protected

Find and return the matching preset mode pointer from traits, or nullptr if not found.

Definition at line 146 of file fan.cpp.

◆ find_preset_mode_() [2/2]

const char * esphome::fan::Fan::find_preset_mode_ ( const char * preset_mode,
size_t len )
protected

Definition at line 150 of file fan.cpp.

◆ get_preset_mode()

StringRef esphome::fan::Fan::get_preset_mode ( ) const
inline

Get the current preset mode.

Returns a StringRef of the string stored in traits, or empty ref if not set. The returned ref points to string literals from codegen (static storage). Traits are set once at startup and valid for the lifetime of the program.

Definition at line 138 of file fan.h.

◆ get_traits()

◆ has_preset_mode()

bool esphome::fan::Fan::has_preset_mode ( ) const
inline

Check if a preset mode is currently active.

Definition at line 141 of file fan.h.

◆ make_call()

FanCall esphome::fan::Fan::make_call ( )

Definition at line 144 of file fan.cpp.

◆ publish_state()

void esphome::fan::Fan::publish_state ( )

Definition at line 197 of file fan.cpp.

◆ restore_state_()

optional< FanRestoreState > esphome::fan::Fan::restore_state_ ( )
protected

Definition at line 225 of file fan.cpp.

◆ save_state_()

void esphome::fan::Fan::save_state_ ( )
protected

Definition at line 260 of file fan.cpp.

◆ set_preset_mode_() [1/4]

bool esphome::fan::Fan::set_preset_mode_ ( const char * preset_mode)
protected

Definition at line 171 of file fan.cpp.

◆ set_preset_mode_() [2/4]

bool esphome::fan::Fan::set_preset_mode_ ( const char * preset_mode,
size_t len )
protected

Set the preset mode (finds and stores pointer from traits).

Returns true if changed. Passing nullptr or empty string clears the preset mode.

Definition at line 154 of file fan.cpp.

◆ set_preset_mode_() [3/4]

bool esphome::fan::Fan::set_preset_mode_ ( const std::string & preset_mode)
protected

Definition at line 175 of file fan.cpp.

◆ set_preset_mode_() [4/4]

bool esphome::fan::Fan::set_preset_mode_ ( StringRef preset_mode)
protected

Definition at line 179 of file fan.cpp.

◆ set_restore_mode()

void esphome::fan::Fan::set_restore_mode ( FanRestoreMode restore_mode)
inline

Set the restore mode of this fan.

Definition at line 132 of file fan.h.

◆ toggle()

FanCall esphome::fan::Fan::toggle ( )

Definition at line 143 of file fan.cpp.

◆ turn_off()

FanCall esphome::fan::Fan::turn_off ( )

Definition at line 142 of file fan.cpp.

◆ turn_on()

FanCall esphome::fan::Fan::turn_on ( )

Definition at line 141 of file fan.cpp.

Field Documentation

◆ direction

FanDirection esphome::fan::Fan::direction {FanDirection::FORWARD}

The current direction of the fan.

Definition at line 117 of file fan.h.

◆ FanCall

friend esphome::fan::Fan::FanCall
protected

Definition at line 144 of file fan.h.

◆ oscillating

bool esphome::fan::Fan::oscillating {false}

The current oscillation state of the fan.

Definition at line 113 of file fan.h.

◆ restore_mode_

FanRestoreMode esphome::fan::Fan::restore_mode_
protected

Definition at line 170 of file fan.h.

◆ rtc_

ESPPreferenceObject esphome::fan::Fan::rtc_
protected

Definition at line 169 of file fan.h.

◆ speed

int esphome::fan::Fan::speed {0}

The current fan speed level.

Definition at line 115 of file fan.h.

◆ state

bool esphome::fan::Fan::state {false}

The current on/off state of the fan.

Definition at line 111 of file fan.h.

◆ state_callback_

LazyCallbackManager<void()> esphome::fan::Fan::state_callback_ {}
protected

Definition at line 168 of file fan.h.


The documentation for this class was generated from the following files: