2019-04-17 12:06:00 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "esphome/core/component.h"
|
|
|
|
#include "esphome/core/helpers.h"
|
|
|
|
|
|
|
|
namespace esphome {
|
|
|
|
namespace text_sensor {
|
|
|
|
|
|
|
|
#define LOG_TEXT_SENSOR(prefix, type, obj) \
|
|
|
|
if (obj != nullptr) { \
|
2019-10-23 14:43:41 +02:00
|
|
|
ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, type, obj->get_name().c_str()); \
|
2019-04-17 12:06:00 +02:00
|
|
|
if (!obj->get_icon().empty()) { \
|
2019-10-23 14:43:41 +02:00
|
|
|
ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, obj->get_icon().c_str()); \
|
2019-04-17 12:06:00 +02:00
|
|
|
} \
|
|
|
|
if (!obj->unique_id().empty()) { \
|
2019-10-23 14:43:41 +02:00
|
|
|
ESP_LOGV(TAG, "%s Unique ID: '%s'", prefix, obj->unique_id().c_str()); \
|
2019-04-17 12:06:00 +02:00
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
class TextSensor : public Nameable {
|
|
|
|
public:
|
|
|
|
explicit TextSensor();
|
2019-04-22 21:56:30 +02:00
|
|
|
explicit TextSensor(const std::string &name);
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
void publish_state(std::string state);
|
|
|
|
|
|
|
|
void set_icon(const std::string &icon);
|
|
|
|
|
|
|
|
void add_on_state_callback(std::function<void(std::string)> callback);
|
|
|
|
|
|
|
|
std::string state;
|
|
|
|
|
|
|
|
// ========== INTERNAL METHODS ==========
|
|
|
|
// (In most use cases you won't need these)
|
|
|
|
std::string get_icon();
|
|
|
|
|
|
|
|
virtual std::string icon();
|
|
|
|
|
|
|
|
virtual std::string unique_id();
|
|
|
|
|
|
|
|
bool has_state();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
uint32_t hash_base() override;
|
|
|
|
|
|
|
|
CallbackManager<void(std::string)> callback_;
|
|
|
|
optional<std::string> icon_;
|
|
|
|
bool has_state_{false};
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace text_sensor
|
|
|
|
} // namespace esphome
|