12 static const char *
const TAG =
"mqtt.fan";
27 ESP_LOGD(TAG,
"'%s' Turning Fan ON.", this->
friendly_name().c_str());
31 ESP_LOGD(TAG,
"'%s' Turning Fan OFF.", this->
friendly_name().c_str());
35 ESP_LOGD(TAG,
"'%s' Toggling Fan.", this->
friendly_name().c_str());
40 ESP_LOGW(TAG,
"Unknown state payload %s", payload.c_str());
47 this->
subscribe(this->get_oscillation_command_topic(),
48 [
this](
const std::string &topic,
const std::string &payload) {
49 auto val =
parse_on_off(payload.c_str(),
"oscillate_on",
"oscillate_off");
52 ESP_LOGD(TAG,
"'%s': Setting oscillating ON", this->
friendly_name().c_str());
56 ESP_LOGD(TAG,
"'%s': Setting oscillating OFF", this->
friendly_name().c_str());
63 ESP_LOGW(TAG,
"Unknown Oscillation Payload %s", payload.c_str());
71 this->
subscribe(this->get_speed_level_command_topic(),
72 [
this](
const std::string &topic,
const std::string &payload) {
75 const int speed_level = speed_level_opt.
value();
77 ESP_LOGD(TAG,
"New speed level %d", speed_level);
80 ESP_LOGW(TAG,
"Invalid speed level %d", speed_level);
84 ESP_LOGW(TAG,
"Invalid speed level %s (int expected)", payload.c_str());
96 LOG_MQTT_COMPONENT(
true,
true);
98 ESP_LOGCONFIG(TAG,
" Oscillation State Topic: '%s'", this->get_oscillation_state_topic().c_str());
99 ESP_LOGCONFIG(TAG,
" Oscillation Command Topic: '%s'", this->get_oscillation_command_topic().c_str());
102 ESP_LOGCONFIG(TAG,
" Speed Level State Topic: '%s'", this->get_speed_level_state_topic().c_str());
103 ESP_LOGCONFIG(TAG,
" Speed Level Command Topic: '%s'", this->get_speed_level_command_topic().c_str());
121 const char *state_s = this->
state_->
state ?
"ON" :
"OFF";
126 bool success = this->
publish(this->get_oscillation_state_topic(),
128 failed = failed || !success;
131 if (traits.supports_speed()) {
133 bool success = this->
publish(this->get_speed_level_state_topic(), payload);
134 failed = failed || !success;
value_type const & value() const
bool state
The current on/off state of the fan.
bool oscillating
The current oscillation state of the fan.
MQTTFanComponent(fan::Fan *state)
void setup() override
Setup the fan subscriptions and discovery.
const EntityBase * get_entity() const override
void status_momentary_warning(const std::string &name, uint32_t length=5000)
void dump_config() override
virtual FanTraits get_traits()=0
void defer(const std::string &name, std::function< void()> &&f)
Defer a callback to the next loop() call.
fan::Fan * get_state() const
constexpr const char *const MQTT_OSCILLATION_STATE_TOPIC
bool supports_oscillation() const
Return if this fan supports oscillation.
bool publish(const std::string &topic, const std::string &payload)
Send a MQTT message.
ParseOnOffState parse_on_off(const char *str, const char *on, const char *off)
Parse a string that contains either on, off or toggle.
int supported_speed_count() const
Return how many speed levels the fan has.
void add_on_state_callback(std::function< void()> &&callback)
Register a callback that will be called each time the state changes.
std::string component_type() const override
'fan' component type for discovery.
state state state void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
void subscribe(const std::string &topic, mqtt_callback_t callback, uint8_t qos=0)
Subscribe to a MQTT topic.
FanCall & set_speed(int speed)
int speed
The current fan speed level.
Simple Helper struct used for Home Assistant MQTT send_discovery().
constexpr const char *const MQTT_OSCILLATION_COMMAND_TOPIC
constexpr const char *const MQTT_PERCENTAGE_COMMAND_TOPIC
FanCall & set_oscillating(bool oscillating)
constexpr const char *const MQTT_PERCENTAGE_STATE_TOPIC
constexpr const char *const MQTT_SPEED_RANGE_MAX
bool send_initial_state() override
Send the full current state to MQTT.
constexpr const char * c_str() const
std::string to_string(int value)
virtual std::string friendly_name() const
Get the friendly name of this MQTT component.
Implementation of SPI Controller mode.
std::string get_state_topic_() const
Get the MQTT topic that new states will be shared to.
std::string get_command_topic_() const
Get the MQTT topic for listening to commands.
const StringRef & get_name() const
bool supports_speed() const
Return if this fan supports speed modes.