Files
esphome-dev/esphome/components/text_sensor/automation.h
T
WeekendWarrior1 3dfc8d4291 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
2021-09-29 23:25:06 +02:00

51 lines
1.4 KiB
C++

#pragma once
#include <utility>
#include "esphome/core/component.h"
#include "esphome/core/automation.h"
#include "esphome/components/text_sensor/text_sensor.h"
namespace esphome {
namespace text_sensor {
class TextSensorStateTrigger : public Trigger<std::string> {
public:
explicit TextSensorStateTrigger(TextSensor *parent) {
parent->add_on_state_callback([this](std::string value) { this->trigger(std::move(value)); });
}
};
class TextSensorStateRawTrigger : public Trigger<std::string> {
public:
explicit TextSensorStateRawTrigger(TextSensor *parent) {
parent->add_on_raw_state_callback([this](std::string value) { this->trigger(std::move(value)); });
}
};
template<typename... Ts> class TextSensorStateCondition : public Condition<Ts...> {
public:
explicit TextSensorStateCondition(TextSensor *parent) : parent_(parent) {}
TEMPLATABLE_VALUE(std::string, state)
bool check(Ts... x) override { return this->parent_->state == this->state_.value(x...); }
protected:
TextSensor *parent_;
};
template<typename... Ts> class TextSensorPublishAction : public Action<Ts...> {
public:
TextSensorPublishAction(TextSensor *sensor) : sensor_(sensor) {}
TEMPLATABLE_VALUE(std::string, state)
void play(Ts... x) override { this->sensor_->publish_state(this->state_.value(x...)); }
protected:
TextSensor *sensor_;
};
} // namespace text_sensor
} // namespace esphome