Files
esphome-dev/esphome/components/template/sensor/template_sensor.cpp
T
2021-09-08 15:36:49 +12:00

28 lines
750 B
C++

#include "template_sensor.h"
#include "esphome/core/log.h"
namespace esphome {
namespace template_ {
static const char *const TAG = "template.sensor";
void TemplateSensor::update() {
if (this->f_.has_value()) {
auto val = (*this->f_)();
if (val.has_value()) {
this->publish_state(*val);
}
} else if (!isnan(this->get_raw_state())) {
this->publish_state(this->get_raw_state());
}
}
float TemplateSensor::get_setup_priority() const { return setup_priority::HARDWARE; }
void TemplateSensor::set_template(std::function<optional<float>()> &&f) { this->f_ = f; }
void TemplateSensor::dump_config() {
LOG_SENSOR("", "Template Sensor", this);
LOG_UPDATE_INTERVAL(this);
}
} // namespace template_
} // namespace esphome