1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-13 16:52:18 +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

@@ -0,0 +1,55 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/entity_base.h"
#include "esphome/core/helpers.h"
#include "text_call.h"
#include "text_traits.h"
namespace esphome {
namespace text {
#define LOG_TEXT(prefix, type, obj) \
if ((obj) != nullptr) { \
ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
if (!(obj)->get_icon().empty()) { \
ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon().c_str()); \
} \
}
/** Base-class for all text inputs.
*
* A text input can use publish_state to send out a new value.
*/
class Text : public EntityBase {
public:
std::string state;
TextTraits traits;
void publish_state(const std::string &state);
/// Return whether this text input has gotten a full state yet.
bool has_state() const { return has_state_; }
/// Instantiate a TextCall object to modify this text component's state.
TextCall make_call() { return TextCall(this); }
void add_on_state_callback(std::function<void(std::string)> &&callback);
protected:
friend class TextCall;
/** Set the value of the text input, this is a virtual method that each text input integration must implement.
*
* This method is called by the TextCall.
*
* @param value The value as validated by the TextCall.
*/
virtual void control(const std::string &value) = 0;
CallbackManager<void(std::string)> state_callback_;
bool has_state_{false};
};
} // namespace text
} // namespace esphome