2019-04-17 12:06:00 +02:00
|
|
|
#include "mqtt_text_sensor.h"
|
|
|
|
#include "esphome/core/log.h"
|
|
|
|
|
2024-02-25 23:29:39 +01:00
|
|
|
#include "mqtt_const.h"
|
|
|
|
|
2021-09-20 11:47:51 +02:00
|
|
|
#ifdef USE_MQTT
|
2019-04-17 12:06:00 +02:00
|
|
|
#ifdef USE_TEXT_SENSOR
|
|
|
|
|
|
|
|
namespace esphome {
|
|
|
|
namespace mqtt {
|
|
|
|
|
2021-06-10 22:19:44 +02:00
|
|
|
static const char *const TAG = "mqtt.text_sensor";
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
using namespace esphome::text_sensor;
|
|
|
|
|
2022-01-23 08:34:43 +01:00
|
|
|
MQTTTextSensor::MQTTTextSensor(TextSensor *sensor) : sensor_(sensor) {}
|
2022-01-01 10:31:43 +01:00
|
|
|
void MQTTTextSensor::send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) {
|
2024-02-25 23:29:39 +01:00
|
|
|
if (!this->sensor_->get_device_class().empty())
|
|
|
|
root[MQTT_DEVICE_CLASS] = this->sensor_->get_device_class();
|
2019-04-17 12:06:00 +02:00
|
|
|
config.command_topic = false;
|
|
|
|
}
|
|
|
|
void MQTTTextSensor::setup() {
|
|
|
|
this->sensor_->add_on_state_callback([this](const std::string &state) { this->publish_state(state); });
|
|
|
|
}
|
|
|
|
|
|
|
|
void MQTTTextSensor::dump_config() {
|
|
|
|
ESP_LOGCONFIG(TAG, "MQTT Text Sensor '%s':", this->sensor_->get_name().c_str());
|
|
|
|
LOG_MQTT_COMPONENT(true, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MQTTTextSensor::publish_state(const std::string &value) { return this->publish(this->get_state_topic_(), value); }
|
|
|
|
bool MQTTTextSensor::send_initial_state() {
|
|
|
|
if (this->sensor_->has_state()) {
|
|
|
|
return this->publish_state(this->sensor_->state);
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::string MQTTTextSensor::component_type() const { return "sensor"; }
|
2021-10-10 10:37:05 +02:00
|
|
|
const EntityBase *MQTTTextSensor::get_entity() const { return this->sensor_; }
|
2019-08-27 19:28:50 +02:00
|
|
|
std::string MQTTTextSensor::unique_id() { return this->sensor_->unique_id(); }
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
} // namespace mqtt
|
|
|
|
} // namespace esphome
|
|
|
|
|
|
|
|
#endif
|
2021-09-20 11:47:51 +02:00
|
|
|
#endif // USE_MQTT
|