23 #define LOG_MQTT_COMPONENT(state_topic, command_topic) \ 25 ESP_LOGCONFIG(TAG, " State Topic: '%s'", this->get_state_topic_().c_str()); \ 27 if (command_topic) { \ 28 ESP_LOGCONFIG(TAG, " Command Topic: '%s'", this->get_command_topic_().c_str()); \ 31 #define MQTT_COMPONENT_CUSTOM_TOPIC_(name, type) \ 33 std::string custom_##name##_##type##_topic_{}; \ 36 void set_custom_##name##_##type##_topic(const std::string &topic) { this->custom_##name##_##type##_topic_ = topic; } \ 37 std::string get_##name##_##type##_topic() const { \ 38 if (this->custom_##name##_##type##_topic_.empty()) \ 39 return this->get_default_topic_for_(#name "/" #type); \ 40 return this->custom_##name##_##type##_topic_; \ 43 #define MQTT_COMPONENT_CUSTOM_TOPIC(name, type) MQTT_COMPONENT_CUSTOM_TOPIC_(name, type) 67 void call_setup()
override;
69 void call_loop()
override;
71 void call_dump_config()
override;
76 virtual bool send_initial_state() = 0;
78 virtual bool is_internal();
81 void set_qos(uint8_t qos);
82 uint8_t get_qos()
const;
85 void set_retain(
bool retain);
86 bool get_retain()
const;
89 void disable_discovery();
90 bool is_discovery_enabled()
const;
93 void set_subscribe_qos(uint8_t qos);
96 virtual std::string component_type()
const = 0;
99 void set_custom_state_topic(
const char *custom_state_topic);
101 void set_custom_command_topic(
const char *custom_command_topic);
103 void set_command_retain(
bool command_retain);
106 float get_setup_priority()
const override;
112 void set_availability(std::string topic, std::string payload_available, std::string payload_not_available);
113 void disable_availability();
116 void schedule_resend_state();
123 bool publish(
const std::string &topic,
const std::string &payload);
138 void subscribe(
const std::string &topic,
mqtt_callback_t callback, uint8_t qos = 0);
149 void subscribe_json(
const std::string &topic,
const mqtt_json_callback_t &callback, uint8_t qos = 0);
160 std::string get_default_topic_for_(
const std::string &suffix)
const;
165 virtual const EntityBase *get_entity()
const = 0;
172 virtual std::string unique_id();
175 virtual std::string friendly_name()
const;
178 virtual std::string get_icon()
const;
181 virtual bool is_disabled_by_default()
const;
184 std::string get_state_topic_()
const;
187 std::string get_command_topic_()
const;
189 bool is_connected_()
const;
192 bool send_discovery_();
197 std::string get_default_object_id_()
const;
204 bool has_custom_state_topic_{
false};
205 bool has_custom_command_topic_{
false};
207 bool command_retain_{
false};
210 uint8_t subscribe_qos_{0};
211 bool discovery_enabled_{
true};
212 bool resend_state_{
false};
Internal struct for MQTT Home Assistant discovery.
std::function< void(const std::string &, const std::string &)> mqtt_callback_t
Callback for MQTT subscriptions.
bool state_topic
If the state topic should be included. Defaults to true.
StringRef is a reference to a string owned by something else.
bool command_topic
If the command topic should be included. Default to true.
Simple Helper struct used for Home Assistant MQTT send_discovery().
std::function< void(JsonObject)> json_build_t
Callback function typedef for building JsonObjects.
std::unique_ptr< Availability > availability_
Implementation of SPI Controller mode.
std::function< void(const std::string &, JsonObject)> mqtt_json_callback_t
MQTTComponent is the base class for all components that interact with MQTT to expose certain function...