15static const char *
const TAG =
"mqtt.component";
31 if (topic_prefix.empty()) {
66 if (discovery_info.
clean) {
67 ESP_LOGV(TAG,
"'%s': Cleaning discovery...", this->
friendly_name().c_str());
71 ESP_LOGV(TAG,
"'%s': Sending discovery...", this->
friendly_name().c_str());
75 [
this](JsonObject root) {
97 switch (this->
get_entity()->get_entity_category()) {
137 char friendly_name_hash[9];
139 friendly_name_hash[8] = 0;
153 if (node_friendly_name.empty()) {
154 node_friendly_name = node_name;
158 JsonObject device_info = root.createNestedObject(
MQTT_DEVICE);
162#ifdef ESPHOME_PROJECT_NAME
164 const char *model = std::strchr(ESPHOME_PROJECT_NAME,
'.');
165 if (model ==
nullptr) {
175#if defined(USE_ESP8266) || defined(USE_ESP32)
177#elif defined(USE_RP2040)
179#elif defined(USE_BK72XX)
181#elif defined(USE_RTL87XX)
183#elif defined(USE_HOST)
187 if (!node_area.empty()) {
232 std::string payload_not_available) {
235 this->
availability_->payload_available = std::move(payload_available);
236 this->
availability_->payload_not_available = std::move(payload_not_available);
const std::string & get_area() const
Get the area of this Application set by pre_setup().
const std::string & get_friendly_name() const
Get the friendly name of this Application set by pre_setup().
std::string get_compilation_time() const
const std::string & get_name() const
Get the name of this Application set by pre_setup().
virtual void setup()
Where the component's initialization should happen.
virtual void dump_config()
virtual void loop()
This method will be called repeatedly.
const StringRef & get_name() const
std::string get_icon() const
bool is_disabled_by_default() const
StringRef is a reference to a string owned by something else.
const std::string & get_topic_prefix() const
Get the topic prefix of this device, using default if necessary.
void subscribe_json(const std::string &topic, const mqtt_json_callback_t &callback, uint8_t qos=0)
Subscribe to a MQTT topic and automatically parse JSON payload.
void register_mqtt_component(MQTTComponent *component)
bool publish(const MQTTMessage &message)
Publish a MQTTMessage.
const MQTTDiscoveryInfo & get_discovery_info() const
Get Home Assistant discovery info.
void subscribe(const std::string &topic, mqtt_callback_t callback, uint8_t qos=0)
Subscribe to an MQTT topic and call callback when a message is received.
const Availability & get_availability()
bool publish_json(const std::string &topic, const json::json_build_t &f, uint8_t qos=0, bool retain=false)
Construct and send a JSON MQTT message.
bool is_discovery_enabled() const
void disable_availability()
std::unique_ptr< Availability > availability_
virtual bool is_internal()
bool is_connected_() const
void call_loop() override
MQTTComponent()
Constructs a MQTTComponent.
void set_custom_state_topic(const char *custom_state_topic)
Set a custom state topic. Set to "" for default behavior.
virtual bool is_disabled_by_default() const
Get whether the underlying Entity is disabled by default.
bool has_custom_command_topic_
std::string get_default_object_id_() const
Generate the Home Assistant MQTT discovery object id by automatically transforming the friendly name.
void set_qos(uint8_t qos)
Set QOS for state messages.
bool is_discovery_enabled() const
void schedule_resend_state()
Internal method for the MQTT client base to schedule a resend of the state on reconnect.
bool publish(const std::string &topic, const std::string &payload)
Send a MQTT message.
void set_command_retain(bool command_retain)
Set whether command message should be retained.
bool send_discovery_()
Internal method to start sending discovery info, this will call send_discovery().
void set_subscribe_qos(uint8_t qos)
Set the QOS for subscribe messages (used in discovery).
virtual std::string unique_id()
A unique ID for this MQTT component, empty for no unique id.
void call_dump_config() override
StringRef custom_state_topic_
void subscribe_json(const std::string &topic, const mqtt_json_callback_t &callback, uint8_t qos=0)
Subscribe to a MQTT topic and automatically parse JSON payload.
void call_setup() override
Override setup_ so that we can call send_discovery() when needed.
bool publish_json(const std::string &topic, const json::json_build_t &f)
Construct and send a JSON MQTT message.
virtual std::string friendly_name() const
Get the friendly name of this MQTT component.
std::string get_discovery_topic_(const MQTTDiscoveryInfo &discovery_info) const
Helper method to get the discovery topic for this component.
std::string get_default_topic_for_(const std::string &suffix) const
Get this components state/command/... topic.
virtual std::string get_icon() const
Get the icon field of this component.
virtual std::string component_type() const =0
Override this method to return the component type (e.g. "light", "sensor", ...)
void set_retain(bool retain)
Set whether state message should be retained.
virtual const EntityBase * get_entity() const =0
Gets the Entity served by this MQTT component.
std::string get_state_topic_() const
Get the MQTT topic that new states will be shared to.
virtual void send_discovery(JsonObject root, SendDiscoveryConfig &config)=0
Send discovery info the Home Assistant, override this.
bool has_custom_state_topic_
virtual bool send_initial_state()=0
void disable_discovery()
Disable discovery. Sets friendly name to "".
float get_setup_priority() const override
MQTT_COMPONENT setup priority.
void set_availability(std::string topic, std::string payload_available, std::string payload_not_available)
Set the Home Assistant availability data.
void set_custom_command_topic(const char *custom_command_topic)
Set a custom command topic. Set to "" for default behavior.
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.
StringRef custom_command_topic_
std::function< void(JsonObject)> json_build_t
Callback function typedef for building JsonObjects.
constexpr const char *const MQTT_COMMAND_RETAIN
@ MQTT_DEVICE_NAME_OBJECT_ID_GENERATOR
constexpr const char *const MQTT_COMMAND_TOPIC
@ MQTT_MAC_ADDRESS_UNIQUE_ID_GENERATOR
constexpr const char *const MQTT_DEVICE_IDENTIFIERS
constexpr const char *const MQTT_PAYLOAD_AVAILABLE
constexpr const char *const MQTT_PAYLOAD_NOT_AVAILABLE
constexpr const char *const MQTT_DEVICE_SUGGESTED_AREA
std::function< void(const std::string &, JsonObject)> mqtt_json_callback_t
constexpr const char *const MQTT_DEVICE_SW_VERSION
constexpr const char *const MQTT_DEVICE_NAME
constexpr const char *const MQTT_UNIQUE_ID
std::function< void(const std::string &, const std::string &)> mqtt_callback_t
Callback for MQTT subscriptions.
constexpr const char *const MQTT_STATE_TOPIC
constexpr const char *const MQTT_QOS
MQTTClientComponent * global_mqtt_client
constexpr const char *const MQTT_ENTITY_CATEGORY
constexpr const char *const MQTT_DEVICE_MODEL
constexpr const char *const MQTT_NAME
constexpr const char *const MQTT_DEVICE_MANUFACTURER
constexpr const char *const MQTT_ENABLED_BY_DEFAULT
constexpr const char *const MQTT_AVAILABILITY_TOPIC
constexpr const char *const MQTT_DEVICE_CONNECTIONS
constexpr const char *const MQTT_ICON
constexpr const char *const MQTT_DEVICE
constexpr const char *const MQTT_OBJECT_ID
const float AFTER_CONNECTION
For components that should be initialized after a data connection (API/MQTT) is connected.
Providing packet encoding functions for exchanging data with a remote host.
uint32_t fnv1_hash(const std::string &str)
Calculate a FNV-1 hash of str.
std::string str_sanitize(const std::string &str)
Sanitizes the input string by removing all characters but alphanumerics, dashes and underscores.
@ ENTITY_CATEGORY_DIAGNOSTIC
std::unique_ptr< T > make_unique(Args &&...args)
std::string get_mac_address()
Get the device MAC address as a string, in lowercase hex notation.
Application App
Global storage of Application pointer - only one Application can exist.
std::string str_snake_case(const std::string &str)
Convert the string to snake case (lowercase with underscores).
std::string payload_not_available
std::string topic
Empty means disabled.
std::string payload_available
Internal struct for MQTT Home Assistant discovery.
MQTTDiscoveryUniqueIdGenerator unique_id_generator
std::string prefix
The Home Assistant discovery prefix. Empty means disabled.
MQTTDiscoveryObjectIdGenerator object_id_generator
bool retain
Whether to retain discovery messages.
Simple Helper struct used for Home Assistant MQTT send_discovery().
bool command_topic
If the command topic should be included. Default to true.
bool state_topic
If the state topic should be included. Defaults to true.