1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-26 23:22:21 +01:00

Add text component (#5336)

Co-authored-by: Maurits <maurits@vloop.nl>
Co-authored-by: mauritskorse <mauritskorse@gmail.com>
Co-authored-by: Daniel Dunn <dannydunn@eternityforest.com>
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
Daniel Dunn
2023-10-25 03:00:32 -06:00
committed by GitHub
parent cf7df9e360
commit e80bd8ed3d
50 changed files with 1545 additions and 7 deletions

View File

@@ -111,6 +111,7 @@ MQTTSensorComponent = mqtt_ns.class_("MQTTSensorComponent", MQTTComponent)
MQTTSwitchComponent = mqtt_ns.class_("MQTTSwitchComponent", MQTTComponent)
MQTTTextSensor = mqtt_ns.class_("MQTTTextSensor", MQTTComponent)
MQTTNumberComponent = mqtt_ns.class_("MQTTNumberComponent", MQTTComponent)
MQTTTextComponent = mqtt_ns.class_("MQTTTextComponent", MQTTComponent)
MQTTSelectComponent = mqtt_ns.class_("MQTTSelectComponent", MQTTComponent)
MQTTButtonComponent = mqtt_ns.class_("MQTTButtonComponent", MQTTComponent)
MQTTLockComponent = mqtt_ns.class_("MQTTLockComponent", MQTTComponent)

View File

@@ -0,0 +1,63 @@
#include "mqtt_text.h"
#include "esphome/core/log.h"
#include "mqtt_const.h"
#ifdef USE_MQTT
#ifdef USE_TEXT
namespace esphome {
namespace mqtt {
static const char *const TAG = "mqtt.text";
using namespace esphome::text;
MQTTTextComponent::MQTTTextComponent(Text *text) : text_(text) {}
void MQTTTextComponent::setup() {
this->subscribe(this->get_command_topic_(), [this](const std::string &topic, const std::string &state) {
auto call = this->text_->make_call();
call.set_value(state);
call.perform();
});
this->text_->add_on_state_callback([this](const std::string &state) { this->publish_state(state); });
}
void MQTTTextComponent::dump_config() {
ESP_LOGCONFIG(TAG, "MQTT text '%s':", this->text_->get_name().c_str());
LOG_MQTT_COMPONENT(true, false)
}
std::string MQTTTextComponent::component_type() const { return "text"; }
const EntityBase *MQTTTextComponent::get_entity() const { return this->text_; }
void MQTTTextComponent::send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) {
switch (this->text_->traits.get_mode()) {
case TEXT_MODE_TEXT:
root[MQTT_MODE] = "text";
break;
case TEXT_MODE_PASSWORD:
root[MQTT_MODE] = "password";
break;
}
config.command_topic = true;
}
bool MQTTTextComponent::send_initial_state() {
if (this->text_->has_state()) {
return this->publish_state(this->text_->state);
} else {
return true;
}
}
bool MQTTTextComponent::publish_state(const std::string &value) {
return this->publish(this->get_state_topic_(), value);
}
} // namespace mqtt
} // namespace esphome
#endif
#endif // USE_MQTT

View File

@@ -0,0 +1,46 @@
#pragma once
#include "esphome/core/defines.h"
#ifdef USE_MQTT
#ifdef USE_TEXT
#include "esphome/components/text/text.h"
#include "mqtt_component.h"
namespace esphome {
namespace mqtt {
class MQTTTextComponent : public mqtt::MQTTComponent {
public:
/** Construct this MQTTTextComponent instance with the provided friendly_name and text
*
* @param text The text input.
*/
explicit MQTTTextComponent(text::Text *text);
// ========== INTERNAL METHODS ==========
// (In most use cases you won't need these)
/// Override setup.
void setup() override;
void dump_config() override;
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override;
bool send_initial_state() override;
bool publish_state(const std::string &value);
protected:
/// Override for MQTTComponent, returns "text".
std::string component_type() const override;
const EntityBase *get_entity() const override;
text::Text *text_;
};
} // namespace mqtt
} // namespace esphome
#endif
#endif // USE_MQTT