mirror of
https://github.com/esphome/esphome.git
synced 2025-07-11 09:33:26 +01:00
.devcontainer
.github
.vscode
docker
esphome
components
a01nyub
a02yyuw
a4988
absolute_humidity
ac_dimmer
adalight
adc
adc128s102
addressable_light
ade7880
ade7953
ade7953_base
ade7953_i2c
ade7953_spi
ads1115
ads1118
ags10
aht10
airthings_ble
airthings_wave_base
airthings_wave_mini
airthings_wave_plus
alarm_control_panel
alpha3
am2315c
am2320
am43
analog_threshold
animation
anova
apds9306
apds9960
api
as3935
as3935_i2c
as3935_spi
as5600
as7341
async_tcp
at581x
atc_mithermometer
atm90e26
atm90e32
b_parasite
ballu
bang_bang
bedjet
beken_spi_led_strip
bh1750
binary
binary_sensor
binary_sensor_map
bk72xx
bl0939
bl0940
bl0942
ble_client
ble_presence
ble_rssi
ble_scanner
bluetooth_proxy
bme280_base
bme280_i2c
bme280_spi
bme680
bme680_bsec
bmi160
bmp085
bmp280
bmp3xx
bmp3xx_base
bmp3xx_i2c
bmp3xx_spi
bmp581
bp1658cj
bp5758d
button
canbus
cap1188
captive_portal
ccs811
cd74hc4067
climate
climate_ir
climate_ir_lg
color
color_temperature
combination
coolix
copy
cover
cs5460a
cse7761
cse7766
cst226
cst816
ct_clamp
current_based
custom
custom_component
cwww
dac7678
daikin
daikin_arc
daikin_brc
dallas
dallas_temp
daly_bms
dashboard_import
datetime
debug
deep_sleep
delonghi
demo
dfplayer
dfrobot_sen0395
dht
dht12
display
display_menu_base
dps310
ds1307
dsmr
duty_cycle
duty_time
e131
ee895
ektf2232
emc2101
emmeti
endstop
ens160
ens160_base
ens160_i2c
ens160_spi
ens210
esp32
esp32_ble
esp32_ble_beacon
esp32_ble_client
esp32_ble_server
esp32_ble_tracker
esp32_camera
esp32_camera_web_server
esp32_can
esp32_dac
esp32_hall
esp32_improv
esp32_rmt
esp32_rmt_led_strip
esp32_touch
esp8266
esp8266_pwm
esphome
ethernet
ethernet_info
event
exposure_notifications
external_components
ezo
ezo_pmp
factory_reset
fan
fastled_base
fastled_clockless
fastled_spi
feedback
fingerprint_grow
font
fs3000
ft5x06
ft63x6
fujitsu_general
gcja5
gdk101
globals
gp8403
gpio
gps
graph
graphical_display_menu
gree
grove_tb6612fng
growatt_solar
gt911
haier
havells_solar
hbridge
hdc1080
he60r
heatpumpir
hitachi_ac344
hitachi_ac424
hlw8012
hm3301
hmc5883l
homeassistant
honeywell_hih_i2c
honeywellabp
honeywellabp2_i2c
host
hrxl_maxsonar_wr
hte501
http_request
htu21d
htu31d
hx711
hydreon_rgxx
hyt271
i2c
i2s_audio
iaqcore
ili9341
ili9xxx
image
improv_base
improv_serial
ina219
ina226
ina260
ina2xx_base
ina2xx_i2c
ina2xx_spi
ina3221
inkbird_ibsth1_mini
inkplate6
integration
internal_temperature
interval
jsn_sr04t
json
kalman_combinator
kamstrup_kmp
key_collector
key_provider
kmeteriso
kuntze
lcd_base
lcd_gpio
lcd_menu
lcd_pcf8574
ld2410
ld2420
ledc
libretiny
libretiny_pwm
light
lightwaverf
lilygo_t5_47
lock
logger
ltr390
ltr_als_ps
lvgl
m5stack_8angle
matrix_keypad
max31855
max31856
max31865
max44009
max6675
max6956
max7219
max7219digit
max9611
mcp23008
mcp23016
mcp23017
mcp23s08
mcp23s17
mcp23x08_base
mcp23x17_base
mcp23xxx_base
mcp2515
mcp3008
mcp3204
mcp4725
mcp4728
mcp47a1
mcp9600
mcp9808
md5
mdns
media_player
mhz19
micro_wake_word
micronova
microphone
mics_4514
midea
midea_ac
midea_ir
mitsubishi
mlx90393
mlx90614
mmc5603
mmc5983
modbus
modbus_controller
modem
monochromatic
mopeka_ble
mopeka_pro_check
mopeka_std_check
mpl3115a2
mpr121
mpu6050
mpu6886
mqtt
__init__.py
custom_mqtt_device.cpp
custom_mqtt_device.h
mqtt_alarm_control_panel.cpp
mqtt_alarm_control_panel.h
mqtt_backend.h
mqtt_backend_esp32.cpp
mqtt_backend_esp32.h
mqtt_backend_esp8266.h
mqtt_backend_libretiny.h
mqtt_binary_sensor.cpp
mqtt_binary_sensor.h
mqtt_button.cpp
mqtt_button.h
mqtt_client.cpp
mqtt_client.h
mqtt_climate.cpp
mqtt_climate.h
mqtt_component.cpp
mqtt_component.h
mqtt_const.h
mqtt_cover.cpp
mqtt_cover.h
mqtt_date.cpp
mqtt_date.h
mqtt_datetime.cpp
mqtt_datetime.h
mqtt_event.cpp
mqtt_event.h
mqtt_fan.cpp
mqtt_fan.h
mqtt_light.cpp
mqtt_light.h
mqtt_lock.cpp
mqtt_lock.h
mqtt_number.cpp
mqtt_number.h
mqtt_select.cpp
mqtt_select.h
mqtt_sensor.cpp
mqtt_sensor.h
mqtt_switch.cpp
mqtt_switch.h
mqtt_text.cpp
mqtt_text.h
mqtt_text_sensor.cpp
mqtt_text_sensor.h
mqtt_time.cpp
mqtt_time.h
mqtt_update.cpp
mqtt_update.h
mqtt_valve.cpp
mqtt_valve.h
mqtt_subscribe
ms5611
ms8607
my9231
neopixelbus
network
nextion
nfc
noblex
ntc
number
one_wire
online_image
ota
output
packages
partition
pca6416a
pca9554
pca9685
pcd8544
pcf85063
pcf8563
pcf8574
pid
pipsolar
pm1006
pmsa003i
pmsx003
pmwcs3
pn532
pn532_i2c
pn532_spi
pn7150
pn7150_i2c
pn7160
pn7160_i2c
pn7160_spi
power_supply
preferences
prometheus
psram
pulse_counter
pulse_meter
pulse_width
pvvx_mithermometer
pylontech
pzem004t
pzemac
pzemdc
qmc5883l
qmp6988
qr_code
qspi_amoled
qwiic_pir
radon_eye_ble
radon_eye_rd200
rc522
rc522_i2c
rc522_spi
rdm6300
remote_base
remote_receiver
remote_transmitter
resistance
resistance_sampler
restart
rf_bridge
rgb
rgbct
rgbw
rgbww
rotary_encoder
rp2040
rp2040_pio
rp2040_pio_led_strip
rp2040_pwm
rpi_dpi_rgb
rtl87xx
rtttl
ruuvi_ble
ruuvitag
safe_mode
scd30
scd4x
script
sdl
sdm_meter
sdp3x
sds011
seeed_mr24hpc1
selec_meter
select
sen0321
sen21231
sen5x
senseair
sensirion_common
sensor
servo
sfa30
sgp30
sgp40
sgp4x
shelly_dimmer
sht3xd
sht4x
shtcx
shutdown
sigma_delta_output
sim800l
slow_pwm
sm10bit_base
sm16716
sm2135
sm2235
sm2335
sm300d2
sml
smt100
sn74hc165
sn74hc595
sntp
socket
sonoff_d1
speaker
speed
spi
spi_device
spi_led_strip
sprinkler
sps30
ssd1306_base
ssd1306_i2c
ssd1306_spi
ssd1322_base
ssd1322_spi
ssd1325_base
ssd1325_spi
ssd1327_base
ssd1327_i2c
ssd1327_spi
ssd1331_base
ssd1331_spi
ssd1351_base
ssd1351_spi
st7567_base
st7567_i2c
st7567_spi
st7701s
st7735
st7789v
st7920
status
status_led
stepper
sts3x
substitutions
sun
sun_gtil2
switch
sx1509
t6615
tca9548a
tcl112
tcs34725
tee501
teleinfo
template
text
text_sensor
thermostat
time
time_based
tlc59208f
tlc5947
tlc5971
tm1621
tm1637
tm1638
tm1651
tmp102
tmp1075
tmp117
tof10120
toshiba
total_daily_energy
touchscreen
tsl2561
tsl2591
tt21100
ttp229_bsf
ttp229_lsf
tuya
tx20
uart
ufire_ec
ufire_ise
uln2003
ultrasonic
update
uponor_smatrix
uptime
valve
vbus
veml3235
veml7700
version
vl53l0x
voice_assistant
voltage_sampler
wake_on_lan
watchdog
waveshare_epaper
web_server
web_server_base
web_server_idf
weikai
weikai_i2c
weikai_spi
whirlpool
whynter
wiegand
wifi
wifi_info
wifi_signal
wireguard
wk2132_i2c
wk2132_spi
wk2168_i2c
wk2168_spi
wk2204_i2c
wk2204_spi
wk2212_i2c
wk2212_spi
wl_134
wled
x9c
xgzp68xx
xiaomi_ble
xiaomi_cgd1
xiaomi_cgdk2
xiaomi_cgg1
xiaomi_cgpr1
xiaomi_gcls002
xiaomi_hhccjcy01
xiaomi_hhccjcy10
xiaomi_hhccpot002
xiaomi_jqjcy01ym
xiaomi_lywsd02
xiaomi_lywsd03mmc
xiaomi_lywsdcgq
xiaomi_mhoc303
xiaomi_mhoc401
xiaomi_miscale
xiaomi_miscale2
xiaomi_mjyd02yla
xiaomi_mue4094rt
xiaomi_rtcgq02lm
xiaomi_wx08zm
xl9535
xpt2046
yashima
zhlt01
zio_ultrasonic
zyaura
__init__.py
core
dashboard
__init__.py
__main__.py
automation.py
codegen.py
config.py
config_helpers.py
config_validation.py
const.py
coroutine.py
cpp_generator.py
cpp_helpers.py
cpp_types.py
espota2.py
external_files.py
final_validate.py
git.py
helpers.py
idf_component.yml
loader.py
log.py
mqtt.py
pins.py
platformio_api.py
schema_extractors.py
storage_json.py
types.py
util.py
voluptuous_schema.py
vscode.py
wizard.py
writer.py
yaml_util.py
zeroconf.py
script
tests
.clang-format
.clang-tidy
.coveragerc
.dockerignore
.editorconfig
.flake8
.gitattributes
.gitignore
.pre-commit-config.yaml
.yamllint
CODEOWNERS
CODE_OF_CONDUCT.md
CONTRIBUTING.md
LICENSE
MANIFEST.in
README.md
platformio.ini
pyproject.toml
requirements.txt
requirements_dev.txt
requirements_optional.txt
requirements_test.txt
sdkconfig.defaults
315 lines
12 KiB
C++
315 lines
12 KiB
C++
#include "mqtt_component.h"
|
|
|
|
#ifdef USE_MQTT
|
|
|
|
#include "esphome/core/application.h"
|
|
#include "esphome/core/helpers.h"
|
|
#include "esphome/core/log.h"
|
|
#include "esphome/core/version.h"
|
|
|
|
#include "mqtt_const.h"
|
|
|
|
namespace esphome {
|
|
namespace mqtt {
|
|
|
|
static const char *const TAG = "mqtt.component";
|
|
|
|
void MQTTComponent::set_qos(uint8_t qos) { this->qos_ = qos; }
|
|
|
|
void MQTTComponent::set_retain(bool retain) { this->retain_ = retain; }
|
|
|
|
std::string MQTTComponent::get_discovery_topic_(const MQTTDiscoveryInfo &discovery_info) const {
|
|
std::string sanitized_name = str_sanitize(App.get_name());
|
|
return discovery_info.prefix + "/" + this->component_type() + "/" + sanitized_name + "/" +
|
|
this->get_default_object_id_() + "/config";
|
|
}
|
|
|
|
std::string MQTTComponent::get_default_topic_for_(const std::string &suffix) const {
|
|
const std::string &topic_prefix = global_mqtt_client->get_topic_prefix();
|
|
if (topic_prefix.empty()) {
|
|
// If the topic_prefix is null, the default topic should be null
|
|
return "";
|
|
}
|
|
|
|
return topic_prefix + "/" + this->component_type() + "/" + this->get_default_object_id_() + "/" + suffix;
|
|
}
|
|
|
|
std::string MQTTComponent::get_state_topic_() const {
|
|
if (this->has_custom_state_topic_)
|
|
return this->custom_state_topic_.str();
|
|
return this->get_default_topic_for_("state");
|
|
}
|
|
|
|
std::string MQTTComponent::get_command_topic_() const {
|
|
if (this->has_custom_command_topic_)
|
|
return this->custom_command_topic_.str();
|
|
return this->get_default_topic_for_("command");
|
|
}
|
|
|
|
bool MQTTComponent::publish(const std::string &topic, const std::string &payload) {
|
|
if (topic.empty())
|
|
return false;
|
|
return global_mqtt_client->publish(topic, payload, this->qos_, this->retain_);
|
|
}
|
|
|
|
bool MQTTComponent::publish_json(const std::string &topic, const json::json_build_t &f) {
|
|
if (topic.empty())
|
|
return false;
|
|
return global_mqtt_client->publish_json(topic, f, this->qos_, this->retain_);
|
|
}
|
|
|
|
bool MQTTComponent::send_discovery_() {
|
|
const MQTTDiscoveryInfo &discovery_info = global_mqtt_client->get_discovery_info();
|
|
|
|
if (discovery_info.clean) {
|
|
ESP_LOGV(TAG, "'%s': Cleaning discovery...", this->friendly_name().c_str());
|
|
return global_mqtt_client->publish(this->get_discovery_topic_(discovery_info), "", 0, this->qos_, true);
|
|
}
|
|
|
|
ESP_LOGV(TAG, "'%s': Sending discovery...", this->friendly_name().c_str());
|
|
|
|
return global_mqtt_client->publish_json(
|
|
this->get_discovery_topic_(discovery_info),
|
|
[this](JsonObject root) {
|
|
SendDiscoveryConfig config;
|
|
config.state_topic = true;
|
|
config.command_topic = true;
|
|
|
|
this->send_discovery(root, config);
|
|
|
|
// Fields from EntityBase
|
|
if (this->get_entity()->has_own_name()) {
|
|
root[MQTT_NAME] = this->friendly_name();
|
|
} else {
|
|
root[MQTT_NAME] = "";
|
|
}
|
|
if (this->is_disabled_by_default())
|
|
root[MQTT_ENABLED_BY_DEFAULT] = false;
|
|
if (!this->get_icon().empty())
|
|
root[MQTT_ICON] = this->get_icon();
|
|
|
|
switch (this->get_entity()->get_entity_category()) {
|
|
case ENTITY_CATEGORY_NONE:
|
|
break;
|
|
case ENTITY_CATEGORY_CONFIG:
|
|
root[MQTT_ENTITY_CATEGORY] = "config";
|
|
break;
|
|
case ENTITY_CATEGORY_DIAGNOSTIC:
|
|
root[MQTT_ENTITY_CATEGORY] = "diagnostic";
|
|
break;
|
|
}
|
|
|
|
if (config.state_topic)
|
|
root[MQTT_STATE_TOPIC] = this->get_state_topic_();
|
|
if (config.command_topic)
|
|
root[MQTT_COMMAND_TOPIC] = this->get_command_topic_();
|
|
if (this->command_retain_)
|
|
root[MQTT_COMMAND_RETAIN] = true;
|
|
|
|
if (this->availability_ == nullptr) {
|
|
if (!global_mqtt_client->get_availability().topic.empty()) {
|
|
root[MQTT_AVAILABILITY_TOPIC] = global_mqtt_client->get_availability().topic;
|
|
if (global_mqtt_client->get_availability().payload_available != "online")
|
|
root[MQTT_PAYLOAD_AVAILABLE] = global_mqtt_client->get_availability().payload_available;
|
|
if (global_mqtt_client->get_availability().payload_not_available != "offline")
|
|
root[MQTT_PAYLOAD_NOT_AVAILABLE] = global_mqtt_client->get_availability().payload_not_available;
|
|
}
|
|
} else if (!this->availability_->topic.empty()) {
|
|
root[MQTT_AVAILABILITY_TOPIC] = this->availability_->topic;
|
|
if (this->availability_->payload_available != "online")
|
|
root[MQTT_PAYLOAD_AVAILABLE] = this->availability_->payload_available;
|
|
if (this->availability_->payload_not_available != "offline")
|
|
root[MQTT_PAYLOAD_NOT_AVAILABLE] = this->availability_->payload_not_available;
|
|
}
|
|
|
|
std::string unique_id = this->unique_id();
|
|
const MQTTDiscoveryInfo &discovery_info = global_mqtt_client->get_discovery_info();
|
|
if (!unique_id.empty()) {
|
|
root[MQTT_UNIQUE_ID] = unique_id;
|
|
} else {
|
|
if (discovery_info.unique_id_generator == MQTT_MAC_ADDRESS_UNIQUE_ID_GENERATOR) {
|
|
char friendly_name_hash[9];
|
|
sprintf(friendly_name_hash, "%08" PRIx32, fnv1_hash(this->friendly_name()));
|
|
friendly_name_hash[8] = 0; // ensure the hash-string ends with null
|
|
root[MQTT_UNIQUE_ID] = get_mac_address() + "-" + this->component_type() + "-" + friendly_name_hash;
|
|
} else {
|
|
// default to almost-unique ID. It's a hack but the only way to get that
|
|
// gorgeous device registry view.
|
|
root[MQTT_UNIQUE_ID] = "ESP" + this->component_type() + this->get_default_object_id_();
|
|
}
|
|
}
|
|
|
|
const std::string &node_name = App.get_name();
|
|
if (discovery_info.object_id_generator == MQTT_DEVICE_NAME_OBJECT_ID_GENERATOR)
|
|
root[MQTT_OBJECT_ID] = node_name + "_" + this->get_default_object_id_();
|
|
|
|
std::string node_friendly_name = App.get_friendly_name();
|
|
if (node_friendly_name.empty()) {
|
|
node_friendly_name = node_name;
|
|
}
|
|
const std::string &node_area = App.get_area();
|
|
|
|
JsonObject device_info = root.createNestedObject(MQTT_DEVICE);
|
|
const auto mac = get_mac_address();
|
|
device_info[MQTT_DEVICE_IDENTIFIERS] = mac;
|
|
device_info[MQTT_DEVICE_NAME] = node_friendly_name;
|
|
#ifdef ESPHOME_PROJECT_NAME
|
|
device_info[MQTT_DEVICE_SW_VERSION] = ESPHOME_PROJECT_VERSION " (ESPHome " ESPHOME_VERSION ")";
|
|
const char *model = std::strchr(ESPHOME_PROJECT_NAME, '.');
|
|
if (model == nullptr) { // must never happen but check anyway
|
|
device_info[MQTT_DEVICE_MODEL] = ESPHOME_BOARD;
|
|
device_info[MQTT_DEVICE_MANUFACTURER] = ESPHOME_PROJECT_NAME;
|
|
} else {
|
|
device_info[MQTT_DEVICE_MODEL] = model + 1;
|
|
device_info[MQTT_DEVICE_MANUFACTURER] = std::string(ESPHOME_PROJECT_NAME, model - ESPHOME_PROJECT_NAME);
|
|
}
|
|
#else
|
|
device_info[MQTT_DEVICE_SW_VERSION] = ESPHOME_VERSION " (" + App.get_compilation_time() + ")";
|
|
device_info[MQTT_DEVICE_MODEL] = ESPHOME_BOARD;
|
|
#if defined(USE_ESP8266) || defined(USE_ESP32)
|
|
device_info[MQTT_DEVICE_MANUFACTURER] = "Espressif";
|
|
#elif defined(USE_RP2040)
|
|
device_info[MQTT_DEVICE_MANUFACTURER] = "Raspberry Pi";
|
|
#elif defined(USE_BK72XX)
|
|
device_info[MQTT_DEVICE_MANUFACTURER] = "Beken";
|
|
#elif defined(USE_RTL87XX)
|
|
device_info[MQTT_DEVICE_MANUFACTURER] = "Realtek";
|
|
#elif defined(USE_HOST)
|
|
device_info[MQTT_DEVICE_MANUFACTURER] = "Host";
|
|
#endif
|
|
#endif
|
|
if (!node_area.empty()) {
|
|
device_info[MQTT_DEVICE_SUGGESTED_AREA] = node_area;
|
|
}
|
|
|
|
device_info[MQTT_DEVICE_CONNECTIONS][0][0] = "mac";
|
|
device_info[MQTT_DEVICE_CONNECTIONS][0][1] = mac;
|
|
},
|
|
this->qos_, discovery_info.retain);
|
|
}
|
|
|
|
uint8_t MQTTComponent::get_qos() const { return this->qos_; }
|
|
|
|
bool MQTTComponent::get_retain() const { return this->retain_; }
|
|
|
|
bool MQTTComponent::is_discovery_enabled() const {
|
|
return this->discovery_enabled_ && global_mqtt_client->is_discovery_enabled();
|
|
}
|
|
|
|
std::string MQTTComponent::get_default_object_id_() const {
|
|
return str_sanitize(str_snake_case(this->friendly_name()));
|
|
}
|
|
|
|
void MQTTComponent::subscribe(const std::string &topic, mqtt_callback_t callback, uint8_t qos) {
|
|
global_mqtt_client->subscribe(topic, std::move(callback), qos);
|
|
}
|
|
|
|
void MQTTComponent::subscribe_json(const std::string &topic, const mqtt_json_callback_t &callback, uint8_t qos) {
|
|
global_mqtt_client->subscribe_json(topic, callback, qos);
|
|
}
|
|
|
|
MQTTComponent::MQTTComponent() = default;
|
|
|
|
float MQTTComponent::get_setup_priority() const { return setup_priority::AFTER_CONNECTION; }
|
|
void MQTTComponent::disable_discovery() { this->discovery_enabled_ = false; }
|
|
void MQTTComponent::set_custom_state_topic(const char *custom_state_topic) {
|
|
this->custom_state_topic_ = StringRef(custom_state_topic);
|
|
this->has_custom_state_topic_ = true;
|
|
}
|
|
void MQTTComponent::set_custom_command_topic(const char *custom_command_topic) {
|
|
this->custom_command_topic_ = StringRef(custom_command_topic);
|
|
this->has_custom_command_topic_ = true;
|
|
}
|
|
void MQTTComponent::set_command_retain(bool command_retain) { this->command_retain_ = command_retain; }
|
|
|
|
void MQTTComponent::set_availability(std::string topic, std::string payload_available,
|
|
std::string payload_not_available) {
|
|
this->availability_ = make_unique<Availability>();
|
|
this->availability_->topic = std::move(topic);
|
|
this->availability_->payload_available = std::move(payload_available);
|
|
this->availability_->payload_not_available = std::move(payload_not_available);
|
|
}
|
|
void MQTTComponent::disable_availability() { this->set_availability("", "", ""); }
|
|
void MQTTComponent::call_setup() {
|
|
if (this->is_internal())
|
|
return;
|
|
|
|
this->setup();
|
|
|
|
global_mqtt_client->register_mqtt_component(this);
|
|
|
|
if (!this->is_connected_())
|
|
return;
|
|
|
|
if (this->is_discovery_enabled()) {
|
|
if (!this->send_discovery_()) {
|
|
this->schedule_resend_state();
|
|
}
|
|
}
|
|
if (!this->send_initial_state()) {
|
|
this->schedule_resend_state();
|
|
}
|
|
}
|
|
|
|
void MQTTComponent::call_loop() {
|
|
if (this->is_internal())
|
|
return;
|
|
|
|
this->loop();
|
|
|
|
if (!this->resend_state_ || !this->is_connected_()) {
|
|
return;
|
|
}
|
|
|
|
this->resend_state_ = false;
|
|
if (this->is_discovery_enabled()) {
|
|
if (!this->send_discovery_()) {
|
|
this->schedule_resend_state();
|
|
}
|
|
}
|
|
if (!this->send_initial_state()) {
|
|
this->schedule_resend_state();
|
|
}
|
|
}
|
|
void MQTTComponent::call_dump_config() {
|
|
if (this->is_internal())
|
|
return;
|
|
|
|
this->dump_config();
|
|
}
|
|
void MQTTComponent::schedule_resend_state() { this->resend_state_ = true; }
|
|
std::string MQTTComponent::unique_id() { return ""; }
|
|
bool MQTTComponent::is_connected_() const { return global_mqtt_client->is_connected(); }
|
|
|
|
// Pull these properties from EntityBase if not overridden
|
|
std::string MQTTComponent::friendly_name() const { return this->get_entity()->get_name(); }
|
|
std::string MQTTComponent::get_icon() const { return this->get_entity()->get_icon(); }
|
|
bool MQTTComponent::is_disabled_by_default() const { return this->get_entity()->is_disabled_by_default(); }
|
|
bool MQTTComponent::is_internal() {
|
|
if (this->has_custom_state_topic_) {
|
|
// If the custom state_topic is null, return true as it is internal and should not publish
|
|
// else, return false, as it is explicitly set to a topic, so it is not internal and should publish
|
|
return this->get_state_topic_().empty();
|
|
}
|
|
|
|
if (this->has_custom_command_topic_) {
|
|
// If the custom command_topic is null, return true as it is internal and should not publish
|
|
// else, return false, as it is explicitly set to a topic, so it is not internal and should publish
|
|
return this->get_command_topic_().empty();
|
|
}
|
|
|
|
// No custom topics have been set
|
|
if (this->get_default_topic_for_("").empty()) {
|
|
// If the default topic prefix is null, then the component, by default, is internal and should not publish
|
|
return true;
|
|
}
|
|
|
|
// Use ESPHome's component internal state if topic_prefix is not null with no custom state_topic or command_topic
|
|
return this->get_entity()->is_internal();
|
|
}
|
|
|
|
} // namespace mqtt
|
|
} // namespace esphome
|
|
|
|
#endif // USE_MQTT
|