esphome/esphome/components/mqtt/mqtt_text_sensor.cpp
Pauline Middelink 071272a27f Fix mqtt_text_sensor to honor unique_id when set. (#698)
* Fix mqtt_text_sensor to honor unique_id when set.
* Remove setting of unique_id in json tree, as the mqtt_component already does this, and in fact overrides what we do here.
* Add unqiue_id() and dump_config() to the wifi_info sensors.
2019-08-27 10:28:50 -07:00

45 lines
1.4 KiB
C++

#include "mqtt_text_sensor.h"
#include "esphome/core/log.h"
#ifdef USE_TEXT_SENSOR
namespace esphome {
namespace mqtt {
static const char *TAG = "mqtt.text_sensor";
using namespace esphome::text_sensor;
MQTTTextSensor::MQTTTextSensor(TextSensor *sensor) : MQTTComponent(), sensor_(sensor) {}
void MQTTTextSensor::send_discovery(JsonObject &root, mqtt::SendDiscoveryConfig &config) {
if (!this->sensor_->get_icon().empty())
root["icon"] = this->sensor_->get_icon();
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;
}
}
bool MQTTTextSensor::is_internal() { return this->sensor_->is_internal(); }
std::string MQTTTextSensor::component_type() const { return "sensor"; }
std::string MQTTTextSensor::friendly_name() const { return this->sensor_->get_name(); }
std::string MQTTTextSensor::unique_id() { return this->sensor_->unique_id(); }
} // namespace mqtt
} // namespace esphome
#endif