12static 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_direction_command_topic(), [
this](
const std::string &topic,
const std::string &payload) {
51 ESP_LOGD(TAG,
"'%s': Setting direction FORWARD", this->
friendly_name().c_str());
55 ESP_LOGD(TAG,
"'%s': Setting direction REVERSE", this->
friendly_name().c_str());
65 ESP_LOGW(TAG,
"Unknown direction Payload %s", payload.c_str());
73 this->
subscribe(this->get_oscillation_command_topic(),
74 [
this](
const std::string &topic,
const std::string &payload) {
75 auto val =
parse_on_off(payload.c_str(),
"oscillate_on",
"oscillate_off");
78 ESP_LOGD(TAG,
"'%s': Setting oscillating ON", this->
friendly_name().c_str());
82 ESP_LOGD(TAG,
"'%s': Setting oscillating OFF", this->
friendly_name().c_str());
89 ESP_LOGW(TAG,
"Unknown Oscillation Payload %s", payload.c_str());
97 this->
subscribe(this->get_speed_level_command_topic(),
98 [
this](
const std::string &topic,
const std::string &payload) {
101 const int speed_level = speed_level_opt.
value();
103 ESP_LOGD(TAG,
"New speed level %d", speed_level);
106 ESP_LOGW(TAG,
"Invalid speed level %d", speed_level);
110 ESP_LOGW(TAG,
"Invalid speed level %s (int expected)", payload.c_str());
122 LOG_MQTT_COMPONENT(
true,
true);
124 ESP_LOGCONFIG(TAG,
" Direction State Topic: '%s'", this->get_direction_state_topic().c_str());
125 ESP_LOGCONFIG(TAG,
" Direction Command Topic: '%s'", this->get_direction_command_topic().c_str());
128 ESP_LOGCONFIG(TAG,
" Oscillation State Topic: '%s'", this->get_oscillation_state_topic().c_str());
129 ESP_LOGCONFIG(TAG,
" Oscillation Command Topic: '%s'", this->get_oscillation_command_topic().c_str());
132 ESP_LOGCONFIG(TAG,
" Speed Level State Topic: '%s'", this->get_speed_level_state_topic().c_str());
133 ESP_LOGCONFIG(TAG,
" Speed Level Command Topic: '%s'", this->get_speed_level_command_topic().c_str());
155 const char *state_s = this->
state_->
state ?
"ON" :
"OFF";
160 bool success = this->
publish(this->get_direction_state_topic(),
162 failed = failed || !success;
165 bool success = this->
publish(this->get_oscillation_state_topic(),
167 failed = failed || !success;
170 if (traits.supports_speed()) {
172 bool success = this->
publish(this->get_speed_level_state_topic(), payload);
173 failed = failed || !success;
void status_momentary_warning(const std::string &name, uint32_t length=5000)
void defer(const std::string &name, std::function< void()> &&f)
Defer a callback to the next loop() call.
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.
virtual std::string friendly_name() const
Get the friendly name of this MQTT component.
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.
void subscribe(const std::string &topic, mqtt_callback_t callback, uint8_t qos=0)
Subscribe to a MQTT topic.
fan::Fan * get_state() const
const EntityBase * get_entity() const override
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.
std::string component_type() const override
'fan' component type for discovery.
void setup() override
Setup the fan subscriptions and discovery.
MQTTFanComponent(fan::Fan *state)
value_type const & value() const
constexpr const char *const MQTT_DIRECTION_COMMAND_TOPIC
constexpr const char *const MQTT_DIRECTION_STATE_TOPIC
constexpr const char *const MQTT_PERCENTAGE_COMMAND_TOPIC
constexpr const char *const MQTT_OSCILLATION_STATE_TOPIC
constexpr const char *const MQTT_SPEED_RANGE_MAX
constexpr const char *const MQTT_OSCILLATION_COMMAND_TOPIC
constexpr const char *const MQTT_PERCENTAGE_STATE_TOPIC
Providing packet encoding functions for exchanging data with a remote host.
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.
std::string to_string(int value)
Simple Helper struct used for Home Assistant MQTT send_discovery().