mirror of
https://github.com/esphome/esphome.git
synced 2025-10-29 21:48:41 +00:00
Compare commits
8 Commits
memory_api
...
memory_api
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c061c9dbfe | ||
|
|
61796c672b | ||
|
|
cf37ffada0 | ||
|
|
20ddc83b60 | ||
|
|
45185bcd37 | ||
|
|
9836a563b8 | ||
|
|
464d949181 | ||
|
|
93d5abb352 |
@@ -62,7 +62,6 @@ from esphome.cpp_types import ( # noqa: F401
|
||||
EntityBase,
|
||||
EntityCategory,
|
||||
ESPTime,
|
||||
FixedVector,
|
||||
GPIOPin,
|
||||
InternalGPIOPin,
|
||||
JsonObject,
|
||||
|
||||
@@ -71,12 +71,10 @@ SERVICE_ARG_NATIVE_TYPES = {
|
||||
"int": cg.int32,
|
||||
"float": float,
|
||||
"string": cg.std_string,
|
||||
"bool[]": cg.FixedVector.template(bool).operator("const").operator("ref"),
|
||||
"int[]": cg.FixedVector.template(cg.int32).operator("const").operator("ref"),
|
||||
"float[]": cg.FixedVector.template(float).operator("const").operator("ref"),
|
||||
"string[]": cg.FixedVector.template(cg.std_string)
|
||||
.operator("const")
|
||||
.operator("ref"),
|
||||
"bool[]": cg.std_vector.template(bool),
|
||||
"int[]": cg.std_vector.template(cg.int32),
|
||||
"float[]": cg.std_vector.template(float),
|
||||
"string[]": cg.std_vector.template(cg.std_string),
|
||||
}
|
||||
CONF_ENCRYPTION = "encryption"
|
||||
CONF_BATCH_DELAY = "batch_delay"
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#ifdef ESPHOME_LOG_HAS_VERY_VERBOSE
|
||||
@@ -160,6 +159,22 @@ class ProtoVarInt {
|
||||
}
|
||||
}
|
||||
}
|
||||
void encode(std::vector<uint8_t> &out) {
|
||||
uint64_t val = this->value_;
|
||||
if (val <= 0x7F) {
|
||||
out.push_back(val);
|
||||
return;
|
||||
}
|
||||
while (val) {
|
||||
uint8_t temp = val & 0x7F;
|
||||
val >>= 7;
|
||||
if (val) {
|
||||
out.push_back(temp | 0x80);
|
||||
} else {
|
||||
out.push_back(temp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
uint64_t value_;
|
||||
@@ -218,86 +233,8 @@ class ProtoWriteBuffer {
|
||||
public:
|
||||
ProtoWriteBuffer(std::vector<uint8_t> *buffer) : buffer_(buffer) {}
|
||||
void write(uint8_t value) { this->buffer_->push_back(value); }
|
||||
|
||||
// Single implementation that all overloads delegate to
|
||||
void encode_varint(uint64_t value) {
|
||||
auto buffer = this->buffer_;
|
||||
size_t start = buffer->size();
|
||||
|
||||
// Fast paths for common cases (1-3 bytes)
|
||||
if (value < (1ULL << 7)) {
|
||||
// 1 byte - very common for field IDs and small lengths
|
||||
buffer->resize(start + 1);
|
||||
buffer->data()[start] = static_cast<uint8_t>(value);
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t *p;
|
||||
if (value < (1ULL << 14)) {
|
||||
// 2 bytes - common for medium field IDs and lengths
|
||||
buffer->resize(start + 2);
|
||||
p = buffer->data() + start;
|
||||
p[0] = (value & 0x7F) | 0x80;
|
||||
p[1] = (value >> 7) & 0x7F;
|
||||
return;
|
||||
}
|
||||
if (value < (1ULL << 21)) {
|
||||
// 3 bytes - rare
|
||||
buffer->resize(start + 3);
|
||||
p = buffer->data() + start;
|
||||
p[0] = (value & 0x7F) | 0x80;
|
||||
p[1] = ((value >> 7) & 0x7F) | 0x80;
|
||||
p[2] = (value >> 14) & 0x7F;
|
||||
return;
|
||||
}
|
||||
|
||||
// Rare case: 4-10 byte values - calculate size from bit position
|
||||
// Value is guaranteed >= (1ULL << 21), so CLZ is safe (non-zero)
|
||||
uint32_t size;
|
||||
#if defined(__GNUC__) || defined(__clang__)
|
||||
// Use compiler intrinsic for efficient bit position lookup
|
||||
size = (64 - __builtin_clzll(value) + 6) / 7;
|
||||
#else
|
||||
// Fallback for compilers without __builtin_clzll
|
||||
if (value < (1ULL << 28)) {
|
||||
size = 4;
|
||||
} else if (value < (1ULL << 35)) {
|
||||
size = 5;
|
||||
} else if (value < (1ULL << 42)) {
|
||||
size = 6;
|
||||
} else if (value < (1ULL << 49)) {
|
||||
size = 7;
|
||||
} else if (value < (1ULL << 56)) {
|
||||
size = 8;
|
||||
} else if (value < (1ULL << 63)) {
|
||||
size = 9;
|
||||
} else {
|
||||
size = 10;
|
||||
}
|
||||
#endif
|
||||
|
||||
buffer->resize(start + size);
|
||||
p = buffer->data() + start;
|
||||
size_t bytes = 0;
|
||||
while (value) {
|
||||
uint8_t temp = value & 0x7F;
|
||||
value >>= 7;
|
||||
p[bytes++] = value ? temp | 0x80 : temp;
|
||||
}
|
||||
}
|
||||
|
||||
// Common case: uint32_t values (field IDs, lengths, most integers)
|
||||
void encode_varint(uint32_t value) { this->encode_varint(static_cast<uint64_t>(value)); }
|
||||
|
||||
// size_t overload (only enabled if size_t is distinct from uint32_t and uint64_t)
|
||||
template<typename T>
|
||||
void encode_varint(T value) requires(std::is_same_v<T, size_t> && !std::is_same_v<size_t, uint32_t> &&
|
||||
!std::is_same_v<size_t, uint64_t>) {
|
||||
this->encode_varint(static_cast<uint64_t>(value));
|
||||
}
|
||||
|
||||
// Rare case: ProtoVarInt wrapper
|
||||
void encode_varint(ProtoVarInt value) { this->encode_varint(value.as_uint64()); }
|
||||
void encode_varint_raw(ProtoVarInt value) { value.encode(*this->buffer_); }
|
||||
void encode_varint_raw(uint32_t value) { this->encode_varint_raw(ProtoVarInt(value)); }
|
||||
/**
|
||||
* Encode a field key (tag/wire type combination).
|
||||
*
|
||||
@@ -312,14 +249,14 @@ class ProtoWriteBuffer {
|
||||
*/
|
||||
void encode_field_raw(uint32_t field_id, uint32_t type) {
|
||||
uint32_t val = (field_id << 3) | (type & WIRE_TYPE_MASK);
|
||||
this->encode_varint(val);
|
||||
this->encode_varint_raw(val);
|
||||
}
|
||||
void encode_string(uint32_t field_id, const char *string, size_t len, bool force = false) {
|
||||
if (len == 0 && !force)
|
||||
return;
|
||||
|
||||
this->encode_field_raw(field_id, 2); // type 2: Length-delimited string
|
||||
this->encode_varint(len);
|
||||
this->encode_varint_raw(len);
|
||||
|
||||
// Using resize + memcpy instead of insert provides significant performance improvement:
|
||||
// ~10-11x faster for 16-32 byte strings, ~3x faster for 64-byte strings
|
||||
@@ -341,13 +278,13 @@ class ProtoWriteBuffer {
|
||||
if (value == 0 && !force)
|
||||
return;
|
||||
this->encode_field_raw(field_id, 0); // type 0: Varint - uint32
|
||||
this->encode_varint(value);
|
||||
this->encode_varint_raw(value);
|
||||
}
|
||||
void encode_uint64(uint32_t field_id, uint64_t value, bool force = false) {
|
||||
if (value == 0 && !force)
|
||||
return;
|
||||
this->encode_field_raw(field_id, 0); // type 0: Varint - uint64
|
||||
this->encode_varint(value);
|
||||
this->encode_varint_raw(ProtoVarInt(value));
|
||||
}
|
||||
void encode_bool(uint32_t field_id, bool value, bool force = false) {
|
||||
if (!value && !force)
|
||||
|
||||
@@ -11,58 +11,23 @@ template<> int32_t get_execute_arg_value<int32_t>(const ExecuteServiceArgument &
|
||||
}
|
||||
template<> float get_execute_arg_value<float>(const ExecuteServiceArgument &arg) { return arg.float_; }
|
||||
template<> std::string get_execute_arg_value<std::string>(const ExecuteServiceArgument &arg) { return arg.string_; }
|
||||
|
||||
// Legacy std::vector versions for external components using custom_api_device.h - optimized with reserve
|
||||
template<> std::vector<bool> get_execute_arg_value<std::vector<bool>>(const ExecuteServiceArgument &arg) {
|
||||
std::vector<bool> result;
|
||||
result.reserve(arg.bool_array.size());
|
||||
result.insert(result.end(), arg.bool_array.begin(), arg.bool_array.end());
|
||||
return result;
|
||||
return std::vector<bool>(arg.bool_array.begin(), arg.bool_array.end());
|
||||
}
|
||||
template<> std::vector<int32_t> get_execute_arg_value<std::vector<int32_t>>(const ExecuteServiceArgument &arg) {
|
||||
std::vector<int32_t> result;
|
||||
result.reserve(arg.int_array.size());
|
||||
result.insert(result.end(), arg.int_array.begin(), arg.int_array.end());
|
||||
return result;
|
||||
return std::vector<int32_t>(arg.int_array.begin(), arg.int_array.end());
|
||||
}
|
||||
template<> std::vector<float> get_execute_arg_value<std::vector<float>>(const ExecuteServiceArgument &arg) {
|
||||
std::vector<float> result;
|
||||
result.reserve(arg.float_array.size());
|
||||
result.insert(result.end(), arg.float_array.begin(), arg.float_array.end());
|
||||
return result;
|
||||
return std::vector<float>(arg.float_array.begin(), arg.float_array.end());
|
||||
}
|
||||
template<> std::vector<std::string> get_execute_arg_value<std::vector<std::string>>(const ExecuteServiceArgument &arg) {
|
||||
std::vector<std::string> result;
|
||||
result.reserve(arg.string_array.size());
|
||||
result.insert(result.end(), arg.string_array.begin(), arg.string_array.end());
|
||||
return result;
|
||||
}
|
||||
|
||||
// New FixedVector const reference versions for YAML-generated services - zero-copy
|
||||
template<>
|
||||
const FixedVector<bool> &get_execute_arg_value<const FixedVector<bool> &>(const ExecuteServiceArgument &arg) {
|
||||
return arg.bool_array;
|
||||
}
|
||||
template<>
|
||||
const FixedVector<int32_t> &get_execute_arg_value<const FixedVector<int32_t> &>(const ExecuteServiceArgument &arg) {
|
||||
return arg.int_array;
|
||||
}
|
||||
template<>
|
||||
const FixedVector<float> &get_execute_arg_value<const FixedVector<float> &>(const ExecuteServiceArgument &arg) {
|
||||
return arg.float_array;
|
||||
}
|
||||
template<>
|
||||
const FixedVector<std::string> &get_execute_arg_value<const FixedVector<std::string> &>(
|
||||
const ExecuteServiceArgument &arg) {
|
||||
return arg.string_array;
|
||||
return std::vector<std::string>(arg.string_array.begin(), arg.string_array.end());
|
||||
}
|
||||
|
||||
template<> enums::ServiceArgType to_service_arg_type<bool>() { return enums::SERVICE_ARG_TYPE_BOOL; }
|
||||
template<> enums::ServiceArgType to_service_arg_type<int32_t>() { return enums::SERVICE_ARG_TYPE_INT; }
|
||||
template<> enums::ServiceArgType to_service_arg_type<float>() { return enums::SERVICE_ARG_TYPE_FLOAT; }
|
||||
template<> enums::ServiceArgType to_service_arg_type<std::string>() { return enums::SERVICE_ARG_TYPE_STRING; }
|
||||
|
||||
// Legacy std::vector versions for external components using custom_api_device.h
|
||||
template<> enums::ServiceArgType to_service_arg_type<std::vector<bool>>() { return enums::SERVICE_ARG_TYPE_BOOL_ARRAY; }
|
||||
template<> enums::ServiceArgType to_service_arg_type<std::vector<int32_t>>() {
|
||||
return enums::SERVICE_ARG_TYPE_INT_ARRAY;
|
||||
@@ -74,18 +39,4 @@ template<> enums::ServiceArgType to_service_arg_type<std::vector<std::string>>()
|
||||
return enums::SERVICE_ARG_TYPE_STRING_ARRAY;
|
||||
}
|
||||
|
||||
// New FixedVector const reference versions for YAML-generated services
|
||||
template<> enums::ServiceArgType to_service_arg_type<const FixedVector<bool> &>() {
|
||||
return enums::SERVICE_ARG_TYPE_BOOL_ARRAY;
|
||||
}
|
||||
template<> enums::ServiceArgType to_service_arg_type<const FixedVector<int32_t> &>() {
|
||||
return enums::SERVICE_ARG_TYPE_INT_ARRAY;
|
||||
}
|
||||
template<> enums::ServiceArgType to_service_arg_type<const FixedVector<float> &>() {
|
||||
return enums::SERVICE_ARG_TYPE_FLOAT_ARRAY;
|
||||
}
|
||||
template<> enums::ServiceArgType to_service_arg_type<const FixedVector<std::string> &>() {
|
||||
return enums::SERVICE_ARG_TYPE_STRING_ARRAY;
|
||||
}
|
||||
|
||||
} // namespace esphome::api
|
||||
|
||||
@@ -96,11 +96,8 @@ template<typename... Ts> class BLEClientWriteAction : public Action<Ts...>, publ
|
||||
BLEClientWriteAction(BLEClient *ble_client) {
|
||||
ble_client->register_ble_node(this);
|
||||
ble_client_ = ble_client;
|
||||
this->construct_simple_value_();
|
||||
}
|
||||
|
||||
~BLEClientWriteAction() { this->destroy_simple_value_(); }
|
||||
|
||||
void set_service_uuid16(uint16_t uuid) { this->service_uuid_ = espbt::ESPBTUUID::from_uint16(uuid); }
|
||||
void set_service_uuid32(uint32_t uuid) { this->service_uuid_ = espbt::ESPBTUUID::from_uint32(uuid); }
|
||||
void set_service_uuid128(uint8_t *uuid) { this->service_uuid_ = espbt::ESPBTUUID::from_raw(uuid); }
|
||||
@@ -109,18 +106,14 @@ template<typename... Ts> class BLEClientWriteAction : public Action<Ts...>, publ
|
||||
void set_char_uuid32(uint32_t uuid) { this->char_uuid_ = espbt::ESPBTUUID::from_uint32(uuid); }
|
||||
void set_char_uuid128(uint8_t *uuid) { this->char_uuid_ = espbt::ESPBTUUID::from_raw(uuid); }
|
||||
|
||||
void set_value_template(std::vector<uint8_t> (*func)(Ts...)) {
|
||||
this->destroy_simple_value_();
|
||||
this->value_.template_func = func;
|
||||
this->has_simple_value_ = false;
|
||||
void set_value_template(std::function<std::vector<uint8_t>(Ts...)> func) {
|
||||
this->value_template_ = std::move(func);
|
||||
has_simple_value_ = false;
|
||||
}
|
||||
|
||||
void set_value_simple(const std::vector<uint8_t> &value) {
|
||||
if (!this->has_simple_value_) {
|
||||
this->construct_simple_value_();
|
||||
}
|
||||
this->value_.simple = value;
|
||||
this->has_simple_value_ = true;
|
||||
this->value_simple_ = value;
|
||||
has_simple_value_ = true;
|
||||
}
|
||||
|
||||
void play(Ts... x) override {}
|
||||
@@ -128,7 +121,7 @@ template<typename... Ts> class BLEClientWriteAction : public Action<Ts...>, publ
|
||||
void play_complex(Ts... x) override {
|
||||
this->num_running_++;
|
||||
this->var_ = std::make_tuple(x...);
|
||||
auto value = this->has_simple_value_ ? this->value_.simple : this->value_.template_func(x...);
|
||||
auto value = this->has_simple_value_ ? this->value_simple_ : this->value_template_(x...);
|
||||
// on write failure, continue the automation chain rather than stopping so that e.g. disconnect can work.
|
||||
if (!write(value))
|
||||
this->play_next_(x...);
|
||||
@@ -201,22 +194,10 @@ template<typename... Ts> class BLEClientWriteAction : public Action<Ts...>, publ
|
||||
}
|
||||
|
||||
private:
|
||||
void construct_simple_value_() { new (&this->value_.simple) std::vector<uint8_t>(); }
|
||||
|
||||
void destroy_simple_value_() {
|
||||
if (this->has_simple_value_) {
|
||||
this->value_.simple.~vector();
|
||||
}
|
||||
}
|
||||
|
||||
BLEClient *ble_client_;
|
||||
bool has_simple_value_ = true;
|
||||
union Value {
|
||||
std::vector<uint8_t> simple;
|
||||
std::vector<uint8_t> (*template_func)(Ts...);
|
||||
Value() {} // trivial constructor
|
||||
~Value() {} // trivial destructor - we manage lifetime via discriminator
|
||||
} value_;
|
||||
std::vector<uint8_t> value_simple_;
|
||||
std::function<std::vector<uint8_t>(Ts...)> value_template_{};
|
||||
espbt::ESPBTUUID service_uuid_;
|
||||
espbt::ESPBTUUID char_uuid_;
|
||||
std::tuple<Ts...> var_{};
|
||||
@@ -232,9 +213,9 @@ template<typename... Ts> class BLEClientPasskeyReplyAction : public Action<Ts...
|
||||
void play(Ts... x) override {
|
||||
uint32_t passkey;
|
||||
if (has_simple_value_) {
|
||||
passkey = this->value_.simple;
|
||||
passkey = this->value_simple_;
|
||||
} else {
|
||||
passkey = this->value_.template_func(x...);
|
||||
passkey = this->value_template_(x...);
|
||||
}
|
||||
if (passkey > 999999)
|
||||
return;
|
||||
@@ -243,23 +224,21 @@ template<typename... Ts> class BLEClientPasskeyReplyAction : public Action<Ts...
|
||||
esp_ble_passkey_reply(remote_bda, true, passkey);
|
||||
}
|
||||
|
||||
void set_value_template(uint32_t (*func)(Ts...)) {
|
||||
this->value_.template_func = func;
|
||||
this->has_simple_value_ = false;
|
||||
void set_value_template(std::function<uint32_t(Ts...)> func) {
|
||||
this->value_template_ = std::move(func);
|
||||
has_simple_value_ = false;
|
||||
}
|
||||
|
||||
void set_value_simple(const uint32_t &value) {
|
||||
this->value_.simple = value;
|
||||
this->has_simple_value_ = true;
|
||||
this->value_simple_ = value;
|
||||
has_simple_value_ = true;
|
||||
}
|
||||
|
||||
private:
|
||||
BLEClient *parent_{nullptr};
|
||||
bool has_simple_value_ = true;
|
||||
union {
|
||||
uint32_t simple;
|
||||
uint32_t (*template_func)(Ts...);
|
||||
} value_{.simple = 0};
|
||||
uint32_t value_simple_{0};
|
||||
std::function<uint32_t(Ts...)> value_template_{};
|
||||
};
|
||||
|
||||
template<typename... Ts> class BLEClientNumericComparisonReplyAction : public Action<Ts...> {
|
||||
@@ -270,29 +249,27 @@ template<typename... Ts> class BLEClientNumericComparisonReplyAction : public Ac
|
||||
esp_bd_addr_t remote_bda;
|
||||
memcpy(remote_bda, parent_->get_remote_bda(), sizeof(esp_bd_addr_t));
|
||||
if (has_simple_value_) {
|
||||
esp_ble_confirm_reply(remote_bda, this->value_.simple);
|
||||
esp_ble_confirm_reply(remote_bda, this->value_simple_);
|
||||
} else {
|
||||
esp_ble_confirm_reply(remote_bda, this->value_.template_func(x...));
|
||||
esp_ble_confirm_reply(remote_bda, this->value_template_(x...));
|
||||
}
|
||||
}
|
||||
|
||||
void set_value_template(bool (*func)(Ts...)) {
|
||||
this->value_.template_func = func;
|
||||
this->has_simple_value_ = false;
|
||||
void set_value_template(std::function<bool(Ts...)> func) {
|
||||
this->value_template_ = std::move(func);
|
||||
has_simple_value_ = false;
|
||||
}
|
||||
|
||||
void set_value_simple(const bool &value) {
|
||||
this->value_.simple = value;
|
||||
this->has_simple_value_ = true;
|
||||
this->value_simple_ = value;
|
||||
has_simple_value_ = true;
|
||||
}
|
||||
|
||||
private:
|
||||
BLEClient *parent_{nullptr};
|
||||
bool has_simple_value_ = true;
|
||||
union {
|
||||
bool simple;
|
||||
bool (*template_func)(Ts...);
|
||||
} value_{.simple = false};
|
||||
bool value_simple_{false};
|
||||
std::function<bool(Ts...)> value_template_{};
|
||||
};
|
||||
|
||||
template<typename... Ts> class BLEClientRemoveBondAction : public Action<Ts...> {
|
||||
|
||||
@@ -117,9 +117,9 @@ void BLESensor::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t ga
|
||||
}
|
||||
|
||||
float BLESensor::parse_data_(uint8_t *value, uint16_t value_len) {
|
||||
if (this->has_data_to_value_) {
|
||||
if (this->data_to_value_func_.has_value()) {
|
||||
std::vector<uint8_t> data(value, value + value_len);
|
||||
return this->data_to_value_func_(data);
|
||||
return (*this->data_to_value_func_)(data);
|
||||
} else {
|
||||
return value[0];
|
||||
}
|
||||
|
||||
@@ -15,6 +15,8 @@ namespace ble_client {
|
||||
|
||||
namespace espbt = esphome::esp32_ble_tracker;
|
||||
|
||||
using data_to_value_t = std::function<float(std::vector<uint8_t>)>;
|
||||
|
||||
class BLESensor : public sensor::Sensor, public PollingComponent, public BLEClientNode {
|
||||
public:
|
||||
void loop() override;
|
||||
@@ -31,17 +33,13 @@ class BLESensor : public sensor::Sensor, public PollingComponent, public BLEClie
|
||||
void set_descr_uuid16(uint16_t uuid) { this->descr_uuid_ = espbt::ESPBTUUID::from_uint16(uuid); }
|
||||
void set_descr_uuid32(uint32_t uuid) { this->descr_uuid_ = espbt::ESPBTUUID::from_uint32(uuid); }
|
||||
void set_descr_uuid128(uint8_t *uuid) { this->descr_uuid_ = espbt::ESPBTUUID::from_raw(uuid); }
|
||||
void set_data_to_value(float (*lambda)(const std::vector<uint8_t> &)) {
|
||||
this->data_to_value_func_ = lambda;
|
||||
this->has_data_to_value_ = true;
|
||||
}
|
||||
void set_data_to_value(data_to_value_t &&lambda) { this->data_to_value_func_ = lambda; }
|
||||
void set_enable_notify(bool notify) { this->notify_ = notify; }
|
||||
uint16_t handle;
|
||||
|
||||
protected:
|
||||
float parse_data_(uint8_t *value, uint16_t value_len);
|
||||
bool has_data_to_value_{false};
|
||||
float (*data_to_value_func_)(const std::vector<uint8_t> &){};
|
||||
optional<data_to_value_t> data_to_value_func_{};
|
||||
bool notify_;
|
||||
espbt::ESPBTUUID service_uuid_;
|
||||
espbt::ESPBTUUID char_uuid_;
|
||||
|
||||
@@ -461,9 +461,7 @@ async def parse_value(value_config, args):
|
||||
if isinstance(value, str):
|
||||
value = list(value.encode(value_config[CONF_STRING_ENCODING]))
|
||||
if isinstance(value, list):
|
||||
# Generate initializer list {1, 2, 3} instead of std::vector<uint8_t>({1, 2, 3})
|
||||
# This calls the set_value(std::initializer_list<uint8_t>) overload
|
||||
return cg.ArrayInitializer(*value)
|
||||
return cg.std_vector.template(cg.uint8)(value)
|
||||
val = cg.RawExpression(f"{value_config[CONF_TYPE]}({cg.safe_exp(value)})")
|
||||
return ByteBuffer_ns.wrap(val, value_config[CONF_ENDIANNESS])
|
||||
|
||||
|
||||
@@ -35,18 +35,13 @@ BLECharacteristic::BLECharacteristic(const ESPBTUUID uuid, uint32_t properties)
|
||||
|
||||
void BLECharacteristic::set_value(ByteBuffer buffer) { this->set_value(buffer.get_data()); }
|
||||
|
||||
void BLECharacteristic::set_value(std::vector<uint8_t> &&buffer) {
|
||||
void BLECharacteristic::set_value(const std::vector<uint8_t> &buffer) {
|
||||
xSemaphoreTake(this->set_value_lock_, 0L);
|
||||
this->value_ = std::move(buffer);
|
||||
this->value_ = buffer;
|
||||
xSemaphoreGive(this->set_value_lock_);
|
||||
}
|
||||
|
||||
void BLECharacteristic::set_value(std::initializer_list<uint8_t> data) {
|
||||
this->set_value(std::vector<uint8_t>(data)); // Delegate to move overload
|
||||
}
|
||||
|
||||
void BLECharacteristic::set_value(const std::string &buffer) {
|
||||
this->set_value(std::vector<uint8_t>(buffer.begin(), buffer.end())); // Delegate to move overload
|
||||
this->set_value(std::vector<uint8_t>(buffer.begin(), buffer.end()));
|
||||
}
|
||||
|
||||
void BLECharacteristic::notify() {
|
||||
|
||||
@@ -33,8 +33,7 @@ class BLECharacteristic {
|
||||
~BLECharacteristic();
|
||||
|
||||
void set_value(ByteBuffer buffer);
|
||||
void set_value(std::vector<uint8_t> &&buffer);
|
||||
void set_value(std::initializer_list<uint8_t> data);
|
||||
void set_value(const std::vector<uint8_t> &buffer);
|
||||
void set_value(const std::string &buffer);
|
||||
|
||||
void set_broadcast_property(bool value);
|
||||
|
||||
@@ -46,17 +46,15 @@ void BLEDescriptor::do_create(BLECharacteristic *characteristic) {
|
||||
this->state_ = CREATING;
|
||||
}
|
||||
|
||||
void BLEDescriptor::set_value(std::vector<uint8_t> &&buffer) { this->set_value_impl_(buffer.data(), buffer.size()); }
|
||||
void BLEDescriptor::set_value(std::vector<uint8_t> buffer) {
|
||||
size_t length = buffer.size();
|
||||
|
||||
void BLEDescriptor::set_value(std::initializer_list<uint8_t> data) { this->set_value_impl_(data.begin(), data.size()); }
|
||||
|
||||
void BLEDescriptor::set_value_impl_(const uint8_t *data, size_t length) {
|
||||
if (length > this->value_.attr_max_len) {
|
||||
ESP_LOGE(TAG, "Size %d too large, must be no bigger than %d", length, this->value_.attr_max_len);
|
||||
return;
|
||||
}
|
||||
this->value_.attr_len = length;
|
||||
memcpy(this->value_.attr_value, data, length);
|
||||
memcpy(this->value_.attr_value, buffer.data(), length);
|
||||
}
|
||||
|
||||
void BLEDescriptor::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if,
|
||||
|
||||
@@ -27,8 +27,7 @@ class BLEDescriptor {
|
||||
void do_create(BLECharacteristic *characteristic);
|
||||
ESPBTUUID get_uuid() const { return this->uuid_; }
|
||||
|
||||
void set_value(std::vector<uint8_t> &&buffer);
|
||||
void set_value(std::initializer_list<uint8_t> data);
|
||||
void set_value(std::vector<uint8_t> buffer);
|
||||
void set_value(ByteBuffer buffer) { this->set_value(buffer.get_data()); }
|
||||
|
||||
void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
|
||||
@@ -43,8 +42,6 @@ class BLEDescriptor {
|
||||
}
|
||||
|
||||
protected:
|
||||
void set_value_impl_(const uint8_t *data, size_t length);
|
||||
|
||||
BLECharacteristic *characteristic_{nullptr};
|
||||
ESPBTUUID uuid_;
|
||||
uint16_t handle_{0xFFFF};
|
||||
|
||||
@@ -270,8 +270,8 @@ void ESP32ImprovComponent::set_error_(improv::Error error) {
|
||||
}
|
||||
}
|
||||
|
||||
void ESP32ImprovComponent::send_response_(std::vector<uint8_t> &&response) {
|
||||
this->rpc_response_->set_value(std::move(response));
|
||||
void ESP32ImprovComponent::send_response_(std::vector<uint8_t> &response) {
|
||||
this->rpc_response_->set_value(ByteBuffer::wrap(response));
|
||||
if (this->state_ != improv::STATE_STOPPED)
|
||||
this->rpc_response_->notify();
|
||||
}
|
||||
@@ -409,8 +409,10 @@ void ESP32ImprovComponent::check_wifi_connection_() {
|
||||
}
|
||||
}
|
||||
#endif
|
||||
this->send_response_(improv::build_rpc_response(improv::WIFI_SETTINGS,
|
||||
std::vector<std::string>(url_strings, url_strings + url_count)));
|
||||
// Pass to build_rpc_response using vector constructor from iterators to avoid extra copies
|
||||
std::vector<uint8_t> data = improv::build_rpc_response(
|
||||
improv::WIFI_SETTINGS, std::vector<std::string>(url_strings, url_strings + url_count));
|
||||
this->send_response_(data);
|
||||
} else if (this->is_active() && this->state_ != improv::STATE_PROVISIONED) {
|
||||
ESP_LOGD(TAG, "WiFi provisioned externally");
|
||||
}
|
||||
|
||||
@@ -109,7 +109,7 @@ class ESP32ImprovComponent : public Component, public improv_base::ImprovBase {
|
||||
void set_state_(improv::State state, bool update_advertising = true);
|
||||
void set_error_(improv::Error error);
|
||||
improv::State get_initial_state_() const;
|
||||
void send_response_(std::vector<uint8_t> &&response);
|
||||
void send_response_(std::vector<uint8_t> &response);
|
||||
void process_incoming_data_();
|
||||
void on_wifi_connect_timeout_();
|
||||
void check_wifi_connection_();
|
||||
|
||||
@@ -14,7 +14,7 @@ from esphome.components.esp32.const import (
|
||||
VARIANT_ESP32S2,
|
||||
VARIANT_ESP32S3,
|
||||
)
|
||||
from esphome.components.network import ip_address_literal
|
||||
from esphome.components.network import IPAddress
|
||||
from esphome.components.spi import CONF_INTERFACE_INDEX, get_spi_interface
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
@@ -320,11 +320,11 @@ def _final_validate_spi(config):
|
||||
def manual_ip(config):
|
||||
return cg.StructInitializer(
|
||||
ManualIP,
|
||||
("static_ip", ip_address_literal(config[CONF_STATIC_IP])),
|
||||
("gateway", ip_address_literal(config[CONF_GATEWAY])),
|
||||
("subnet", ip_address_literal(config[CONF_SUBNET])),
|
||||
("dns1", ip_address_literal(config[CONF_DNS1])),
|
||||
("dns2", ip_address_literal(config[CONF_DNS2])),
|
||||
("static_ip", IPAddress(str(config[CONF_STATIC_IP]))),
|
||||
("gateway", IPAddress(str(config[CONF_GATEWAY]))),
|
||||
("subnet", IPAddress(str(config[CONF_SUBNET]))),
|
||||
("dns1", IPAddress(str(config[CONF_DNS1]))),
|
||||
("dns2", IPAddress(str(config[CONF_DNS2]))),
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -124,7 +124,7 @@ class HttpRequestComponent : public Component {
|
||||
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
|
||||
|
||||
void set_useragent(const char *useragent) { this->useragent_ = useragent; }
|
||||
void set_timeout(uint32_t timeout) { this->timeout_ = timeout; }
|
||||
void set_timeout(uint16_t timeout) { this->timeout_ = timeout; }
|
||||
void set_watchdog_timeout(uint32_t watchdog_timeout) { this->watchdog_timeout_ = watchdog_timeout; }
|
||||
uint32_t get_watchdog_timeout() const { return this->watchdog_timeout_; }
|
||||
void set_follow_redirects(bool follow_redirects) { this->follow_redirects_ = follow_redirects; }
|
||||
@@ -173,7 +173,7 @@ class HttpRequestComponent : public Component {
|
||||
const char *useragent_{nullptr};
|
||||
bool follow_redirects_{};
|
||||
uint16_t redirect_limit_{};
|
||||
uint32_t timeout_{4500};
|
||||
uint16_t timeout_{4500};
|
||||
uint32_t watchdog_timeout_{0};
|
||||
};
|
||||
|
||||
|
||||
@@ -57,9 +57,9 @@ class AddressableLightEffect : public LightEffect {
|
||||
|
||||
class AddressableLambdaLightEffect : public AddressableLightEffect {
|
||||
public:
|
||||
AddressableLambdaLightEffect(const char *name, void (*f)(AddressableLight &, Color, bool initial_run),
|
||||
AddressableLambdaLightEffect(const char *name, std::function<void(AddressableLight &, Color, bool initial_run)> f,
|
||||
uint32_t update_interval)
|
||||
: AddressableLightEffect(name), f_(f), update_interval_(update_interval) {}
|
||||
: AddressableLightEffect(name), f_(std::move(f)), update_interval_(update_interval) {}
|
||||
void start() override { this->initial_run_ = true; }
|
||||
void apply(AddressableLight &it, const Color ¤t_color) override {
|
||||
const uint32_t now = millis();
|
||||
@@ -72,7 +72,7 @@ class AddressableLambdaLightEffect : public AddressableLightEffect {
|
||||
}
|
||||
|
||||
protected:
|
||||
void (*f_)(AddressableLight &, Color, bool initial_run);
|
||||
std::function<void(AddressableLight &, Color, bool initial_run)> f_;
|
||||
uint32_t update_interval_;
|
||||
uint32_t last_run_{0};
|
||||
bool initial_run_;
|
||||
|
||||
@@ -112,8 +112,8 @@ class RandomLightEffect : public LightEffect {
|
||||
|
||||
class LambdaLightEffect : public LightEffect {
|
||||
public:
|
||||
LambdaLightEffect(const char *name, void (*f)(bool initial_run), uint32_t update_interval)
|
||||
: LightEffect(name), f_(f), update_interval_(update_interval) {}
|
||||
LambdaLightEffect(const char *name, std::function<void(bool initial_run)> f, uint32_t update_interval)
|
||||
: LightEffect(name), f_(std::move(f)), update_interval_(update_interval) {}
|
||||
|
||||
void start() override { this->initial_run_ = true; }
|
||||
void apply() override {
|
||||
@@ -130,7 +130,7 @@ class LambdaLightEffect : public LightEffect {
|
||||
uint32_t get_current_index() const { return this->get_index(); }
|
||||
|
||||
protected:
|
||||
void (*f_)(bool initial_run);
|
||||
std::function<void(bool initial_run)> f_;
|
||||
uint32_t update_interval_;
|
||||
uint32_t last_run_{0};
|
||||
bool initial_run_;
|
||||
|
||||
@@ -33,8 +33,8 @@ class ModbusBinarySensor : public Component, public binary_sensor::BinarySensor,
|
||||
|
||||
void dump_config() override;
|
||||
|
||||
using transform_func_t = optional<bool> (*)(ModbusBinarySensor *, bool, const std::vector<uint8_t> &);
|
||||
void set_template(transform_func_t f) { this->transform_func_ = f; }
|
||||
using transform_func_t = std::function<optional<bool>(ModbusBinarySensor *, bool, const std::vector<uint8_t> &)>;
|
||||
void set_template(transform_func_t &&f) { this->transform_func_ = f; }
|
||||
|
||||
protected:
|
||||
optional<transform_func_t> transform_func_{nullopt};
|
||||
|
||||
@@ -31,10 +31,10 @@ class ModbusNumber : public number::Number, public Component, public SensorItem
|
||||
void set_parent(ModbusController *parent) { this->parent_ = parent; }
|
||||
void set_write_multiply(float factor) { this->multiply_by_ = factor; }
|
||||
|
||||
using transform_func_t = optional<float> (*)(ModbusNumber *, float, const std::vector<uint8_t> &);
|
||||
using write_transform_func_t = optional<float> (*)(ModbusNumber *, float, std::vector<uint16_t> &);
|
||||
void set_template(transform_func_t f) { this->transform_func_ = f; }
|
||||
void set_write_template(write_transform_func_t f) { this->write_transform_func_ = f; }
|
||||
using transform_func_t = std::function<optional<float>(ModbusNumber *, float, const std::vector<uint8_t> &)>;
|
||||
using write_transform_func_t = std::function<optional<float>(ModbusNumber *, float, std::vector<uint16_t> &)>;
|
||||
void set_template(transform_func_t &&f) { this->transform_func_ = f; }
|
||||
void set_write_template(write_transform_func_t &&f) { this->write_transform_func_ = f; }
|
||||
void set_use_write_mutiple(bool use_write_multiple) { this->use_write_multiple_ = use_write_multiple; }
|
||||
|
||||
protected:
|
||||
|
||||
@@ -29,8 +29,8 @@ class ModbusFloatOutput : public output::FloatOutput, public Component, public S
|
||||
// Do nothing
|
||||
void parse_and_publish(const std::vector<uint8_t> &data) override{};
|
||||
|
||||
using write_transform_func_t = optional<float> (*)(ModbusFloatOutput *, float, std::vector<uint16_t> &);
|
||||
void set_write_template(write_transform_func_t f) { this->write_transform_func_ = f; }
|
||||
using write_transform_func_t = std::function<optional<float>(ModbusFloatOutput *, float, std::vector<uint16_t> &)>;
|
||||
void set_write_template(write_transform_func_t &&f) { this->write_transform_func_ = f; }
|
||||
void set_use_write_mutiple(bool use_write_multiple) { this->use_write_multiple_ = use_write_multiple; }
|
||||
|
||||
protected:
|
||||
@@ -60,8 +60,8 @@ class ModbusBinaryOutput : public output::BinaryOutput, public Component, public
|
||||
// Do nothing
|
||||
void parse_and_publish(const std::vector<uint8_t> &data) override{};
|
||||
|
||||
using write_transform_func_t = optional<bool> (*)(ModbusBinaryOutput *, bool, std::vector<uint8_t> &);
|
||||
void set_write_template(write_transform_func_t f) { this->write_transform_func_ = f; }
|
||||
using write_transform_func_t = std::function<optional<bool>(ModbusBinaryOutput *, bool, std::vector<uint8_t> &)>;
|
||||
void set_write_template(write_transform_func_t &&f) { this->write_transform_func_ = f; }
|
||||
void set_use_write_mutiple(bool use_write_multiple) { this->use_write_multiple_ = use_write_multiple; }
|
||||
|
||||
protected:
|
||||
|
||||
@@ -28,7 +28,7 @@ void ModbusSelect::parse_and_publish(const std::vector<uint8_t> &data) {
|
||||
|
||||
if (map_it != this->mapping_.cend()) {
|
||||
size_t idx = std::distance(this->mapping_.cbegin(), map_it);
|
||||
new_state = std::string(this->option_at(idx));
|
||||
new_state = std::string(this->traits.get_options()[idx]);
|
||||
ESP_LOGV(TAG, "Found option %s for value %lld", new_state->c_str(), value);
|
||||
} else {
|
||||
ESP_LOGE(TAG, "No option found for mapping %lld", value);
|
||||
|
||||
@@ -26,15 +26,16 @@ class ModbusSelect : public Component, public select::Select, public SensorItem
|
||||
this->mapping_ = std::move(mapping);
|
||||
}
|
||||
|
||||
using transform_func_t = optional<std::string> (*)(ModbusSelect *const, int64_t, const std::vector<uint8_t> &);
|
||||
using write_transform_func_t = optional<int64_t> (*)(ModbusSelect *const, const std::string &, int64_t,
|
||||
std::vector<uint16_t> &);
|
||||
using transform_func_t =
|
||||
std::function<optional<std::string>(ModbusSelect *const, int64_t, const std::vector<uint8_t> &)>;
|
||||
using write_transform_func_t =
|
||||
std::function<optional<int64_t>(ModbusSelect *const, const std::string &, int64_t, std::vector<uint16_t> &)>;
|
||||
|
||||
void set_parent(ModbusController *const parent) { this->parent_ = parent; }
|
||||
void set_use_write_mutiple(bool use_write_multiple) { this->use_write_multiple_ = use_write_multiple; }
|
||||
void set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
|
||||
void set_template(transform_func_t f) { this->transform_func_ = f; }
|
||||
void set_write_template(write_transform_func_t f) { this->write_transform_func_ = f; }
|
||||
void set_template(transform_func_t &&f) { this->transform_func_ = f; }
|
||||
void set_write_template(write_transform_func_t &&f) { this->write_transform_func_ = f; }
|
||||
|
||||
void dump_config() override;
|
||||
void parse_and_publish(const std::vector<uint8_t> &data) override;
|
||||
|
||||
@@ -25,9 +25,9 @@ class ModbusSensor : public Component, public sensor::Sensor, public SensorItem
|
||||
|
||||
void parse_and_publish(const std::vector<uint8_t> &data) override;
|
||||
void dump_config() override;
|
||||
using transform_func_t = optional<float> (*)(ModbusSensor *, float, const std::vector<uint8_t> &);
|
||||
using transform_func_t = std::function<optional<float>(ModbusSensor *, float, const std::vector<uint8_t> &)>;
|
||||
|
||||
void set_template(transform_func_t f) { this->transform_func_ = f; }
|
||||
void set_template(transform_func_t &&f) { this->transform_func_ = f; }
|
||||
|
||||
protected:
|
||||
optional<transform_func_t> transform_func_{nullopt};
|
||||
|
||||
@@ -34,10 +34,10 @@ class ModbusSwitch : public Component, public switch_::Switch, public SensorItem
|
||||
void parse_and_publish(const std::vector<uint8_t> &data) override;
|
||||
void set_parent(ModbusController *parent) { this->parent_ = parent; }
|
||||
|
||||
using transform_func_t = optional<bool> (*)(ModbusSwitch *, bool, const std::vector<uint8_t> &);
|
||||
using write_transform_func_t = optional<bool> (*)(ModbusSwitch *, bool, std::vector<uint8_t> &);
|
||||
void set_template(transform_func_t f) { this->publish_transform_func_ = f; }
|
||||
void set_write_template(write_transform_func_t f) { this->write_transform_func_ = f; }
|
||||
using transform_func_t = std::function<optional<bool>(ModbusSwitch *, bool, const std::vector<uint8_t> &)>;
|
||||
using write_transform_func_t = std::function<optional<bool>(ModbusSwitch *, bool, std::vector<uint8_t> &)>;
|
||||
void set_template(transform_func_t &&f) { this->publish_transform_func_ = f; }
|
||||
void set_write_template(write_transform_func_t &&f) { this->write_transform_func_ = f; }
|
||||
void set_use_write_mutiple(bool use_write_multiple) { this->use_write_multiple_ = use_write_multiple; }
|
||||
|
||||
protected:
|
||||
|
||||
@@ -30,8 +30,9 @@ class ModbusTextSensor : public Component, public text_sensor::TextSensor, publi
|
||||
void dump_config() override;
|
||||
|
||||
void parse_and_publish(const std::vector<uint8_t> &data) override;
|
||||
using transform_func_t = optional<std::string> (*)(ModbusTextSensor *, std::string, const std::vector<uint8_t> &);
|
||||
void set_template(transform_func_t f) { this->transform_func_ = f; }
|
||||
using transform_func_t =
|
||||
std::function<optional<std::string>(ModbusTextSensor *, std::string, const std::vector<uint8_t> &)>;
|
||||
void set_template(transform_func_t &&f) { this->transform_func_ = f; }
|
||||
|
||||
protected:
|
||||
optional<transform_func_t> transform_func_{nullopt};
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import ipaddress
|
||||
|
||||
import esphome.codegen as cg
|
||||
from esphome.components.esp32 import add_idf_sdkconfig_option
|
||||
import esphome.config_validation as cv
|
||||
@@ -12,41 +10,6 @@ AUTO_LOAD = ["mdns"]
|
||||
network_ns = cg.esphome_ns.namespace("network")
|
||||
IPAddress = network_ns.class_("IPAddress")
|
||||
|
||||
|
||||
def ip_address_literal(ip: str | int | None) -> cg.MockObj:
|
||||
"""Generate an IPAddress with compile-time initialization instead of runtime parsing.
|
||||
|
||||
This function parses the IP address in Python during code generation and generates
|
||||
a call to the 4-octet constructor (IPAddress(192, 168, 1, 1)) instead of the
|
||||
string constructor (IPAddress("192.168.1.1")). This eliminates runtime string
|
||||
parsing overhead and reduces flash usage on embedded systems.
|
||||
|
||||
Args:
|
||||
ip: IP address as string (e.g., "192.168.1.1"), ipaddress.IPv4Address, or None
|
||||
|
||||
Returns:
|
||||
IPAddress expression that uses 4-octet constructor for efficiency
|
||||
"""
|
||||
if ip is None:
|
||||
return IPAddress(0, 0, 0, 0)
|
||||
|
||||
try:
|
||||
# Parse using Python's ipaddress module
|
||||
ip_obj = ipaddress.ip_address(ip)
|
||||
except (ValueError, TypeError):
|
||||
pass
|
||||
else:
|
||||
# Only support IPv4 for now
|
||||
if isinstance(ip_obj, ipaddress.IPv4Address):
|
||||
# Extract octets from the packed bytes representation
|
||||
octets = ip_obj.packed
|
||||
# Generate call to 4-octet constructor: IPAddress(192, 168, 1, 1)
|
||||
return IPAddress(octets[0], octets[1], octets[2], octets[3])
|
||||
|
||||
# Fallback to string constructor if parsing fails
|
||||
return IPAddress(str(ip))
|
||||
|
||||
|
||||
CONFIG_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.SplitDefault(
|
||||
|
||||
@@ -540,23 +540,6 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
|
||||
*/
|
||||
void goto_page(uint8_t page);
|
||||
|
||||
/**
|
||||
* Set the visibility of a component.
|
||||
*
|
||||
* @param component The component name.
|
||||
* @param show True to show the component, false to hide it.
|
||||
*
|
||||
* @see show_component()
|
||||
* @see hide_component()
|
||||
*
|
||||
* Example:
|
||||
* ```cpp
|
||||
* it.set_component_visibility("textview", true); // Equivalent to show_component("textview")
|
||||
* it.set_component_visibility("textview", false); // Equivalent to hide_component("textview")
|
||||
* ```
|
||||
*/
|
||||
void set_component_visibility(const char *component, bool show) override;
|
||||
|
||||
/**
|
||||
* Hide a component.
|
||||
* @param component The component name.
|
||||
|
||||
@@ -45,7 +45,6 @@ class NextionBase {
|
||||
virtual void set_component_pressed_font_color(const char *component, Color color) = 0;
|
||||
virtual void set_component_font(const char *component, uint8_t font_id) = 0;
|
||||
|
||||
virtual void set_component_visibility(const char *component, bool show) = 0;
|
||||
virtual void show_component(const char *component) = 0;
|
||||
virtual void hide_component(const char *component) = 0;
|
||||
|
||||
|
||||
@@ -201,13 +201,13 @@ void Nextion::set_component_font(const char *component, uint8_t font_id) {
|
||||
this->add_no_result_to_queue_with_printf_("set_component_font", "%s.font=%" PRIu8, component, font_id);
|
||||
}
|
||||
|
||||
void Nextion::set_component_visibility(const char *component, bool show) {
|
||||
this->add_no_result_to_queue_with_printf_("set_component_visibility", "vis %s,%d", component, show ? 1 : 0);
|
||||
void Nextion::hide_component(const char *component) {
|
||||
this->add_no_result_to_queue_with_printf_("hide_component", "vis %s,0", component);
|
||||
}
|
||||
|
||||
void Nextion::hide_component(const char *component) { this->set_component_visibility(component, false); }
|
||||
|
||||
void Nextion::show_component(const char *component) { this->set_component_visibility(component, true); }
|
||||
void Nextion::show_component(const char *component) {
|
||||
this->add_no_result_to_queue_with_printf_("show_component", "vis %s,1", component);
|
||||
}
|
||||
|
||||
void Nextion::enable_component_touch(const char *component) {
|
||||
this->add_no_result_to_queue_with_printf_("enable_component_touch", "tsw %s,1", component);
|
||||
|
||||
@@ -81,11 +81,13 @@ void NextionComponent::update_component_settings(bool force_update) {
|
||||
|
||||
this->component_flags_.visible_needs_update = false;
|
||||
|
||||
this->nextion_->set_component_visibility(name_to_send.c_str(), this->component_flags_.visible);
|
||||
if (!this->component_flags_.visible) {
|
||||
if (this->component_flags_.visible) {
|
||||
this->nextion_->show_component(name_to_send.c_str());
|
||||
this->send_state_to_nextion();
|
||||
} else {
|
||||
this->nextion_->hide_component(name_to_send.c_str());
|
||||
return;
|
||||
}
|
||||
this->send_state_to_nextion();
|
||||
}
|
||||
|
||||
if (this->component_flags_.bco_needs_update || (force_update && this->component_flags_.bco2_is_set)) {
|
||||
|
||||
@@ -174,6 +174,11 @@ bool Nextion::upload_tft(uint32_t baud_rate, bool exit_reparse) {
|
||||
|
||||
// Check if baud rate is supported
|
||||
this->original_baud_rate_ = this->parent_->get_baud_rate();
|
||||
static const std::vector<uint32_t> SUPPORTED_BAUD_RATES = {2400, 4800, 9600, 19200, 31250, 38400, 57600,
|
||||
115200, 230400, 250000, 256000, 512000, 921600};
|
||||
if (std::find(SUPPORTED_BAUD_RATES.begin(), SUPPORTED_BAUD_RATES.end(), baud_rate) == SUPPORTED_BAUD_RATES.end()) {
|
||||
baud_rate = this->original_baud_rate_;
|
||||
}
|
||||
ESP_LOGD(TAG, "Baud rate: %" PRIu32, baud_rate);
|
||||
|
||||
// Define the configuration for the HTTP client
|
||||
|
||||
@@ -177,6 +177,11 @@ bool Nextion::upload_tft(uint32_t baud_rate, bool exit_reparse) {
|
||||
|
||||
// Check if baud rate is supported
|
||||
this->original_baud_rate_ = this->parent_->get_baud_rate();
|
||||
static const std::vector<uint32_t> SUPPORTED_BAUD_RATES = {2400, 4800, 9600, 19200, 31250, 38400, 57600,
|
||||
115200, 230400, 250000, 256000, 512000, 921600};
|
||||
if (std::find(SUPPORTED_BAUD_RATES.begin(), SUPPORTED_BAUD_RATES.end(), baud_rate) == SUPPORTED_BAUD_RATES.end()) {
|
||||
baud_rate = this->original_baud_rate_;
|
||||
}
|
||||
ESP_LOGD(TAG, "Baud rate: %" PRIu32, baud_rate);
|
||||
|
||||
// Define the configuration for the HTTP client
|
||||
|
||||
@@ -17,7 +17,6 @@ from esphome.const import (
|
||||
CONF_FAMILY,
|
||||
CONF_GROUP,
|
||||
CONF_ID,
|
||||
CONF_INDEX,
|
||||
CONF_INVERTED,
|
||||
CONF_LEVEL,
|
||||
CONF_MAGNITUDE,
|
||||
@@ -617,49 +616,6 @@ async def dooya_action(var, config, args):
|
||||
cg.add(var.set_check(template_))
|
||||
|
||||
|
||||
# Dyson
|
||||
DysonData, DysonBinarySensor, DysonTrigger, DysonAction, DysonDumper = declare_protocol(
|
||||
"Dyson"
|
||||
)
|
||||
DYSON_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.Required(CONF_CODE): cv.hex_uint16_t,
|
||||
cv.Optional(CONF_INDEX, default=0xFF): cv.hex_uint8_t,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@register_binary_sensor("dyson", DysonBinarySensor, DYSON_SCHEMA)
|
||||
def dyson_binary_sensor(var, config):
|
||||
cg.add(
|
||||
var.set_data(
|
||||
cg.StructInitializer(
|
||||
DysonData,
|
||||
("code", config[CONF_CODE]),
|
||||
("index", config[CONF_INDEX]),
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@register_trigger("dyson", DysonTrigger, DysonData)
|
||||
def dyson_trigger(var, config):
|
||||
pass
|
||||
|
||||
|
||||
@register_dumper("dyson", DysonDumper)
|
||||
def dyson_dumper(var, config):
|
||||
pass
|
||||
|
||||
|
||||
@register_action("dyson", DysonAction, DYSON_SCHEMA)
|
||||
async def dyson_action(var, config, args):
|
||||
template_ = await cg.templatable(config[CONF_CODE], args, cg.uint16)
|
||||
cg.add(var.set_code(template_))
|
||||
template_ = await cg.templatable(config[CONF_INDEX], args, cg.uint8)
|
||||
cg.add(var.set_index(template_))
|
||||
|
||||
|
||||
# JVC
|
||||
JVCData, JVCBinarySensor, JVCTrigger, JVCAction, JVCDumper = declare_protocol("JVC")
|
||||
JVC_SCHEMA = cv.Schema({cv.Required(CONF_DATA): cv.hex_uint32_t})
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
#include "dyson_protocol.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
#include <cinttypes>
|
||||
|
||||
namespace esphome {
|
||||
namespace remote_base {
|
||||
|
||||
static const char *const TAG = "remote.dyson";
|
||||
|
||||
// pulsewidth [µs]
|
||||
constexpr uint32_t PW_MARK_US = 780;
|
||||
constexpr uint32_t PW_SHORT_US = 720;
|
||||
constexpr uint32_t PW_LONG_US = 1500;
|
||||
constexpr uint32_t PW_START_US = 2280;
|
||||
|
||||
// MSB of 15 bit dyson code
|
||||
constexpr uint16_t MSB_DYSON = (1 << 14);
|
||||
|
||||
// required symbols in transmit buffer = (start_symbol + 15 data_symbols)
|
||||
constexpr uint32_t N_SYMBOLS_REQ = 2u * (1 + 15);
|
||||
|
||||
void DysonProtocol::encode(RemoteTransmitData *dst, const DysonData &data) {
|
||||
uint32_t raw_code = (data.code << 2) + (data.index & 3);
|
||||
dst->set_carrier_frequency(36000);
|
||||
dst->reserve(N_SYMBOLS_REQ + 1);
|
||||
dst->item(PW_START_US, PW_SHORT_US);
|
||||
for (uint16_t mask = MSB_DYSON; mask != 0; mask >>= 1) {
|
||||
if (mask == (mask & raw_code)) {
|
||||
dst->item(PW_MARK_US, PW_LONG_US);
|
||||
} else {
|
||||
dst->item(PW_MARK_US, PW_SHORT_US);
|
||||
}
|
||||
}
|
||||
dst->mark(PW_MARK_US); // final carrier pulse
|
||||
}
|
||||
|
||||
optional<DysonData> DysonProtocol::decode(RemoteReceiveData src) {
|
||||
uint32_t n_received = static_cast<uint32_t>(src.size());
|
||||
uint16_t raw_code = 0;
|
||||
DysonData data{
|
||||
.code = 0,
|
||||
.index = 0,
|
||||
};
|
||||
if (n_received < N_SYMBOLS_REQ)
|
||||
return {}; // invalid frame length
|
||||
if (!src.expect_item(PW_START_US, PW_SHORT_US))
|
||||
return {}; // start not found
|
||||
for (uint16_t mask = MSB_DYSON; mask != 0; mask >>= 1) {
|
||||
if (src.expect_item(PW_MARK_US, PW_SHORT_US)) {
|
||||
raw_code &= ~mask; // zero detected
|
||||
} else if (src.expect_item(PW_MARK_US, PW_LONG_US)) {
|
||||
raw_code |= mask; // one detected
|
||||
} else {
|
||||
return {}; // invalid data item
|
||||
}
|
||||
}
|
||||
data.code = raw_code >> 2; // extract button code
|
||||
data.index = raw_code & 3; // extract rolling index
|
||||
if (src.expect_mark(PW_MARK_US)) { // check total length
|
||||
return data;
|
||||
}
|
||||
return {}; // frame not complete
|
||||
}
|
||||
|
||||
void DysonProtocol::dump(const DysonData &data) {
|
||||
ESP_LOGI(TAG, "Dyson: code=0x%x rolling index=%d", data.code, data.index);
|
||||
}
|
||||
|
||||
} // namespace remote_base
|
||||
} // namespace esphome
|
||||
@@ -1,46 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "remote_base.h"
|
||||
|
||||
#include <cinttypes>
|
||||
|
||||
namespace esphome {
|
||||
namespace remote_base {
|
||||
|
||||
static constexpr uint8_t IGNORE_INDEX = 0xFF;
|
||||
|
||||
struct DysonData {
|
||||
uint16_t code; // the button, e.g. power, swing, fan++, ...
|
||||
uint8_t index; // the rolling index counter
|
||||
bool operator==(const DysonData &rhs) const {
|
||||
if (IGNORE_INDEX == index || IGNORE_INDEX == rhs.index) {
|
||||
return code == rhs.code;
|
||||
}
|
||||
return code == rhs.code && index == rhs.index;
|
||||
}
|
||||
};
|
||||
|
||||
class DysonProtocol : public RemoteProtocol<DysonData> {
|
||||
public:
|
||||
void encode(RemoteTransmitData *dst, const DysonData &data) override;
|
||||
optional<DysonData> decode(RemoteReceiveData src) override;
|
||||
void dump(const DysonData &data) override;
|
||||
};
|
||||
|
||||
DECLARE_REMOTE_PROTOCOL(Dyson)
|
||||
|
||||
template<typename... Ts> class DysonAction : public RemoteTransmitterActionBase<Ts...> {
|
||||
public:
|
||||
TEMPLATABLE_VALUE(uint16_t, code)
|
||||
TEMPLATABLE_VALUE(uint8_t, index)
|
||||
|
||||
void encode(RemoteTransmitData *dst, Ts... x) override {
|
||||
DysonData data{};
|
||||
data.code = this->code_.value(x...);
|
||||
data.index = this->index_.value(x...);
|
||||
DysonProtocol().encode(dst, data);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace remote_base
|
||||
} // namespace esphome
|
||||
@@ -60,7 +60,5 @@ optional<std::string> Select::at(size_t index) const {
|
||||
}
|
||||
}
|
||||
|
||||
const char *Select::option_at(size_t index) const { return traits.get_options().at(index); }
|
||||
|
||||
} // namespace select
|
||||
} // namespace esphome
|
||||
|
||||
@@ -56,9 +56,6 @@ class Select : public EntityBase {
|
||||
/// Return the (optional) option value at the provided index offset.
|
||||
optional<std::string> at(size_t index) const;
|
||||
|
||||
/// Return the option value at the provided index offset (as const char* from flash).
|
||||
const char *option_at(size_t index) const;
|
||||
|
||||
void add_on_state_callback(std::function<void(std::string, size_t)> &&callback);
|
||||
|
||||
protected:
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
import logging
|
||||
from re import Match
|
||||
from typing import Any
|
||||
|
||||
from esphome import core
|
||||
from esphome.config_helpers import Extend, Remove, merge_config, merge_dicts_ordered
|
||||
@@ -41,34 +39,7 @@ async def to_code(config):
|
||||
pass
|
||||
|
||||
|
||||
def _restore_data_base(value: Any, orig_value: ESPHomeDataBase) -> ESPHomeDataBase:
|
||||
"""This function restores ESPHomeDataBase metadata held by the original string.
|
||||
This is needed because during jinja evaluation, strings can be replaced by other types,
|
||||
but we want to keep the original metadata for error reporting and source mapping.
|
||||
For example, if a substitution replaces a string with a dictionary, we want that items
|
||||
in the dictionary to still point to the original document location
|
||||
"""
|
||||
if isinstance(value, ESPHomeDataBase):
|
||||
return value
|
||||
if isinstance(value, dict):
|
||||
return {
|
||||
_restore_data_base(k, orig_value): _restore_data_base(v, orig_value)
|
||||
for k, v in value.items()
|
||||
}
|
||||
if isinstance(value, list):
|
||||
return [_restore_data_base(v, orig_value) for v in value]
|
||||
if isinstance(value, str):
|
||||
return make_data_base(value, orig_value)
|
||||
return value
|
||||
|
||||
|
||||
def _expand_jinja(
|
||||
value: str | JinjaStr,
|
||||
orig_value: str | JinjaStr,
|
||||
path,
|
||||
jinja: Jinja,
|
||||
ignore_missing: bool,
|
||||
) -> Any:
|
||||
def _expand_jinja(value, orig_value, path, jinja, ignore_missing):
|
||||
if has_jinja(value):
|
||||
# If the original value passed in to this function is a JinjaStr, it means it contains an unresolved
|
||||
# Jinja expression from a previous pass.
|
||||
@@ -94,17 +65,10 @@ def _expand_jinja(
|
||||
f"\nSee {'->'.join(str(x) for x in path)}",
|
||||
path,
|
||||
)
|
||||
# If the original, unexpanded string, contained document metadata (ESPHomeDatabase),
|
||||
# assign this same document metadata to the resulting value.
|
||||
if isinstance(orig_value, ESPHomeDataBase):
|
||||
value = _restore_data_base(value, orig_value)
|
||||
|
||||
return value
|
||||
|
||||
|
||||
def _expand_substitutions(
|
||||
substitutions: dict, value: str, path, jinja: Jinja, ignore_missing: bool
|
||||
) -> Any:
|
||||
def _expand_substitutions(substitutions, value, path, jinja, ignore_missing):
|
||||
if "$" not in value:
|
||||
return value
|
||||
|
||||
@@ -112,14 +76,14 @@ def _expand_substitutions(
|
||||
|
||||
i = 0
|
||||
while True:
|
||||
m: Match[str] = cv.VARIABLE_PROG.search(value, i)
|
||||
m = cv.VARIABLE_PROG.search(value, i)
|
||||
if not m:
|
||||
# No more variable substitutions found. See if the remainder looks like a jinja template
|
||||
value = _expand_jinja(value, orig_value, path, jinja, ignore_missing)
|
||||
break
|
||||
|
||||
i, j = m.span(0)
|
||||
name: str = m.group(1)
|
||||
name = m.group(1)
|
||||
if name.startswith("{") and name.endswith("}"):
|
||||
name = name[1:-1]
|
||||
if name not in substitutions:
|
||||
@@ -134,7 +98,7 @@ def _expand_substitutions(
|
||||
i = j
|
||||
continue
|
||||
|
||||
sub: Any = substitutions[name]
|
||||
sub = substitutions[name]
|
||||
|
||||
if i == 0 and j == len(value):
|
||||
# The variable spans the whole expression, e.g., "${varName}". Return its resolved value directly
|
||||
@@ -157,13 +121,7 @@ def _expand_substitutions(
|
||||
return value
|
||||
|
||||
|
||||
def _substitute_item(
|
||||
substitutions: dict,
|
||||
item: Any,
|
||||
path: list[int | str],
|
||||
jinja: Jinja,
|
||||
ignore_missing: bool,
|
||||
) -> Any | None:
|
||||
def _substitute_item(substitutions, item, path, jinja, ignore_missing):
|
||||
if isinstance(item, ESPLiteralValue):
|
||||
return None # do not substitute inside literal blocks
|
||||
if isinstance(item, list):
|
||||
@@ -202,9 +160,7 @@ def _substitute_item(
|
||||
return None
|
||||
|
||||
|
||||
def do_substitution_pass(
|
||||
config: dict, command_line_substitutions: dict, ignore_missing: bool = False
|
||||
) -> None:
|
||||
def do_substitution_pass(config, command_line_substitutions, ignore_missing=False):
|
||||
if CONF_SUBSTITUTIONS not in config and not command_line_substitutions:
|
||||
return
|
||||
|
||||
|
||||
@@ -1,14 +1,10 @@
|
||||
from ast import literal_eval
|
||||
from collections.abc import Iterator
|
||||
from itertools import chain, islice
|
||||
import logging
|
||||
import math
|
||||
import re
|
||||
from types import GeneratorType
|
||||
from typing import Any
|
||||
|
||||
import jinja2 as jinja
|
||||
from jinja2.nativetypes import NativeCodeGenerator, NativeTemplate
|
||||
from jinja2.sandbox import SandboxedEnvironment
|
||||
|
||||
from esphome.yaml_util import ESPLiteralValue
|
||||
|
||||
@@ -28,7 +24,7 @@ detect_jinja_re = re.compile(
|
||||
)
|
||||
|
||||
|
||||
def has_jinja(st: str) -> bool:
|
||||
def has_jinja(st):
|
||||
return detect_jinja_re.search(st) is not None
|
||||
|
||||
|
||||
@@ -113,56 +109,12 @@ class TrackerContext(jinja.runtime.Context):
|
||||
return val
|
||||
|
||||
|
||||
def _concat_nodes_override(values: Iterator[Any]) -> Any:
|
||||
"""
|
||||
This function customizes how Jinja preserves native types when concatenating
|
||||
multiple result nodes together. If the result is a single node, its value
|
||||
is returned. Otherwise, the nodes are concatenated as strings. If
|
||||
the result can be parsed with `ast.literal_eval`, the parsed
|
||||
value is returned. Otherwise, the string is returned.
|
||||
This helps preserve metadata such as ESPHomeDataBase from original values
|
||||
and mimicks how HomeAssistant deals with template evaluation and preserving
|
||||
the original datatype.
|
||||
"""
|
||||
head: list[Any] = list(islice(values, 2))
|
||||
|
||||
if not head:
|
||||
return None
|
||||
|
||||
if len(head) == 1:
|
||||
raw = head[0]
|
||||
if not isinstance(raw, str):
|
||||
return raw
|
||||
else:
|
||||
if isinstance(values, GeneratorType):
|
||||
values = chain(head, values)
|
||||
raw = "".join([str(v) for v in values])
|
||||
|
||||
try:
|
||||
# Attempt to parse the concatenated string into a Python literal.
|
||||
# This allows expressions like "1 + 2" to be evaluated to the integer 3.
|
||||
# If the result is also a string or there is a parsing error,
|
||||
# fall back to returning the raw string. This is consistent with
|
||||
# Home Assistant's behavior when evaluating templates
|
||||
result = literal_eval(raw)
|
||||
if not isinstance(result, str):
|
||||
return result
|
||||
|
||||
except (ValueError, SyntaxError, MemoryError, TypeError):
|
||||
pass
|
||||
return raw
|
||||
|
||||
|
||||
class Jinja(jinja.Environment):
|
||||
class Jinja(SandboxedEnvironment):
|
||||
"""
|
||||
Wraps a Jinja environment
|
||||
"""
|
||||
|
||||
# jinja environment customization overrides
|
||||
code_generator_class = NativeCodeGenerator
|
||||
concat = staticmethod(_concat_nodes_override)
|
||||
|
||||
def __init__(self, context_vars: dict):
|
||||
def __init__(self, context_vars):
|
||||
super().__init__(
|
||||
trim_blocks=True,
|
||||
lstrip_blocks=True,
|
||||
@@ -190,10 +142,19 @@ class Jinja(jinja.Environment):
|
||||
**SAFE_GLOBALS,
|
||||
}
|
||||
|
||||
def expand(self, content_str: str | JinjaStr) -> Any:
|
||||
def safe_eval(self, expr):
|
||||
try:
|
||||
result = literal_eval(expr)
|
||||
if not isinstance(result, str):
|
||||
return result
|
||||
except (ValueError, SyntaxError, MemoryError, TypeError):
|
||||
pass
|
||||
return expr
|
||||
|
||||
def expand(self, content_str):
|
||||
"""
|
||||
Renders a string that may contain Jinja expressions or statements
|
||||
Returns the resulting value if all variables and expressions could be resolved.
|
||||
Returns the resulting processed string if all values could be resolved.
|
||||
Otherwise, it returns a tagged (JinjaStr) string that captures variables
|
||||
in scope (upvalues), like a closure for later evaluation.
|
||||
"""
|
||||
@@ -211,7 +172,7 @@ class Jinja(jinja.Environment):
|
||||
self.context_trace = {}
|
||||
try:
|
||||
template = self.from_string(content_str)
|
||||
result = template.render(override_vars)
|
||||
result = self.safe_eval(template.render(override_vars))
|
||||
if isinstance(result, Undefined):
|
||||
print("" + result) # force a UndefinedError exception
|
||||
except (TemplateSyntaxError, UndefinedError) as err:
|
||||
@@ -240,10 +201,3 @@ class Jinja(jinja.Environment):
|
||||
content_str.result = result
|
||||
|
||||
return result, None
|
||||
|
||||
|
||||
class JinjaTemplate(NativeTemplate):
|
||||
environment_class = Jinja
|
||||
|
||||
|
||||
Jinja.template_class = JinjaTemplate
|
||||
|
||||
@@ -10,6 +10,9 @@ from .. import template_ns
|
||||
TemplateBinarySensor = template_ns.class_(
|
||||
"TemplateBinarySensor", binary_sensor.BinarySensor, cg.Component
|
||||
)
|
||||
StatelessTemplateBinarySensor = template_ns.class_(
|
||||
"StatelessTemplateBinarySensor", binary_sensor.BinarySensor, cg.Component
|
||||
)
|
||||
|
||||
CONFIG_SCHEMA = (
|
||||
binary_sensor.binary_sensor_schema(TemplateBinarySensor)
|
||||
@@ -26,28 +29,33 @@ CONFIG_SCHEMA = (
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = await binary_sensor.new_binary_sensor(config)
|
||||
await cg.register_component(var, config)
|
||||
|
||||
# Check if we have a lambda first - determines which class to instantiate
|
||||
if lamb := config.get(CONF_LAMBDA):
|
||||
# Use new_lambda_pvariable to create either TemplateBinarySensor or StatelessTemplateBinarySensor
|
||||
template_ = await cg.process_lambda(
|
||||
lamb, [], return_type=cg.optional.template(bool)
|
||||
)
|
||||
cg.add(var.set_template(template_))
|
||||
if condition := config.get(CONF_CONDITION):
|
||||
var = automation.new_lambda_pvariable(
|
||||
config[CONF_ID], template_, StatelessTemplateBinarySensor
|
||||
)
|
||||
# Manually register as binary sensor since we didn't use new_binary_sensor
|
||||
await binary_sensor.register_binary_sensor(var, config)
|
||||
await cg.register_component(var, config)
|
||||
elif condition := config.get(CONF_CONDITION):
|
||||
# For conditions, create stateful version and set template
|
||||
var = await binary_sensor.new_binary_sensor(config)
|
||||
await cg.register_component(var, config)
|
||||
condition = await automation.build_condition(
|
||||
condition, cg.TemplateArguments(), []
|
||||
)
|
||||
# Generate a stateless lambda that calls condition.check()
|
||||
# capture="" is safe because condition is a global variable in generated C++ code
|
||||
# and doesn't need to be captured. This allows implicit conversion to function pointer.
|
||||
template_ = LambdaExpression(
|
||||
f"return {condition.check()};",
|
||||
[],
|
||||
return_type=cg.optional.template(bool),
|
||||
capture="",
|
||||
f"return {condition.check()};", [], return_type=cg.optional.template(bool)
|
||||
)
|
||||
cg.add(var.set_template(template_))
|
||||
else:
|
||||
# No lambda or condition - just create the base template sensor
|
||||
var = await binary_sensor.new_binary_sensor(config)
|
||||
await cg.register_component(var, config)
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
|
||||
@@ -6,18 +6,13 @@ namespace template_ {
|
||||
|
||||
static const char *const TAG = "template.binary_sensor";
|
||||
|
||||
void TemplateBinarySensor::setup() { this->loop(); }
|
||||
|
||||
void TemplateBinarySensor::loop() {
|
||||
if (!this->f_.has_value())
|
||||
return;
|
||||
|
||||
auto s = (*this->f_)();
|
||||
if (s.has_value()) {
|
||||
this->publish_state(*s);
|
||||
}
|
||||
// Template instantiations
|
||||
template<typename F> void TemplateBinarySensorBase<F>::dump_config() {
|
||||
LOG_BINARY_SENSOR("", "Template Binary Sensor", this);
|
||||
}
|
||||
void TemplateBinarySensor::dump_config() { LOG_BINARY_SENSOR("", "Template Binary Sensor", this); }
|
||||
|
||||
template class TemplateBinarySensorBase<std::function<optional<bool>()>>;
|
||||
template class TemplateBinarySensorBase<optional<bool> (*)()>;
|
||||
|
||||
} // namespace template_
|
||||
} // namespace esphome
|
||||
|
||||
@@ -6,18 +6,41 @@
|
||||
namespace esphome {
|
||||
namespace template_ {
|
||||
|
||||
class TemplateBinarySensor : public Component, public binary_sensor::BinarySensor {
|
||||
template<typename F> class TemplateBinarySensorBase : public Component, public binary_sensor::BinarySensor {
|
||||
public:
|
||||
void set_template(optional<bool> (*f)()) { this->f_ = f; }
|
||||
void setup() override { this->loop(); }
|
||||
|
||||
void loop() override {
|
||||
if (this->f_ == nullptr)
|
||||
return;
|
||||
auto s = this->f_();
|
||||
if (s.has_value()) {
|
||||
this->publish_state(*s);
|
||||
}
|
||||
}
|
||||
|
||||
void setup() override;
|
||||
void loop() override;
|
||||
void dump_config() override;
|
||||
|
||||
float get_setup_priority() const override { return setup_priority::HARDWARE; }
|
||||
|
||||
protected:
|
||||
optional<optional<bool> (*)()> f_;
|
||||
F f_;
|
||||
};
|
||||
|
||||
class TemplateBinarySensor : public TemplateBinarySensorBase<std::function<optional<bool>()>> {
|
||||
public:
|
||||
TemplateBinarySensor() { this->f_ = nullptr; }
|
||||
void set_template(std::function<optional<bool>()> &&f) { this->f_ = f; }
|
||||
};
|
||||
|
||||
/** Optimized template binary sensor for stateless lambdas (no capture).
|
||||
*
|
||||
* Uses function pointer instead of std::function to reduce memory overhead.
|
||||
* Memory: 4 bytes (function pointer on 32-bit) vs 32 bytes (std::function).
|
||||
*/
|
||||
class StatelessTemplateBinarySensor : public TemplateBinarySensorBase<optional<bool> (*)()> {
|
||||
public:
|
||||
explicit StatelessTemplateBinarySensor(optional<bool> (*f)()) { this->f_ = f; }
|
||||
};
|
||||
|
||||
} // namespace template_
|
||||
|
||||
@@ -23,6 +23,9 @@ from esphome.const import (
|
||||
from .. import template_ns
|
||||
|
||||
TemplateCover = template_ns.class_("TemplateCover", cover.Cover, cg.Component)
|
||||
StatelessTemplateCover = template_ns.class_(
|
||||
"StatelessTemplateCover", cover.Cover, cg.Component
|
||||
)
|
||||
|
||||
TemplateCoverRestoreMode = template_ns.enum("TemplateCoverRestoreMode")
|
||||
RESTORE_MODES = {
|
||||
@@ -63,13 +66,22 @@ CONFIG_SCHEMA = (
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = await cover.new_cover(config)
|
||||
await cg.register_component(var, config)
|
||||
if CONF_LAMBDA in config:
|
||||
# Use new_lambda_pvariable to create either TemplateCover or StatelessTemplateCover
|
||||
template_ = await cg.process_lambda(
|
||||
config[CONF_LAMBDA], [], return_type=cg.optional.template(float)
|
||||
)
|
||||
cg.add(var.set_state_lambda(template_))
|
||||
var = automation.new_lambda_pvariable(
|
||||
config[CONF_ID], template_, StatelessTemplateCover
|
||||
)
|
||||
# Manually register as cover since we didn't use new_cover
|
||||
await cover.register_cover(var, config)
|
||||
await cg.register_component(var, config)
|
||||
else:
|
||||
# No state lambda - just create the base template cover
|
||||
var = await cover.new_cover(config)
|
||||
await cg.register_component(var, config)
|
||||
|
||||
if CONF_OPEN_ACTION in config:
|
||||
await automation.build_automation(
|
||||
var.get_open_trigger(), [], config[CONF_OPEN_ACTION]
|
||||
|
||||
@@ -8,14 +8,8 @@ using namespace esphome::cover;
|
||||
|
||||
static const char *const TAG = "template.cover";
|
||||
|
||||
TemplateCover::TemplateCover()
|
||||
: open_trigger_(new Trigger<>()),
|
||||
close_trigger_(new Trigger<>),
|
||||
stop_trigger_(new Trigger<>()),
|
||||
toggle_trigger_(new Trigger<>()),
|
||||
position_trigger_(new Trigger<float>()),
|
||||
tilt_trigger_(new Trigger<float>()) {}
|
||||
void TemplateCover::setup() {
|
||||
// Template instantiations
|
||||
template<typename StateF, typename TiltF> void TemplateCoverBase<StateF, TiltF>::setup() {
|
||||
switch (this->restore_mode_) {
|
||||
case COVER_NO_RESTORE:
|
||||
break;
|
||||
@@ -34,43 +28,12 @@ void TemplateCover::setup() {
|
||||
}
|
||||
}
|
||||
}
|
||||
void TemplateCover::loop() {
|
||||
bool changed = false;
|
||||
|
||||
if (this->state_f_.has_value()) {
|
||||
auto s = (*this->state_f_)();
|
||||
if (s.has_value()) {
|
||||
auto pos = clamp(*s, 0.0f, 1.0f);
|
||||
if (pos != this->position) {
|
||||
this->position = pos;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (this->tilt_f_.has_value()) {
|
||||
auto s = (*this->tilt_f_)();
|
||||
if (s.has_value()) {
|
||||
auto tilt = clamp(*s, 0.0f, 1.0f);
|
||||
if (tilt != this->tilt) {
|
||||
this->tilt = tilt;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (changed)
|
||||
this->publish_state();
|
||||
template<typename StateF, typename TiltF> void TemplateCoverBase<StateF, TiltF>::dump_config() {
|
||||
LOG_COVER("", "Template Cover", this);
|
||||
}
|
||||
void TemplateCover::set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
|
||||
void TemplateCover::set_assumed_state(bool assumed_state) { this->assumed_state_ = assumed_state; }
|
||||
void TemplateCover::set_state_lambda(optional<float> (*f)()) { this->state_f_ = f; }
|
||||
float TemplateCover::get_setup_priority() const { return setup_priority::HARDWARE; }
|
||||
Trigger<> *TemplateCover::get_open_trigger() const { return this->open_trigger_; }
|
||||
Trigger<> *TemplateCover::get_close_trigger() const { return this->close_trigger_; }
|
||||
Trigger<> *TemplateCover::get_stop_trigger() const { return this->stop_trigger_; }
|
||||
Trigger<> *TemplateCover::get_toggle_trigger() const { return this->toggle_trigger_; }
|
||||
void TemplateCover::dump_config() { LOG_COVER("", "Template Cover", this); }
|
||||
void TemplateCover::control(const CoverCall &call) {
|
||||
|
||||
template<typename StateF, typename TiltF> void TemplateCoverBase<StateF, TiltF>::control(const CoverCall &call) {
|
||||
if (call.get_stop()) {
|
||||
this->stop_prev_trigger_();
|
||||
this->stop_trigger_->trigger();
|
||||
@@ -113,7 +76,8 @@ void TemplateCover::control(const CoverCall &call) {
|
||||
|
||||
this->publish_state();
|
||||
}
|
||||
CoverTraits TemplateCover::get_traits() {
|
||||
|
||||
template<typename StateF, typename TiltF> CoverTraits TemplateCoverBase<StateF, TiltF>::get_traits() {
|
||||
auto traits = CoverTraits();
|
||||
traits.set_is_assumed_state(this->assumed_state_);
|
||||
traits.set_supports_stop(this->has_stop_);
|
||||
@@ -122,19 +86,16 @@ CoverTraits TemplateCover::get_traits() {
|
||||
traits.set_supports_tilt(this->has_tilt_);
|
||||
return traits;
|
||||
}
|
||||
Trigger<float> *TemplateCover::get_position_trigger() const { return this->position_trigger_; }
|
||||
Trigger<float> *TemplateCover::get_tilt_trigger() const { return this->tilt_trigger_; }
|
||||
void TemplateCover::set_tilt_lambda(optional<float> (*tilt_f)()) { this->tilt_f_ = tilt_f; }
|
||||
void TemplateCover::set_has_stop(bool has_stop) { this->has_stop_ = has_stop; }
|
||||
void TemplateCover::set_has_toggle(bool has_toggle) { this->has_toggle_ = has_toggle; }
|
||||
void TemplateCover::set_has_position(bool has_position) { this->has_position_ = has_position; }
|
||||
void TemplateCover::set_has_tilt(bool has_tilt) { this->has_tilt_ = has_tilt; }
|
||||
void TemplateCover::stop_prev_trigger_() {
|
||||
|
||||
template<typename StateF, typename TiltF> void TemplateCoverBase<StateF, TiltF>::stop_prev_trigger_() {
|
||||
if (this->prev_command_trigger_ != nullptr) {
|
||||
this->prev_command_trigger_->stop_action();
|
||||
this->prev_command_trigger_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
template class TemplateCoverBase<std::function<optional<float>()>, std::function<optional<float>()>>;
|
||||
template class TemplateCoverBase<optional<float> (*)(), optional<float> (*)()>;
|
||||
|
||||
} // namespace template_
|
||||
} // namespace esphome
|
||||
|
||||
@@ -13,31 +13,59 @@ enum TemplateCoverRestoreMode {
|
||||
COVER_RESTORE_AND_CALL,
|
||||
};
|
||||
|
||||
class TemplateCover : public cover::Cover, public Component {
|
||||
template<typename StateF, typename TiltF> class TemplateCoverBase : public cover::Cover, public Component {
|
||||
public:
|
||||
TemplateCover();
|
||||
TemplateCoverBase()
|
||||
: open_trigger_(new Trigger<>()),
|
||||
close_trigger_(new Trigger<>()),
|
||||
stop_trigger_(new Trigger<>()),
|
||||
toggle_trigger_(new Trigger<>()),
|
||||
position_trigger_(new Trigger<float>()),
|
||||
tilt_trigger_(new Trigger<float>()) {}
|
||||
|
||||
void set_state_lambda(optional<float> (*f)());
|
||||
Trigger<> *get_open_trigger() const;
|
||||
Trigger<> *get_close_trigger() const;
|
||||
Trigger<> *get_stop_trigger() const;
|
||||
Trigger<> *get_toggle_trigger() const;
|
||||
Trigger<float> *get_position_trigger() const;
|
||||
Trigger<float> *get_tilt_trigger() const;
|
||||
void set_optimistic(bool optimistic);
|
||||
void set_assumed_state(bool assumed_state);
|
||||
void set_tilt_lambda(optional<float> (*tilt_f)());
|
||||
void set_has_stop(bool has_stop);
|
||||
void set_has_position(bool has_position);
|
||||
void set_has_tilt(bool has_tilt);
|
||||
void set_has_toggle(bool has_toggle);
|
||||
void set_restore_mode(TemplateCoverRestoreMode restore_mode) { restore_mode_ = restore_mode; }
|
||||
void loop() override {
|
||||
bool changed = false;
|
||||
if (this->state_f_.has_value()) {
|
||||
auto s = (*this->state_f_)();
|
||||
if (s.has_value()) {
|
||||
auto pos = clamp(*s, 0.0f, 1.0f);
|
||||
if (pos != this->position) {
|
||||
this->position = pos;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (this->tilt_f_.has_value()) {
|
||||
auto s = (*this->tilt_f_)();
|
||||
if (s.has_value()) {
|
||||
auto tilt = clamp(*s, 0.0f, 1.0f);
|
||||
if (tilt != this->tilt) {
|
||||
this->tilt = tilt;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (changed)
|
||||
this->publish_state();
|
||||
}
|
||||
|
||||
void setup() override;
|
||||
void loop() override;
|
||||
void dump_config() override;
|
||||
float get_setup_priority() const override { return setup_priority::HARDWARE; }
|
||||
|
||||
float get_setup_priority() const override;
|
||||
Trigger<> *get_open_trigger() const { return this->open_trigger_; }
|
||||
Trigger<> *get_close_trigger() const { return this->close_trigger_; }
|
||||
Trigger<> *get_stop_trigger() const { return this->stop_trigger_; }
|
||||
Trigger<> *get_toggle_trigger() const { return this->toggle_trigger_; }
|
||||
Trigger<float> *get_position_trigger() const { return this->position_trigger_; }
|
||||
Trigger<float> *get_tilt_trigger() const { return this->tilt_trigger_; }
|
||||
void set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
|
||||
void set_assumed_state(bool assumed_state) { this->assumed_state_ = assumed_state; }
|
||||
void set_has_stop(bool has_stop) { this->has_stop_ = has_stop; }
|
||||
void set_has_position(bool has_position) { this->has_position_ = has_position; }
|
||||
void set_has_tilt(bool has_tilt) { this->has_tilt_ = has_tilt; }
|
||||
void set_has_toggle(bool has_toggle) { this->has_toggle_ = has_toggle; }
|
||||
void set_restore_mode(TemplateCoverRestoreMode restore_mode) { restore_mode_ = restore_mode; }
|
||||
|
||||
protected:
|
||||
void control(const cover::CoverCall &call) override;
|
||||
@@ -45,8 +73,8 @@ class TemplateCover : public cover::Cover, public Component {
|
||||
void stop_prev_trigger_();
|
||||
|
||||
TemplateCoverRestoreMode restore_mode_{COVER_RESTORE};
|
||||
optional<optional<float> (*)()> state_f_;
|
||||
optional<optional<float> (*)()> tilt_f_;
|
||||
optional<StateF> state_f_;
|
||||
optional<TiltF> tilt_f_;
|
||||
bool assumed_state_{false};
|
||||
bool optimistic_{false};
|
||||
Trigger<> *open_trigger_;
|
||||
@@ -62,5 +90,22 @@ class TemplateCover : public cover::Cover, public Component {
|
||||
bool has_tilt_{false};
|
||||
};
|
||||
|
||||
class TemplateCover : public TemplateCoverBase<std::function<optional<float>()>, std::function<optional<float>()>> {
|
||||
public:
|
||||
void set_state_lambda(std::function<optional<float>()> &&f) { this->state_f_ = f; }
|
||||
void set_tilt_lambda(std::function<optional<float>()> &&tilt_f) { this->tilt_f_ = tilt_f; }
|
||||
};
|
||||
|
||||
/** Optimized template cover for stateless lambdas (no capture).
|
||||
*
|
||||
* Uses function pointers instead of std::function to reduce memory overhead.
|
||||
* Memory: 4 bytes (function pointer on 32-bit) vs 32 bytes (std::function) per lambda.
|
||||
*/
|
||||
class StatelessTemplateCover : public TemplateCoverBase<optional<float> (*)(), optional<float> (*)()> {
|
||||
public:
|
||||
explicit StatelessTemplateCover(optional<float> (*state_f)()) { this->state_f_ = state_f; }
|
||||
void set_tilt_lambda(optional<float> (*tilt_f)()) { this->tilt_f_ = tilt_f; }
|
||||
};
|
||||
|
||||
} // namespace template_
|
||||
} // namespace esphome
|
||||
|
||||
@@ -5,6 +5,7 @@ import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
CONF_DAY,
|
||||
CONF_HOUR,
|
||||
CONF_ID,
|
||||
CONF_INITIAL_VALUE,
|
||||
CONF_LAMBDA,
|
||||
CONF_MINUTE,
|
||||
@@ -25,14 +26,23 @@ CODEOWNERS = ["@rfdarter"]
|
||||
TemplateDate = template_ns.class_(
|
||||
"TemplateDate", datetime.DateEntity, cg.PollingComponent
|
||||
)
|
||||
StatelessTemplateDate = template_ns.class_(
|
||||
"StatelessTemplateDate", datetime.DateEntity, cg.PollingComponent
|
||||
)
|
||||
|
||||
TemplateTime = template_ns.class_(
|
||||
"TemplateTime", datetime.TimeEntity, cg.PollingComponent
|
||||
)
|
||||
StatelessTemplateTime = template_ns.class_(
|
||||
"StatelessTemplateTime", datetime.TimeEntity, cg.PollingComponent
|
||||
)
|
||||
|
||||
TemplateDateTime = template_ns.class_(
|
||||
"TemplateDateTime", datetime.DateTimeEntity, cg.PollingComponent
|
||||
)
|
||||
StatelessTemplateDateTime = template_ns.class_(
|
||||
"StatelessTemplateDateTime", datetime.DateTimeEntity, cg.PollingComponent
|
||||
)
|
||||
|
||||
|
||||
def validate(config):
|
||||
@@ -99,15 +109,30 @@ CONFIG_SCHEMA = cv.All(
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = await datetime.new_datetime(config)
|
||||
|
||||
if CONF_LAMBDA in config:
|
||||
# Use new_lambda_pvariable to create either Template* or StatelessTemplate*
|
||||
template_ = await cg.process_lambda(
|
||||
config[CONF_LAMBDA], [], return_type=cg.optional.template(cg.ESPTime)
|
||||
)
|
||||
cg.add(var.set_template(template_))
|
||||
# Determine the appropriate stateless class based on type
|
||||
if config[CONF_TYPE] == "DATE":
|
||||
stateless_class = StatelessTemplateDate
|
||||
elif config[CONF_TYPE] == "TIME":
|
||||
stateless_class = StatelessTemplateTime
|
||||
else: # DATETIME
|
||||
stateless_class = StatelessTemplateDateTime
|
||||
|
||||
var = automation.new_lambda_pvariable(
|
||||
config[CONF_ID], template_, stateless_class
|
||||
)
|
||||
# Manually register as datetime since we didn't use new_datetime
|
||||
await datetime.register_datetime(var, config)
|
||||
await cg.register_component(var, config)
|
||||
else:
|
||||
# No lambda - just create the base template datetime
|
||||
var = await datetime.new_datetime(config)
|
||||
await cg.register_component(var, config)
|
||||
|
||||
cg.add(var.set_optimistic(config[CONF_OPTIMISTIC]))
|
||||
cg.add(var.set_restore_value(config[CONF_RESTORE_VALUE]))
|
||||
|
||||
@@ -146,5 +171,3 @@ async def to_code(config):
|
||||
[(cg.ESPTime, "x")],
|
||||
config[CONF_SET_ACTION],
|
||||
)
|
||||
|
||||
await cg.register_component(var, config)
|
||||
|
||||
@@ -9,7 +9,8 @@ namespace template_ {
|
||||
|
||||
static const char *const TAG = "template.date";
|
||||
|
||||
void TemplateDate::setup() {
|
||||
// Template instantiations
|
||||
template<typename F> void TemplateDateBase<F>::setup() {
|
||||
if (this->f_.has_value())
|
||||
return;
|
||||
|
||||
@@ -36,21 +37,7 @@ void TemplateDate::setup() {
|
||||
this->publish_state();
|
||||
}
|
||||
|
||||
void TemplateDate::update() {
|
||||
if (!this->f_.has_value())
|
||||
return;
|
||||
|
||||
auto val = (*this->f_)();
|
||||
if (!val.has_value())
|
||||
return;
|
||||
|
||||
this->year_ = val->year;
|
||||
this->month_ = val->month;
|
||||
this->day_ = val->day_of_month;
|
||||
this->publish_state();
|
||||
}
|
||||
|
||||
void TemplateDate::control(const datetime::DateCall &call) {
|
||||
template<typename F> void TemplateDateBase<F>::control(const datetime::DateCall &call) {
|
||||
bool has_year = call.get_year().has_value();
|
||||
bool has_month = call.get_month().has_value();
|
||||
bool has_day = call.get_day().has_value();
|
||||
@@ -99,12 +86,15 @@ void TemplateDate::control(const datetime::DateCall &call) {
|
||||
}
|
||||
}
|
||||
|
||||
void TemplateDate::dump_config() {
|
||||
template<typename F> void TemplateDateBase<F>::dump_config() {
|
||||
LOG_DATETIME_DATE("", "Template Date", this);
|
||||
ESP_LOGCONFIG(TAG, " Optimistic: %s", YESNO(this->optimistic_));
|
||||
LOG_UPDATE_INTERVAL(this);
|
||||
}
|
||||
|
||||
template class TemplateDateBase<std::function<optional<ESPTime>()>>;
|
||||
template class TemplateDateBase<optional<ESPTime> (*)()>;
|
||||
|
||||
} // namespace template_
|
||||
} // namespace esphome
|
||||
|
||||
|
||||
@@ -13,12 +13,23 @@
|
||||
namespace esphome {
|
||||
namespace template_ {
|
||||
|
||||
class TemplateDate : public datetime::DateEntity, public PollingComponent {
|
||||
template<typename F> class TemplateDateBase : public datetime::DateEntity, public PollingComponent {
|
||||
public:
|
||||
void set_template(optional<ESPTime> (*f)()) { this->f_ = f; }
|
||||
void update() override {
|
||||
if (!this->f_.has_value())
|
||||
return;
|
||||
|
||||
auto val = (*this->f_)();
|
||||
if (!val.has_value())
|
||||
return;
|
||||
|
||||
this->year_ = val->year;
|
||||
this->month_ = val->month;
|
||||
this->day_ = val->day_of_month;
|
||||
this->publish_state();
|
||||
}
|
||||
|
||||
void setup() override;
|
||||
void update() override;
|
||||
void dump_config() override;
|
||||
float get_setup_priority() const override { return setup_priority::HARDWARE; }
|
||||
|
||||
@@ -35,11 +46,26 @@ class TemplateDate : public datetime::DateEntity, public PollingComponent {
|
||||
ESPTime initial_value_{};
|
||||
bool restore_value_{false};
|
||||
Trigger<ESPTime> *set_trigger_ = new Trigger<ESPTime>();
|
||||
optional<optional<ESPTime> (*)()> f_;
|
||||
optional<F> f_;
|
||||
|
||||
ESPPreferenceObject pref_;
|
||||
};
|
||||
|
||||
class TemplateDate : public TemplateDateBase<std::function<optional<ESPTime>()>> {
|
||||
public:
|
||||
void set_template(std::function<optional<ESPTime>()> &&f) { this->f_ = f; }
|
||||
};
|
||||
|
||||
/** Optimized template date for stateless lambdas (no capture).
|
||||
*
|
||||
* Uses function pointers instead of std::function to reduce memory overhead.
|
||||
* Memory: 4 bytes (function pointer on 32-bit) vs 32 bytes (std::function) per lambda.
|
||||
*/
|
||||
class StatelessTemplateDate : public TemplateDateBase<optional<ESPTime> (*)()> {
|
||||
public:
|
||||
explicit StatelessTemplateDate(optional<ESPTime> (*f)()) { this->f_ = f; }
|
||||
};
|
||||
|
||||
} // namespace template_
|
||||
} // namespace esphome
|
||||
|
||||
|
||||
@@ -9,7 +9,8 @@ namespace template_ {
|
||||
|
||||
static const char *const TAG = "template.datetime";
|
||||
|
||||
void TemplateDateTime::setup() {
|
||||
// Template instantiations
|
||||
template<typename F> void TemplateDateTimeBase<F>::setup() {
|
||||
if (this->f_.has_value())
|
||||
return;
|
||||
|
||||
@@ -39,24 +40,7 @@ void TemplateDateTime::setup() {
|
||||
this->publish_state();
|
||||
}
|
||||
|
||||
void TemplateDateTime::update() {
|
||||
if (!this->f_.has_value())
|
||||
return;
|
||||
|
||||
auto val = (*this->f_)();
|
||||
if (!val.has_value())
|
||||
return;
|
||||
|
||||
this->year_ = val->year;
|
||||
this->month_ = val->month;
|
||||
this->day_ = val->day_of_month;
|
||||
this->hour_ = val->hour;
|
||||
this->minute_ = val->minute;
|
||||
this->second_ = val->second;
|
||||
this->publish_state();
|
||||
}
|
||||
|
||||
void TemplateDateTime::control(const datetime::DateTimeCall &call) {
|
||||
template<typename F> void TemplateDateTimeBase<F>::control(const datetime::DateTimeCall &call) {
|
||||
bool has_year = call.get_year().has_value();
|
||||
bool has_month = call.get_month().has_value();
|
||||
bool has_day = call.get_day().has_value();
|
||||
@@ -138,12 +122,15 @@ void TemplateDateTime::control(const datetime::DateTimeCall &call) {
|
||||
}
|
||||
}
|
||||
|
||||
void TemplateDateTime::dump_config() {
|
||||
template<typename F> void TemplateDateTimeBase<F>::dump_config() {
|
||||
LOG_DATETIME_DATETIME("", "Template DateTime", this);
|
||||
ESP_LOGCONFIG(TAG, " Optimistic: %s", YESNO(this->optimistic_));
|
||||
LOG_UPDATE_INTERVAL(this);
|
||||
}
|
||||
|
||||
template class TemplateDateTimeBase<std::function<optional<ESPTime>()>>;
|
||||
template class TemplateDateTimeBase<optional<ESPTime> (*)()>;
|
||||
|
||||
} // namespace template_
|
||||
} // namespace esphome
|
||||
|
||||
|
||||
@@ -13,12 +13,26 @@
|
||||
namespace esphome {
|
||||
namespace template_ {
|
||||
|
||||
class TemplateDateTime : public datetime::DateTimeEntity, public PollingComponent {
|
||||
template<typename F> class TemplateDateTimeBase : public datetime::DateTimeEntity, public PollingComponent {
|
||||
public:
|
||||
void set_template(optional<ESPTime> (*f)()) { this->f_ = f; }
|
||||
void update() override {
|
||||
if (!this->f_.has_value())
|
||||
return;
|
||||
|
||||
auto val = (*this->f_)();
|
||||
if (!val.has_value())
|
||||
return;
|
||||
|
||||
this->year_ = val->year;
|
||||
this->month_ = val->month;
|
||||
this->day_ = val->day_of_month;
|
||||
this->hour_ = val->hour;
|
||||
this->minute_ = val->minute;
|
||||
this->second_ = val->second;
|
||||
this->publish_state();
|
||||
}
|
||||
|
||||
void setup() override;
|
||||
void update() override;
|
||||
void dump_config() override;
|
||||
float get_setup_priority() const override { return setup_priority::HARDWARE; }
|
||||
|
||||
@@ -35,11 +49,26 @@ class TemplateDateTime : public datetime::DateTimeEntity, public PollingComponen
|
||||
ESPTime initial_value_{};
|
||||
bool restore_value_{false};
|
||||
Trigger<ESPTime> *set_trigger_ = new Trigger<ESPTime>();
|
||||
optional<optional<ESPTime> (*)()> f_;
|
||||
optional<F> f_;
|
||||
|
||||
ESPPreferenceObject pref_;
|
||||
};
|
||||
|
||||
class TemplateDateTime : public TemplateDateTimeBase<std::function<optional<ESPTime>()>> {
|
||||
public:
|
||||
void set_template(std::function<optional<ESPTime>()> &&f) { this->f_ = f; }
|
||||
};
|
||||
|
||||
/** Optimized template datetime for stateless lambdas (no capture).
|
||||
*
|
||||
* Uses function pointers instead of std::function to reduce memory overhead.
|
||||
* Memory: 4 bytes (function pointer on 32-bit) vs 32 bytes (std::function) per lambda.
|
||||
*/
|
||||
class StatelessTemplateDateTime : public TemplateDateTimeBase<optional<ESPTime> (*)()> {
|
||||
public:
|
||||
explicit StatelessTemplateDateTime(optional<ESPTime> (*f)()) { this->f_ = f; }
|
||||
};
|
||||
|
||||
} // namespace template_
|
||||
} // namespace esphome
|
||||
|
||||
|
||||
@@ -9,7 +9,8 @@ namespace template_ {
|
||||
|
||||
static const char *const TAG = "template.time";
|
||||
|
||||
void TemplateTime::setup() {
|
||||
// Template instantiations
|
||||
template<typename F> void TemplateTimeBase<F>::setup() {
|
||||
if (this->f_.has_value())
|
||||
return;
|
||||
|
||||
@@ -36,21 +37,7 @@ void TemplateTime::setup() {
|
||||
this->publish_state();
|
||||
}
|
||||
|
||||
void TemplateTime::update() {
|
||||
if (!this->f_.has_value())
|
||||
return;
|
||||
|
||||
auto val = (*this->f_)();
|
||||
if (!val.has_value())
|
||||
return;
|
||||
|
||||
this->hour_ = val->hour;
|
||||
this->minute_ = val->minute;
|
||||
this->second_ = val->second;
|
||||
this->publish_state();
|
||||
}
|
||||
|
||||
void TemplateTime::control(const datetime::TimeCall &call) {
|
||||
template<typename F> void TemplateTimeBase<F>::control(const datetime::TimeCall &call) {
|
||||
bool has_hour = call.get_hour().has_value();
|
||||
bool has_minute = call.get_minute().has_value();
|
||||
bool has_second = call.get_second().has_value();
|
||||
@@ -99,12 +86,15 @@ void TemplateTime::control(const datetime::TimeCall &call) {
|
||||
}
|
||||
}
|
||||
|
||||
void TemplateTime::dump_config() {
|
||||
template<typename F> void TemplateTimeBase<F>::dump_config() {
|
||||
LOG_DATETIME_TIME("", "Template Time", this);
|
||||
ESP_LOGCONFIG(TAG, " Optimistic: %s", YESNO(this->optimistic_));
|
||||
LOG_UPDATE_INTERVAL(this);
|
||||
}
|
||||
|
||||
template class TemplateTimeBase<std::function<optional<ESPTime>()>>;
|
||||
template class TemplateTimeBase<optional<ESPTime> (*)()>;
|
||||
|
||||
} // namespace template_
|
||||
} // namespace esphome
|
||||
|
||||
|
||||
@@ -13,12 +13,23 @@
|
||||
namespace esphome {
|
||||
namespace template_ {
|
||||
|
||||
class TemplateTime : public datetime::TimeEntity, public PollingComponent {
|
||||
template<typename F> class TemplateTimeBase : public datetime::TimeEntity, public PollingComponent {
|
||||
public:
|
||||
void set_template(optional<ESPTime> (*f)()) { this->f_ = f; }
|
||||
void update() override {
|
||||
if (!this->f_.has_value())
|
||||
return;
|
||||
|
||||
auto val = (*this->f_)();
|
||||
if (!val.has_value())
|
||||
return;
|
||||
|
||||
this->hour_ = val->hour;
|
||||
this->minute_ = val->minute;
|
||||
this->second_ = val->second;
|
||||
this->publish_state();
|
||||
}
|
||||
|
||||
void setup() override;
|
||||
void update() override;
|
||||
void dump_config() override;
|
||||
float get_setup_priority() const override { return setup_priority::HARDWARE; }
|
||||
|
||||
@@ -35,11 +46,26 @@ class TemplateTime : public datetime::TimeEntity, public PollingComponent {
|
||||
ESPTime initial_value_{};
|
||||
bool restore_value_{false};
|
||||
Trigger<ESPTime> *set_trigger_ = new Trigger<ESPTime>();
|
||||
optional<optional<ESPTime> (*)()> f_;
|
||||
optional<F> f_;
|
||||
|
||||
ESPPreferenceObject pref_;
|
||||
};
|
||||
|
||||
class TemplateTime : public TemplateTimeBase<std::function<optional<ESPTime>()>> {
|
||||
public:
|
||||
void set_template(std::function<optional<ESPTime>()> &&f) { this->f_ = f; }
|
||||
};
|
||||
|
||||
/** Optimized template time for stateless lambdas (no capture).
|
||||
*
|
||||
* Uses function pointers instead of std::function to reduce memory overhead.
|
||||
* Memory: 4 bytes (function pointer on 32-bit) vs 32 bytes (std::function) per lambda.
|
||||
*/
|
||||
class StatelessTemplateTime : public TemplateTimeBase<optional<ESPTime> (*)()> {
|
||||
public:
|
||||
explicit StatelessTemplateTime(optional<ESPTime> (*f)()) { this->f_ = f; }
|
||||
};
|
||||
|
||||
} // namespace template_
|
||||
} // namespace esphome
|
||||
|
||||
|
||||
@@ -16,6 +16,9 @@ from esphome.const import (
|
||||
from .. import template_ns
|
||||
|
||||
TemplateLock = template_ns.class_("TemplateLock", lock.Lock, cg.Component)
|
||||
StatelessTemplateLock = template_ns.class_(
|
||||
"StatelessTemplateLock", lock.Lock, cg.Component
|
||||
)
|
||||
|
||||
TemplateLockPublishAction = template_ns.class_(
|
||||
"TemplateLockPublishAction",
|
||||
@@ -55,14 +58,22 @@ CONFIG_SCHEMA = cv.All(
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = await lock.new_lock(config)
|
||||
await cg.register_component(var, config)
|
||||
|
||||
if CONF_LAMBDA in config:
|
||||
# Use new_lambda_pvariable to create either TemplateLock or StatelessTemplateLock
|
||||
template_ = await cg.process_lambda(
|
||||
config[CONF_LAMBDA], [], return_type=cg.optional.template(lock.LockState)
|
||||
)
|
||||
cg.add(var.set_state_lambda(template_))
|
||||
var = automation.new_lambda_pvariable(
|
||||
config[CONF_ID], template_, StatelessTemplateLock
|
||||
)
|
||||
# Manually register as lock since we didn't use new_lock
|
||||
await lock.register_lock(var, config)
|
||||
await cg.register_component(var, config)
|
||||
else:
|
||||
# No lambda - just create the base template lock
|
||||
var = await lock.new_lock(config)
|
||||
await cg.register_component(var, config)
|
||||
|
||||
if CONF_UNLOCK_ACTION in config:
|
||||
await automation.build_automation(
|
||||
var.get_unlock_trigger(), [], config[CONF_UNLOCK_ACTION]
|
||||
|
||||
@@ -8,19 +8,8 @@ using namespace esphome::lock;
|
||||
|
||||
static const char *const TAG = "template.lock";
|
||||
|
||||
TemplateLock::TemplateLock()
|
||||
: lock_trigger_(new Trigger<>()), unlock_trigger_(new Trigger<>()), open_trigger_(new Trigger<>()) {}
|
||||
|
||||
void TemplateLock::loop() {
|
||||
if (!this->f_.has_value())
|
||||
return;
|
||||
auto val = (*this->f_)();
|
||||
if (!val.has_value())
|
||||
return;
|
||||
|
||||
this->publish_state(*val);
|
||||
}
|
||||
void TemplateLock::control(const lock::LockCall &call) {
|
||||
// Template instantiations
|
||||
template<typename F> void TemplateLockBase<F>::control(const lock::LockCall &call) {
|
||||
if (this->prev_trigger_ != nullptr) {
|
||||
this->prev_trigger_->stop_action();
|
||||
}
|
||||
@@ -37,23 +26,22 @@ void TemplateLock::control(const lock::LockCall &call) {
|
||||
if (this->optimistic_)
|
||||
this->publish_state(state);
|
||||
}
|
||||
void TemplateLock::open_latch() {
|
||||
|
||||
template<typename F> void TemplateLockBase<F>::open_latch() {
|
||||
if (this->prev_trigger_ != nullptr) {
|
||||
this->prev_trigger_->stop_action();
|
||||
}
|
||||
this->prev_trigger_ = this->open_trigger_;
|
||||
this->open_trigger_->trigger();
|
||||
}
|
||||
void TemplateLock::set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
|
||||
void TemplateLock::set_state_lambda(optional<lock::LockState> (*f)()) { this->f_ = f; }
|
||||
float TemplateLock::get_setup_priority() const { return setup_priority::HARDWARE; }
|
||||
Trigger<> *TemplateLock::get_lock_trigger() const { return this->lock_trigger_; }
|
||||
Trigger<> *TemplateLock::get_unlock_trigger() const { return this->unlock_trigger_; }
|
||||
Trigger<> *TemplateLock::get_open_trigger() const { return this->open_trigger_; }
|
||||
void TemplateLock::dump_config() {
|
||||
|
||||
template<typename F> void TemplateLockBase<F>::dump_config() {
|
||||
LOG_LOCK("", "Template Lock", this);
|
||||
ESP_LOGCONFIG(TAG, " Optimistic: %s", YESNO(this->optimistic_));
|
||||
}
|
||||
|
||||
template class TemplateLockBase<std::function<optional<lock::LockState>()>>;
|
||||
template class TemplateLockBase<optional<lock::LockState> (*)()>;
|
||||
|
||||
} // namespace template_
|
||||
} // namespace esphome
|
||||
|
||||
@@ -7,26 +7,35 @@
|
||||
namespace esphome {
|
||||
namespace template_ {
|
||||
|
||||
class TemplateLock : public lock::Lock, public Component {
|
||||
template<typename F> class TemplateLockBase : public lock::Lock, public Component {
|
||||
public:
|
||||
TemplateLock();
|
||||
TemplateLockBase()
|
||||
: lock_trigger_(new Trigger<>()), unlock_trigger_(new Trigger<>()), open_trigger_(new Trigger<>()) {}
|
||||
|
||||
void loop() override {
|
||||
if (!this->f_.has_value())
|
||||
return;
|
||||
auto val = (*this->f_)();
|
||||
if (!val.has_value())
|
||||
return;
|
||||
|
||||
this->publish_state(*val);
|
||||
}
|
||||
|
||||
void dump_config() override;
|
||||
|
||||
void set_state_lambda(optional<lock::LockState> (*f)());
|
||||
Trigger<> *get_lock_trigger() const;
|
||||
Trigger<> *get_unlock_trigger() const;
|
||||
Trigger<> *get_open_trigger() const;
|
||||
void set_optimistic(bool optimistic);
|
||||
void loop() override;
|
||||
Trigger<> *get_lock_trigger() const { return this->lock_trigger_; }
|
||||
Trigger<> *get_unlock_trigger() const { return this->unlock_trigger_; }
|
||||
Trigger<> *get_open_trigger() const { return this->open_trigger_; }
|
||||
void set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
|
||||
|
||||
float get_setup_priority() const override;
|
||||
float get_setup_priority() const override { return setup_priority::HARDWARE; }
|
||||
|
||||
protected:
|
||||
void control(const lock::LockCall &call) override;
|
||||
void open_latch() override;
|
||||
|
||||
optional<optional<lock::LockState> (*)()> f_;
|
||||
optional<F> f_;
|
||||
bool optimistic_{false};
|
||||
Trigger<> *lock_trigger_;
|
||||
Trigger<> *unlock_trigger_;
|
||||
@@ -34,5 +43,20 @@ class TemplateLock : public lock::Lock, public Component {
|
||||
Trigger<> *prev_trigger_{nullptr};
|
||||
};
|
||||
|
||||
class TemplateLock : public TemplateLockBase<std::function<optional<lock::LockState>()>> {
|
||||
public:
|
||||
void set_state_lambda(std::function<optional<lock::LockState>()> &&f) { this->f_ = f; }
|
||||
};
|
||||
|
||||
/** Optimized template lock for stateless lambdas (no capture).
|
||||
*
|
||||
* Uses function pointers instead of std::function to reduce memory overhead.
|
||||
* Memory: 4 bytes (function pointer on 32-bit) vs 32 bytes (std::function) per lambda.
|
||||
*/
|
||||
class StatelessTemplateLock : public TemplateLockBase<optional<lock::LockState> (*)()> {
|
||||
public:
|
||||
explicit StatelessTemplateLock(optional<lock::LockState> (*f)()) { this->f_ = f; }
|
||||
};
|
||||
|
||||
} // namespace template_
|
||||
} // namespace esphome
|
||||
|
||||
@@ -19,6 +19,9 @@ from .. import template_ns
|
||||
TemplateNumber = template_ns.class_(
|
||||
"TemplateNumber", number.Number, cg.PollingComponent
|
||||
)
|
||||
StatelessTemplateNumber = template_ns.class_(
|
||||
"StatelessTemplateNumber", number.Number, cg.PollingComponent
|
||||
)
|
||||
|
||||
|
||||
def validate_min_max(config):
|
||||
@@ -66,23 +69,33 @@ CONFIG_SCHEMA = cv.All(
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(var, config)
|
||||
await number.register_number(
|
||||
var,
|
||||
config,
|
||||
min_value=config[CONF_MIN_VALUE],
|
||||
max_value=config[CONF_MAX_VALUE],
|
||||
step=config[CONF_STEP],
|
||||
)
|
||||
|
||||
if CONF_LAMBDA in config:
|
||||
# Use new_lambda_pvariable to create either TemplateNumber or StatelessTemplateNumber
|
||||
template_ = await cg.process_lambda(
|
||||
config[CONF_LAMBDA], [], return_type=cg.optional.template(float)
|
||||
)
|
||||
cg.add(var.set_template(template_))
|
||||
|
||||
var = automation.new_lambda_pvariable(
|
||||
config[CONF_ID], template_, StatelessTemplateNumber
|
||||
)
|
||||
await cg.register_component(var, config)
|
||||
await number.register_number(
|
||||
var,
|
||||
config,
|
||||
min_value=config[CONF_MIN_VALUE],
|
||||
max_value=config[CONF_MAX_VALUE],
|
||||
step=config[CONF_STEP],
|
||||
)
|
||||
else:
|
||||
# No lambda - just create the base template number
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(var, config)
|
||||
await number.register_number(
|
||||
var,
|
||||
config,
|
||||
min_value=config[CONF_MIN_VALUE],
|
||||
max_value=config[CONF_MAX_VALUE],
|
||||
step=config[CONF_STEP],
|
||||
)
|
||||
cg.add(var.set_optimistic(config[CONF_OPTIMISTIC]))
|
||||
cg.add(var.set_initial_value(config[CONF_INITIAL_VALUE]))
|
||||
if CONF_RESTORE_VALUE in config:
|
||||
|
||||
@@ -6,7 +6,8 @@ namespace template_ {
|
||||
|
||||
static const char *const TAG = "template.number";
|
||||
|
||||
void TemplateNumber::setup() {
|
||||
// Template instantiations
|
||||
template<typename F> void TemplateNumberBase<F>::setup() {
|
||||
if (this->f_.has_value())
|
||||
return;
|
||||
|
||||
@@ -26,18 +27,7 @@ void TemplateNumber::setup() {
|
||||
this->publish_state(value);
|
||||
}
|
||||
|
||||
void TemplateNumber::update() {
|
||||
if (!this->f_.has_value())
|
||||
return;
|
||||
|
||||
auto val = (*this->f_)();
|
||||
if (!val.has_value())
|
||||
return;
|
||||
|
||||
this->publish_state(*val);
|
||||
}
|
||||
|
||||
void TemplateNumber::control(float value) {
|
||||
template<typename F> void TemplateNumberBase<F>::control(float value) {
|
||||
this->set_trigger_->trigger(value);
|
||||
|
||||
if (this->optimistic_)
|
||||
@@ -46,11 +36,15 @@ void TemplateNumber::control(float value) {
|
||||
if (this->restore_value_)
|
||||
this->pref_.save(&value);
|
||||
}
|
||||
void TemplateNumber::dump_config() {
|
||||
|
||||
template<typename F> void TemplateNumberBase<F>::dump_config() {
|
||||
LOG_NUMBER("", "Template Number", this);
|
||||
ESP_LOGCONFIG(TAG, " Optimistic: %s", YESNO(this->optimistic_));
|
||||
LOG_UPDATE_INTERVAL(this);
|
||||
}
|
||||
|
||||
template class TemplateNumberBase<std::function<optional<float>()>>;
|
||||
template class TemplateNumberBase<optional<float> (*)()>;
|
||||
|
||||
} // namespace template_
|
||||
} // namespace esphome
|
||||
|
||||
@@ -8,13 +8,22 @@
|
||||
namespace esphome {
|
||||
namespace template_ {
|
||||
|
||||
class TemplateNumber : public number::Number, public PollingComponent {
|
||||
template<typename F> class TemplateNumberBase : public number::Number, public PollingComponent {
|
||||
public:
|
||||
void set_template(optional<float> (*f)()) { this->f_ = f; }
|
||||
TemplateNumberBase() : set_trigger_(new Trigger<float>()) {}
|
||||
|
||||
void setup() override;
|
||||
void update() override;
|
||||
void dump_config() override;
|
||||
|
||||
void update() override {
|
||||
if (!this->f_.has_value())
|
||||
return;
|
||||
auto val = (*this->f_)();
|
||||
if (!val.has_value())
|
||||
return;
|
||||
this->publish_state(*val);
|
||||
}
|
||||
|
||||
float get_setup_priority() const override { return setup_priority::HARDWARE; }
|
||||
|
||||
Trigger<float> *get_set_trigger() const { return set_trigger_; }
|
||||
@@ -27,11 +36,26 @@ class TemplateNumber : public number::Number, public PollingComponent {
|
||||
bool optimistic_{false};
|
||||
float initial_value_{NAN};
|
||||
bool restore_value_{false};
|
||||
Trigger<float> *set_trigger_ = new Trigger<float>();
|
||||
optional<optional<float> (*)()> f_;
|
||||
Trigger<float> *set_trigger_;
|
||||
optional<F> f_;
|
||||
|
||||
ESPPreferenceObject pref_;
|
||||
};
|
||||
|
||||
class TemplateNumber : public TemplateNumberBase<std::function<optional<float>()>> {
|
||||
public:
|
||||
void set_template(std::function<optional<float>()> &&f) { this->f_ = f; }
|
||||
};
|
||||
|
||||
/** Optimized template number for stateless lambdas (no capture).
|
||||
*
|
||||
* Uses function pointer instead of std::function to reduce memory overhead.
|
||||
* Memory: 4 bytes (function pointer on 32-bit) vs 32 bytes (std::function).
|
||||
*/
|
||||
class StatelessTemplateNumber : public TemplateNumberBase<optional<float> (*)()> {
|
||||
public:
|
||||
explicit StatelessTemplateNumber(optional<float> (*f)()) { this->f_ = f; }
|
||||
};
|
||||
|
||||
} // namespace template_
|
||||
} // namespace esphome
|
||||
|
||||
@@ -17,6 +17,9 @@ from .. import template_ns
|
||||
TemplateSelect = template_ns.class_(
|
||||
"TemplateSelect", select.Select, cg.PollingComponent
|
||||
)
|
||||
StatelessTemplateSelect = template_ns.class_(
|
||||
"StatelessTemplateSelect", select.Select, cg.PollingComponent
|
||||
)
|
||||
|
||||
|
||||
def validate(config):
|
||||
@@ -62,17 +65,22 @@ CONFIG_SCHEMA = cv.All(
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(var, config)
|
||||
await select.register_select(var, config, options=config[CONF_OPTIONS])
|
||||
|
||||
if CONF_LAMBDA in config:
|
||||
# Use new_lambda_pvariable to create either TemplateSelect or StatelessTemplateSelect
|
||||
template_ = await cg.process_lambda(
|
||||
config[CONF_LAMBDA], [], return_type=cg.optional.template(cg.std_string)
|
||||
)
|
||||
cg.add(var.set_template(template_))
|
||||
|
||||
var = automation.new_lambda_pvariable(
|
||||
config[CONF_ID], template_, StatelessTemplateSelect
|
||||
)
|
||||
await cg.register_component(var, config)
|
||||
await select.register_select(var, config, options=config[CONF_OPTIONS])
|
||||
else:
|
||||
# No lambda - just create the base template select
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(var, config)
|
||||
await select.register_select(var, config, options=config[CONF_OPTIONS])
|
||||
|
||||
# Only set if non-default to avoid bloating setup() function
|
||||
if config[CONF_OPTIMISTIC]:
|
||||
cg.add(var.set_optimistic(True))
|
||||
|
||||
@@ -6,7 +6,8 @@ namespace template_ {
|
||||
|
||||
static const char *const TAG = "template.select";
|
||||
|
||||
void TemplateSelect::setup() {
|
||||
// Template instantiations
|
||||
template<typename F> void TemplateSelectBase<F>::setup() {
|
||||
if (this->f_.has_value())
|
||||
return;
|
||||
|
||||
@@ -16,34 +17,18 @@ void TemplateSelect::setup() {
|
||||
size_t restored_index;
|
||||
if (this->pref_.load(&restored_index) && this->has_index(restored_index)) {
|
||||
index = restored_index;
|
||||
ESP_LOGD(TAG, "State from restore: %s", this->option_at(index));
|
||||
ESP_LOGD(TAG, "State from restore: %s", this->at(index).value().c_str());
|
||||
} else {
|
||||
ESP_LOGD(TAG, "State from initial (could not load or invalid stored index): %s", this->option_at(index));
|
||||
ESP_LOGD(TAG, "State from initial (could not load or invalid stored index): %s", this->at(index).value().c_str());
|
||||
}
|
||||
} else {
|
||||
ESP_LOGD(TAG, "State from initial: %s", this->option_at(index));
|
||||
ESP_LOGD(TAG, "State from initial: %s", this->at(index).value().c_str());
|
||||
}
|
||||
|
||||
this->publish_state(this->at(index).value());
|
||||
}
|
||||
|
||||
void TemplateSelect::update() {
|
||||
if (!this->f_.has_value())
|
||||
return;
|
||||
|
||||
auto val = (*this->f_)();
|
||||
if (!val.has_value())
|
||||
return;
|
||||
|
||||
if (!this->has_option(*val)) {
|
||||
ESP_LOGE(TAG, "Lambda returned an invalid option: %s", (*val).c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
this->publish_state(*val);
|
||||
}
|
||||
|
||||
void TemplateSelect::control(const std::string &value) {
|
||||
template<typename F> void TemplateSelectBase<F>::control(const std::string &value) {
|
||||
this->set_trigger_->trigger(value);
|
||||
|
||||
if (this->optimistic_)
|
||||
@@ -55,7 +40,7 @@ void TemplateSelect::control(const std::string &value) {
|
||||
}
|
||||
}
|
||||
|
||||
void TemplateSelect::dump_config() {
|
||||
template<typename F> void TemplateSelectBase<F>::dump_config() {
|
||||
LOG_SELECT("", "Template Select", this);
|
||||
LOG_UPDATE_INTERVAL(this);
|
||||
if (this->f_.has_value())
|
||||
@@ -64,8 +49,12 @@ void TemplateSelect::dump_config() {
|
||||
" Optimistic: %s\n"
|
||||
" Initial Option: %s\n"
|
||||
" Restore Value: %s",
|
||||
YESNO(this->optimistic_), this->option_at(this->initial_option_index_), YESNO(this->restore_value_));
|
||||
YESNO(this->optimistic_), this->at(this->initial_option_index_).value().c_str(),
|
||||
YESNO(this->restore_value_));
|
||||
}
|
||||
|
||||
template class TemplateSelectBase<std::function<optional<std::string>()>>;
|
||||
template class TemplateSelectBase<optional<std::string> (*)()>;
|
||||
|
||||
} // namespace template_
|
||||
} // namespace esphome
|
||||
|
||||
@@ -8,13 +8,26 @@
|
||||
namespace esphome {
|
||||
namespace template_ {
|
||||
|
||||
class TemplateSelect : public select::Select, public PollingComponent {
|
||||
template<typename F> class TemplateSelectBase : public select::Select, public PollingComponent {
|
||||
public:
|
||||
void set_template(optional<std::string> (*f)()) { this->f_ = f; }
|
||||
TemplateSelectBase() : set_trigger_(new Trigger<std::string>()) {}
|
||||
|
||||
void setup() override;
|
||||
void update() override;
|
||||
void dump_config() override;
|
||||
|
||||
void update() override {
|
||||
if (!this->f_.has_value())
|
||||
return;
|
||||
auto val = (*this->f_)();
|
||||
if (!val.has_value())
|
||||
return;
|
||||
if (!this->has_option(*val)) {
|
||||
ESP_LOGE("template.select", "Lambda returned an invalid option: %s", (*val).c_str());
|
||||
return;
|
||||
}
|
||||
this->publish_state(*val);
|
||||
}
|
||||
|
||||
float get_setup_priority() const override { return setup_priority::HARDWARE; }
|
||||
|
||||
Trigger<std::string> *get_set_trigger() const { return this->set_trigger_; }
|
||||
@@ -27,11 +40,26 @@ class TemplateSelect : public select::Select, public PollingComponent {
|
||||
bool optimistic_ = false;
|
||||
size_t initial_option_index_{0};
|
||||
bool restore_value_ = false;
|
||||
Trigger<std::string> *set_trigger_ = new Trigger<std::string>();
|
||||
optional<optional<std::string> (*)()> f_;
|
||||
Trigger<std::string> *set_trigger_;
|
||||
optional<F> f_;
|
||||
|
||||
ESPPreferenceObject pref_;
|
||||
};
|
||||
|
||||
class TemplateSelect : public TemplateSelectBase<std::function<optional<std::string>()>> {
|
||||
public:
|
||||
void set_template(std::function<optional<std::string>()> &&f) { this->f_ = f; }
|
||||
};
|
||||
|
||||
/** Optimized template select for stateless lambdas (no capture).
|
||||
*
|
||||
* Uses function pointer instead of std::function to reduce memory overhead.
|
||||
* Memory: 4 bytes (function pointer on 32-bit) vs 32 bytes (std::function).
|
||||
*/
|
||||
class StatelessTemplateSelect : public TemplateSelectBase<optional<std::string> (*)()> {
|
||||
public:
|
||||
explicit StatelessTemplateSelect(optional<std::string> (*f)()) { this->f_ = f; }
|
||||
};
|
||||
|
||||
} // namespace template_
|
||||
} // namespace esphome
|
||||
|
||||
@@ -9,6 +9,9 @@ from .. import template_ns
|
||||
TemplateSensor = template_ns.class_(
|
||||
"TemplateSensor", sensor.Sensor, cg.PollingComponent
|
||||
)
|
||||
StatelessTemplateSensor = template_ns.class_(
|
||||
"StatelessTemplateSensor", sensor.Sensor, cg.PollingComponent
|
||||
)
|
||||
|
||||
CONFIG_SCHEMA = (
|
||||
sensor.sensor_schema(
|
||||
@@ -25,14 +28,21 @@ CONFIG_SCHEMA = (
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = await sensor.new_sensor(config)
|
||||
await cg.register_component(var, config)
|
||||
|
||||
if CONF_LAMBDA in config:
|
||||
# Use new_lambda_pvariable to create either TemplateSensor or StatelessTemplateSensor
|
||||
template_ = await cg.process_lambda(
|
||||
config[CONF_LAMBDA], [], return_type=cg.optional.template(float)
|
||||
)
|
||||
cg.add(var.set_template(template_))
|
||||
var = automation.new_lambda_pvariable(
|
||||
config[CONF_ID], template_, StatelessTemplateSensor
|
||||
)
|
||||
# Manually register as sensor since we didn't use new_sensor
|
||||
await sensor.register_sensor(var, config)
|
||||
await cg.register_component(var, config)
|
||||
else:
|
||||
# No lambda - just create the base template sensor
|
||||
var = await sensor.new_sensor(config)
|
||||
await cg.register_component(var, config)
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
|
||||
@@ -7,21 +7,14 @@ namespace template_ {
|
||||
|
||||
static const char *const TAG = "template.sensor";
|
||||
|
||||
void TemplateSensor::update() {
|
||||
if (!this->f_.has_value())
|
||||
return;
|
||||
|
||||
auto val = (*this->f_)();
|
||||
if (val.has_value()) {
|
||||
this->publish_state(*val);
|
||||
}
|
||||
}
|
||||
float TemplateSensor::get_setup_priority() const { return setup_priority::HARDWARE; }
|
||||
void TemplateSensor::set_template(optional<float> (*f)()) { this->f_ = f; }
|
||||
void TemplateSensor::dump_config() {
|
||||
// Template instantiations
|
||||
template<typename F> void TemplateSensorBase<F>::dump_config() {
|
||||
LOG_SENSOR("", "Template Sensor", this);
|
||||
LOG_UPDATE_INTERVAL(this);
|
||||
}
|
||||
|
||||
template class TemplateSensorBase<std::function<optional<float>()>>;
|
||||
template class TemplateSensorBase<optional<float> (*)()>;
|
||||
|
||||
} // namespace template_
|
||||
} // namespace esphome
|
||||
|
||||
@@ -6,18 +6,38 @@
|
||||
namespace esphome {
|
||||
namespace template_ {
|
||||
|
||||
class TemplateSensor : public sensor::Sensor, public PollingComponent {
|
||||
template<typename F> class TemplateSensorBase : public sensor::Sensor, public PollingComponent {
|
||||
public:
|
||||
void set_template(optional<float> (*f)());
|
||||
|
||||
void update() override;
|
||||
void update() override {
|
||||
if (!this->f_.has_value())
|
||||
return;
|
||||
auto val = (*this->f_)();
|
||||
if (val.has_value()) {
|
||||
this->publish_state(*val);
|
||||
}
|
||||
}
|
||||
|
||||
void dump_config() override;
|
||||
|
||||
float get_setup_priority() const override;
|
||||
float get_setup_priority() const override { return setup_priority::HARDWARE; }
|
||||
|
||||
protected:
|
||||
optional<optional<float> (*)()> f_;
|
||||
optional<F> f_;
|
||||
};
|
||||
|
||||
class TemplateSensor : public TemplateSensorBase<std::function<optional<float>()>> {
|
||||
public:
|
||||
void set_template(std::function<optional<float>()> &&f) { this->f_ = f; }
|
||||
};
|
||||
|
||||
/** Optimized template sensor for stateless lambdas (no capture).
|
||||
*
|
||||
* Uses function pointer instead of std::function to reduce memory overhead.
|
||||
* Memory: 4 bytes (function pointer on 32-bit) vs 32 bytes (std::function).
|
||||
*/
|
||||
class StatelessTemplateSensor : public TemplateSensorBase<optional<float> (*)()> {
|
||||
public:
|
||||
explicit StatelessTemplateSensor(optional<float> (*f)()) { this->f_ = f; }
|
||||
};
|
||||
|
||||
} // namespace template_
|
||||
|
||||
@@ -16,6 +16,9 @@ from esphome.const import (
|
||||
from .. import template_ns
|
||||
|
||||
TemplateSwitch = template_ns.class_("TemplateSwitch", switch.Switch, cg.Component)
|
||||
StatelessTemplateSwitch = template_ns.class_(
|
||||
"StatelessTemplateSwitch", switch.Switch, cg.Component
|
||||
)
|
||||
|
||||
|
||||
def validate(config):
|
||||
@@ -55,14 +58,22 @@ CONFIG_SCHEMA = cv.All(
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = await switch.new_switch(config)
|
||||
await cg.register_component(var, config)
|
||||
|
||||
if CONF_LAMBDA in config:
|
||||
# Use new_lambda_pvariable to create either TemplateSwitch or StatelessTemplateSwitch
|
||||
template_ = await cg.process_lambda(
|
||||
config[CONF_LAMBDA], [], return_type=cg.optional.template(bool)
|
||||
)
|
||||
cg.add(var.set_state_lambda(template_))
|
||||
var = automation.new_lambda_pvariable(
|
||||
config[CONF_ID], template_, StatelessTemplateSwitch
|
||||
)
|
||||
# Manually register as switch since we didn't use new_switch
|
||||
await switch.register_switch(var, config)
|
||||
await cg.register_component(var, config)
|
||||
else:
|
||||
# No lambda - just create the base template switch
|
||||
var = await switch.new_switch(config)
|
||||
await cg.register_component(var, config)
|
||||
|
||||
if CONF_TURN_OFF_ACTION in config:
|
||||
await automation.build_automation(
|
||||
var.get_turn_off_trigger(), [], config[CONF_TURN_OFF_ACTION]
|
||||
|
||||
@@ -6,18 +6,8 @@ namespace template_ {
|
||||
|
||||
static const char *const TAG = "template.switch";
|
||||
|
||||
TemplateSwitch::TemplateSwitch() : turn_on_trigger_(new Trigger<>()), turn_off_trigger_(new Trigger<>()) {}
|
||||
|
||||
void TemplateSwitch::loop() {
|
||||
if (!this->f_.has_value())
|
||||
return;
|
||||
auto s = (*this->f_)();
|
||||
if (!s.has_value())
|
||||
return;
|
||||
|
||||
this->publish_state(*s);
|
||||
}
|
||||
void TemplateSwitch::write_state(bool state) {
|
||||
// Template instantiations
|
||||
template<typename F> void TemplateSwitchBase<F>::write_state(bool state) {
|
||||
if (this->prev_trigger_ != nullptr) {
|
||||
this->prev_trigger_->stop_action();
|
||||
}
|
||||
@@ -33,13 +23,8 @@ void TemplateSwitch::write_state(bool state) {
|
||||
if (this->optimistic_)
|
||||
this->publish_state(state);
|
||||
}
|
||||
void TemplateSwitch::set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
|
||||
bool TemplateSwitch::assumed_state() { return this->assumed_state_; }
|
||||
void TemplateSwitch::set_state_lambda(optional<bool> (*f)()) { this->f_ = f; }
|
||||
float TemplateSwitch::get_setup_priority() const { return setup_priority::HARDWARE - 2.0f; }
|
||||
Trigger<> *TemplateSwitch::get_turn_on_trigger() const { return this->turn_on_trigger_; }
|
||||
Trigger<> *TemplateSwitch::get_turn_off_trigger() const { return this->turn_off_trigger_; }
|
||||
void TemplateSwitch::setup() {
|
||||
|
||||
template<typename F> void TemplateSwitchBase<F>::setup() {
|
||||
optional<bool> initial_state = this->get_initial_state_with_restore_mode();
|
||||
|
||||
if (initial_state.has_value()) {
|
||||
@@ -52,11 +37,14 @@ void TemplateSwitch::setup() {
|
||||
}
|
||||
}
|
||||
}
|
||||
void TemplateSwitch::dump_config() {
|
||||
|
||||
template<typename F> void TemplateSwitchBase<F>::dump_config() {
|
||||
LOG_SWITCH("", "Template Switch", this);
|
||||
ESP_LOGCONFIG(TAG, " Optimistic: %s", YESNO(this->optimistic_));
|
||||
}
|
||||
void TemplateSwitch::set_assumed_state(bool assumed_state) { this->assumed_state_ = assumed_state; }
|
||||
|
||||
template class TemplateSwitchBase<std::function<optional<bool>()>>;
|
||||
template class TemplateSwitchBase<optional<bool> (*)()>;
|
||||
|
||||
} // namespace template_
|
||||
} // namespace esphome
|
||||
|
||||
@@ -7,28 +7,35 @@
|
||||
namespace esphome {
|
||||
namespace template_ {
|
||||
|
||||
class TemplateSwitch : public switch_::Switch, public Component {
|
||||
template<typename F> class TemplateSwitchBase : public switch_::Switch, public Component {
|
||||
public:
|
||||
TemplateSwitch();
|
||||
TemplateSwitchBase() : turn_on_trigger_(new Trigger<>()), turn_off_trigger_(new Trigger<>()) {}
|
||||
|
||||
void setup() override;
|
||||
void dump_config() override;
|
||||
|
||||
void set_state_lambda(optional<bool> (*f)());
|
||||
Trigger<> *get_turn_on_trigger() const;
|
||||
Trigger<> *get_turn_off_trigger() const;
|
||||
void set_optimistic(bool optimistic);
|
||||
void set_assumed_state(bool assumed_state);
|
||||
void loop() override;
|
||||
void loop() override {
|
||||
if (!this->f_.has_value())
|
||||
return;
|
||||
auto s = (*this->f_)();
|
||||
if (!s.has_value())
|
||||
return;
|
||||
this->publish_state(*s);
|
||||
}
|
||||
|
||||
float get_setup_priority() const override;
|
||||
Trigger<> *get_turn_on_trigger() const { return this->turn_on_trigger_; }
|
||||
Trigger<> *get_turn_off_trigger() const { return this->turn_off_trigger_; }
|
||||
void set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
|
||||
void set_assumed_state(bool assumed_state) { this->assumed_state_ = assumed_state; }
|
||||
|
||||
float get_setup_priority() const override { return setup_priority::HARDWARE - 2.0f; }
|
||||
|
||||
protected:
|
||||
bool assumed_state() override;
|
||||
bool assumed_state() override { return this->assumed_state_; }
|
||||
|
||||
void write_state(bool state) override;
|
||||
|
||||
optional<optional<bool> (*)()> f_;
|
||||
optional<F> f_;
|
||||
bool optimistic_{false};
|
||||
bool assumed_state_{false};
|
||||
Trigger<> *turn_on_trigger_;
|
||||
@@ -36,5 +43,20 @@ class TemplateSwitch : public switch_::Switch, public Component {
|
||||
Trigger<> *prev_trigger_{nullptr};
|
||||
};
|
||||
|
||||
class TemplateSwitch : public TemplateSwitchBase<std::function<optional<bool>()>> {
|
||||
public:
|
||||
void set_state_lambda(std::function<optional<bool>()> &&f) { this->f_ = f; }
|
||||
};
|
||||
|
||||
/** Optimized template switch for stateless lambdas (no capture).
|
||||
*
|
||||
* Uses function pointer instead of std::function to reduce memory overhead.
|
||||
* Memory: 4 bytes (function pointer on 32-bit) vs 32 bytes (std::function).
|
||||
*/
|
||||
class StatelessTemplateSwitch : public TemplateSwitchBase<optional<bool> (*)()> {
|
||||
public:
|
||||
explicit StatelessTemplateSwitch(optional<bool> (*f)()) { this->f_ = f; }
|
||||
};
|
||||
|
||||
} // namespace template_
|
||||
} // namespace esphome
|
||||
|
||||
@@ -3,6 +3,7 @@ import esphome.codegen as cg
|
||||
from esphome.components import text
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
CONF_ID,
|
||||
CONF_INITIAL_VALUE,
|
||||
CONF_LAMBDA,
|
||||
CONF_MAX_LENGTH,
|
||||
@@ -16,6 +17,9 @@ from esphome.const import (
|
||||
from .. import template_ns
|
||||
|
||||
TemplateText = template_ns.class_("TemplateText", text.Text, cg.PollingComponent)
|
||||
StatelessTemplateText = template_ns.class_(
|
||||
"StatelessTemplateText", text.Text, cg.PollingComponent
|
||||
)
|
||||
|
||||
TextSaverBase = template_ns.class_("TemplateTextSaverBase")
|
||||
TextSaverTemplate = template_ns.class_("TextSaver", TextSaverBase)
|
||||
@@ -65,21 +69,31 @@ CONFIG_SCHEMA = cv.All(
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = await text.new_text(
|
||||
config,
|
||||
min_length=config[CONF_MIN_LENGTH],
|
||||
max_length=config[CONF_MAX_LENGTH],
|
||||
pattern=config.get(CONF_PATTERN),
|
||||
)
|
||||
await cg.register_component(var, config)
|
||||
|
||||
if CONF_LAMBDA in config:
|
||||
# Use new_lambda_pvariable to create either TemplateText or StatelessTemplateText
|
||||
template_ = await cg.process_lambda(
|
||||
config[CONF_LAMBDA], [], return_type=cg.optional.template(cg.std_string)
|
||||
)
|
||||
cg.add(var.set_template(template_))
|
||||
|
||||
var = automation.new_lambda_pvariable(
|
||||
config[CONF_ID], template_, StatelessTemplateText
|
||||
)
|
||||
await cg.register_component(var, config)
|
||||
await text.register_text(
|
||||
var,
|
||||
config,
|
||||
min_length=config[CONF_MIN_LENGTH],
|
||||
max_length=config[CONF_MAX_LENGTH],
|
||||
pattern=config.get(CONF_PATTERN),
|
||||
)
|
||||
else:
|
||||
# No lambda - just create the base template text
|
||||
var = await text.new_text(
|
||||
config,
|
||||
min_length=config[CONF_MIN_LENGTH],
|
||||
max_length=config[CONF_MAX_LENGTH],
|
||||
pattern=config.get(CONF_PATTERN),
|
||||
)
|
||||
await cg.register_component(var, config)
|
||||
cg.add(var.set_optimistic(config[CONF_OPTIMISTIC]))
|
||||
if initial_value_config := config.get(CONF_INITIAL_VALUE):
|
||||
cg.add(var.set_initial_value(initial_value_config))
|
||||
|
||||
@@ -6,7 +6,8 @@ namespace template_ {
|
||||
|
||||
static const char *const TAG = "template.text";
|
||||
|
||||
void TemplateText::setup() {
|
||||
// Template instantiations
|
||||
template<typename F> void TemplateTextBase<F>::setup() {
|
||||
if (!(this->f_ == nullptr)) {
|
||||
if (this->f_.has_value())
|
||||
return;
|
||||
@@ -25,21 +26,7 @@ void TemplateText::setup() {
|
||||
this->publish_state(value);
|
||||
}
|
||||
|
||||
void TemplateText::update() {
|
||||
if (this->f_ == nullptr)
|
||||
return;
|
||||
|
||||
if (!this->f_.has_value())
|
||||
return;
|
||||
|
||||
auto val = (*this->f_)();
|
||||
if (!val.has_value())
|
||||
return;
|
||||
|
||||
this->publish_state(*val);
|
||||
}
|
||||
|
||||
void TemplateText::control(const std::string &value) {
|
||||
template<typename F> void TemplateTextBase<F>::control(const std::string &value) {
|
||||
this->set_trigger_->trigger(value);
|
||||
|
||||
if (this->optimistic_)
|
||||
@@ -51,11 +38,15 @@ void TemplateText::control(const std::string &value) {
|
||||
}
|
||||
}
|
||||
}
|
||||
void TemplateText::dump_config() {
|
||||
|
||||
template<typename F> void TemplateTextBase<F>::dump_config() {
|
||||
LOG_TEXT("", "Template Text Input", this);
|
||||
ESP_LOGCONFIG(TAG, " Optimistic: %s", YESNO(this->optimistic_));
|
||||
LOG_UPDATE_INTERVAL(this);
|
||||
}
|
||||
|
||||
template class TemplateTextBase<std::function<optional<std::string>()>>;
|
||||
template class TemplateTextBase<optional<std::string> (*)()>;
|
||||
|
||||
} // namespace template_
|
||||
} // namespace esphome
|
||||
|
||||
@@ -59,13 +59,24 @@ template<uint8_t SZ> class TextSaver : public TemplateTextSaverBase {
|
||||
}
|
||||
};
|
||||
|
||||
class TemplateText : public text::Text, public PollingComponent {
|
||||
template<typename F> class TemplateTextBase : public text::Text, public PollingComponent {
|
||||
public:
|
||||
void set_template(optional<std::string> (*f)()) { this->f_ = f; }
|
||||
TemplateTextBase() : set_trigger_(new Trigger<std::string>()) {}
|
||||
|
||||
void setup() override;
|
||||
void update() override;
|
||||
void dump_config() override;
|
||||
|
||||
void update() override {
|
||||
if (this->f_ == nullptr)
|
||||
return;
|
||||
if (!this->f_.has_value())
|
||||
return;
|
||||
auto val = (*this->f_)();
|
||||
if (!val.has_value())
|
||||
return;
|
||||
this->publish_state(*val);
|
||||
}
|
||||
|
||||
float get_setup_priority() const override { return setup_priority::HARDWARE; }
|
||||
|
||||
Trigger<std::string> *get_set_trigger() const { return this->set_trigger_; }
|
||||
@@ -77,11 +88,26 @@ class TemplateText : public text::Text, public PollingComponent {
|
||||
void control(const std::string &value) override;
|
||||
bool optimistic_ = false;
|
||||
std::string initial_value_;
|
||||
Trigger<std::string> *set_trigger_ = new Trigger<std::string>();
|
||||
optional<optional<std::string> (*)()> f_{nullptr};
|
||||
Trigger<std::string> *set_trigger_;
|
||||
optional<F> f_{nullptr};
|
||||
|
||||
TemplateTextSaverBase *pref_ = nullptr;
|
||||
};
|
||||
|
||||
class TemplateText : public TemplateTextBase<std::function<optional<std::string>()>> {
|
||||
public:
|
||||
void set_template(std::function<optional<std::string>()> &&f) { this->f_ = f; }
|
||||
};
|
||||
|
||||
/** Optimized template text for stateless lambdas (no capture).
|
||||
*
|
||||
* Uses function pointer instead of std::function to reduce memory overhead.
|
||||
* Memory: 4 bytes (function pointer on 32-bit) vs 32 bytes (std::function).
|
||||
*/
|
||||
class StatelessTemplateText : public TemplateTextBase<optional<std::string> (*)()> {
|
||||
public:
|
||||
explicit StatelessTemplateText(optional<std::string> (*f)()) { this->f_ = f; }
|
||||
};
|
||||
|
||||
} // namespace template_
|
||||
} // namespace esphome
|
||||
|
||||
@@ -10,6 +10,9 @@ from .. import template_ns
|
||||
TemplateTextSensor = template_ns.class_(
|
||||
"TemplateTextSensor", text_sensor.TextSensor, cg.PollingComponent
|
||||
)
|
||||
StatelessTemplateTextSensor = template_ns.class_(
|
||||
"StatelessTemplateTextSensor", text_sensor.TextSensor, cg.PollingComponent
|
||||
)
|
||||
|
||||
CONFIG_SCHEMA = (
|
||||
text_sensor.text_sensor_schema()
|
||||
@@ -24,14 +27,21 @@ CONFIG_SCHEMA = (
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = await text_sensor.new_text_sensor(config)
|
||||
await cg.register_component(var, config)
|
||||
|
||||
if CONF_LAMBDA in config:
|
||||
# Use new_lambda_pvariable to create either TemplateTextSensor or StatelessTemplateTextSensor
|
||||
template_ = await cg.process_lambda(
|
||||
config[CONF_LAMBDA], [], return_type=cg.optional.template(cg.std_string)
|
||||
)
|
||||
cg.add(var.set_template(template_))
|
||||
var = automation.new_lambda_pvariable(
|
||||
config[CONF_ID], template_, StatelessTemplateTextSensor
|
||||
)
|
||||
# Manually register as text sensor since we didn't use new_text_sensor
|
||||
await text_sensor.register_text_sensor(var, config)
|
||||
await cg.register_component(var, config)
|
||||
else:
|
||||
# No lambda - just create the base template text sensor
|
||||
var = await text_sensor.new_text_sensor(config)
|
||||
await cg.register_component(var, config)
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
|
||||
@@ -6,18 +6,11 @@ namespace template_ {
|
||||
|
||||
static const char *const TAG = "template.text_sensor";
|
||||
|
||||
void TemplateTextSensor::update() {
|
||||
if (!this->f_.has_value())
|
||||
return;
|
||||
// Template instantiations
|
||||
template<typename F> void TemplateTextSensorBase<F>::dump_config() { LOG_TEXT_SENSOR("", "Template Sensor", this); }
|
||||
|
||||
auto val = (*this->f_)();
|
||||
if (val.has_value()) {
|
||||
this->publish_state(*val);
|
||||
}
|
||||
}
|
||||
float TemplateTextSensor::get_setup_priority() const { return setup_priority::HARDWARE; }
|
||||
void TemplateTextSensor::set_template(optional<std::string> (*f)()) { this->f_ = f; }
|
||||
void TemplateTextSensor::dump_config() { LOG_TEXT_SENSOR("", "Template Sensor", this); }
|
||||
template class TemplateTextSensorBase<std::function<optional<std::string>()>>;
|
||||
template class TemplateTextSensorBase<optional<std::string> (*)()>;
|
||||
|
||||
} // namespace template_
|
||||
} // namespace esphome
|
||||
|
||||
@@ -7,18 +7,38 @@
|
||||
namespace esphome {
|
||||
namespace template_ {
|
||||
|
||||
class TemplateTextSensor : public text_sensor::TextSensor, public PollingComponent {
|
||||
template<typename F> class TemplateTextSensorBase : public text_sensor::TextSensor, public PollingComponent {
|
||||
public:
|
||||
void set_template(optional<std::string> (*f)());
|
||||
void update() override {
|
||||
if (!this->f_.has_value())
|
||||
return;
|
||||
auto val = (*this->f_)();
|
||||
if (val.has_value()) {
|
||||
this->publish_state(*val);
|
||||
}
|
||||
}
|
||||
|
||||
void update() override;
|
||||
|
||||
float get_setup_priority() const override;
|
||||
float get_setup_priority() const override { return setup_priority::HARDWARE; }
|
||||
|
||||
void dump_config() override;
|
||||
|
||||
protected:
|
||||
optional<optional<std::string> (*)()> f_{};
|
||||
optional<F> f_;
|
||||
};
|
||||
|
||||
class TemplateTextSensor : public TemplateTextSensorBase<std::function<optional<std::string>()>> {
|
||||
public:
|
||||
void set_template(std::function<optional<std::string>()> &&f) { this->f_ = f; }
|
||||
};
|
||||
|
||||
/** Optimized template text sensor for stateless lambdas (no capture).
|
||||
*
|
||||
* Uses function pointer instead of std::function to reduce memory overhead.
|
||||
* Memory: 4 bytes (function pointer on 32-bit) vs 32 bytes (std::function).
|
||||
*/
|
||||
class StatelessTemplateTextSensor : public TemplateTextSensorBase<optional<std::string> (*)()> {
|
||||
public:
|
||||
explicit StatelessTemplateTextSensor(optional<std::string> (*f)()) { this->f_ = f; }
|
||||
};
|
||||
|
||||
} // namespace template_
|
||||
|
||||
@@ -20,6 +20,9 @@ from esphome.const import (
|
||||
from .. import template_ns
|
||||
|
||||
TemplateValve = template_ns.class_("TemplateValve", valve.Valve, cg.Component)
|
||||
StatelessTemplateValve = template_ns.class_(
|
||||
"StatelessTemplateValve", valve.Valve, cg.Component
|
||||
)
|
||||
|
||||
TemplateValvePublishAction = template_ns.class_(
|
||||
"TemplateValvePublishAction", automation.Action, cg.Parented.template(TemplateValve)
|
||||
@@ -62,13 +65,22 @@ CONFIG_SCHEMA = (
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = await valve.new_valve(config)
|
||||
await cg.register_component(var, config)
|
||||
if lambda_config := config.get(CONF_LAMBDA):
|
||||
# Use new_lambda_pvariable to create either TemplateValve or StatelessTemplateValve
|
||||
template_ = await cg.process_lambda(
|
||||
lambda_config, [], return_type=cg.optional.template(float)
|
||||
)
|
||||
cg.add(var.set_state_lambda(template_))
|
||||
var = automation.new_lambda_pvariable(
|
||||
config[CONF_ID], template_, StatelessTemplateValve
|
||||
)
|
||||
# Manually register as valve since we didn't use new_valve
|
||||
await valve.register_valve(var, config)
|
||||
await cg.register_component(var, config)
|
||||
else:
|
||||
# No lambda - just create the base template valve
|
||||
var = await valve.new_valve(config)
|
||||
await cg.register_component(var, config)
|
||||
|
||||
if open_action_config := config.get(CONF_OPEN_ACTION):
|
||||
await automation.build_automation(
|
||||
var.get_open_trigger(), [], open_action_config
|
||||
|
||||
@@ -8,14 +8,8 @@ using namespace esphome::valve;
|
||||
|
||||
static const char *const TAG = "template.valve";
|
||||
|
||||
TemplateValve::TemplateValve()
|
||||
: open_trigger_(new Trigger<>()),
|
||||
close_trigger_(new Trigger<>),
|
||||
stop_trigger_(new Trigger<>()),
|
||||
toggle_trigger_(new Trigger<>()),
|
||||
position_trigger_(new Trigger<float>()) {}
|
||||
|
||||
void TemplateValve::setup() {
|
||||
// Template instantiations
|
||||
template<typename F> void TemplateValveBase<F>::setup() {
|
||||
switch (this->restore_mode_) {
|
||||
case VALVE_NO_RESTORE:
|
||||
break;
|
||||
@@ -35,35 +29,7 @@ void TemplateValve::setup() {
|
||||
}
|
||||
}
|
||||
|
||||
void TemplateValve::loop() {
|
||||
bool changed = false;
|
||||
|
||||
if (this->state_f_.has_value()) {
|
||||
auto s = (*this->state_f_)();
|
||||
if (s.has_value()) {
|
||||
auto pos = clamp(*s, 0.0f, 1.0f);
|
||||
if (pos != this->position) {
|
||||
this->position = pos;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (changed)
|
||||
this->publish_state();
|
||||
}
|
||||
|
||||
void TemplateValve::set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
|
||||
void TemplateValve::set_assumed_state(bool assumed_state) { this->assumed_state_ = assumed_state; }
|
||||
void TemplateValve::set_state_lambda(optional<float> (*f)()) { this->state_f_ = f; }
|
||||
float TemplateValve::get_setup_priority() const { return setup_priority::HARDWARE; }
|
||||
|
||||
Trigger<> *TemplateValve::get_open_trigger() const { return this->open_trigger_; }
|
||||
Trigger<> *TemplateValve::get_close_trigger() const { return this->close_trigger_; }
|
||||
Trigger<> *TemplateValve::get_stop_trigger() const { return this->stop_trigger_; }
|
||||
Trigger<> *TemplateValve::get_toggle_trigger() const { return this->toggle_trigger_; }
|
||||
|
||||
void TemplateValve::dump_config() {
|
||||
template<typename F> void TemplateValveBase<F>::dump_config() {
|
||||
LOG_VALVE("", "Template Valve", this);
|
||||
ESP_LOGCONFIG(TAG,
|
||||
" Has position: %s\n"
|
||||
@@ -71,7 +37,7 @@ void TemplateValve::dump_config() {
|
||||
YESNO(this->has_position_), YESNO(this->optimistic_));
|
||||
}
|
||||
|
||||
void TemplateValve::control(const ValveCall &call) {
|
||||
template<typename F> void TemplateValveBase<F>::control(const ValveCall &call) {
|
||||
if (call.get_stop()) {
|
||||
this->stop_prev_trigger_();
|
||||
this->stop_trigger_->trigger();
|
||||
@@ -106,7 +72,7 @@ void TemplateValve::control(const ValveCall &call) {
|
||||
this->publish_state();
|
||||
}
|
||||
|
||||
ValveTraits TemplateValve::get_traits() {
|
||||
template<typename F> valve::ValveTraits TemplateValveBase<F>::get_traits() {
|
||||
auto traits = ValveTraits();
|
||||
traits.set_is_assumed_state(this->assumed_state_);
|
||||
traits.set_supports_stop(this->has_stop_);
|
||||
@@ -115,18 +81,15 @@ ValveTraits TemplateValve::get_traits() {
|
||||
return traits;
|
||||
}
|
||||
|
||||
Trigger<float> *TemplateValve::get_position_trigger() const { return this->position_trigger_; }
|
||||
|
||||
void TemplateValve::set_has_stop(bool has_stop) { this->has_stop_ = has_stop; }
|
||||
void TemplateValve::set_has_toggle(bool has_toggle) { this->has_toggle_ = has_toggle; }
|
||||
void TemplateValve::set_has_position(bool has_position) { this->has_position_ = has_position; }
|
||||
|
||||
void TemplateValve::stop_prev_trigger_() {
|
||||
template<typename F> void TemplateValveBase<F>::stop_prev_trigger_() {
|
||||
if (this->prev_command_trigger_ != nullptr) {
|
||||
this->prev_command_trigger_->stop_action();
|
||||
this->prev_command_trigger_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
template class TemplateValveBase<std::function<optional<float>()>>;
|
||||
template class TemplateValveBase<optional<float> (*)()>;
|
||||
|
||||
} // namespace template_
|
||||
} // namespace esphome
|
||||
|
||||
@@ -13,28 +13,48 @@ enum TemplateValveRestoreMode {
|
||||
VALVE_RESTORE_AND_CALL,
|
||||
};
|
||||
|
||||
class TemplateValve : public valve::Valve, public Component {
|
||||
template<typename F> class TemplateValveBase : public valve::Valve, public Component {
|
||||
public:
|
||||
TemplateValve();
|
||||
TemplateValveBase()
|
||||
: open_trigger_(new Trigger<>()),
|
||||
close_trigger_(new Trigger<>()),
|
||||
stop_trigger_(new Trigger<>()),
|
||||
toggle_trigger_(new Trigger<>()),
|
||||
position_trigger_(new Trigger<float>()) {}
|
||||
|
||||
void set_state_lambda(optional<float> (*f)());
|
||||
Trigger<> *get_open_trigger() const;
|
||||
Trigger<> *get_close_trigger() const;
|
||||
Trigger<> *get_stop_trigger() const;
|
||||
Trigger<> *get_toggle_trigger() const;
|
||||
Trigger<float> *get_position_trigger() const;
|
||||
void set_optimistic(bool optimistic);
|
||||
void set_assumed_state(bool assumed_state);
|
||||
void set_has_stop(bool has_stop);
|
||||
void set_has_position(bool has_position);
|
||||
void set_has_toggle(bool has_toggle);
|
||||
void set_restore_mode(TemplateValveRestoreMode restore_mode) { restore_mode_ = restore_mode; }
|
||||
void loop() override {
|
||||
bool changed = false;
|
||||
|
||||
if (this->state_f_.has_value()) {
|
||||
auto s = (*this->state_f_)();
|
||||
if (s.has_value()) {
|
||||
auto pos = clamp(*s, 0.0f, 1.0f);
|
||||
if (pos != this->position) {
|
||||
this->position = pos;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (changed)
|
||||
this->publish_state();
|
||||
}
|
||||
|
||||
void setup() override;
|
||||
void loop() override;
|
||||
void dump_config() override;
|
||||
float get_setup_priority() const override { return setup_priority::HARDWARE; }
|
||||
|
||||
float get_setup_priority() const override;
|
||||
Trigger<> *get_open_trigger() const { return this->open_trigger_; }
|
||||
Trigger<> *get_close_trigger() const { return this->close_trigger_; }
|
||||
Trigger<> *get_stop_trigger() const { return this->stop_trigger_; }
|
||||
Trigger<> *get_toggle_trigger() const { return this->toggle_trigger_; }
|
||||
Trigger<float> *get_position_trigger() const { return this->position_trigger_; }
|
||||
void set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
|
||||
void set_assumed_state(bool assumed_state) { this->assumed_state_ = assumed_state; }
|
||||
void set_has_stop(bool has_stop) { this->has_stop_ = has_stop; }
|
||||
void set_has_position(bool has_position) { this->has_position_ = has_position; }
|
||||
void set_has_toggle(bool has_toggle) { this->has_toggle_ = has_toggle; }
|
||||
void set_restore_mode(TemplateValveRestoreMode restore_mode) { restore_mode_ = restore_mode; }
|
||||
|
||||
protected:
|
||||
void control(const valve::ValveCall &call) override;
|
||||
@@ -42,7 +62,7 @@ class TemplateValve : public valve::Valve, public Component {
|
||||
void stop_prev_trigger_();
|
||||
|
||||
TemplateValveRestoreMode restore_mode_{VALVE_NO_RESTORE};
|
||||
optional<optional<float> (*)()> state_f_;
|
||||
optional<F> state_f_;
|
||||
bool assumed_state_{false};
|
||||
bool optimistic_{false};
|
||||
Trigger<> *open_trigger_;
|
||||
@@ -56,5 +76,20 @@ class TemplateValve : public valve::Valve, public Component {
|
||||
bool has_position_{false};
|
||||
};
|
||||
|
||||
class TemplateValve : public TemplateValveBase<std::function<optional<float>()>> {
|
||||
public:
|
||||
void set_state_lambda(std::function<optional<float>()> &&f) { this->state_f_ = f; }
|
||||
};
|
||||
|
||||
/** Optimized template valve for stateless lambdas (no capture).
|
||||
*
|
||||
* Uses function pointers instead of std::function to reduce memory overhead.
|
||||
* Memory: 4 bytes (function pointer on 32-bit) vs 32 bytes (std::function) per lambda.
|
||||
*/
|
||||
class StatelessTemplateValve : public TemplateValveBase<optional<float> (*)()> {
|
||||
public:
|
||||
explicit StatelessTemplateValve(optional<float> (*f)()) { this->state_f_ = f; }
|
||||
};
|
||||
|
||||
} // namespace template_
|
||||
} // namespace esphome
|
||||
|
||||
@@ -67,9 +67,7 @@ void TuyaClimate::setup() {
|
||||
}
|
||||
if (this->eco_id_.has_value()) {
|
||||
this->parent_->register_listener(*this->eco_id_, [this](const TuyaDatapoint &datapoint) {
|
||||
// Whether data type is BOOL or ENUM, it will still be a 1 or a 0, so the functions below are valid in both cases
|
||||
this->eco_ = datapoint.value_bool;
|
||||
this->eco_type_ = datapoint.type;
|
||||
ESP_LOGV(TAG, "MCU reported eco is: %s", ONOFF(this->eco_));
|
||||
this->compute_preset_();
|
||||
this->compute_target_temperature_();
|
||||
@@ -178,11 +176,7 @@ void TuyaClimate::control(const climate::ClimateCall &call) {
|
||||
if (this->eco_id_.has_value()) {
|
||||
const bool eco = preset == climate::CLIMATE_PRESET_ECO;
|
||||
ESP_LOGV(TAG, "Setting eco: %s", ONOFF(eco));
|
||||
if (this->eco_type_ == TuyaDatapointType::ENUM) {
|
||||
this->parent_->set_enum_datapoint_value(*this->eco_id_, eco);
|
||||
} else {
|
||||
this->parent_->set_boolean_datapoint_value(*this->eco_id_, eco);
|
||||
}
|
||||
this->parent_->set_boolean_datapoint_value(*this->eco_id_, eco);
|
||||
}
|
||||
if (this->sleep_id_.has_value()) {
|
||||
const bool sleep = preset == climate::CLIMATE_PRESET_SLEEP;
|
||||
|
||||
@@ -104,7 +104,6 @@ class TuyaClimate : public climate::Climate, public Component {
|
||||
optional<uint8_t> eco_id_{};
|
||||
optional<uint8_t> sleep_id_{};
|
||||
optional<float> eco_temperature_{};
|
||||
TuyaDatapointType eco_type_{};
|
||||
uint8_t active_state_;
|
||||
uint8_t fan_state_;
|
||||
optional<uint8_t> swing_vertical_id_{};
|
||||
|
||||
@@ -99,26 +99,10 @@ void IDFUARTComponent::setup() {
|
||||
}
|
||||
|
||||
void IDFUARTComponent::load_settings(bool dump_config) {
|
||||
esp_err_t err;
|
||||
|
||||
if (uart_is_driver_installed(this->uart_num_)) {
|
||||
err = uart_driver_delete(this->uart_num_);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGW(TAG, "uart_driver_delete failed: %s", esp_err_to_name(err));
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
}
|
||||
err = uart_driver_install(this->uart_num_, // UART number
|
||||
this->rx_buffer_size_, // RX ring buffer size
|
||||
0, // TX ring buffer size. If zero, driver will not use a TX buffer and TX function will
|
||||
// block task until all data has been sent out
|
||||
20, // event queue size/depth
|
||||
&this->uart_event_queue_, // event queue
|
||||
0 // Flags used to allocate the interrupt
|
||||
);
|
||||
uart_config_t uart_config = this->get_config_();
|
||||
esp_err_t err = uart_param_config(this->uart_num_, &uart_config);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGW(TAG, "uart_driver_install failed: %s", esp_err_to_name(err));
|
||||
ESP_LOGW(TAG, "uart_param_config failed: %s", esp_err_to_name(err));
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
@@ -135,12 +119,10 @@ void IDFUARTComponent::load_settings(bool dump_config) {
|
||||
int8_t flow_control = this->flow_control_pin_ != nullptr ? this->flow_control_pin_->get_pin() : -1;
|
||||
|
||||
uint32_t invert = 0;
|
||||
if (this->tx_pin_ != nullptr && this->tx_pin_->is_inverted()) {
|
||||
if (this->tx_pin_ != nullptr && this->tx_pin_->is_inverted())
|
||||
invert |= UART_SIGNAL_TXD_INV;
|
||||
}
|
||||
if (this->rx_pin_ != nullptr && this->rx_pin_->is_inverted()) {
|
||||
if (this->rx_pin_ != nullptr && this->rx_pin_->is_inverted())
|
||||
invert |= UART_SIGNAL_RXD_INV;
|
||||
}
|
||||
|
||||
err = uart_set_line_inverse(this->uart_num_, invert);
|
||||
if (err != ESP_OK) {
|
||||
@@ -156,6 +138,26 @@ void IDFUARTComponent::load_settings(bool dump_config) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (uart_is_driver_installed(this->uart_num_)) {
|
||||
uart_driver_delete(this->uart_num_);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGW(TAG, "uart_driver_delete failed: %s", esp_err_to_name(err));
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
}
|
||||
err = uart_driver_install(this->uart_num_, /* UART RX ring buffer size. */ this->rx_buffer_size_,
|
||||
/* UART TX ring buffer size. If set to zero, driver will not use TX buffer, TX function will
|
||||
block task until all data have been sent out.*/
|
||||
0,
|
||||
/* UART event queue size/depth. */ 20, &(this->uart_event_queue_),
|
||||
/* Flags used to allocate the interrupt. */ 0);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGW(TAG, "uart_driver_install failed: %s", esp_err_to_name(err));
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
|
||||
err = uart_set_rx_full_threshold(this->uart_num_, this->rx_full_threshold_);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGW(TAG, "uart_set_rx_full_threshold failed: %s", esp_err_to_name(err));
|
||||
@@ -171,32 +173,24 @@ void IDFUARTComponent::load_settings(bool dump_config) {
|
||||
}
|
||||
|
||||
auto mode = this->flow_control_pin_ != nullptr ? UART_MODE_RS485_HALF_DUPLEX : UART_MODE_UART;
|
||||
err = uart_set_mode(this->uart_num_, mode); // per docs, must be called only after uart_driver_install()
|
||||
err = uart_set_mode(this->uart_num_, mode);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGW(TAG, "uart_set_mode failed: %s", esp_err_to_name(err));
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
|
||||
uart_config_t uart_config = this->get_config_();
|
||||
err = uart_param_config(this->uart_num_, &uart_config);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGW(TAG, "uart_param_config failed: %s", esp_err_to_name(err));
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
|
||||
if (dump_config) {
|
||||
ESP_LOGCONFIG(TAG, "Reloaded UART %u", this->uart_num_);
|
||||
ESP_LOGCONFIG(TAG, "UART %u was reloaded.", this->uart_num_);
|
||||
this->dump_config();
|
||||
}
|
||||
}
|
||||
|
||||
void IDFUARTComponent::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "UART Bus %u:", this->uart_num_);
|
||||
LOG_PIN(" TX Pin: ", this->tx_pin_);
|
||||
LOG_PIN(" RX Pin: ", this->rx_pin_);
|
||||
LOG_PIN(" Flow Control Pin: ", this->flow_control_pin_);
|
||||
LOG_PIN(" TX Pin: ", tx_pin_);
|
||||
LOG_PIN(" RX Pin: ", rx_pin_);
|
||||
LOG_PIN(" Flow Control Pin: ", flow_control_pin_);
|
||||
if (this->rx_pin_ != nullptr) {
|
||||
ESP_LOGCONFIG(TAG,
|
||||
" RX Buffer Size: %u\n"
|
||||
|
||||
@@ -3,7 +3,7 @@ from esphome.automation import Condition
|
||||
import esphome.codegen as cg
|
||||
from esphome.components.const import CONF_USE_PSRAM
|
||||
from esphome.components.esp32 import add_idf_sdkconfig_option, const, get_esp32_variant
|
||||
from esphome.components.network import ip_address_literal
|
||||
from esphome.components.network import IPAddress
|
||||
from esphome.config_helpers import filter_source_files_from_platform
|
||||
import esphome.config_validation as cv
|
||||
from esphome.config_validation import only_with_esp_idf
|
||||
@@ -334,7 +334,9 @@ def eap_auth(config):
|
||||
|
||||
|
||||
def safe_ip(ip):
|
||||
return ip_address_literal(ip)
|
||||
if ip is None:
|
||||
return IPAddress(0, 0, 0, 0)
|
||||
return IPAddress(str(ip))
|
||||
|
||||
|
||||
def manual_ip(config):
|
||||
|
||||
@@ -4,8 +4,6 @@
|
||||
#include "esphome/core/defines.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/preferences.h"
|
||||
#include <concepts>
|
||||
#include <functional>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
|
||||
@@ -23,7 +23,6 @@ size_t = global_ns.namespace("size_t")
|
||||
const_char_ptr = global_ns.namespace("const char *")
|
||||
NAN = global_ns.namespace("NAN")
|
||||
esphome_ns = global_ns # using namespace esphome;
|
||||
FixedVector = esphome_ns.class_("FixedVector")
|
||||
App = esphome_ns.App
|
||||
EntityBase = esphome_ns.class_("EntityBase")
|
||||
Component = esphome_ns.class_("Component")
|
||||
|
||||
@@ -120,7 +120,7 @@ def prepare(
|
||||
cert_file.flush()
|
||||
key_file.write(config[CONF_MQTT].get(CONF_CLIENT_CERTIFICATE_KEY))
|
||||
key_file.flush()
|
||||
context.load_cert_chain(cert_file.name, key_file.name)
|
||||
context.load_cert_chain(cert_file, key_file)
|
||||
client.tls_set_context(context)
|
||||
|
||||
try:
|
||||
|
||||
@@ -19,9 +19,7 @@ classifiers = [
|
||||
"Programming Language :: Python :: 3",
|
||||
"Topic :: Home Automation",
|
||||
]
|
||||
|
||||
# Python 3.14 is currently not supported by IDF <= 5.5.1, see https://github.com/esphome/esphome/issues/11502
|
||||
requires-python = ">=3.11.0,<3.14"
|
||||
requires-python = ">=3.11.0"
|
||||
|
||||
dynamic = ["dependencies", "optional-dependencies", "version"]
|
||||
|
||||
|
||||
@@ -48,6 +48,7 @@ import sys
|
||||
from typing import Any
|
||||
|
||||
from helpers import (
|
||||
BASE_BUS_COMPONENTS,
|
||||
CPP_FILE_EXTENSIONS,
|
||||
PYTHON_FILE_EXTENSIONS,
|
||||
changed_files,
|
||||
@@ -452,7 +453,7 @@ def detect_memory_impact_config(
|
||||
# Get actually changed files (not dependencies)
|
||||
files = changed_files(branch)
|
||||
|
||||
# Find all changed components (excluding core)
|
||||
# Find all changed components (excluding core and base bus components)
|
||||
# Also collect platform hints from platform-specific filenames
|
||||
changed_component_set: set[str] = set()
|
||||
has_core_cpp_changes = False
|
||||
@@ -461,13 +462,13 @@ def detect_memory_impact_config(
|
||||
for file in files:
|
||||
component = get_component_from_path(file)
|
||||
if component:
|
||||
# Add all changed components, including base bus components
|
||||
# Base bus components (uart, i2c, spi, etc.) should still be analyzed
|
||||
# when directly changed, even though they're also used as dependencies
|
||||
changed_component_set.add(component)
|
||||
# Check if this is a platform-specific file
|
||||
if platform_hint := _detect_platform_hint_from_filename(file):
|
||||
platform_hints.append(platform_hint)
|
||||
# Skip base bus components as they're used across many builds
|
||||
if component not in BASE_BUS_COMPONENTS:
|
||||
changed_component_set.add(component)
|
||||
# Check if this is a platform-specific file
|
||||
platform_hint = _detect_platform_hint_from_filename(file)
|
||||
if platform_hint:
|
||||
platform_hints.append(platform_hint)
|
||||
elif file.startswith("esphome/") and file.endswith(CPP_FILE_EXTENSIONS):
|
||||
# Core ESPHome C++ files changed (not component-specific)
|
||||
# Only C++ files affect memory usage
|
||||
|
||||
@@ -66,6 +66,5 @@ def test_text_config_lamda_is_set(generate_main):
|
||||
main_cpp = generate_main("tests/component_tests/text/test_text.yaml")
|
||||
|
||||
# Then
|
||||
# Stateless lambda optimization: empty capture list allows function pointer conversion
|
||||
assert "it_4->set_template([]() -> esphome::optional<std::string> {" in main_cpp
|
||||
assert 'return std::string{"Hello"};' in main_cpp
|
||||
|
||||
@@ -3,52 +3,3 @@ esp32_ble_tracker:
|
||||
ble_client:
|
||||
- mac_address: 01:02:03:04:05:06
|
||||
id: test_blec
|
||||
on_connect:
|
||||
- ble_client.ble_write:
|
||||
id: test_blec
|
||||
service_uuid: "abcd1234-abcd-1234-abcd-abcd12345678"
|
||||
characteristic_uuid: "abcd1235-abcd-1234-abcd-abcd12345678"
|
||||
value: !lambda |-
|
||||
return std::vector<uint8_t>{0x01, 0x02, 0x03};
|
||||
- ble_client.ble_write:
|
||||
id: test_blec
|
||||
service_uuid: "abcd1234-abcd-1234-abcd-abcd12345678"
|
||||
characteristic_uuid: "abcd1235-abcd-1234-abcd-abcd12345678"
|
||||
value: [0x04, 0x05, 0x06]
|
||||
on_passkey_request:
|
||||
- ble_client.passkey_reply:
|
||||
id: test_blec
|
||||
passkey: !lambda |-
|
||||
return 123456;
|
||||
- ble_client.passkey_reply:
|
||||
id: test_blec
|
||||
passkey: 654321
|
||||
on_numeric_comparison_request:
|
||||
- ble_client.numeric_comparison_reply:
|
||||
id: test_blec
|
||||
accept: !lambda |-
|
||||
return true;
|
||||
- ble_client.numeric_comparison_reply:
|
||||
id: test_blec
|
||||
accept: false
|
||||
|
||||
sensor:
|
||||
- platform: ble_client
|
||||
ble_client_id: test_blec
|
||||
type: characteristic
|
||||
id: test_sensor_lambda
|
||||
name: "BLE Sensor with Lambda"
|
||||
service_uuid: "abcd1234-abcd-1234-abcd-abcd12345678"
|
||||
characteristic_uuid: "abcd1236-abcd-1234-abcd-abcd12345678"
|
||||
lambda: |-
|
||||
if (x.size() >= 2) {
|
||||
return (float)(x[0] | (x[1] << 8)) / 100.0;
|
||||
}
|
||||
return NAN;
|
||||
- platform: ble_client
|
||||
ble_client_id: test_blec
|
||||
type: characteristic
|
||||
id: test_sensor_no_lambda
|
||||
name: "BLE Sensor without Lambda"
|
||||
service_uuid: "abcd1234-abcd-1234-abcd-abcd12345678"
|
||||
characteristic_uuid: "abcd1237-abcd-1234-abcd-abcd12345678"
|
||||
|
||||
@@ -56,14 +56,6 @@ binary_sensor:
|
||||
register_type: read
|
||||
address: 0x3200
|
||||
bitmask: 0x80
|
||||
- platform: modbus_controller
|
||||
modbus_controller_id: modbus_controller1
|
||||
id: modbus_binary_sensor2
|
||||
name: Test Binary Sensor with Lambda
|
||||
register_type: read
|
||||
address: 0x3201
|
||||
lambda: |-
|
||||
return x;
|
||||
|
||||
number:
|
||||
- platform: modbus_controller
|
||||
@@ -73,16 +65,6 @@ number:
|
||||
address: 0x9001
|
||||
value_type: U_WORD
|
||||
multiply: 1.0
|
||||
- platform: modbus_controller
|
||||
modbus_controller_id: modbus_controller1
|
||||
id: modbus_number2
|
||||
name: Test Number with Lambda
|
||||
address: 0x9002
|
||||
value_type: U_WORD
|
||||
lambda: |-
|
||||
return x * 2.0;
|
||||
write_lambda: |-
|
||||
return x / 2.0;
|
||||
|
||||
output:
|
||||
- platform: modbus_controller
|
||||
@@ -92,14 +74,6 @@ output:
|
||||
register_type: holding
|
||||
value_type: U_WORD
|
||||
multiply: 1000
|
||||
- platform: modbus_controller
|
||||
modbus_controller_id: modbus_controller1
|
||||
id: modbus_output2
|
||||
address: 2049
|
||||
register_type: holding
|
||||
value_type: U_WORD
|
||||
write_lambda: |-
|
||||
return x * 100.0;
|
||||
|
||||
select:
|
||||
- platform: modbus_controller
|
||||
@@ -113,34 +87,6 @@ select:
|
||||
"One": 1
|
||||
"Two": 2
|
||||
"Three": 3
|
||||
- platform: modbus_controller
|
||||
modbus_controller_id: modbus_controller1
|
||||
id: modbus_select2
|
||||
name: Test Select with Lambda
|
||||
address: 1001
|
||||
value_type: U_WORD
|
||||
optionsmap:
|
||||
"Off": 0
|
||||
"On": 1
|
||||
"Two": 2
|
||||
lambda: |-
|
||||
ESP_LOGD("Reg1001", "Received value %lld", x);
|
||||
if (x > 1) {
|
||||
return std::string("Two");
|
||||
} else if (x == 1) {
|
||||
return std::string("On");
|
||||
}
|
||||
return std::string("Off");
|
||||
write_lambda: |-
|
||||
ESP_LOGD("Reg1001", "Set option to %s (%lld)", x.c_str(), value);
|
||||
if (x == "On") {
|
||||
return 1;
|
||||
}
|
||||
if (x == "Two") {
|
||||
payload.push_back(0x0002);
|
||||
return 0;
|
||||
}
|
||||
return value;
|
||||
|
||||
sensor:
|
||||
- platform: modbus_controller
|
||||
@@ -151,15 +97,6 @@ sensor:
|
||||
address: 0x9001
|
||||
unit_of_measurement: "AH"
|
||||
value_type: U_WORD
|
||||
- platform: modbus_controller
|
||||
modbus_controller_id: modbus_controller1
|
||||
id: modbus_sensor2
|
||||
name: Test Sensor with Lambda
|
||||
register_type: holding
|
||||
address: 0x9002
|
||||
value_type: U_WORD
|
||||
lambda: |-
|
||||
return x / 10.0;
|
||||
|
||||
switch:
|
||||
- platform: modbus_controller
|
||||
@@ -169,16 +106,6 @@ switch:
|
||||
register_type: coil
|
||||
address: 0x15
|
||||
bitmask: 1
|
||||
- platform: modbus_controller
|
||||
modbus_controller_id: modbus_controller1
|
||||
id: modbus_switch2
|
||||
name: Test Switch with Lambda
|
||||
register_type: coil
|
||||
address: 0x16
|
||||
lambda: |-
|
||||
return !x;
|
||||
write_lambda: |-
|
||||
return !x;
|
||||
|
||||
text_sensor:
|
||||
- platform: modbus_controller
|
||||
@@ -190,13 +117,3 @@ text_sensor:
|
||||
register_count: 3
|
||||
raw_encode: HEXBYTES
|
||||
response_size: 6
|
||||
- platform: modbus_controller
|
||||
modbus_controller_id: modbus_controller1
|
||||
id: modbus_text_sensor2
|
||||
name: Test Text Sensor with Lambda
|
||||
register_type: holding
|
||||
address: 0x9014
|
||||
register_count: 2
|
||||
response_size: 4
|
||||
lambda: |-
|
||||
return "Modified: " + x;
|
||||
|
||||
@@ -48,11 +48,6 @@ on_drayton:
|
||||
- logger.log:
|
||||
format: "on_drayton: %u %u %u"
|
||||
args: ["x.address", "x.channel", "x.command"]
|
||||
on_dyson:
|
||||
then:
|
||||
- logger.log:
|
||||
format: "on_dyson: %u %u"
|
||||
args: ["x.code", "x.index"]
|
||||
on_gobox:
|
||||
then:
|
||||
- logger.log:
|
||||
|
||||
@@ -6,13 +6,6 @@ button:
|
||||
remote_transmitter.transmit_beo4:
|
||||
source: 0x01
|
||||
command: 0x0C
|
||||
- platform: template
|
||||
name: Dyson fan up
|
||||
id: dyson_fan_up
|
||||
on_press:
|
||||
remote_transmitter.transmit_dyson:
|
||||
code: 0x1215
|
||||
index: 0x0
|
||||
- platform: template
|
||||
name: JVC Off
|
||||
id: living_room_lights_on
|
||||
|
||||
@@ -19,41 +19,3 @@ uart:
|
||||
|
||||
packet_transport:
|
||||
- platform: uart
|
||||
|
||||
switch:
|
||||
# Test uart switch with single state (array)
|
||||
- platform: uart
|
||||
name: "UART Switch Single Array"
|
||||
uart_id: uart_uart
|
||||
data: [0x01, 0x02, 0x03]
|
||||
# Test uart switch with single state (string)
|
||||
- platform: uart
|
||||
name: "UART Switch Single String"
|
||||
uart_id: uart_uart
|
||||
data: "ON"
|
||||
# Test uart switch with turn_on/turn_off (arrays)
|
||||
- platform: uart
|
||||
name: "UART Switch Dual Array"
|
||||
uart_id: uart_uart
|
||||
data:
|
||||
turn_on: [0xA0, 0xA1, 0xA2]
|
||||
turn_off: [0xB0, 0xB1, 0xB2]
|
||||
# Test uart switch with turn_on/turn_off (strings)
|
||||
- platform: uart
|
||||
name: "UART Switch Dual String"
|
||||
uart_id: uart_uart
|
||||
data:
|
||||
turn_on: "TURN_ON"
|
||||
turn_off: "TURN_OFF"
|
||||
|
||||
button:
|
||||
# Test uart button with array data
|
||||
- platform: uart
|
||||
name: "UART Button Array"
|
||||
uart_id: uart_uart
|
||||
data: [0xFF, 0xEE, 0xDD]
|
||||
# Test uart button with string data
|
||||
- platform: uart
|
||||
name: "UART Button String"
|
||||
uart_id: uart_uart
|
||||
data: "BUTTON_PRESS"
|
||||
|
||||
@@ -13,21 +13,3 @@ uart:
|
||||
rx_buffer_size: 512
|
||||
parity: EVEN
|
||||
stop_bits: 2
|
||||
|
||||
switch:
|
||||
- platform: uart
|
||||
name: "UART Switch Array"
|
||||
uart_id: uart_uart
|
||||
data: [0x01, 0x02, 0x03]
|
||||
- platform: uart
|
||||
name: "UART Switch Dual"
|
||||
uart_id: uart_uart
|
||||
data:
|
||||
turn_on: [0xA0, 0xA1]
|
||||
turn_off: [0xB0, 0xB1]
|
||||
|
||||
button:
|
||||
- platform: uart
|
||||
name: "UART Button"
|
||||
uart_id: uart_uart
|
||||
data: [0xFF, 0xEE]
|
||||
|
||||
@@ -849,47 +849,39 @@ def test_detect_memory_impact_config_no_components_with_tests(tmp_path: Path) ->
|
||||
assert result["should_run"] == "false"
|
||||
|
||||
|
||||
def test_detect_memory_impact_config_includes_base_bus_components(
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
"""Test that base bus components (i2c, spi, uart) are included when directly changed.
|
||||
|
||||
Base bus components should be analyzed for memory impact when they are directly
|
||||
changed, even though they are often used as dependencies. This ensures that
|
||||
optimizations to base components (like using move semantics or initializer_list)
|
||||
are properly measured.
|
||||
"""
|
||||
def test_detect_memory_impact_config_skips_base_bus_components(tmp_path: Path) -> None:
|
||||
"""Test that base bus components (i2c, spi, uart) are skipped."""
|
||||
# Create test directory structure
|
||||
tests_dir = tmp_path / "tests" / "components"
|
||||
|
||||
# uart component (base bus component that should be included)
|
||||
uart_dir = tests_dir / "uart"
|
||||
uart_dir.mkdir(parents=True)
|
||||
(uart_dir / "test.esp32-idf.yaml").write_text("test: uart")
|
||||
# i2c component (should be skipped as it's a base bus component)
|
||||
i2c_dir = tests_dir / "i2c"
|
||||
i2c_dir.mkdir(parents=True)
|
||||
(i2c_dir / "test.esp32-idf.yaml").write_text("test: i2c")
|
||||
|
||||
# wifi component (regular component)
|
||||
# wifi component (should not be skipped)
|
||||
wifi_dir = tests_dir / "wifi"
|
||||
wifi_dir.mkdir(parents=True)
|
||||
(wifi_dir / "test.esp32-idf.yaml").write_text("test: wifi")
|
||||
|
||||
# Mock changed_files to return both uart and wifi
|
||||
# Mock changed_files to return both i2c and wifi
|
||||
with (
|
||||
patch.object(determine_jobs, "root_path", str(tmp_path)),
|
||||
patch.object(helpers, "root_path", str(tmp_path)),
|
||||
patch.object(determine_jobs, "changed_files") as mock_changed_files,
|
||||
):
|
||||
mock_changed_files.return_value = [
|
||||
"esphome/components/uart/automation.h", # Header file with inline code
|
||||
"esphome/components/i2c/i2c.cpp",
|
||||
"esphome/components/wifi/wifi.cpp",
|
||||
]
|
||||
determine_jobs._component_has_tests.cache_clear()
|
||||
|
||||
result = determine_jobs.detect_memory_impact_config()
|
||||
|
||||
# Should include both uart and wifi
|
||||
# Should only include wifi, not i2c
|
||||
assert result["should_run"] == "true"
|
||||
assert set(result["components"]) == {"uart", "wifi"}
|
||||
assert result["platform"] == "esp32-idf" # Common platform
|
||||
assert result["components"] == ["wifi"]
|
||||
assert "i2c" not in result["components"]
|
||||
|
||||
|
||||
def test_detect_memory_impact_config_with_variant_tests(tmp_path: Path) -> None:
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import glob
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from esphome import config as config_module, yaml_util
|
||||
from esphome.components import substitutions
|
||||
@@ -61,29 +60,6 @@ def write_yaml(path: Path, data: dict) -> None:
|
||||
path.write_text(yaml_util.dump(data), encoding="utf-8")
|
||||
|
||||
|
||||
def verify_database(value: Any, path: str = "") -> str | None:
|
||||
if isinstance(value, list):
|
||||
for i, v in enumerate(value):
|
||||
result = verify_database(v, f"{path}[{i}]")
|
||||
if result is not None:
|
||||
return result
|
||||
return None
|
||||
if isinstance(value, dict):
|
||||
for k, v in value.items():
|
||||
key_result = verify_database(k, f"{path}/{k}")
|
||||
if key_result is not None:
|
||||
return key_result
|
||||
value_result = verify_database(v, f"{path}/{k}")
|
||||
if value_result is not None:
|
||||
return value_result
|
||||
return None
|
||||
if isinstance(value, str):
|
||||
if not isinstance(value, yaml_util.ESPHomeDataBase):
|
||||
return f"{path}: {value!r} is not ESPHomeDataBase"
|
||||
return None
|
||||
return None
|
||||
|
||||
|
||||
def test_substitutions_fixtures(fixture_path):
|
||||
base_dir = fixture_path / "substitutions"
|
||||
sources = sorted(glob.glob(str(base_dir / "*.input.yaml")))
|
||||
@@ -107,9 +83,6 @@ def test_substitutions_fixtures(fixture_path):
|
||||
substitutions.do_substitution_pass(config, None)
|
||||
|
||||
resolve_extend_remove(config)
|
||||
verify_database_result = verify_database(config)
|
||||
if verify_database_result is not None:
|
||||
raise AssertionError(verify_database_result)
|
||||
|
||||
# Also load expected using ESPHome's loader, or use {} if missing and DEV_MODE
|
||||
if expected_path.is_file():
|
||||
|
||||
Reference in New Issue
Block a user