11static const char *
const TAG =
"mqtt.fan";
26 ESP_LOGD(TAG,
"'%s' Turning Fan ON.", this->
friendly_name_().c_str());
30 ESP_LOGD(TAG,
"'%s' Turning Fan OFF.", this->
friendly_name_().c_str());
34 ESP_LOGD(TAG,
"'%s' Toggling Fan.", this->
friendly_name_().c_str());
39 ESP_LOGW(TAG,
"Unknown state payload %s", payload.c_str());
46 this->
subscribe(this->get_direction_command_topic(), [
this](
const std::string &topic,
const std::string &payload) {
50 ESP_LOGD(TAG,
"'%s': Setting direction FORWARD", this->
friendly_name_().c_str());
54 ESP_LOGD(TAG,
"'%s': Setting direction REVERSE", this->
friendly_name_().c_str());
64 ESP_LOGW(TAG,
"Unknown direction Payload %s", payload.c_str());
72 this->
subscribe(this->get_oscillation_command_topic(),
73 [
this](
const std::string &topic,
const std::string &payload) {
74 auto val =
parse_on_off(payload.c_str(),
"oscillate_on",
"oscillate_off");
77 ESP_LOGD(TAG,
"'%s': Setting oscillating ON", this->
friendly_name_().c_str());
81 ESP_LOGD(TAG,
"'%s': Setting oscillating OFF", this->
friendly_name_().c_str());
88 ESP_LOGW(TAG,
"Unknown Oscillation Payload %s", payload.c_str());
96 this->
subscribe(this->get_speed_level_command_topic(),
97 [
this](
const std::string &topic,
const std::string &payload) {
100 const int speed_level = speed_level_opt.
value();
102 ESP_LOGD(TAG,
"New speed level %d", speed_level);
105 ESP_LOGW(TAG,
"Invalid speed level %d", speed_level);
109 ESP_LOGW(TAG,
"Invalid speed level %s (int expected)", payload.c_str());
121 LOG_MQTT_COMPONENT(
true,
true);
124 " Direction State Topic: '%s'\n"
125 " Direction Command Topic: '%s'",
126 this->get_direction_state_topic().c_str(), this->get_direction_command_topic().c_str());
130 " Oscillation State Topic: '%s'\n"
131 " Oscillation Command Topic: '%s'",
132 this->get_oscillation_state_topic().c_str(), this->get_oscillation_command_topic().c_str());
136 " Speed Level State Topic: '%s'\n"
137 " Speed Level Command Topic: '%s'",
138 this->get_speed_level_state_topic().c_str(), this->get_speed_level_command_topic().c_str());
147 root[MQTT_DIRECTION_COMMAND_TOPIC] = this->get_direction_command_topic();
148 root[MQTT_DIRECTION_STATE_TOPIC] = this->get_direction_state_topic();
151 root[MQTT_OSCILLATION_COMMAND_TOPIC] = this->get_oscillation_command_topic();
152 root[MQTT_OSCILLATION_STATE_TOPIC] = this->get_oscillation_state_topic();
155 root[MQTT_PERCENTAGE_COMMAND_TOPIC] = this->get_speed_level_command_topic();
156 root[MQTT_PERCENTAGE_STATE_TOPIC] = this->get_speed_level_state_topic();
161 const char *state_s = this->
state_->
state ?
"ON" :
"OFF";
166 bool success = this->
publish(this->get_direction_state_topic(),
168 failed = failed || !success;
171 bool success = this->
publish(this->get_oscillation_state_topic(),
173 failed = failed || !success;
176 if (traits.supports_speed()) {
179 bool success = this->
publish(this->get_speed_level_state_topic(), buf,
len);
180 failed = failed || !success;
ESPDEPRECATED("Use const char* overload instead. Removed in 2026.7.0", "2026.1.0") void defer(const std voi defer)(const char *name, std::function< void()> &&f)
Defer a callback to the next loop() call.
void status_momentary_warning(const char *name, uint32_t length=5000)
Set warning status flag and automatically clear it after a timeout.
const StringRef & get_name() const
constexpr const char * c_str() const
FanCall & set_oscillating(bool oscillating)
FanCall & set_direction(FanDirection direction)
FanCall & set_speed(int speed)
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.
bool oscillating
The current oscillation state of the fan.
bool state
The current on/off state of the fan.
int speed
The current fan speed level.
int supported_speed_count() const
Return how many speed levels the fan has.
bool supports_direction() const
Return if this fan supports changing direction.
bool supports_speed() const
Return if this fan supports speed modes.
bool supports_oscillation() const
Return if this fan supports oscillation.
bool publish(const std::string &topic, const std::string &payload)
Send a MQTT message.
std::string get_state_topic_() const
Get the MQTT topic that new states will be shared to.
std::string friendly_name_() const
Get the friendly name of this MQTT component.
std::string get_command_topic_() const
Get the MQTT topic for listening to commands.
void subscribe(const std::string &topic, mqtt_callback_t callback, uint8_t qos=0)
Subscribe to a MQTT topic.
fan::Fan * get_state() const
void dump_config() override
state state state state void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
bool send_initial_state() override
Send the full current state to MQTT.
void setup() override
Setup the fan subscriptions and discovery.
MQTTFanComponent(fan::Fan *state)
value_type const & value() const
MQTT_COMPONENT_TYPE(MQTTAlarmControlPanelComponent, "alarm_control_panel") const EntityBase *MQTTAlarmControlPanelComponent
ParseOnOffState parse_on_off(const char *str, const char *on, const char *off)
Parse a string that contains either on, off or toggle.
optional< T > parse_number(const char *str)
Parse an unsigned decimal number from a null-terminated string.
Simple Helper struct used for Home Assistant MQTT send_discovery().