1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-06 13:22:19 +01:00

String manipulation filters for text sensors (#2393)

* initial text sensor filter POC

* fixed verbose logging

* add append, prepend, substitute filters

* add to lower, get to upper working without dummy

* clang lint

* more linting...

* std::move append and prepend filters

* fix verbose filter::input logging

* value.c_str() in input print

* lambda filter verbose log fix

* correct log tag, neaten to upper and to lower

* add on_raw_value automation/trigger
This commit is contained in:
WeekendWarrior1
2021-09-30 07:25:06 +10:00
committed by GitHub
parent 4f5e4f3b86
commit 3dfc8d4291
6 changed files with 373 additions and 5 deletions

View File

@@ -2,6 +2,7 @@
#include "esphome/core/component.h"
#include "esphome/core/helpers.h"
#include "esphome/components/text_sensor/filter.h"
namespace esphome {
namespace text_sensor {
@@ -22,13 +23,33 @@ class TextSensor : public Nameable {
explicit TextSensor();
explicit TextSensor(const std::string &name);
/// Getter-syntax for .state.
std::string get_state() const;
/// Getter-syntax for .raw_state
std::string get_raw_state() const;
void publish_state(const std::string &state);
void set_icon(const std::string &icon);
/// Add a filter to the filter chain. Will be appended to the back.
void add_filter(Filter *filter);
/// Add a list of vectors to the back of the filter chain.
void add_filters(const std::vector<Filter *> &filters);
/// Clear the filters and replace them by filters.
void set_filters(const std::vector<Filter *> &filters);
/// Clear the entire filter chain.
void clear_filters();
void add_on_state_callback(std::function<void(std::string)> callback);
/// Add a callback that will be called every time the sensor sends a raw value.
void add_on_raw_state_callback(std::function<void(std::string)> callback);
std::string state;
std::string raw_state;
// ========== INTERNAL METHODS ==========
// (In most use cases you won't need these)
@@ -40,10 +61,16 @@ class TextSensor : public Nameable {
bool has_state();
void internal_send_state_to_frontend(const std::string &state);
protected:
uint32_t hash_base() override;
CallbackManager<void(std::string)> callback_;
CallbackManager<void(std::string)> raw_callback_; ///< Storage for raw state callbacks.
CallbackManager<void(std::string)> callback_; ///< Storage for filtered state callbacks.
Filter *filter_list_{nullptr}; ///< Store all active filters.
optional<std::string> icon_;
bool has_state_{false};
};