mirror of
https://github.com/esphome/esphome.git
synced 2026-09-11 23:37:34 +00:00
convert a few more
This commit is contained in:
@@ -19,8 +19,8 @@ class AnalogThresholdBinarySensor : public Component, public binary_sensor::Bina
|
||||
|
||||
protected:
|
||||
sensor::Sensor *sensor_{nullptr};
|
||||
TemplatableValue<float> upper_threshold_{};
|
||||
TemplatableValue<float> lower_threshold_{};
|
||||
TemplatableFn<float> upper_threshold_{};
|
||||
TemplatableFn<float> lower_threshold_{};
|
||||
bool raw_state_{false}; // Pre-filter state for hysteresis logic
|
||||
};
|
||||
|
||||
|
||||
@@ -275,7 +275,7 @@ template<typename... Ts> class APIRespondAction : public Action<Ts...> {
|
||||
|
||||
protected:
|
||||
APIServer *parent_;
|
||||
TemplatableValue<bool, Ts...> success_{true};
|
||||
TemplatableFn<bool, Ts...> success_{[](Ts...) -> bool { return true; }};
|
||||
TemplatableValue<std::string, Ts...> error_message_{""};
|
||||
#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES_JSON
|
||||
std::function<void(Ts..., JsonObject)> json_builder_;
|
||||
|
||||
@@ -36,7 +36,7 @@ class TimeoutFilter : public Filter, public Component {
|
||||
template<typename T> void set_timeout_value(T timeout) { this->timeout_delay_ = timeout; }
|
||||
|
||||
protected:
|
||||
TemplatableValue<uint32_t> timeout_delay_{};
|
||||
TemplatableFn<uint32_t> timeout_delay_{};
|
||||
};
|
||||
|
||||
class DelayedOnOffFilter final : public Filter, public Component {
|
||||
@@ -49,8 +49,8 @@ class DelayedOnOffFilter final : public Filter, public Component {
|
||||
template<typename T> void set_off_delay(T delay) { this->off_delay_ = delay; }
|
||||
|
||||
protected:
|
||||
TemplatableValue<uint32_t> on_delay_{};
|
||||
TemplatableValue<uint32_t> off_delay_{};
|
||||
TemplatableFn<uint32_t> on_delay_{};
|
||||
TemplatableFn<uint32_t> off_delay_{};
|
||||
};
|
||||
|
||||
class DelayedOnFilter : public Filter, public Component {
|
||||
@@ -62,7 +62,7 @@ class DelayedOnFilter : public Filter, public Component {
|
||||
template<typename T> void set_delay(T delay) { this->delay_ = delay; }
|
||||
|
||||
protected:
|
||||
TemplatableValue<uint32_t> delay_{};
|
||||
TemplatableFn<uint32_t> delay_{};
|
||||
};
|
||||
|
||||
class DelayedOffFilter : public Filter, public Component {
|
||||
@@ -74,7 +74,7 @@ class DelayedOffFilter : public Filter, public Component {
|
||||
template<typename T> void set_delay(T delay) { this->delay_ = delay; }
|
||||
|
||||
protected:
|
||||
TemplatableValue<uint32_t> delay_{};
|
||||
TemplatableFn<uint32_t> delay_{};
|
||||
};
|
||||
|
||||
class InvertFilter : public Filter {
|
||||
@@ -155,7 +155,7 @@ class SettleFilter : public Filter, public Component {
|
||||
template<typename T> void set_delay(T delay) { this->delay_ = delay; }
|
||||
|
||||
protected:
|
||||
TemplatableValue<uint32_t> delay_{};
|
||||
TemplatableFn<uint32_t> delay_{};
|
||||
bool steady_{true};
|
||||
};
|
||||
|
||||
|
||||
@@ -199,7 +199,7 @@ void MDNSComponent::dump_config() {
|
||||
ESP_LOGV(TAG, " Services:");
|
||||
for (const auto &service : this->services_) {
|
||||
ESP_LOGV(TAG, " - %s, %s, %d", MDNS_STR_ARG(service.service_type), MDNS_STR_ARG(service.proto),
|
||||
const_cast<TemplatableValue<uint16_t> &>(service.port).value());
|
||||
service.port.value());
|
||||
for (const auto &record : service.txt_records) {
|
||||
ESP_LOGV(TAG, " TXT: %s = %s", MDNS_STR_ARG(record.key), MDNS_STR_ARG(record.value));
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ static void register_esp32(MDNSComponent *comp, StaticVector<MDNSService, MDNS_S
|
||||
txt_records.get()[i].key = MDNS_STR_ARG(record.key);
|
||||
txt_records.get()[i].value = MDNS_STR_ARG(record.value);
|
||||
}
|
||||
uint16_t port = const_cast<TemplatableValue<uint16_t> &>(service.port).value();
|
||||
uint16_t port = service.port.value();
|
||||
err = mdns_service_add(nullptr, MDNS_STR_ARG(service.service_type), MDNS_STR_ARG(service.proto), port,
|
||||
txt_records.get(), service.txt_records.size());
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ static void register_esp8266(MDNSComponent *, StaticVector<MDNSService, MDNS_SER
|
||||
while (progmem_read_byte((const uint8_t *) service_type) == '_') {
|
||||
service_type++;
|
||||
}
|
||||
uint16_t port = const_cast<TemplatableValue<uint16_t> &>(service.port).value();
|
||||
uint16_t port = service.port.value();
|
||||
MDNS.addService(FPSTR(service_type), FPSTR(proto), port);
|
||||
for (const auto &record : service.txt_records) {
|
||||
MDNS.addServiceTxt(FPSTR(service_type), FPSTR(proto), FPSTR(MDNS_STR_ARG(record.key)),
|
||||
|
||||
@@ -27,7 +27,7 @@ static void register_libretiny(MDNSComponent *, StaticVector<MDNSService, MDNS_S
|
||||
while (*service_type == '_') {
|
||||
service_type++;
|
||||
}
|
||||
uint16_t port_ = const_cast<TemplatableValue<uint16_t> &>(service.port).value();
|
||||
uint16_t port_ = service.port.value();
|
||||
MDNS.addService(service_type, proto, port_);
|
||||
for (const auto &record : service.txt_records) {
|
||||
MDNS.addServiceTxt(service_type, proto, MDNS_STR_ARG(record.key), MDNS_STR_ARG(record.value));
|
||||
|
||||
@@ -32,7 +32,7 @@ static void register_rp2040(MDNSComponent *, StaticVector<MDNSService, MDNS_SERV
|
||||
while (*service_type == '_') {
|
||||
service_type++;
|
||||
}
|
||||
uint16_t port = const_cast<TemplatableValue<uint16_t> &>(service.port).value();
|
||||
uint16_t port = service.port.value();
|
||||
MDNS.addService(service_type, proto, port);
|
||||
for (const auto &record : service.txt_records) {
|
||||
MDNS.addServiceTxt(service_type, proto, MDNS_STR_ARG(record.key), MDNS_STR_ARG(record.value));
|
||||
|
||||
@@ -213,17 +213,17 @@ optional<float> LambdaFilter::new_value(float value) {
|
||||
}
|
||||
|
||||
// OffsetFilter
|
||||
OffsetFilter::OffsetFilter(TemplatableValue<float> offset) : offset_(std::move(offset)) {}
|
||||
OffsetFilter::OffsetFilter(TemplatableFn<float> offset) : offset_(std::move(offset)) {}
|
||||
|
||||
optional<float> OffsetFilter::new_value(float value) { return value + this->offset_.value(); }
|
||||
|
||||
// MultiplyFilter
|
||||
MultiplyFilter::MultiplyFilter(TemplatableValue<float> multiplier) : multiplier_(std::move(multiplier)) {}
|
||||
MultiplyFilter::MultiplyFilter(TemplatableFn<float> multiplier) : multiplier_(std::move(multiplier)) {}
|
||||
|
||||
optional<float> MultiplyFilter::new_value(float value) { return value * this->multiplier_.value(); }
|
||||
|
||||
// ValueListFilter helper (non-template, shared by all ValueListFilter<N> instantiations)
|
||||
bool value_list_matches_any(Sensor *parent, float sensor_value, const TemplatableValue<float> *values, size_t count) {
|
||||
bool value_list_matches_any(Sensor *parent, float sensor_value, const TemplatableFn<float> *values, size_t count) {
|
||||
int8_t accuracy = parent->get_accuracy_decimals();
|
||||
float accuracy_mult = pow10_int(accuracy);
|
||||
float rounded_sensor = roundf(accuracy_mult * sensor_value);
|
||||
@@ -258,7 +258,7 @@ optional<float> ThrottleFilter::new_value(float value) {
|
||||
}
|
||||
|
||||
// ThrottleWithPriorityFilter helper (non-template, keeps App access in .cpp)
|
||||
optional<float> throttle_with_priority_new_value(Sensor *parent, float value, const TemplatableValue<float> *values,
|
||||
optional<float> throttle_with_priority_new_value(Sensor *parent, float value, const TemplatableFn<float> *values,
|
||||
size_t count, uint32_t &last_input, uint32_t min_time_between_inputs) {
|
||||
const uint32_t now = App.get_loop_component_start_time();
|
||||
if (last_input == 0 || now - last_input >= min_time_between_inputs ||
|
||||
|
||||
@@ -311,26 +311,26 @@ class StatelessLambdaFilter : public Filter {
|
||||
/// A simple filter that adds `offset` to each value it receives.
|
||||
class OffsetFilter : public Filter {
|
||||
public:
|
||||
explicit OffsetFilter(TemplatableValue<float> offset);
|
||||
explicit OffsetFilter(TemplatableFn<float> offset);
|
||||
|
||||
optional<float> new_value(float value) override;
|
||||
|
||||
protected:
|
||||
TemplatableValue<float> offset_;
|
||||
TemplatableFn<float> offset_;
|
||||
};
|
||||
|
||||
/// A simple filter that multiplies to each value it receives by `multiplier`.
|
||||
class MultiplyFilter : public Filter {
|
||||
public:
|
||||
explicit MultiplyFilter(TemplatableValue<float> multiplier);
|
||||
explicit MultiplyFilter(TemplatableFn<float> multiplier);
|
||||
optional<float> new_value(float value) override;
|
||||
|
||||
protected:
|
||||
TemplatableValue<float> multiplier_;
|
||||
TemplatableFn<float> multiplier_;
|
||||
};
|
||||
|
||||
/// Non-template helper for value matching (implementation in filter.cpp)
|
||||
bool value_list_matches_any(Sensor *parent, float sensor_value, const TemplatableValue<float> *values, size_t count);
|
||||
bool value_list_matches_any(Sensor *parent, float sensor_value, const TemplatableFn<float> *values, size_t count);
|
||||
|
||||
/** Base class for filters that compare sensor values against a fixed list of configured values.
|
||||
*
|
||||
@@ -342,7 +342,7 @@ bool value_list_matches_any(Sensor *parent, float sensor_value, const Templatabl
|
||||
*/
|
||||
template<size_t N> class ValueListFilter : public Filter {
|
||||
protected:
|
||||
explicit ValueListFilter(std::initializer_list<TemplatableValue<float>> values) {
|
||||
explicit ValueListFilter(std::initializer_list<TemplatableFn<float>> values) {
|
||||
init_array_from(this->values_, values);
|
||||
}
|
||||
|
||||
@@ -351,13 +351,13 @@ template<size_t N> class ValueListFilter : public Filter {
|
||||
return value_list_matches_any(this->parent_, sensor_value, this->values_.data(), N);
|
||||
}
|
||||
|
||||
std::array<TemplatableValue<float>, N> values_{};
|
||||
std::array<TemplatableFn<float>, N> values_{};
|
||||
};
|
||||
|
||||
/// A simple filter that only forwards the filter chain if it doesn't receive `value_to_filter_out`.
|
||||
template<size_t N> class FilterOutValueFilter : public ValueListFilter<N> {
|
||||
public:
|
||||
explicit FilterOutValueFilter(std::initializer_list<TemplatableValue<float>> values_to_filter_out)
|
||||
explicit FilterOutValueFilter(std::initializer_list<TemplatableFn<float>> values_to_filter_out)
|
||||
: ValueListFilter<N>(values_to_filter_out) {}
|
||||
|
||||
optional<float> new_value(float value) override {
|
||||
@@ -379,14 +379,14 @@ class ThrottleFilter : public Filter {
|
||||
};
|
||||
|
||||
/// Non-template helper for ThrottleWithPriorityFilter (implementation in filter.cpp)
|
||||
optional<float> throttle_with_priority_new_value(Sensor *parent, float value, const TemplatableValue<float> *values,
|
||||
optional<float> throttle_with_priority_new_value(Sensor *parent, float value, const TemplatableFn<float> *values,
|
||||
size_t count, uint32_t &last_input, uint32_t min_time_between_inputs);
|
||||
|
||||
/// Same as 'throttle' but will immediately publish values contained in `value_to_prioritize`.
|
||||
template<size_t N> class ThrottleWithPriorityFilter : public ValueListFilter<N> {
|
||||
public:
|
||||
explicit ThrottleWithPriorityFilter(uint32_t min_time_between_inputs,
|
||||
std::initializer_list<TemplatableValue<float>> prioritized_values)
|
||||
std::initializer_list<TemplatableFn<float>> prioritized_values)
|
||||
: ValueListFilter<N>(prioritized_values), min_time_between_inputs_(min_time_between_inputs) {}
|
||||
|
||||
optional<float> new_value(float value) override {
|
||||
@@ -430,15 +430,15 @@ class TimeoutFilterLast : public TimeoutFilterBase {
|
||||
// Timeout filter with configured value - evaluates TemplatableValue after timeout
|
||||
class TimeoutFilterConfigured : public TimeoutFilterBase {
|
||||
public:
|
||||
explicit TimeoutFilterConfigured(uint32_t time_period, const TemplatableValue<float> &new_value)
|
||||
explicit TimeoutFilterConfigured(uint32_t time_period, const TemplatableFn<float> &new_value)
|
||||
: TimeoutFilterBase(time_period), value_(new_value) {}
|
||||
|
||||
optional<float> new_value(float value) override;
|
||||
|
||||
protected:
|
||||
float get_output_value() override { return this->value_.value(); }
|
||||
TemplatableValue<float> value_; // 16 bytes (configured output value, can be lambda)
|
||||
// Total: 8 (base) + 16 = 24 bytes + vtable ptr + Component overhead
|
||||
TemplatableFn<float> value_; // 4 bytes (configured output value, can be lambda)
|
||||
// Total: 8 (base) + 4 = 12 bytes + vtable ptr + Component overhead
|
||||
};
|
||||
|
||||
class DebounceFilter : public Filter, public Component {
|
||||
|
||||
Reference in New Issue
Block a user