mirror of
https://github.com/esphome/esphome.git
synced 2026-10-14 10:12:52 +00:00
Merge branch 'platformio-pch-libretiny' into pch-strict-ci
This commit is contained in:
@@ -549,6 +549,7 @@ esphome/components/sx127x/* @swoboda1337
|
||||
esphome/components/sy6970/* @linkedupbits
|
||||
esphome/components/syslog/* @clydebarrow
|
||||
esphome/components/t6615/* @tylermenezes
|
||||
esphome/components/tas2780/* @remcom
|
||||
esphome/components/tc74/* @sethgirvan
|
||||
esphome/components/tca9548a/* @andreashergert1984
|
||||
esphome/components/tca9555/* @mobrembski
|
||||
|
||||
+12
-8
@@ -339,9 +339,9 @@ def _check_key_in_schema(
|
||||
schema = schema.schema[markers[part]]
|
||||
|
||||
|
||||
async def _apply_parent(config: ConfigType) -> str:
|
||||
async def _apply_parent(config: ConfigType, id_key: str = CONF_ID) -> str:
|
||||
# Global-scope qualified so a trigger arg named like the id cannot shadow it.
|
||||
return f"::{await cg.get_variable(config[CONF_ID])}"
|
||||
return f"::{await cg.get_variable(config[id_key])}"
|
||||
|
||||
|
||||
def _apply_lambda_args(args: TemplateArgsType) -> TemplateArgsType:
|
||||
@@ -396,12 +396,14 @@ def register_apply_action(
|
||||
schema: cv.Schema,
|
||||
*fields: ApplyField | ApplyCall,
|
||||
call: str | None = None,
|
||||
id_key: str = CONF_ID,
|
||||
) -> None:
|
||||
"""Register an action that only forwards config values to its parent, with no C++ class.
|
||||
|
||||
Generates one stateless function for ``ApplyAction<Ts...>``: parent and constants are baked
|
||||
in, lambdas are called inline with the trigger args. With ``call`` every statement targets
|
||||
the call object ``auto apply_call = parent->call()``, and ``apply_call.perform()`` is appended.
|
||||
Generates one stateless function for ``ApplyAction<Ts...>``: the parent (read from
|
||||
``id_key``) and constants are baked in, lambdas are called inline with the trigger args.
|
||||
With ``call`` every statement targets the call object ``auto apply_call = parent->call()``,
|
||||
and ``apply_call.perform()`` is appended.
|
||||
"""
|
||||
# An action stores the value, so a std::string constant stays in flash on ESP8266.
|
||||
statements_spec = [
|
||||
@@ -414,6 +416,7 @@ def register_apply_action(
|
||||
)
|
||||
for c in (f if isinstance(f, ApplyCall) else f.call() for f in fields)
|
||||
]
|
||||
_check_key_in_schema(name, schema, id_key)
|
||||
for _, members in statements_spec:
|
||||
for conf_key, _, _ in members:
|
||||
_check_key_in_schema(name, schema, conf_key)
|
||||
@@ -424,7 +427,7 @@ def register_apply_action(
|
||||
template_arg: cg.TemplateArguments,
|
||||
args: TemplateArgsType,
|
||||
) -> MockObj:
|
||||
parent = await _apply_parent(config)
|
||||
parent = await _apply_parent(config, id_key)
|
||||
lambda_args = _apply_lambda_args(args)
|
||||
receiver = "apply_call." if call else f"{parent}->"
|
||||
statements: list[str] = []
|
||||
@@ -451,7 +454,7 @@ def register_apply_action(
|
||||
|
||||
|
||||
def register_apply_condition(
|
||||
name: str, schema: cv.Schema, check: str | ApplyCall
|
||||
name: str, schema: cv.Schema, check: str | ApplyCall, id_key: str = CONF_ID
|
||||
) -> None:
|
||||
"""Register a condition that is one expression on its parent, with no C++ class.
|
||||
|
||||
@@ -463,6 +466,7 @@ def register_apply_condition(
|
||||
"""
|
||||
call = check if isinstance(check, ApplyCall) else ApplyCall(check)
|
||||
members = call.members
|
||||
_check_key_in_schema(name, schema, id_key)
|
||||
for conf_key, _, _ in members:
|
||||
_check_key_in_schema(name, schema, conf_key)
|
||||
|
||||
@@ -472,7 +476,7 @@ def register_apply_condition(
|
||||
template_arg: cg.TemplateArguments,
|
||||
args: TemplateArgsType,
|
||||
) -> MockObj:
|
||||
parent = await _apply_parent(config)
|
||||
parent = await _apply_parent(config, id_key)
|
||||
lambda_args = _apply_lambda_args(args)
|
||||
exprs = await _render_values(
|
||||
name,
|
||||
|
||||
@@ -600,7 +600,7 @@ bool APIConnection::send_light_state(light::LightState *light) {
|
||||
uint16_t APIConnection::try_send_light_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size) {
|
||||
auto *light = static_cast<light::LightState *>(entity);
|
||||
LightStateResponse resp;
|
||||
auto values = light->remote_values;
|
||||
auto values = light->get_reported_values();
|
||||
auto color_mode = values.get_color_mode();
|
||||
resp.state = values.is_on();
|
||||
resp.color_mode = static_cast<enums::ColorMode>(color_mode);
|
||||
@@ -2267,7 +2267,12 @@ bool APIConnection::send_message_(uint32_t payload_size, uint16_t message_type,
|
||||
// Capacity reserved above, cannot fail
|
||||
(void) shared_buf.resize(write_start + payload_size);
|
||||
ProtoWriteBuffer buffer{&shared_buf, write_start};
|
||||
encode_fn(msg, buffer PROTO_ENCODE_DEBUG_INIT(&shared_buf));
|
||||
uint8_t *end = encode_fn(msg, buffer PROTO_ENCODE_DEBUG_INIT(&shared_buf));
|
||||
#ifdef ESPHOME_DEBUG_API
|
||||
proto_check_encode_end(end, shared_buf.data() + shared_buf.size());
|
||||
#else
|
||||
(void) end;
|
||||
#endif
|
||||
return this->send_buffer(ProtoWriteBuffer{&shared_buf}, message_type);
|
||||
}
|
||||
// encode_to_buffer is defined inline in api_connection.h (ESPHOME_ALWAYS_INLINE)
|
||||
|
||||
@@ -346,11 +346,7 @@ class APIConnection final : public APIServerConnectionBase {
|
||||
/// Returns false as soon as the TCP buffer is full. Marked nodiscard so we
|
||||
/// have no silent failures: every caller must handle (or log) a refusal.
|
||||
template<typename T> [[nodiscard]] bool send_message(const T &msg) {
|
||||
if constexpr (T::ESTIMATED_SIZE == 0) {
|
||||
return this->send_message_(0, T::MESSAGE_TYPE, &encode_msg_noop, &msg);
|
||||
} else {
|
||||
return this->send_message_(msg.calculate_size(), T::MESSAGE_TYPE, &proto_encode_msg<T>, &msg);
|
||||
}
|
||||
return this->send_message_(T::calc_size_msg(&msg), T::MESSAGE_TYPE, &T::encode_msg, &msg);
|
||||
}
|
||||
|
||||
/// Clear the shared write buffer and reserve space for the first message.
|
||||
@@ -406,16 +402,6 @@ class APIConnection final : public APIServerConnectionBase {
|
||||
void process_state_subscriptions_();
|
||||
#endif
|
||||
|
||||
// Size thunk — converts void* back to concrete type for direct calculate_size() call
|
||||
template<typename T> static uint32_t calc_size(const void *msg) {
|
||||
return static_cast<const T *>(msg)->calculate_size();
|
||||
}
|
||||
|
||||
// Shared no-op encode thunk for empty messages (ESTIMATED_SIZE == 0)
|
||||
static uint8_t *encode_msg_noop(const void *, ProtoWriteBuffer &buf PROTO_ENCODE_DEBUG_PARAM) {
|
||||
return buf.get_pos();
|
||||
}
|
||||
|
||||
// Non-template buffer management for send_message
|
||||
bool send_message_(uint32_t payload_size, uint16_t message_type, MessageEncodeFn encode_fn, const void *msg);
|
||||
|
||||
@@ -434,11 +420,7 @@ class APIConnection final : public APIServerConnectionBase {
|
||||
// Hot paths (state/info) go through fill_and_encode_entity_state/info instead.
|
||||
// batch_message_type_ is already set by dispatch_message_ before reaching here.
|
||||
template<typename T> static uint16_t encode_message_to_buffer(T &msg, APIConnection *conn, uint32_t remaining_size) {
|
||||
if constexpr (T::ESTIMATED_SIZE == 0) {
|
||||
return encode_to_buffer_slow(0, &encode_msg_noop, &msg, conn, remaining_size);
|
||||
} else {
|
||||
return encode_to_buffer_slow(msg.calculate_size(), &proto_encode_msg<T>, &msg, conn, remaining_size);
|
||||
}
|
||||
return encode_to_buffer_slow(T::calc_size_msg(&msg), &T::encode_msg, &msg, conn, remaining_size);
|
||||
}
|
||||
|
||||
// Non-template core — fills state fields and encodes
|
||||
@@ -450,7 +432,7 @@ class APIConnection final : public APIServerConnectionBase {
|
||||
template<typename T>
|
||||
static uint16_t fill_and_encode_entity_state(EntityBase *entity, T &msg, APIConnection *conn,
|
||||
uint32_t remaining_size) {
|
||||
return fill_and_encode_entity_state(entity, msg, &calc_size<T>, &proto_encode_msg<T>, conn, remaining_size);
|
||||
return fill_and_encode_entity_state(entity, msg, &T::calc_size_msg, &T::encode_msg, conn, remaining_size);
|
||||
}
|
||||
|
||||
// Non-template core — fills info fields, allocates buffers, and encodes
|
||||
@@ -462,7 +444,7 @@ class APIConnection final : public APIServerConnectionBase {
|
||||
template<typename T>
|
||||
static uint16_t fill_and_encode_entity_info(EntityBase *entity, T &msg, APIConnection *conn,
|
||||
uint32_t remaining_size) {
|
||||
return fill_and_encode_entity_info(entity, msg, &calc_size<T>, &proto_encode_msg<T>, conn, remaining_size);
|
||||
return fill_and_encode_entity_info(entity, msg, &T::calc_size_msg, &T::encode_msg, conn, remaining_size);
|
||||
}
|
||||
|
||||
// Non-template core — fills device_class, then delegates to fill_and_encode_entity_info
|
||||
@@ -476,8 +458,8 @@ class APIConnection final : public APIServerConnectionBase {
|
||||
static uint16_t fill_and_encode_entity_info_with_device_class(EntityBase *entity, T &msg,
|
||||
StringRef &device_class_field, APIConnection *conn,
|
||||
uint32_t remaining_size) {
|
||||
return fill_and_encode_entity_info_with_device_class(entity, msg, device_class_field, &calc_size<T>,
|
||||
&proto_encode_msg<T>, conn, remaining_size);
|
||||
return fill_and_encode_entity_info_with_device_class(entity, msg, device_class_field, &T::calc_size_msg,
|
||||
&T::encode_msg, conn, remaining_size);
|
||||
}
|
||||
|
||||
#ifdef USE_VOICE_ASSISTANT
|
||||
|
||||
@@ -46,7 +46,13 @@ inline uint16_t ESPHOME_ALWAYS_INLINE APIConnection::encode_to_buffer(uint32_t c
|
||||
return 0;
|
||||
}
|
||||
ProtoWriteBuffer buffer{&shared_buf, shared_buf.size() - calculated_size};
|
||||
encode_fn(msg, buffer PROTO_ENCODE_DEBUG_INIT(&shared_buf));
|
||||
uint8_t *end = encode_fn(msg, buffer PROTO_ENCODE_DEBUG_INIT(&shared_buf));
|
||||
#ifdef ESPHOME_DEBUG_API
|
||||
// A body that writes fewer bytes than calculate_size() promised would ship stale buffer bytes
|
||||
proto_check_encode_end(end, shared_buf.data() + shared_buf.size());
|
||||
#else
|
||||
(void) end;
|
||||
#endif
|
||||
|
||||
return total_calculated_size;
|
||||
}
|
||||
|
||||
+2662
-2638
File diff suppressed because it is too large
Load Diff
+875
-352
File diff suppressed because it is too large
Load Diff
@@ -455,8 +455,9 @@ void APIServer::send_homeassistant_action(const HomeassistantActionRequest &call
|
||||
// Home Assistant subscribes to actions shortly *after* authenticating, so actions
|
||||
// fired right at connection time (on_client_connected, on_time_sync, ...) can
|
||||
// arrive before the subscription and are lost - warn instead of failing silently.
|
||||
ESP_LOGW(TAG, "Home Assistant %s '%s' dropped; %s",
|
||||
call.is_event ? LOG_STR_LITERAL("event") : LOG_STR_LITERAL("action"), call.service.c_str(),
|
||||
ESP_LOGW(TAG, "Home Assistant %s '%.*s' dropped; %s",
|
||||
call.is_event ? LOG_STR_LITERAL("event") : LOG_STR_LITERAL("action"),
|
||||
static_cast<int>(call.service.size()), call.service.empty() ? "" : call.service.c_str(),
|
||||
this->is_connected() ? LOG_STR_LITERAL("client has not subscribed to actions (yet)")
|
||||
: LOG_STR_LITERAL("no client connected"));
|
||||
}
|
||||
|
||||
@@ -195,6 +195,12 @@ void proto_check_bounds_failed(const uint8_t *pos, size_t bytes, const uint8_t *
|
||||
ESP_LOGE(TAG, "Proto encode bounds check failed in %s: need %zu bytes, %td available", caller, bytes, end - pos);
|
||||
abort();
|
||||
}
|
||||
void proto_check_encode_end(const uint8_t *end, const uint8_t *expected) {
|
||||
if (end == expected)
|
||||
return;
|
||||
ESP_LOGE(TAG, "Proto encode ended %td bytes off the calculated size", end - expected);
|
||||
abort();
|
||||
}
|
||||
void ProtoWriteBuffer::debug_check_bounds_(size_t bytes, const char *caller) {
|
||||
if (this->pos_ + bytes > this->buffer_->data() + this->buffer_->size()) {
|
||||
ESP_LOGE(TAG, "ProtoWriteBuffer bounds check failed in %s: bytes=%zu offset=%td buf_size=%zu", caller, bytes,
|
||||
@@ -210,77 +216,78 @@ void ProtoWriteBuffer::debug_check_encode_size_(uint32_t field_id, uint32_t expe
|
||||
|
||||
#endif
|
||||
|
||||
void ProtoDecodableMessage::decode(const uint8_t *buffer, size_t length) {
|
||||
void ProtoDecodableMessage::decode_fields(void *msg, const uint8_t *buffer, size_t length, DecodeFieldFn field) {
|
||||
const uint8_t *ptr = buffer;
|
||||
const uint8_t *end = buffer + length;
|
||||
|
||||
while (ptr < end) {
|
||||
// Parse field header - ptr < end guarantees len >= 1
|
||||
// Single-byte varints dominate, so that case advances the cursor inline.
|
||||
auto read_varint = [&](proto_varint_value_t &value) ESPHOME_ALWAYS_INLINE {
|
||||
if (ptr == end)
|
||||
return false;
|
||||
if (*ptr < 0x80) [[likely]] {
|
||||
value = *ptr++;
|
||||
return true;
|
||||
}
|
||||
auto res = ProtoVarInt::parse_non_empty(ptr, end - ptr);
|
||||
if (!res.has_value()) {
|
||||
if (!res.has_value())
|
||||
return false;
|
||||
value = res.value;
|
||||
ptr += res.consumed;
|
||||
return true;
|
||||
};
|
||||
|
||||
while (ptr < end) {
|
||||
proto_varint_value_t tag_value;
|
||||
if (!read_varint(tag_value)) {
|
||||
ESP_LOGV(TAG, "Invalid field start at offset %ld", (long) (ptr - buffer));
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t tag = static_cast<uint32_t>(res.value);
|
||||
uint32_t tag = static_cast<uint32_t>(tag_value);
|
||||
uint32_t field_type = tag & WIRE_TYPE_MASK;
|
||||
uint32_t field_id = tag >> 3;
|
||||
ptr += res.consumed;
|
||||
// Length-delimited fields move this past the length prefix
|
||||
const uint8_t *data = ptr;
|
||||
proto_varint_value_t scalar;
|
||||
|
||||
switch (field_type) {
|
||||
case WIRE_TYPE_VARINT: { // VarInt
|
||||
res = ProtoVarInt::parse(ptr, end - ptr);
|
||||
if (!res.has_value()) {
|
||||
ESP_LOGV(TAG, "Invalid VarInt at offset %ld", (long) (ptr - buffer));
|
||||
return;
|
||||
}
|
||||
if (!this->decode_varint(field_id, res.value)) {
|
||||
ESP_LOGV(TAG, "Cannot decode VarInt field %" PRIu32 " with value %" PRIu64 "!", field_id,
|
||||
static_cast<uint64_t>(res.value));
|
||||
}
|
||||
ptr += res.consumed;
|
||||
break;
|
||||
}
|
||||
case WIRE_TYPE_LENGTH_DELIMITED: { // Length-delimited
|
||||
res = ProtoVarInt::parse(ptr, end - ptr);
|
||||
if (!res.has_value()) {
|
||||
ESP_LOGV(TAG, "Invalid Length Delimited at offset %ld", (long) (ptr - buffer));
|
||||
return;
|
||||
}
|
||||
uint32_t field_length = static_cast<uint32_t>(res.value);
|
||||
ptr += res.consumed;
|
||||
if (field_length > static_cast<size_t>(end - ptr)) {
|
||||
ESP_LOGV(TAG, "Out-of-bounds Length Delimited at offset %ld", (long) (ptr - buffer));
|
||||
return;
|
||||
}
|
||||
if (!this->decode_length(field_id, ProtoLengthDelimited(ptr, field_length))) {
|
||||
ESP_LOGV(TAG, "Cannot decode Length Delimited field %" PRIu32 "!", field_id);
|
||||
}
|
||||
ptr += field_length;
|
||||
break;
|
||||
}
|
||||
case WIRE_TYPE_FIXED32: { // 32-bit
|
||||
if (end - ptr < 4) {
|
||||
ESP_LOGV(TAG, "Out-of-bounds Fixed32-bit at offset %ld", (long) (ptr - buffer));
|
||||
return;
|
||||
}
|
||||
uint32_t val;
|
||||
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
||||
// Protobuf fixed32 is little-endian — direct load on LE platforms
|
||||
memcpy(&val, ptr, 4);
|
||||
#else
|
||||
val = encode_uint32(ptr[3], ptr[2], ptr[1], ptr[0]);
|
||||
#endif
|
||||
if (!this->decode_32bit(field_id, Proto32Bit(val))) {
|
||||
ESP_LOGV(TAG, "Cannot decode 32-bit field %" PRIu32 " with value %" PRIu32 "!", field_id, val);
|
||||
}
|
||||
ptr += 4;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
ESP_LOGV(TAG, "Invalid field type %" PRIu32 " at offset %ld", field_type, (long) (ptr - buffer));
|
||||
if (field_type == WIRE_TYPE_VARINT) [[likely]] {
|
||||
if (!read_varint(scalar)) {
|
||||
ESP_LOGV(TAG, "Invalid VarInt at offset %ld", (long) (ptr - buffer));
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
switch (field_type) {
|
||||
case WIRE_TYPE_LENGTH_DELIMITED: {
|
||||
proto_varint_value_t length_value;
|
||||
if (!read_varint(length_value)) {
|
||||
ESP_LOGV(TAG, "Invalid Length Delimited at offset %ld", (long) (ptr - buffer));
|
||||
return;
|
||||
}
|
||||
uint32_t field_length = static_cast<uint32_t>(length_value);
|
||||
if (field_length > static_cast<size_t>(end - ptr)) {
|
||||
ESP_LOGV(TAG, "Out-of-bounds Length Delimited at offset %ld", (long) (ptr - buffer));
|
||||
return;
|
||||
}
|
||||
data = ptr;
|
||||
scalar = field_length;
|
||||
ptr += field_length;
|
||||
break;
|
||||
}
|
||||
case WIRE_TYPE_FIXED32: {
|
||||
if (end - ptr < 4) {
|
||||
ESP_LOGV(TAG, "Out-of-bounds Fixed32-bit at offset %ld", (long) (ptr - buffer));
|
||||
return;
|
||||
}
|
||||
// Byte loads instead of memcpy: ESP-IDF passes -fno-builtin-memcpy, which made this a call
|
||||
scalar = encode_uint32(ptr[3], ptr[2], ptr[1], ptr[0]);
|
||||
ptr += 4;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
ESP_LOGV(TAG, "Invalid field type %" PRIu32 " at offset %ld", field_type, (long) (ptr - buffer));
|
||||
return;
|
||||
}
|
||||
}
|
||||
field(msg, tag, data, scalar);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+243
-170
@@ -10,6 +10,7 @@
|
||||
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#ifdef ESPHOME_LOG_HAS_VERY_VERBOSE
|
||||
@@ -170,40 +171,43 @@ class ProtoVarInt {
|
||||
class ProtoMessage;
|
||||
class ProtoSize;
|
||||
|
||||
class ProtoLengthDelimited {
|
||||
/// Case label for decode_field(): the wire tag of a field, so a field that arrives with another wire
|
||||
/// type matches no case.
|
||||
constexpr uint32_t proto_tag(uint32_t field_id, uint32_t wire_type) { return (field_id << 3) | wire_type; }
|
||||
|
||||
/// One decoded field: the payload pointer and a scalar holding the varint or fixed32 value, or the
|
||||
/// length of a length-delimited field. The wire type in the tag says which applies; accessors do not check.
|
||||
class ProtoFieldValue {
|
||||
public:
|
||||
explicit ProtoLengthDelimited(const uint8_t *value, size_t length) : value_(value), length_(length) {}
|
||||
std::string as_string() const { return std::string(reinterpret_cast<const char *>(this->value_), this->length_); }
|
||||
ProtoFieldValue(const uint8_t *data, proto_varint_value_t scalar) : data_(data), scalar_(scalar) {}
|
||||
|
||||
// Direct access to raw data without string allocation
|
||||
const uint8_t *data() const { return this->value_; }
|
||||
size_t size() const { return this->length_; }
|
||||
proto_varint_value_t as_varint() const { return this->scalar_; }
|
||||
// A bool is sent as 0 or 1, so the low word is enough and saves a second compare with 64 bit varints
|
||||
bool as_bool() const { return static_cast<uint32_t>(this->scalar_) != 0; }
|
||||
|
||||
/// Decode the length-delimited data into a message instance.
|
||||
// Length-delimited accessors
|
||||
const uint8_t *data() const { return this->data_; }
|
||||
size_t size() const { return static_cast<size_t>(this->scalar_); }
|
||||
std::string as_string() const { return std::string(reinterpret_cast<const char *>(this->data_), this->size()); }
|
||||
/// Decode the length-delimited payload into a message instance.
|
||||
/// Template preserves concrete type so decode() resolves statically.
|
||||
template<typename T> void decode_to_message(T &msg) const;
|
||||
template<typename T> void decode_to_message(T &msg) const { msg.decode(this->data_, this->size()); }
|
||||
|
||||
protected:
|
||||
const uint8_t *const value_;
|
||||
const size_t length_;
|
||||
};
|
||||
|
||||
class Proto32Bit {
|
||||
public:
|
||||
explicit Proto32Bit(uint32_t value) : value_(value) {}
|
||||
uint32_t as_fixed32() const { return this->value_; }
|
||||
int32_t as_sfixed32() const { return static_cast<int32_t>(this->value_); }
|
||||
// Fixed32 accessors
|
||||
uint32_t as_fixed32() const { return static_cast<uint32_t>(this->scalar_); }
|
||||
int32_t as_sfixed32() const { return static_cast<int32_t>(this->as_fixed32()); }
|
||||
float as_float() const {
|
||||
union {
|
||||
uint32_t raw;
|
||||
float value;
|
||||
} s{};
|
||||
s.raw = this->value_;
|
||||
s.raw = this->as_fixed32();
|
||||
return s.value;
|
||||
}
|
||||
|
||||
protected:
|
||||
const uint32_t value_;
|
||||
private:
|
||||
const uint8_t *data_;
|
||||
proto_varint_value_t scalar_;
|
||||
};
|
||||
|
||||
// NOTE: Proto64Bit class removed - wire type 1 (64-bit fixed) not supported
|
||||
@@ -221,6 +225,9 @@ class Proto32Bit {
|
||||
proto_check_bounds_failed(pos, n, proto_debug_end_, __builtin_FUNCTION()); \
|
||||
} while (0)
|
||||
void proto_check_bounds_failed(const uint8_t *pos, size_t bytes, const uint8_t *end, const char *caller);
|
||||
/// Aborts unless an encode body ended exactly where calculate_size() promised. A plain check rather than
|
||||
/// assert(), so NDEBUG cannot switch it off.
|
||||
void proto_check_encode_end(const uint8_t *end, const uint8_t *expected);
|
||||
#else
|
||||
#define PROTO_ENCODE_DEBUG_PARAM
|
||||
#define PROTO_ENCODE_DEBUG_ARG
|
||||
@@ -252,7 +259,7 @@ class ProtoWriteBuffer {
|
||||
*
|
||||
* Following https://protobuf.dev/programming-guides/encoding/#structure
|
||||
*/
|
||||
void encode_field_raw(uint32_t field_id, uint32_t type) { this->encode_varint_raw((field_id << 3) | type); }
|
||||
void encode_field_raw(uint32_t field_id, uint32_t type) { this->encode_varint_raw(proto_tag(field_id, type)); }
|
||||
/// Single-pass encode for repeated submessage elements.
|
||||
/// Thin template wrapper; all buffer work is in the non-template core.
|
||||
template<typename T> void encode_sub_message(uint32_t field_id, const T &value);
|
||||
@@ -287,19 +294,31 @@ class ProtoWriteBuffer {
|
||||
uint8_t *pos_;
|
||||
};
|
||||
|
||||
// A four byte unaligned store is a memcpy call on ESP-IDF (-fno-builtin-memcpy) and on ARM cores without
|
||||
// unaligned access (Cortex-M0+, ARM9), so those targets share one outlined byte store helper per fixed32
|
||||
// field. Elsewhere the write inlines to a single store, or on ESP8266 to a few stores that measured
|
||||
// faster than a call, so it stays inline.
|
||||
#if defined(USE_ESP32) || (defined(__arm__) && !defined(__ARM_FEATURE_UNALIGNED))
|
||||
#define PROTO_OUTLINE_FOR_SIZE __attribute__((noinline))
|
||||
#define PROTO_FIXED32_BYTE_STORES true
|
||||
#else
|
||||
#define PROTO_OUTLINE_FOR_SIZE inline
|
||||
#define PROTO_FIXED32_BYTE_STORES false
|
||||
#endif
|
||||
|
||||
// Varint encoding thresholds — used by both proto_encode_* free functions and ProtoSize.
|
||||
constexpr uint32_t VARINT_MAX_1_BYTE = 1 << 7; // 128
|
||||
constexpr uint32_t VARINT_MAX_2_BYTE = 1 << 14; // 16384
|
||||
|
||||
/// Static encode helpers for generated encode() functions.
|
||||
/// Generated code hoists buffer.pos_ into a local uint8_t *__restrict__ pos,
|
||||
/// then calls these methods which take pos by reference. No struct, no overhead.
|
||||
/// For sub-messages, pos is synced back to buffer before the call and reloaded after.
|
||||
/// Static encode helpers for the generated encode bodies. Each takes the write cursor by value and
|
||||
/// returns it advanced, so outlined calls at -Os chain through the return register instead of a
|
||||
/// stack slot. Helpers without a _force suffix skip fields holding the proto3 default.
|
||||
class ProtoEncode {
|
||||
public:
|
||||
/// Write a multi-byte varint directly through a pos pointer.
|
||||
template<typename T>
|
||||
static inline void encode_varint_raw_loop(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, T value) {
|
||||
[[nodiscard]] static inline uint8_t *encode_varint_raw_loop(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
T value) {
|
||||
do {
|
||||
PROTO_ENCODE_CHECK_BOUNDS(pos, 1);
|
||||
*pos++ = static_cast<uint8_t>(value | 0x80);
|
||||
@@ -307,48 +326,49 @@ class ProtoEncode {
|
||||
} while (value > 0x7F);
|
||||
PROTO_ENCODE_CHECK_BOUNDS(pos, 1);
|
||||
*pos++ = static_cast<uint8_t>(value);
|
||||
return pos;
|
||||
}
|
||||
static inline void ESPHOME_ALWAYS_INLINE encode_varint_raw(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
uint32_t value) {
|
||||
[[nodiscard]] static inline uint8_t *ESPHOME_ALWAYS_INLINE
|
||||
encode_varint_raw(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t value) {
|
||||
if (value < VARINT_MAX_1_BYTE) [[likely]] {
|
||||
PROTO_ENCODE_CHECK_BOUNDS(pos, 1);
|
||||
*pos++ = static_cast<uint8_t>(value);
|
||||
return;
|
||||
return pos;
|
||||
}
|
||||
encode_varint_raw_loop(pos PROTO_ENCODE_DEBUG_ARG, value);
|
||||
return encode_varint_raw_loop(pos PROTO_ENCODE_DEBUG_ARG, value);
|
||||
}
|
||||
/// Encode a varint that is expected to be 1-2 bytes (e.g. zigzag RSSI, small lengths).
|
||||
static inline void ESPHOME_ALWAYS_INLINE encode_varint_raw_short(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
uint32_t value) {
|
||||
[[nodiscard]] static inline uint8_t *ESPHOME_ALWAYS_INLINE
|
||||
encode_varint_raw_short(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t value) {
|
||||
if (value < VARINT_MAX_1_BYTE) [[likely]] {
|
||||
PROTO_ENCODE_CHECK_BOUNDS(pos, 1);
|
||||
*pos++ = static_cast<uint8_t>(value);
|
||||
return;
|
||||
return pos;
|
||||
}
|
||||
if (value < VARINT_MAX_2_BYTE) [[likely]] {
|
||||
PROTO_ENCODE_CHECK_BOUNDS(pos, 2);
|
||||
*pos++ = static_cast<uint8_t>(value | 0x80);
|
||||
*pos++ = static_cast<uint8_t>(value >> 7);
|
||||
return;
|
||||
return pos;
|
||||
}
|
||||
encode_varint_raw_loop(pos PROTO_ENCODE_DEBUG_ARG, value);
|
||||
return encode_varint_raw_loop(pos PROTO_ENCODE_DEBUG_ARG, value);
|
||||
}
|
||||
static inline void ESPHOME_ALWAYS_INLINE encode_varint_raw_64(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
uint64_t value) {
|
||||
[[nodiscard]] static inline uint8_t *ESPHOME_ALWAYS_INLINE
|
||||
encode_varint_raw_64(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint64_t value) {
|
||||
if (value < VARINT_MAX_1_BYTE) [[likely]] {
|
||||
PROTO_ENCODE_CHECK_BOUNDS(pos, 1);
|
||||
*pos++ = static_cast<uint8_t>(value);
|
||||
return;
|
||||
return pos;
|
||||
}
|
||||
encode_varint_raw_loop(pos PROTO_ENCODE_DEBUG_ARG, value);
|
||||
return encode_varint_raw_loop(pos PROTO_ENCODE_DEBUG_ARG, value);
|
||||
}
|
||||
/// Encode a 48-bit MAC address (stored in a uint64) as varint.
|
||||
/// Real MAC addresses occupy the full 48 bits (OUI in upper 24), so the
|
||||
/// fast path -- any non-zero bit in the top 6 of 48 -- emits exactly 7 bytes
|
||||
/// with no per-byte branch. Falls back to the general loop otherwise.
|
||||
/// Caller must guarantee value fits in 48 bits (checked in debug builds).
|
||||
static inline void ESPHOME_ALWAYS_INLINE encode_varint_raw_48bit(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
uint64_t value) {
|
||||
[[nodiscard]] static inline uint8_t *ESPHOME_ALWAYS_INLINE
|
||||
encode_varint_raw_48bit(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint64_t value) {
|
||||
#ifdef ESPHOME_DEBUG_API
|
||||
assert(value < (1ULL << (MAC_ADDRESS_SIZE * 8)) && "encode_varint_raw_48bit: value exceeds 48 bits");
|
||||
#endif
|
||||
@@ -363,38 +383,39 @@ class ProtoEncode {
|
||||
pos[4] = static_cast<uint8_t>((value >> 28) | 0x80);
|
||||
pos[5] = static_cast<uint8_t>((value >> 35) | 0x80);
|
||||
pos[6] = static_cast<uint8_t>(value >> 42);
|
||||
pos += 7;
|
||||
return;
|
||||
return pos + 7;
|
||||
}
|
||||
encode_varint_raw_64(pos PROTO_ENCODE_DEBUG_ARG, value);
|
||||
return encode_varint_raw_64(pos PROTO_ENCODE_DEBUG_ARG, value);
|
||||
}
|
||||
static inline void ESPHOME_ALWAYS_INLINE encode_field_raw(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
uint32_t field_id, uint32_t type) {
|
||||
encode_varint_raw(pos PROTO_ENCODE_DEBUG_ARG, (field_id << 3) | type);
|
||||
[[nodiscard]] static inline uint8_t *ESPHOME_ALWAYS_INLINE
|
||||
encode_field_raw(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, uint32_t type) {
|
||||
return encode_varint_raw(pos PROTO_ENCODE_DEBUG_ARG, proto_tag(field_id, type));
|
||||
}
|
||||
/// Write a single precomputed tag byte. Tag must be < 128.
|
||||
static inline void ESPHOME_ALWAYS_INLINE write_raw_byte(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
uint8_t b) {
|
||||
[[nodiscard]] static inline uint8_t *ESPHOME_ALWAYS_INLINE
|
||||
write_raw_byte(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint8_t b) {
|
||||
PROTO_ENCODE_CHECK_BOUNDS(pos, 1);
|
||||
*pos++ = b;
|
||||
return pos;
|
||||
}
|
||||
/// Reserve one byte for later backpatch (e.g., sub-message length).
|
||||
/// Advances pos past the reserved byte without writing a value.
|
||||
static inline void ESPHOME_ALWAYS_INLINE reserve_byte(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM) {
|
||||
[[nodiscard]] static inline uint8_t *ESPHOME_ALWAYS_INLINE
|
||||
reserve_byte(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM) {
|
||||
PROTO_ENCODE_CHECK_BOUNDS(pos, 1);
|
||||
pos++;
|
||||
return pos + 1;
|
||||
}
|
||||
/// Write raw bytes to the buffer (no tag, no length prefix).
|
||||
static inline void ESPHOME_ALWAYS_INLINE encode_raw(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
const void *data, size_t len) {
|
||||
[[nodiscard]] static inline uint8_t *ESPHOME_ALWAYS_INLINE
|
||||
encode_raw(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, const void *data, size_t len) {
|
||||
PROTO_ENCODE_CHECK_BOUNDS(pos, len);
|
||||
std::memcpy(pos, data, len);
|
||||
pos += len;
|
||||
return pos + len;
|
||||
}
|
||||
/// Encode tag + 1-byte length + raw string data. For strings with max_data_length < 128.
|
||||
/// Tag must be a single-byte varint (< 128). Always encodes (no zero check).
|
||||
static inline void encode_short_string_force(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint8_t tag,
|
||||
const StringRef &ref) {
|
||||
[[nodiscard]] static inline uint8_t *encode_short_string_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
uint8_t tag, const StringRef &ref) {
|
||||
#ifdef ESPHOME_DEBUG_API
|
||||
assert(ref.size() < 128 && "encode_short_string_force: string exceeds max_data_length < 128");
|
||||
#endif
|
||||
@@ -402,137 +423,191 @@ class ProtoEncode {
|
||||
pos[0] = tag;
|
||||
pos[1] = static_cast<uint8_t>(ref.size());
|
||||
std::memcpy(pos + 2, ref.c_str(), ref.size());
|
||||
pos += 2 + ref.size();
|
||||
return pos + 2 + ref.size();
|
||||
}
|
||||
/// Write a precomputed tag byte + 32-bit value in one operation.
|
||||
static inline void ESPHOME_ALWAYS_INLINE write_tag_and_fixed32(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
uint8_t tag, uint32_t value) {
|
||||
/// Write a precomputed tag byte + 32-bit value. Outlined on embedded: one copy beats inline stores per field.
|
||||
[[nodiscard]] static PROTO_OUTLINE_FOR_SIZE uint8_t *write_tag_and_fixed32(
|
||||
uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint8_t tag, uint32_t value) {
|
||||
PROTO_ENCODE_CHECK_BOUNDS(pos, 5);
|
||||
pos[0] = tag;
|
||||
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
||||
std::memcpy(pos + 1, &value, 4);
|
||||
#else
|
||||
pos[1] = static_cast<uint8_t>(value & 0xFF);
|
||||
pos[2] = static_cast<uint8_t>((value >> 8) & 0xFF);
|
||||
pos[3] = static_cast<uint8_t>((value >> 16) & 0xFF);
|
||||
pos[4] = static_cast<uint8_t>((value >> 24) & 0xFF);
|
||||
#endif
|
||||
pos += 5;
|
||||
write_fixed32_le(pos + 1, value);
|
||||
return pos + 5;
|
||||
}
|
||||
static inline void encode_string(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id,
|
||||
const char *string, size_t len, bool force = false) {
|
||||
if (len == 0 && !force)
|
||||
return;
|
||||
encode_field_raw(pos PROTO_ENCODE_DEBUG_ARG, field_id, 2); // type 2: Length-delimited string
|
||||
[[nodiscard]] static inline uint8_t *encode_string_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
uint32_t field_id, const char *string, size_t len) {
|
||||
pos = encode_field_raw(pos PROTO_ENCODE_DEBUG_ARG, field_id, 2); // type 2: Length-delimited string
|
||||
// NOLINTNEXTLINE(readability-inconsistent-ifelse-braces) -- false positive on [[likely]] attribute
|
||||
if (len < VARINT_MAX_1_BYTE) [[likely]] {
|
||||
PROTO_ENCODE_CHECK_BOUNDS(pos, 1 + len);
|
||||
*pos++ = static_cast<uint8_t>(len);
|
||||
} else {
|
||||
encode_varint_raw_loop(pos PROTO_ENCODE_DEBUG_ARG, len);
|
||||
pos = encode_varint_raw_loop(pos PROTO_ENCODE_DEBUG_ARG, len);
|
||||
PROTO_ENCODE_CHECK_BOUNDS(pos, len);
|
||||
}
|
||||
std::memcpy(pos, string, len);
|
||||
pos += len;
|
||||
return pos + len;
|
||||
}
|
||||
static inline void encode_string(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id,
|
||||
const std::string &value, bool force = false) {
|
||||
encode_string(pos PROTO_ENCODE_DEBUG_ARG, field_id, value.data(), value.size(), force);
|
||||
[[nodiscard]] static inline uint8_t *encode_string(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
uint32_t field_id, const char *string, size_t len) {
|
||||
if (len == 0)
|
||||
return pos;
|
||||
return encode_string_force(pos PROTO_ENCODE_DEBUG_ARG, field_id, string, len);
|
||||
}
|
||||
static inline void encode_string(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id,
|
||||
const StringRef &ref, bool force = false) {
|
||||
encode_string(pos PROTO_ENCODE_DEBUG_ARG, field_id, ref.c_str(), ref.size(), force);
|
||||
[[nodiscard]] static inline uint8_t *encode_string_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
uint32_t field_id, const std::string &value) {
|
||||
return encode_string_force(pos PROTO_ENCODE_DEBUG_ARG, field_id, value.data(), value.size());
|
||||
}
|
||||
static inline void encode_bytes(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id,
|
||||
const uint8_t *data, size_t len, bool force = false) {
|
||||
encode_string(pos PROTO_ENCODE_DEBUG_ARG, field_id, reinterpret_cast<const char *>(data), len, force);
|
||||
[[nodiscard]] static inline uint8_t *encode_string(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
uint32_t field_id, const StringRef &ref) {
|
||||
return encode_string(pos PROTO_ENCODE_DEBUG_ARG, field_id, ref.c_str(), ref.size());
|
||||
}
|
||||
static inline void encode_uint32(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id,
|
||||
uint32_t value, bool force = false) {
|
||||
if (value == 0 && !force)
|
||||
return;
|
||||
encode_field_raw(pos PROTO_ENCODE_DEBUG_ARG, field_id, 0);
|
||||
encode_varint_raw(pos PROTO_ENCODE_DEBUG_ARG, value);
|
||||
[[nodiscard]] static inline uint8_t *encode_string_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
uint32_t field_id, const StringRef &ref) {
|
||||
return encode_string_force(pos PROTO_ENCODE_DEBUG_ARG, field_id, ref.c_str(), ref.size());
|
||||
}
|
||||
static inline void encode_uint64(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id,
|
||||
uint64_t value, bool force = false) {
|
||||
if (value == 0 && !force)
|
||||
return;
|
||||
encode_field_raw(pos PROTO_ENCODE_DEBUG_ARG, field_id, 0);
|
||||
encode_varint_raw_64(pos PROTO_ENCODE_DEBUG_ARG, value);
|
||||
[[nodiscard]] static inline uint8_t *encode_bytes(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
uint32_t field_id, const uint8_t *data, size_t len) {
|
||||
return encode_string(pos PROTO_ENCODE_DEBUG_ARG, field_id, reinterpret_cast<const char *>(data), len);
|
||||
}
|
||||
static inline void encode_bool(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, bool value,
|
||||
bool force = false) {
|
||||
if (!value && !force)
|
||||
return;
|
||||
encode_field_raw(pos PROTO_ENCODE_DEBUG_ARG, field_id, 0);
|
||||
[[nodiscard]] static inline uint8_t *encode_bytes_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
uint32_t field_id, const uint8_t *data, size_t len) {
|
||||
return encode_string_force(pos PROTO_ENCODE_DEBUG_ARG, field_id, reinterpret_cast<const char *>(data), len);
|
||||
}
|
||||
[[nodiscard]] static inline uint8_t *encode_uint32_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
uint32_t field_id, uint32_t value) {
|
||||
pos = encode_field_raw(pos PROTO_ENCODE_DEBUG_ARG, field_id, 0);
|
||||
return encode_varint_raw(pos PROTO_ENCODE_DEBUG_ARG, value);
|
||||
}
|
||||
[[nodiscard]] static inline uint8_t *encode_uint32(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
uint32_t field_id, uint32_t value) {
|
||||
if (value == 0)
|
||||
return pos;
|
||||
return encode_uint32_force(pos PROTO_ENCODE_DEBUG_ARG, field_id, value);
|
||||
}
|
||||
[[nodiscard]] static inline uint8_t *encode_uint64_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
uint32_t field_id, uint64_t value) {
|
||||
pos = encode_field_raw(pos PROTO_ENCODE_DEBUG_ARG, field_id, 0);
|
||||
return encode_varint_raw_64(pos PROTO_ENCODE_DEBUG_ARG, value);
|
||||
}
|
||||
[[nodiscard]] static inline uint8_t *encode_uint64(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
uint32_t field_id, uint64_t value) {
|
||||
if (value == 0)
|
||||
return pos;
|
||||
return encode_uint64_force(pos PROTO_ENCODE_DEBUG_ARG, field_id, value);
|
||||
}
|
||||
[[nodiscard]] static inline uint8_t *encode_bool_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
uint32_t field_id, bool value) {
|
||||
pos = encode_field_raw(pos PROTO_ENCODE_DEBUG_ARG, field_id, 0);
|
||||
PROTO_ENCODE_CHECK_BOUNDS(pos, 1);
|
||||
*pos++ = value ? 0x01 : 0x00;
|
||||
return pos;
|
||||
}
|
||||
static inline void encode_fixed32(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id,
|
||||
uint32_t value, bool force = false) {
|
||||
if (value == 0 && !force)
|
||||
return;
|
||||
encode_field_raw(pos PROTO_ENCODE_DEBUG_ARG, field_id, 5);
|
||||
[[nodiscard]] static inline uint8_t *encode_bool(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
uint32_t field_id, bool value) {
|
||||
if (!value)
|
||||
return pos;
|
||||
return encode_bool_force(pos PROTO_ENCODE_DEBUG_ARG, field_id, value);
|
||||
}
|
||||
/// Tag + fixed32 for multi-byte tags; single-byte tags use write_tag_and_fixed32.
|
||||
[[nodiscard]] static PROTO_OUTLINE_FOR_SIZE uint8_t *encode_fixed32_force(
|
||||
uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, uint32_t value) {
|
||||
pos = encode_field_raw(pos PROTO_ENCODE_DEBUG_ARG, field_id, 5);
|
||||
PROTO_ENCODE_CHECK_BOUNDS(pos, 4);
|
||||
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
||||
std::memcpy(pos, &value, 4);
|
||||
pos += 4;
|
||||
#else
|
||||
*pos++ = (value >> 0) & 0xFF;
|
||||
*pos++ = (value >> 8) & 0xFF;
|
||||
*pos++ = (value >> 16) & 0xFF;
|
||||
*pos++ = (value >> 24) & 0xFF;
|
||||
#endif
|
||||
write_fixed32_le(pos, value);
|
||||
return pos + 4;
|
||||
}
|
||||
[[nodiscard]] static inline uint8_t *encode_fixed32(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
uint32_t field_id, uint32_t value) {
|
||||
if (value == 0)
|
||||
return pos;
|
||||
return encode_fixed32_force(pos PROTO_ENCODE_DEBUG_ARG, field_id, value);
|
||||
}
|
||||
// NOTE: Wire type 1 (64-bit fixed: double, fixed64, sfixed64) is intentionally
|
||||
// not supported to reduce overhead on embedded systems. All ESPHome devices are
|
||||
// 32-bit microcontrollers where 64-bit operations are expensive. If 64-bit support
|
||||
// is needed in the future, the necessary encoding/decoding functions must be added.
|
||||
static inline void encode_float(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, float value,
|
||||
bool force = false) {
|
||||
uint32_t raw = float_to_raw(value);
|
||||
if (raw == 0 && !force)
|
||||
return;
|
||||
encode_fixed32(pos PROTO_ENCODE_DEBUG_ARG, field_id, raw);
|
||||
[[nodiscard]] static inline uint8_t *encode_float(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
uint32_t field_id, float value) {
|
||||
return encode_fixed32(pos PROTO_ENCODE_DEBUG_ARG, field_id, float_to_raw(value));
|
||||
}
|
||||
static inline void encode_int32(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, int32_t value,
|
||||
bool force = false) {
|
||||
[[nodiscard]] static inline uint8_t *encode_float_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
uint32_t field_id, float value) {
|
||||
return encode_fixed32_force(pos PROTO_ENCODE_DEBUG_ARG, field_id, float_to_raw(value));
|
||||
}
|
||||
[[nodiscard]] static inline uint8_t *encode_int32_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
uint32_t field_id, int32_t value) {
|
||||
if (value < 0) {
|
||||
// negative int32 is always 10 byte long
|
||||
encode_uint64(pos PROTO_ENCODE_DEBUG_ARG, field_id, static_cast<uint64_t>(value), force);
|
||||
return;
|
||||
return encode_uint64_force(pos PROTO_ENCODE_DEBUG_ARG, field_id, static_cast<uint64_t>(value));
|
||||
}
|
||||
encode_uint32(pos PROTO_ENCODE_DEBUG_ARG, field_id, static_cast<uint32_t>(value), force);
|
||||
return encode_uint32_force(pos PROTO_ENCODE_DEBUG_ARG, field_id, static_cast<uint32_t>(value));
|
||||
}
|
||||
static inline void encode_int64(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, int64_t value,
|
||||
bool force = false) {
|
||||
encode_uint64(pos PROTO_ENCODE_DEBUG_ARG, field_id, static_cast<uint64_t>(value), force);
|
||||
[[nodiscard]] static inline uint8_t *encode_int32(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
uint32_t field_id, int32_t value) {
|
||||
if (value == 0)
|
||||
return pos;
|
||||
return encode_int32_force(pos PROTO_ENCODE_DEBUG_ARG, field_id, value);
|
||||
}
|
||||
static inline void encode_sint32(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id,
|
||||
int32_t value, bool force = false) {
|
||||
encode_uint32(pos PROTO_ENCODE_DEBUG_ARG, field_id, encode_zigzag32(value), force);
|
||||
[[nodiscard]] static inline uint8_t *encode_int64(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
uint32_t field_id, int64_t value) {
|
||||
return encode_uint64(pos PROTO_ENCODE_DEBUG_ARG, field_id, static_cast<uint64_t>(value));
|
||||
}
|
||||
static inline void encode_sint64(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id,
|
||||
int64_t value, bool force = false) {
|
||||
encode_uint64(pos PROTO_ENCODE_DEBUG_ARG, field_id, encode_zigzag64(value), force);
|
||||
[[nodiscard]] static inline uint8_t *encode_int64_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
uint32_t field_id, int64_t value) {
|
||||
return encode_uint64_force(pos PROTO_ENCODE_DEBUG_ARG, field_id, static_cast<uint64_t>(value));
|
||||
}
|
||||
/// Sub-message encoding: sync pos to buffer, delegate, get pos from return value.
|
||||
[[nodiscard]] static inline uint8_t *encode_sint32(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
uint32_t field_id, int32_t value) {
|
||||
return encode_uint32(pos PROTO_ENCODE_DEBUG_ARG, field_id, encode_zigzag32(value));
|
||||
}
|
||||
[[nodiscard]] static inline uint8_t *encode_sint32_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
uint32_t field_id, int32_t value) {
|
||||
return encode_uint32_force(pos PROTO_ENCODE_DEBUG_ARG, field_id, encode_zigzag32(value));
|
||||
}
|
||||
[[nodiscard]] static inline uint8_t *encode_sint64(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
uint32_t field_id, int64_t value) {
|
||||
return encode_uint64(pos PROTO_ENCODE_DEBUG_ARG, field_id, encode_zigzag64(value));
|
||||
}
|
||||
[[nodiscard]] static inline uint8_t *encode_sint64_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
uint32_t field_id, int64_t value) {
|
||||
return encode_uint64_force(pos PROTO_ENCODE_DEBUG_ARG, field_id, encode_zigzag64(value));
|
||||
}
|
||||
/// Sub-message encoding: sync pos to buffer, delegate, read the cursor back.
|
||||
template<typename T>
|
||||
static inline void encode_sub_message(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, ProtoWriteBuffer &buffer,
|
||||
uint32_t field_id, const T &value) {
|
||||
[[nodiscard]] static inline uint8_t *encode_sub_message(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
ProtoWriteBuffer &buffer, uint32_t field_id, const T &value) {
|
||||
buffer.set_pos(pos);
|
||||
buffer.encode_sub_message(field_id, value);
|
||||
pos = buffer.get_pos();
|
||||
return buffer.get_pos();
|
||||
}
|
||||
template<typename T>
|
||||
static inline void encode_optional_sub_message(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
ProtoWriteBuffer &buffer, uint32_t field_id, const T &value) {
|
||||
[[nodiscard]] static inline uint8_t *encode_optional_sub_message(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
ProtoWriteBuffer &buffer, uint32_t field_id,
|
||||
const T &value) {
|
||||
buffer.set_pos(pos);
|
||||
buffer.encode_optional_sub_message(field_id, value);
|
||||
pos = buffer.get_pos();
|
||||
return buffer.get_pos();
|
||||
}
|
||||
|
||||
private:
|
||||
/// Unaligned little endian store of four bytes: byte stores where the outlined helper lives (ESP-IDF, ARM
|
||||
/// without unaligned access), otherwise a memcpy the compiler folds into one store. Callers bounds check
|
||||
/// and advance the cursor themselves.
|
||||
static inline void ESPHOME_ALWAYS_INLINE write_fixed32_le(uint8_t *__restrict__ pos, uint32_t value) {
|
||||
if constexpr (PROTO_FIXED32_BYTE_STORES) {
|
||||
// Spelled out so the outlined helper does not itself become a memcpy call
|
||||
pos[0] = static_cast<uint8_t>(value);
|
||||
pos[1] = static_cast<uint8_t>(value >> 8);
|
||||
pos[2] = static_cast<uint8_t>(value >> 16);
|
||||
pos[3] = static_cast<uint8_t>(value >> 24);
|
||||
} else {
|
||||
const uint32_t le = convert_little_endian(value);
|
||||
__builtin_memcpy(pos, &le, 4);
|
||||
}
|
||||
}
|
||||
};
|
||||
#undef PROTO_OUTLINE_FOR_SIZE
|
||||
#undef PROTO_FIXED32_BYTE_STORES
|
||||
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
/**
|
||||
@@ -624,11 +699,12 @@ class DumpBuffer {
|
||||
|
||||
class ProtoMessage {
|
||||
public:
|
||||
// Non-virtual defaults for messages with no fields.
|
||||
// Concrete message classes hide these with their own implementations.
|
||||
// All call sites use templates to preserve the concrete type, so virtual
|
||||
// dispatch is not needed. This eliminates per-message vtable entries for
|
||||
// encode/calculate_size, saving ~1.3 KB of flash across all message types.
|
||||
// Non-virtual defaults for messages with no fields; generated classes hide all four. The
|
||||
// static encode_msg/calc_size_msg take const void * so &T::encode_msg needs no thunk.
|
||||
static uint8_t *encode_msg(const void *self, ProtoWriteBuffer &buffer PROTO_ENCODE_DEBUG_PARAM) {
|
||||
return buffer.get_pos();
|
||||
}
|
||||
static uint32_t calc_size_msg(const void *self) { return 0; }
|
||||
uint8_t *encode(ProtoWriteBuffer &buffer PROTO_ENCODE_DEBUG_PARAM) const { return buffer.get_pos(); }
|
||||
uint32_t calculate_size() const { return 0; }
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
@@ -648,7 +724,13 @@ class ProtoMessage {
|
||||
// Base class for messages that support decoding
|
||||
class ProtoDecodableMessage : public ProtoMessage {
|
||||
public:
|
||||
void decode(const uint8_t *buffer, size_t length);
|
||||
/// Stores one decoded field into \p msg; generated per message type. \p scalar is the varint or
|
||||
/// fixed32 value, or the length of the length-delimited payload at \p data. An unknown field or
|
||||
/// wrong wire type matches no case and is skipped.
|
||||
using DecodeFieldFn = void (*)(void *msg, uint32_t tag, const uint8_t *data, proto_varint_value_t scalar);
|
||||
/// Walk \p buffer and hand every field to \p field. The generated decode() passes the message's
|
||||
/// own decode_field, so decodable messages carry no vtable.
|
||||
static void decode_fields(void *msg, const uint8_t *buffer, size_t length, DecodeFieldFn field);
|
||||
|
||||
/**
|
||||
* Count occurrences of a repeated field in a protobuf buffer.
|
||||
@@ -660,14 +742,15 @@ class ProtoDecodableMessage : public ProtoMessage {
|
||||
* @return Number of times the field appears in the buffer
|
||||
*/
|
||||
static uint32_t count_repeated_field(const uint8_t *buffer, size_t length, uint32_t target_field_id);
|
||||
|
||||
protected:
|
||||
~ProtoDecodableMessage() = default;
|
||||
virtual bool decode_varint(uint32_t field_id, proto_varint_value_t value) { return false; }
|
||||
virtual bool decode_length(uint32_t field_id, ProtoLengthDelimited value) { return false; }
|
||||
virtual bool decode_32bit(uint32_t field_id, Proto32Bit value) { return false; }
|
||||
// NOTE: decode_64bit removed - wire type 1 not supported
|
||||
// The destructor stays accessible on purpose: the generated messages are aggregates that brace
|
||||
// initialise sub message members, which copies a base temporary. That trades away the compile time
|
||||
// guard against deleting through this type; messages are stack locals and never owned through a base
|
||||
// pointer. ProtoMessage keeps its guard for the dump builds.
|
||||
};
|
||||
#ifndef HAS_PROTO_MESSAGE_DUMP
|
||||
// decode() passes decode_field explicitly, so nothing here may add a vtable
|
||||
static_assert(!std::is_polymorphic_v<ProtoDecodableMessage>, "decodable messages carry no vtable");
|
||||
#endif
|
||||
|
||||
class ProtoSize {
|
||||
public:
|
||||
@@ -792,7 +875,7 @@ class ProtoSize {
|
||||
* @return The number of bytes needed to encode the field ID and wire type
|
||||
*/
|
||||
static constexpr uint32_t field(uint32_t field_id, uint32_t type) {
|
||||
uint32_t tag = (field_id << 3) | (type & WIRE_TYPE_MASK);
|
||||
uint32_t tag = proto_tag(field_id, type & WIRE_TYPE_MASK);
|
||||
return varint(tag);
|
||||
}
|
||||
|
||||
@@ -876,24 +959,14 @@ class ProtoSize {
|
||||
|
||||
// Implementation of methods that depend on ProtoSize being fully defined
|
||||
|
||||
// Encode thunk — converts void* back to concrete type for direct encode() call
|
||||
template<typename T> uint8_t *proto_encode_msg(const void *msg, ProtoWriteBuffer &buf PROTO_ENCODE_DEBUG_PARAM) {
|
||||
return static_cast<const T *>(msg)->encode(buf PROTO_ENCODE_DEBUG_ARG);
|
||||
}
|
||||
|
||||
// Thin template wrapper; delegates to non-template core in proto.cpp.
|
||||
template<typename T> inline void ProtoWriteBuffer::encode_sub_message(uint32_t field_id, const T &value) {
|
||||
this->encode_sub_message(field_id, &value, &proto_encode_msg<T>);
|
||||
this->encode_sub_message(field_id, &value, &T::encode_msg);
|
||||
}
|
||||
|
||||
// Thin template wrapper; delegates to non-template core.
|
||||
template<typename T> inline void ProtoWriteBuffer::encode_optional_sub_message(uint32_t field_id, const T &value) {
|
||||
this->encode_optional_sub_message(field_id, value.calculate_size(), &value, &proto_encode_msg<T>);
|
||||
}
|
||||
|
||||
// Template decode_to_message - preserves concrete type so decode() resolves statically
|
||||
template<typename T> void ProtoLengthDelimited::decode_to_message(T &msg) const {
|
||||
msg.decode(this->value_, this->length_);
|
||||
this->encode_optional_sub_message(field_id, T::calc_size_msg(&value), &value, &T::encode_msg);
|
||||
}
|
||||
|
||||
template<typename T> const char *proto_enum_to_string(T value);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import i2c, sensor
|
||||
from esphome.components.const import UNIT_COUNTS
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
CONF_CLEAR,
|
||||
@@ -33,8 +34,6 @@ CONF_F7 = "f7"
|
||||
CONF_F8 = "f8"
|
||||
CONF_NIR = "nir"
|
||||
|
||||
UNIT_COUNTS = "#"
|
||||
|
||||
AS7341_GAIN = as7341_ns.enum("AS7341Gain")
|
||||
GAIN_OPTIONS = {
|
||||
"X0.5": AS7341_GAIN.AS7341_GAIN_0_5X,
|
||||
|
||||
@@ -18,26 +18,6 @@ struct MultiClickTriggerEvent {
|
||||
uint32_t max_length;
|
||||
};
|
||||
|
||||
class PressTrigger final : public Trigger<> {
|
||||
public:
|
||||
explicit PressTrigger(BinarySensor *parent) {
|
||||
parent->add_on_state_callback([this](bool state) {
|
||||
if (state)
|
||||
this->trigger();
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
class ReleaseTrigger final : public Trigger<> {
|
||||
public:
|
||||
explicit ReleaseTrigger(BinarySensor *parent) {
|
||||
parent->add_on_state_callback([this](bool state) {
|
||||
if (!state)
|
||||
this->trigger();
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
bool match_interval(uint32_t min_length, uint32_t max_length, uint32_t length);
|
||||
|
||||
class ClickTrigger final : public Trigger<> {
|
||||
@@ -140,21 +120,6 @@ template<size_t N> class MultiClickTrigger final : public MultiClickTriggerBase
|
||||
std::array<MultiClickTriggerEvent, N> timing_storage_{};
|
||||
};
|
||||
|
||||
class StateTrigger final : public Trigger<bool> {
|
||||
public:
|
||||
explicit StateTrigger(BinarySensor *parent) {
|
||||
parent->add_on_state_callback([this](bool state) { this->trigger(state); });
|
||||
}
|
||||
};
|
||||
|
||||
class StateChangeTrigger final : public Trigger<optional<bool>, optional<bool> > {
|
||||
public:
|
||||
explicit StateChangeTrigger(BinarySensor *parent) {
|
||||
parent->add_full_state_callback(
|
||||
[this](optional<bool> old_state, optional<bool> state) { this->trigger(old_state, state); });
|
||||
}
|
||||
};
|
||||
|
||||
template<typename... Ts> class BinarySensorInvalidateAction final : public Action<Ts...> {
|
||||
public:
|
||||
explicit BinarySensorInvalidateAction(BinarySensor *sensor) : sensor_(sensor) {}
|
||||
|
||||
@@ -16,11 +16,4 @@ template<typename... Ts> class PressAction final : public Action<Ts...> {
|
||||
Button *button_;
|
||||
};
|
||||
|
||||
class ButtonPressTrigger final : public Trigger<> {
|
||||
public:
|
||||
ButtonPressTrigger(Button *button) {
|
||||
button->add_on_press_callback([this]() { this->trigger(); });
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace esphome::button
|
||||
|
||||
@@ -61,3 +61,4 @@ ICON_SOLAR_POWER = "mdi:solar-power"
|
||||
KEY_METADATA = "metadata"
|
||||
|
||||
UNIT_AMPERE_HOUR = "Ah"
|
||||
UNIT_COUNTS = "#"
|
||||
|
||||
@@ -3,7 +3,7 @@ import encodings
|
||||
from esphome import automation
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import esp32_ble
|
||||
from esphome.components.const import CONF_MANUFACTURER
|
||||
from esphome.components.const import CONF_DESCRIPTION, CONF_MANUFACTURER
|
||||
from esphome.components.esp32 import request_bluetooth
|
||||
from esphome.components.esp32_ble import BTLoggers, bt_uuid
|
||||
import esphome.config_validation as cv
|
||||
@@ -37,7 +37,6 @@ CONF_ADVERTISE = "advertise"
|
||||
CONF_APPEARANCE = "appearance"
|
||||
CONF_BROADCAST = "broadcast"
|
||||
CONF_CHARACTERISTICS = "characteristics"
|
||||
CONF_DESCRIPTION = "description"
|
||||
CONF_DESCRIPTORS = "descriptors"
|
||||
CONF_ENDIANNESS = "endianness"
|
||||
CONF_FIRMWARE_VERSION = "firmware_version"
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/components/event/event.h"
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/core/component.h"
|
||||
|
||||
namespace esphome::event {
|
||||
|
||||
class EventTrigger final : public Trigger<StringRef> {
|
||||
public:
|
||||
EventTrigger(Event *event) {
|
||||
event->add_on_event_callback([this](StringRef event_type) { this->trigger(event_type); });
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace esphome::event
|
||||
@@ -22,6 +22,7 @@ from esphome.const import (
|
||||
UNIT_AMPERE,
|
||||
UNIT_CELSIUS,
|
||||
UNIT_HERTZ,
|
||||
UNIT_KILOWATT_HOURS,
|
||||
UNIT_VOLT,
|
||||
UNIT_WATT,
|
||||
)
|
||||
@@ -33,7 +34,6 @@ CONF_TOTAL_GENERATION_TIME = "total_generation_time"
|
||||
CONF_TODAY_GENERATION_TIME = "today_generation_time"
|
||||
CONF_PV1 = "pv1"
|
||||
CONF_PV2 = "pv2"
|
||||
UNIT_KILOWATT_HOURS = "kWh"
|
||||
UNIT_HOURS = "h"
|
||||
UNIT_KOHM = "kΩ"
|
||||
UNIT_MILLIAMPERE = "mA"
|
||||
|
||||
@@ -23,6 +23,7 @@ from esphome.const import (
|
||||
UNIT_AMPERE,
|
||||
UNIT_DEGREES,
|
||||
UNIT_HERTZ,
|
||||
UNIT_KILOWATT_HOURS,
|
||||
UNIT_MINUTE,
|
||||
UNIT_VOLT,
|
||||
UNIT_VOLT_AMPS_REACTIVE,
|
||||
@@ -36,7 +37,6 @@ CONF_TOTAL_GENERATION_TIME = "total_generation_time"
|
||||
CONF_TODAY_GENERATION_TIME = "today_generation_time"
|
||||
CONF_PV1 = "pv1"
|
||||
CONF_PV2 = "pv2"
|
||||
UNIT_KILOWATT_HOURS = "kWh"
|
||||
UNIT_HOURS = "h"
|
||||
UNIT_KOHM = "kΩ"
|
||||
UNIT_MILLIAMPERE = "mA"
|
||||
|
||||
@@ -24,6 +24,7 @@ from esphome.const import (
|
||||
STATE_CLASS_TOTAL_INCREASING,
|
||||
UNIT_AMPERE,
|
||||
UNIT_CELSIUS,
|
||||
UNIT_MILLIVOLT,
|
||||
UNIT_VOLT,
|
||||
UNIT_WATT,
|
||||
UNIT_WATT_HOURS,
|
||||
@@ -44,7 +45,6 @@ CONF_TEMPERATURE_COEFFICIENT = "temperature_coefficient"
|
||||
CONF_RESET_ON_BOOT = "reset_on_boot"
|
||||
UNIT_COULOMB = "C"
|
||||
UNIT_JOULE = "J"
|
||||
UNIT_MILLIVOLT = "mV"
|
||||
|
||||
ina2xx_base_ns = cg.esphome_ns.namespace("ina2xx_base")
|
||||
INA2XX = ina2xx_base_ns.class_("INA2XX", cg.PollingComponent)
|
||||
|
||||
@@ -343,6 +343,7 @@ RESTORE_MODES = {
|
||||
# Schema default that also matches the C++ initializer in light_state.h; codegen
|
||||
# skips the setter when the config equals it.
|
||||
DEFAULT_FLASH_TRANSITION_LENGTH = "0s"
|
||||
CONF_TRANSITION_STATE_PUBLISH_INTERVAL = "transition_state_publish_interval"
|
||||
|
||||
LIGHT_SCHEMA = (
|
||||
cv.ENTITY_BASE_SCHEMA.extend(web_server.WEBSERVER_SORTING_SCHEMA)
|
||||
@@ -393,6 +394,11 @@ BRIGHTNESS_ONLY_LIGHT_SCHEMA = LIGHT_SCHEMA.extend(
|
||||
cv.Optional(
|
||||
CONF_FLASH_TRANSITION_LENGTH, default=DEFAULT_FLASH_TRANSITION_LENGTH
|
||||
): cv.positive_time_period_milliseconds,
|
||||
# Below 150ms a device cannot publish any faster and only spends CPU and traffic
|
||||
cv.Optional(CONF_TRANSITION_STATE_PUBLISH_INTERVAL): cv.All(
|
||||
cv.positive_time_period_milliseconds,
|
||||
cv.Range(min=cv.TimePeriod(milliseconds=150)),
|
||||
),
|
||||
cv.Optional(CONF_EFFECTS): validate_effects(MONOCHROMATIC_EFFECTS),
|
||||
}
|
||||
)
|
||||
@@ -406,6 +412,11 @@ RGB_LIGHT_SCHEMA = BRIGHTNESS_ONLY_LIGHT_SCHEMA.extend(
|
||||
ADDRESSABLE_LIGHT_SCHEMA = RGB_LIGHT_SCHEMA.extend(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(AddressableLightState),
|
||||
# The addressable transformer writes the LED buffer directly, so there is no
|
||||
# intermediate state to publish
|
||||
cv.Optional(CONF_TRANSITION_STATE_PUBLISH_INTERVAL): cv.invalid(
|
||||
"transition_state_publish_interval is not supported on addressable lights"
|
||||
),
|
||||
cv.Optional(CONF_EFFECTS): validate_effects(ADDRESSABLE_EFFECTS),
|
||||
cv.Optional(CONF_COLOR_CORRECT): cv.All(
|
||||
[cv.percentage], cv.Length(min=3, max=4)
|
||||
@@ -513,6 +524,10 @@ async def setup_light_core_(light_var, config, output_var):
|
||||
DEFAULT_FLASH_TRANSITION_LENGTH
|
||||
):
|
||||
cg.add(light_var.set_flash_transition_length(flash_transition_length))
|
||||
# Setting an interval opts this light in and compiles the feature in
|
||||
if (interval := config.get(CONF_TRANSITION_STATE_PUBLISH_INTERVAL)) is not None:
|
||||
cg.add(light_var.set_transition_state_publish_interval(interval))
|
||||
cg.add_define("USE_LIGHT_TRANSITION_PUBLISH_INTERVAL")
|
||||
if (gamma_correct := config.get(CONF_GAMMA_CORRECT)) is not None:
|
||||
cg.add(light_var.set_gamma_correct(gamma_correct))
|
||||
fwd_arr = _get_or_create_gamma_table(gamma_correct)
|
||||
|
||||
@@ -32,7 +32,7 @@ void LightJSONSchema::dump_json(LightState &state, JsonObject root) {
|
||||
root[ESPHOME_F("effect_count")] = state.get_effect_count();
|
||||
}
|
||||
|
||||
auto values = state.remote_values;
|
||||
auto values = state.get_reported_values();
|
||||
|
||||
const auto color_mode = values.get_color_mode();
|
||||
const auto *mode_str = get_color_mode_json_str(color_mode);
|
||||
@@ -131,6 +131,13 @@ void LightJSONSchema::parse_color_json(LightState &state, LightCall &call, JsonO
|
||||
call.set_white(float(root[ESPHOME_F("white_value")]) / 255.0f);
|
||||
}
|
||||
|
||||
if (root[ESPHOME_F("white")].is<uint8_t>()) {
|
||||
// White stays full because ESPHome multiplies brightness and white
|
||||
call.set_color_mode_if_supported(ColorMode::WHITE);
|
||||
call.set_brightness(float(root[ESPHOME_F("white")]) / 255.0f);
|
||||
call.set_white(1.0f);
|
||||
}
|
||||
|
||||
if (root[ESPHOME_F("color_temp")].is<uint16_t>()) {
|
||||
call.set_color_temperature(float(root[ESPHOME_F("color_temp")]));
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
#include "light_state.h"
|
||||
#include "esp_color_correction.h"
|
||||
#include "esphome/core/defines.h"
|
||||
#ifdef USE_LIGHT_TRANSITION_PUBLISH_INTERVAL
|
||||
#include "esphome/core/application.h"
|
||||
#endif
|
||||
#include "esphome/core/controller_registry.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "light_output.h"
|
||||
@@ -105,6 +108,13 @@ void LightState::dump_config() {
|
||||
" Default Transition Length: %.1fs\n"
|
||||
" Gamma Correct: %.2f",
|
||||
this->default_transition_length_ / 1e3f, this->gamma_correct_);
|
||||
#ifdef USE_LIGHT_TRANSITION_PUBLISH_INTERVAL
|
||||
// The define is build wide; only lights that set the option have an interval
|
||||
if (this->transition_state_publish_interval_ != 0) {
|
||||
ESP_LOGCONFIG(TAG, " Transition State Publish Interval: %" PRIu32 "ms",
|
||||
this->transition_state_publish_interval_);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
if (traits.supports_color_capability(ColorCapability::COLOR_TEMPERATURE)) {
|
||||
ESP_LOGCONFIG(TAG,
|
||||
@@ -130,13 +140,30 @@ void LightState::loop() {
|
||||
this->next_write_ = true;
|
||||
}
|
||||
|
||||
if (this->transformer_->is_finished()) {
|
||||
const bool finished = this->transformer_->is_finished();
|
||||
#ifdef USE_LIGHT_TRANSITION_PUBLISH_INTERVAL
|
||||
if (this->transition_publish_enabled_ && !finished) {
|
||||
const uint32_t now = App.get_loop_component_start_time();
|
||||
if (now - this->last_transition_state_publish_ >= this->transition_state_publish_interval_) {
|
||||
this->publish_state();
|
||||
this->last_transition_state_publish_ = now;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if (finished) {
|
||||
// if the transition has written directly to the output, current_values is outdated, so update it
|
||||
this->current_values = this->transformer_->get_target_values();
|
||||
|
||||
this->transformer_->stop();
|
||||
this->is_transformer_active_ = false;
|
||||
this->transformer_ = nullptr;
|
||||
#ifdef USE_LIGHT_TRANSITION_PUBLISH_INTERVAL
|
||||
if (this->transition_publish_enabled_) {
|
||||
// Report the end state from remote_values; a flash's stop() left publishing to us
|
||||
this->transition_publish_enabled_ = false;
|
||||
this->publish_state();
|
||||
}
|
||||
#endif
|
||||
if (this->target_state_reached_listeners_) {
|
||||
for (auto *listener : *this->target_state_reached_listeners_) {
|
||||
listener->on_light_target_state_reached();
|
||||
@@ -348,10 +375,7 @@ void LightState::stop_effect_() {
|
||||
void LightState::start_transition_(const LightColorValues &target, uint32_t length, bool set_remote_values) {
|
||||
this->transformer_ = this->output_->create_default_transition();
|
||||
this->transformer_->setup(this->current_values, target, length);
|
||||
|
||||
if (set_remote_values) {
|
||||
this->remote_values = target;
|
||||
}
|
||||
this->set_transformer_remote_values_(target, set_remote_values);
|
||||
// Enable loop while transition is active
|
||||
this->enable_loop();
|
||||
}
|
||||
@@ -365,10 +389,7 @@ void LightState::start_flash_(const LightColorValues &target, uint32_t length, b
|
||||
|
||||
this->transformer_ = make_unique<LightFlashTransformer>(*this);
|
||||
this->transformer_->setup(end_colors, target, length);
|
||||
|
||||
if (set_remote_values) {
|
||||
this->remote_values = target;
|
||||
};
|
||||
this->set_transformer_remote_values_(target, set_remote_values);
|
||||
// Enable loop while flash is active
|
||||
this->enable_loop();
|
||||
}
|
||||
@@ -376,6 +397,9 @@ void LightState::start_flash_(const LightColorValues &target, uint32_t length, b
|
||||
void LightState::set_immediately_(const LightColorValues &target, bool set_remote_values) {
|
||||
this->is_transformer_active_ = false;
|
||||
this->transformer_ = nullptr;
|
||||
#ifdef USE_LIGHT_TRANSITION_PUBLISH_INTERVAL
|
||||
this->transition_publish_enabled_ = false;
|
||||
#endif
|
||||
this->current_values = target;
|
||||
if (set_remote_values) {
|
||||
this->remote_values = target;
|
||||
@@ -391,6 +415,18 @@ void LightState::disable_loop_if_idle_() {
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef USE_LIGHT_TRANSITION_PUBLISH_INTERVAL
|
||||
void LightState::set_transformer_remote_values_(const LightColorValues &target, bool set_remote_values) {
|
||||
this->transition_publish_enabled_ = set_remote_values && this->transition_state_publish_interval_ > 0;
|
||||
if (this->transition_publish_enabled_) {
|
||||
this->last_transition_state_publish_ = App.get_loop_component_start_time();
|
||||
}
|
||||
if (set_remote_values) {
|
||||
this->remote_values = target;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
void LightState::save_remote_values_() {
|
||||
LightStateRTCState saved;
|
||||
saved.color_mode = this->remote_values.get_color_mode();
|
||||
|
||||
@@ -100,6 +100,21 @@ class LightState : public EntityBase, public Component {
|
||||
LightCall turn_on();
|
||||
LightCall turn_off();
|
||||
LightCall toggle();
|
||||
|
||||
/// The values reported to the frontend: current_values while a light publishes intermediate
|
||||
/// states on an interval, otherwise remote_values. Each interval sample is a publish_state(),
|
||||
/// so on_state automations run on every sample as well.
|
||||
const LightColorValues &get_reported_values() const {
|
||||
#ifdef USE_LIGHT_TRANSITION_PUBLISH_INTERVAL
|
||||
if (this->transition_publish_enabled_) {
|
||||
return this->current_values;
|
||||
}
|
||||
#endif
|
||||
return this->remote_values;
|
||||
}
|
||||
|
||||
/// True from the call that starts a transition or flash until it reaches its target.
|
||||
bool is_transitioning() const { return this->transformer_ != nullptr; }
|
||||
LightCall make_call();
|
||||
|
||||
// ========== INTERNAL METHODS ==========
|
||||
@@ -172,6 +187,13 @@ class LightState : public EntityBase, public Component {
|
||||
void set_gamma_correct(float gamma_correct) { this->gamma_correct_ = gamma_correct; }
|
||||
float get_gamma_correct() const { return this->gamma_correct_; }
|
||||
|
||||
#ifdef USE_LIGHT_TRANSITION_PUBLISH_INTERVAL
|
||||
void set_transition_state_publish_interval(uint32_t transition_state_publish_interval) {
|
||||
this->transition_state_publish_interval_ = transition_state_publish_interval;
|
||||
}
|
||||
uint32_t get_transition_state_publish_interval() const { return this->transition_state_publish_interval_; }
|
||||
#endif
|
||||
|
||||
#ifdef USE_LIGHT_GAMMA_LUT
|
||||
/// Set pre-computed gamma forward lookup table (256-entry uint16 PROGMEM array)
|
||||
void set_gamma_table(const uint16_t *forward) { this->gamma_table_ = forward; }
|
||||
@@ -291,6 +313,7 @@ class LightState : public EntityBase, public Component {
|
||||
friend LightOutput;
|
||||
friend LightCall;
|
||||
friend class AddressableLight;
|
||||
friend class LightFlashTransformer;
|
||||
|
||||
/// Internal method to start an effect with the given index
|
||||
void start_effect_(uint32_t effect_index);
|
||||
@@ -307,6 +330,18 @@ class LightState : public EntityBase, public Component {
|
||||
/// Internal method to set the color values to target immediately (with no transition).
|
||||
void set_immediately_(const LightColorValues &target, bool set_remote_values);
|
||||
|
||||
/// Point remote_values at the new transformer's target and, when this light publishes
|
||||
/// intermediate states on an interval, start the interval clock.
|
||||
#ifdef USE_LIGHT_TRANSITION_PUBLISH_INTERVAL
|
||||
void set_transformer_remote_values_(const LightColorValues &target, bool set_remote_values);
|
||||
#else
|
||||
void set_transformer_remote_values_(const LightColorValues &target, bool set_remote_values) {
|
||||
if (set_remote_values) {
|
||||
this->remote_values = target;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Internal method to save the current remote_values to the preferences
|
||||
void save_remote_values_();
|
||||
|
||||
@@ -357,6 +392,10 @@ class LightState : public EntityBase, public Component {
|
||||
uint32_t default_transition_length_{};
|
||||
/// Transition length to use for flash transitions.
|
||||
uint32_t flash_transition_length_{}; // Keep in sync with DEFAULT_FLASH_TRANSITION_LENGTH in __init__.py
|
||||
#ifdef USE_LIGHT_TRANSITION_PUBLISH_INTERVAL
|
||||
uint32_t transition_state_publish_interval_{0};
|
||||
uint32_t last_transition_state_publish_{0};
|
||||
#endif
|
||||
/// Gamma correction factor for the light.
|
||||
float gamma_correct_{};
|
||||
#ifdef USE_LIGHT_GAMMA_LUT
|
||||
@@ -367,6 +406,10 @@ class LightState : public EntityBase, public Component {
|
||||
bool next_write_{true};
|
||||
// for effects, true if a transformer (transition) is active.
|
||||
bool is_transformer_active_{false};
|
||||
#ifdef USE_LIGHT_TRANSITION_PUBLISH_INTERVAL
|
||||
/// True while the active transformer publishes current_values on an interval from loop().
|
||||
bool transition_publish_enabled_{false};
|
||||
#endif
|
||||
/// Restore mode of the light.
|
||||
LightRestoreMode restore_mode_;
|
||||
};
|
||||
|
||||
@@ -105,6 +105,11 @@ class LightFlashTransformer : public LightTransformer {
|
||||
}
|
||||
this->state_.current_values = this->get_start_values();
|
||||
this->state_.remote_values = this->get_start_values();
|
||||
#ifdef USE_LIGHT_TRANSITION_PUBLISH_INTERVAL
|
||||
// The light reports the end state itself once the transformer finishes
|
||||
if (this->state_.transition_publish_enabled_)
|
||||
return;
|
||||
#endif
|
||||
this->state_.publish_state();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import i2c, sensor
|
||||
from esphome.components.const import UNIT_COUNTS
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
CONF_AMBIENT_LIGHT,
|
||||
@@ -28,7 +29,6 @@ CONF_UV_INDEX = "uv_index"
|
||||
CONF_UV = "uv"
|
||||
CONF_WINDOW_CORRECTION_FACTOR = "window_correction_factor"
|
||||
|
||||
UNIT_COUNTS = "#"
|
||||
UNIT_UVI = "UVI"
|
||||
|
||||
LTR390GAIN = ltr390_ns.enum("LTR390GAIN")
|
||||
|
||||
@@ -3,6 +3,7 @@ from typing import Any
|
||||
from esphome import automation
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import i2c, sensor
|
||||
from esphome.components.const import UNIT_COUNTS
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
CONF_ACTUAL_GAIN,
|
||||
@@ -42,7 +43,6 @@ CONF_PS_LOW_THRESHOLD = "ps_low_threshold"
|
||||
ICON_BRIGHTNESS_7 = "mdi:brightness-7"
|
||||
ICON_GAIN = "mdi:multiplication"
|
||||
ICON_PROXIMITY = "mdi:hand-wave-outline"
|
||||
UNIT_COUNTS = "#"
|
||||
|
||||
ltr501_ns = cg.esphome_ns.namespace("ltr501")
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ from typing import Any
|
||||
from esphome import automation
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import i2c, sensor
|
||||
from esphome.components.const import UNIT_COUNTS
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
CONF_ACTUAL_GAIN,
|
||||
@@ -41,7 +42,6 @@ CONF_PS_LOW_THRESHOLD = "ps_low_threshold"
|
||||
ICON_BRIGHTNESS_7 = "mdi:brightness-7"
|
||||
ICON_GAIN = "mdi:multiplication"
|
||||
ICON_PROXIMITY = "mdi:hand-wave-outline"
|
||||
UNIT_COUNTS = "#"
|
||||
|
||||
ltr_als_ps_ns = cg.esphome_ns.namespace("ltr_als_ps")
|
||||
|
||||
|
||||
@@ -680,7 +680,6 @@ CONF_BODY = "body"
|
||||
CONF_BUTTONS = "buttons"
|
||||
CONF_CHANGE_RATE = "change_rate"
|
||||
CONF_CLOSE_BUTTON = "close_button"
|
||||
CONF_COLOR_DEPTH = "color_depth"
|
||||
CONF_COLOR_END = "color_end"
|
||||
CONF_COLOR_START = "color_start"
|
||||
CONF_CONTAINER = "container"
|
||||
@@ -767,7 +766,6 @@ CONF_RESUME_ON_INPUT = "resume_on_input"
|
||||
CONF_RIGHT_BUTTON = "right_button"
|
||||
CONF_ROLLOVER = "rollover"
|
||||
CONF_ROOT_BACK_BTN = "root_back_btn"
|
||||
CONF_ROWS = "rows"
|
||||
CONF_SCALE = "scale"
|
||||
CONF_SCALE_LINES = "scale_lines"
|
||||
CONF_SCROLLBAR_MODE = "scrollbar_mode"
|
||||
|
||||
@@ -614,7 +614,6 @@ class Nextion final : public NextionBase, public PollingComponent, public uart::
|
||||
*/
|
||||
void disable_component_touch(const char *component);
|
||||
|
||||
#ifdef USE_NEXTION_WAVEFORM
|
||||
/**
|
||||
* Add waveform data to a waveform component
|
||||
* @param component_id The integer component id.
|
||||
@@ -624,7 +623,6 @@ class Nextion final : public NextionBase, public PollingComponent, public uart::
|
||||
void add_waveform_data(uint8_t component_id, uint8_t channel_number, uint8_t value);
|
||||
|
||||
void open_waveform_channel(uint8_t component_id, uint8_t channel_number, uint8_t value);
|
||||
#endif // USE_NEXTION_WAVEFORM
|
||||
|
||||
/**
|
||||
* Display a picture at coordinates.
|
||||
|
||||
@@ -217,7 +217,6 @@ void Nextion::set_component_value(const char *component, int32_t value) {
|
||||
this->add_no_result_to_queue_with_printf_(".val", "%s.val=%" PRId32, component, value);
|
||||
}
|
||||
|
||||
#ifdef USE_NEXTION_WAVEFORM
|
||||
void Nextion::add_waveform_data(uint8_t component_id, uint8_t channel_number, uint8_t value) {
|
||||
this->add_no_result_to_queue_with_printf_("add", "add %" PRIu8 ",%" PRIu8 ",%" PRIu8, component_id, channel_number,
|
||||
value);
|
||||
@@ -227,7 +226,6 @@ void Nextion::open_waveform_channel(uint8_t component_id, uint8_t channel_number
|
||||
this->add_no_result_to_queue_with_printf_("addt", "addt %" PRIu8 ",%" PRIu8 ",%" PRIu8, component_id, channel_number,
|
||||
value);
|
||||
}
|
||||
#endif // USE_NEXTION_WAVEFORM
|
||||
|
||||
void Nextion::set_component_coordinates(const char *component, uint16_t x, uint16_t y) {
|
||||
this->add_no_result_to_queue_with_printf_(".xcen", "%s.xcen=%" PRIu16, component, x);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from esphome import automation
|
||||
import esphome.codegen as cg
|
||||
from esphome.components.const import CONF_ON_STATE_CHANGE
|
||||
from esphome.config_helpers import (
|
||||
filter_source_files_from_defines,
|
||||
filter_source_files_from_platform,
|
||||
@@ -38,7 +39,6 @@ CONF_ON_ABORT = "on_abort"
|
||||
CONF_ON_BEGIN = "on_begin"
|
||||
CONF_ON_END = "on_end"
|
||||
CONF_ON_PROGRESS = "on_progress"
|
||||
CONF_ON_STATE_CHANGE = "on_state_change"
|
||||
|
||||
|
||||
ota_ns = cg.esphome_ns.namespace("ota")
|
||||
|
||||
@@ -322,7 +322,7 @@ void PrometheusHandler::light_row_(AsyncResponseStream *stream, light::LightStat
|
||||
// State
|
||||
print_metric_labels_(stream, ESPHOME_F("esphome_light_state"), obj, area, node, friendly_name);
|
||||
stream->print(ESPHOME_F("\"} "));
|
||||
stream->print(obj->remote_values.is_on());
|
||||
stream->print(obj->get_reported_values().is_on());
|
||||
stream->print(ESPHOME_F("\n"));
|
||||
// Brightness and RGBW
|
||||
light::LightColorValues color = obj->current_values;
|
||||
|
||||
@@ -20,8 +20,7 @@ from esphome.const import (
|
||||
CONF_VALUE,
|
||||
PlatformFramework,
|
||||
)
|
||||
from esphome.core import CORE, ID
|
||||
from esphome.cpp_generator import MockObj, TemplateArgsType
|
||||
from esphome.core import CORE
|
||||
from esphome.types import ConfigType
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -38,11 +37,6 @@ remote_transmitter_ns = cg.esphome_ns.namespace("remote_transmitter")
|
||||
RemoteTransmitterComponent = remote_transmitter_ns.class_(
|
||||
"RemoteTransmitterComponent", remote_base.RemoteTransmitterBase, cg.Component
|
||||
)
|
||||
DigitalWriteAction = remote_transmitter_ns.class_(
|
||||
"DigitalWriteAction",
|
||||
automation.Action,
|
||||
cg.Parented.template(RemoteTransmitterComponent),
|
||||
)
|
||||
|
||||
|
||||
# Keep in sync with the USE_LIBRETINY_VARIANT_RTL8720C / REMOTE_TRANSMITTER_BK_PWM gates in
|
||||
@@ -138,23 +132,12 @@ DIGITAL_WRITE_ACTION_SCHEMA = cv.maybe_simple_value(
|
||||
)
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
automation.register_apply_action(
|
||||
"remote_transmitter.digital_write",
|
||||
DigitalWriteAction,
|
||||
DIGITAL_WRITE_ACTION_SCHEMA,
|
||||
synchronous=True,
|
||||
automation.ApplyField(CONF_VALUE, "digital_write", cg.bool_),
|
||||
id_key=CONF_TRANSMITTER_ID,
|
||||
)
|
||||
async def digital_write_action_to_code(
|
||||
config: ConfigType,
|
||||
action_id: ID,
|
||||
template_arg: cg.TemplateArguments,
|
||||
args: TemplateArgsType,
|
||||
) -> MockObj:
|
||||
var = cg.new_Pvariable(action_id, template_arg)
|
||||
await cg.register_parented(var, config[CONF_TRANSMITTER_ID])
|
||||
template_ = await cg.templatable(config[CONF_VALUE], args, cg.bool_)
|
||||
cg.add(var.set_value(template_))
|
||||
return var
|
||||
|
||||
|
||||
async def to_code(config: ConfigType) -> None:
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/components/remote_transmitter/remote_transmitter.h"
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
|
||||
namespace esphome::remote_transmitter {
|
||||
|
||||
template<typename... Ts>
|
||||
class DigitalWriteAction final : public Action<Ts...>, public Parented<RemoteTransmitterComponent> {
|
||||
public:
|
||||
TEMPLATABLE_VALUE(bool, value)
|
||||
void play(const Ts &...x) override { this->parent_->digital_write(this->value_.value(x...)); }
|
||||
};
|
||||
|
||||
} // namespace esphome::remote_transmitter
|
||||
@@ -27,6 +27,9 @@ from esphome.const import (
|
||||
STATE_CLASS_TOTAL_INCREASING,
|
||||
UNIT_AMPERE,
|
||||
UNIT_HERTZ,
|
||||
UNIT_KILOVOLT_AMPS_HOURS,
|
||||
UNIT_KILOVOLT_AMPS_REACTIVE_HOURS,
|
||||
UNIT_KILOWATT_HOURS,
|
||||
UNIT_VOLT,
|
||||
UNIT_VOLT_AMPS,
|
||||
UNIT_VOLT_AMPS_REACTIVE,
|
||||
@@ -44,9 +47,6 @@ CONF_MAXIMUM_DEMAND_ACTIVE_POWER = "maximum_demand_active_power"
|
||||
CONF_MAXIMUM_DEMAND_REACTIVE_POWER = "maximum_demand_reactive_power"
|
||||
CONF_MAXIMUM_DEMAND_APPARENT_POWER = "maximum_demand_apparent_power"
|
||||
|
||||
UNIT_KILOWATT_HOURS = "kWh"
|
||||
UNIT_KILOVOLT_AMPS_HOURS = "kVAh"
|
||||
UNIT_KILOVOLT_AMPS_REACTIVE_HOURS = "kVARh"
|
||||
|
||||
selec_meter_ns = cg.esphome_ns.namespace("selec_meter")
|
||||
SelecMeter = selec_meter_ns.class_(
|
||||
|
||||
@@ -20,10 +20,6 @@ PlayAction = speaker_ns.class_(
|
||||
)
|
||||
|
||||
|
||||
IsPlayingCondition = speaker_ns.class_("IsPlayingCondition", automation.Condition)
|
||||
IsStoppedCondition = speaker_ns.class_("IsStoppedCondition", automation.Condition)
|
||||
|
||||
|
||||
async def setup_speaker_core_(var, config):
|
||||
if audio_dac_config := config.get(CONF_AUDIO_DAC):
|
||||
aud_dac = await cg.get_variable(audio_dac_config)
|
||||
@@ -47,12 +43,6 @@ SPEAKER_AUTOMATION_SCHEMA = automation.maybe_simple_id(
|
||||
)
|
||||
|
||||
|
||||
async def speaker_action(config, action_id, template_arg, args):
|
||||
var = cg.new_Pvariable(action_id, template_arg)
|
||||
await cg.register_parented(var, config[CONF_ID])
|
||||
return var
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
"speaker.play",
|
||||
PlayAction,
|
||||
@@ -91,13 +81,12 @@ for _name, _call in (
|
||||
_name, SPEAKER_AUTOMATION_SCHEMA, automation.ApplyCall(_call)
|
||||
)
|
||||
|
||||
automation.register_condition(
|
||||
"speaker.is_playing", IsPlayingCondition, SPEAKER_AUTOMATION_SCHEMA
|
||||
)(speaker_action)
|
||||
|
||||
automation.register_condition(
|
||||
"speaker.is_stopped", IsStoppedCondition, SPEAKER_AUTOMATION_SCHEMA
|
||||
)(speaker_action)
|
||||
automation.register_apply_condition(
|
||||
"speaker.is_playing", SPEAKER_AUTOMATION_SCHEMA, "is_running()"
|
||||
)
|
||||
automation.register_apply_condition(
|
||||
"speaker.is_stopped", SPEAKER_AUTOMATION_SCHEMA, "is_stopped()"
|
||||
)
|
||||
|
||||
|
||||
automation.register_apply_action(
|
||||
|
||||
@@ -38,14 +38,4 @@ template<typename... Ts> class PlayAction final : public Action<Ts...>, public P
|
||||
} data_;
|
||||
};
|
||||
|
||||
template<typename... Ts> class IsPlayingCondition final : public Condition<Ts...>, public Parented<Speaker> {
|
||||
public:
|
||||
bool check(const Ts &...x) override { return this->parent_->is_running(); }
|
||||
};
|
||||
|
||||
template<typename... Ts> class IsStoppedCondition final : public Condition<Ts...>, public Parented<Speaker> {
|
||||
public:
|
||||
bool check(const Ts &...x) override { return this->parent_->is_stopped(); }
|
||||
};
|
||||
|
||||
} // namespace esphome::speaker
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/components/switch/switch.h"
|
||||
|
||||
namespace esphome::switch_ {
|
||||
|
||||
class SwitchStateTrigger final : public Trigger<bool> {
|
||||
public:
|
||||
SwitchStateTrigger(Switch *a_switch) {
|
||||
a_switch->add_on_state_callback([this](bool state) { this->trigger(state); });
|
||||
}
|
||||
};
|
||||
|
||||
class SwitchTurnOnTrigger final : public Trigger<> {
|
||||
public:
|
||||
SwitchTurnOnTrigger(Switch *a_switch) {
|
||||
a_switch->add_on_state_callback([this](bool state) {
|
||||
if (state) {
|
||||
this->trigger();
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
class SwitchTurnOffTrigger final : public Trigger<> {
|
||||
public:
|
||||
SwitchTurnOffTrigger(Switch *a_switch) {
|
||||
a_switch->add_on_state_callback([this](bool state) {
|
||||
if (!state) {
|
||||
this->trigger();
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace esphome::switch_
|
||||
@@ -0,0 +1 @@
|
||||
CODEOWNERS = ["@remcom"]
|
||||
@@ -0,0 +1,108 @@
|
||||
from esphome import automation
|
||||
from esphome.automation import maybe_simple_id
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import i2c
|
||||
from esphome.components.audio_dac import AudioDac
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_CHANNEL, CONF_ID, CONF_POWER_MODE
|
||||
from esphome.types import ConfigType
|
||||
|
||||
DEPENDENCIES = ["i2c"]
|
||||
|
||||
tas2780_ns = cg.esphome_ns.namespace("tas2780")
|
||||
TAS2780 = tas2780_ns.class_("TAS2780", AudioDac, cg.PollingComponent, i2c.I2CDevice)
|
||||
ChannelSelect = tas2780_ns.enum("ChannelSelect")
|
||||
|
||||
CHANNELS = {
|
||||
"mono": ChannelSelect.MONO_DWN_MIX,
|
||||
"left": ChannelSelect.LEFT_CHANNEL,
|
||||
"right": ChannelSelect.RIGHT_CHANNEL,
|
||||
}
|
||||
|
||||
CONF_VOL_RANGE_MIN = "vol_range_min"
|
||||
CONF_VOL_RANGE_MAX = "vol_range_max"
|
||||
CONF_AMP_LEVEL = "amp_level"
|
||||
|
||||
_AMP_LEVEL = cv.int_range(min=0, max=20)
|
||||
_POWER_MODE = cv.int_range(min=0, max=3)
|
||||
|
||||
|
||||
def _validate_vol_range(config: ConfigType) -> ConfigType:
|
||||
if config[CONF_VOL_RANGE_MIN] >= config[CONF_VOL_RANGE_MAX]:
|
||||
raise cv.Invalid(f"{CONF_VOL_RANGE_MIN} must be less than {CONF_VOL_RANGE_MAX}")
|
||||
return config
|
||||
|
||||
|
||||
CONFIG_SCHEMA = cv.All(
|
||||
cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(TAS2780),
|
||||
cv.Optional(CONF_AMP_LEVEL, default=8): _AMP_LEVEL,
|
||||
cv.Optional(CONF_POWER_MODE, default=2): _POWER_MODE,
|
||||
cv.Optional(CONF_VOL_RANGE_MIN, default=0.3): cv.percentage,
|
||||
cv.Optional(CONF_VOL_RANGE_MAX, default=1.0): cv.percentage,
|
||||
cv.Optional(CONF_CHANNEL, default="mono"): cv.enum(CHANNELS),
|
||||
}
|
||||
)
|
||||
.extend(cv.polling_component_schema("5s"))
|
||||
.extend(i2c.i2c_device_schema(0x38)),
|
||||
_validate_vol_range,
|
||||
)
|
||||
|
||||
|
||||
TAS2780_ACTION_SCHEMA = maybe_simple_id({cv.GenerateID(): cv.use_id(TAS2780)})
|
||||
|
||||
for _name, _call in (
|
||||
("tas2780.deactivate", "deactivate()"),
|
||||
("tas2780.reset", "reset()"),
|
||||
):
|
||||
automation.register_apply_action(
|
||||
_name, TAS2780_ACTION_SCHEMA, automation.ApplyCall(_call)
|
||||
)
|
||||
|
||||
# Without power_mode the configured mode stays; activate() re-initializes only on a change.
|
||||
automation.register_apply_action(
|
||||
"tas2780.activate",
|
||||
maybe_simple_id(
|
||||
{
|
||||
cv.GenerateID(): cv.use_id(TAS2780),
|
||||
cv.Optional(CONF_POWER_MODE): cv.templatable(_POWER_MODE),
|
||||
}
|
||||
),
|
||||
automation.ApplyField(CONF_POWER_MODE, "set_power_mode", cg.uint8),
|
||||
automation.ApplyCall("activate()"),
|
||||
)
|
||||
|
||||
automation.register_apply_action(
|
||||
"tas2780.update_config",
|
||||
cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.use_id(TAS2780),
|
||||
cv.Optional(CONF_VOL_RANGE_MIN): cv.templatable(cv.percentage),
|
||||
cv.Optional(CONF_VOL_RANGE_MAX): cv.templatable(cv.percentage),
|
||||
cv.Optional(CONF_AMP_LEVEL): cv.templatable(_AMP_LEVEL),
|
||||
cv.Optional(CONF_CHANNEL): cv.templatable(cv.enum(CHANNELS)),
|
||||
}
|
||||
).add_extra(
|
||||
cv.has_at_least_one_key(
|
||||
CONF_VOL_RANGE_MIN, CONF_VOL_RANGE_MAX, CONF_AMP_LEVEL, CONF_CHANNEL
|
||||
)
|
||||
),
|
||||
automation.ApplyField(CONF_AMP_LEVEL, "set_amp_level", cg.uint8),
|
||||
automation.ApplyField(CONF_VOL_RANGE_MIN, "set_vol_range_min", cg.float_),
|
||||
automation.ApplyField(CONF_VOL_RANGE_MAX, "set_vol_range_max", cg.float_),
|
||||
automation.ApplyField(CONF_CHANNEL, "set_selected_channel", ChannelSelect),
|
||||
automation.ApplyCall("apply_config()"),
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config: ConfigType) -> None:
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(var, config)
|
||||
await i2c.register_i2c_device(var, config)
|
||||
|
||||
cg.add(var.set_amp_level(config[CONF_AMP_LEVEL]))
|
||||
cg.add(var.set_power_mode(config[CONF_POWER_MODE]))
|
||||
cg.add(var.set_vol_range_min(config[CONF_VOL_RANGE_MIN]))
|
||||
cg.add(var.set_vol_range_max(config[CONF_VOL_RANGE_MAX]))
|
||||
cg.add(var.set_selected_channel(config[CONF_CHANNEL]))
|
||||
@@ -0,0 +1,406 @@
|
||||
#include "tas2780.h"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome::tas2780 {
|
||||
|
||||
static const char *const TAG = "tas2780";
|
||||
|
||||
static constexpr uint8_t TAS2780_PAGE_SELECT = 0x00; // Page Select
|
||||
static constexpr uint8_t TAS2780_PAGE_0 = 0x00; // Page 0
|
||||
static constexpr uint8_t TAS2780_PAGE_1 = 0x01; // Page 1
|
||||
static constexpr uint8_t TAS2780_PAGE_FD = 0xFD; // Page 0xFD
|
||||
|
||||
/* PAGE 0 */
|
||||
static constexpr uint8_t TAS2780_SW_RESET = 0x01; // Software Reset
|
||||
static constexpr uint8_t TAS2780_SW_RESET_CMD = 0x01; // Trigger software reset
|
||||
static constexpr uint8_t TAS2780_MODE_CTRL = 0x02; // Device operational mode
|
||||
static constexpr uint8_t TAS2780_MODE_CTRL_MODE_MASK = 0x07;
|
||||
static constexpr uint8_t TAS2780_MODE_CTRL_MODE_ACTIVE = 0x00;
|
||||
static constexpr uint8_t TAS2780_MODE_CTRL_MODE_ACTIVE_MUTED = 0x01;
|
||||
static constexpr uint8_t TAS2780_MODE_CTRL_MODE_SFTW_SHTDWN = 0x02;
|
||||
|
||||
static constexpr uint8_t TAS2780_CHNL_0 = 0x03; // Y Bridge and Channel settings
|
||||
static constexpr uint8_t TAS2780_CHNL_0_CDS_MODE_SHIFT = 6;
|
||||
static constexpr uint8_t TAS2780_CHNL_0_CDS_MODE_MASK = (0x03 << TAS2780_CHNL_0_CDS_MODE_SHIFT);
|
||||
static constexpr uint8_t TAS2780_CHNL_0_AMP_LEVEL_SHIFT = 1;
|
||||
static constexpr uint8_t TAS2780_CHNL_0_AMP_LEVEL_MASK = (0x1F) << TAS2780_CHNL_0_AMP_LEVEL_SHIFT;
|
||||
static constexpr uint8_t TAS2780_AMP_LEVEL_MAX = 0x14; // Codes above 20 are reserved
|
||||
|
||||
static constexpr uint8_t TAS2780_DC_BLK0 = 0x04; // SAR Filter and DC Path Blocker
|
||||
static constexpr uint8_t TAS2780_DC_BLK0_VBAT1S_MODE_MASK = (1 << 7);
|
||||
static constexpr uint8_t TAS2780_DC_BLK1 = 0x05; // Record DC Blocker
|
||||
static constexpr uint8_t TAS2780_DC_BLK1_RESET_VAL = 0x41; // Presence check, there is no WHO_AM_I register
|
||||
|
||||
static constexpr uint8_t TAS2780_TDM_CFG2 = 0x0A; // TDM Configuration 2
|
||||
static constexpr uint8_t TAS2780_TDM_CFG2_RX_SCFG_SHIFT = 4;
|
||||
static constexpr uint8_t TAS2780_TDM_CFG2_RX_SCFG_MASK = (3 << TAS2780_TDM_CFG2_RX_SCFG_SHIFT);
|
||||
static constexpr uint8_t TAS2780_TDM_CFG2_RX_WLEN_SHIFT = 2;
|
||||
static constexpr uint8_t TAS2780_TDM_CFG2_RX_WLEN_MASK = (3 << TAS2780_TDM_CFG2_RX_WLEN_SHIFT);
|
||||
static constexpr uint8_t TAS2780_TDM_CFG2_RX_WLEN_32BIT = (3 << TAS2780_TDM_CFG2_RX_WLEN_SHIFT);
|
||||
static constexpr uint8_t TAS2780_TDM_CFG2_RX_SLEN_MASK = (3 << 0);
|
||||
static constexpr uint8_t TAS2780_TDM_CFG2_RX_SLEN_32BIT = 2;
|
||||
|
||||
static constexpr uint8_t TAS2780_TDM_CFG5 = 0x0E; // TDM Configuration 5
|
||||
static constexpr uint8_t TAS2780_TDM_CFG5_TX_VSNS_EN_SLOT4 = 0x44; // vsns TX enable, slot 4
|
||||
static constexpr uint8_t TAS2780_TDM_CFG6 = 0x0F; // TDM Configuration 6
|
||||
static constexpr uint8_t TAS2780_TDM_CFG6_TX_ISNS_EN_SLOT0 = 0x40; // isns TX enable, slot 0
|
||||
|
||||
static constexpr uint8_t TAS2780_DVC = 0x1A; // Digital Volume Control
|
||||
static constexpr uint8_t TAS2780_DVC_MAX_ATTEN = 200; // 0 dB (0x00) to -100 dB (0xC8) in 0.5 dB steps
|
||||
|
||||
/* Interrupts */
|
||||
static constexpr uint8_t TAS2780_INT_MASK_ALL = 0xFF; // Mask all interrupts
|
||||
static constexpr uint8_t TAS2780_INT_MASK0 = 0x3B; // Interrupt Mask 0
|
||||
static constexpr uint8_t TAS2780_INT_MASK1 = 0x3C; // Interrupt Mask 1
|
||||
static constexpr uint8_t TAS2780_INT_MASK1_0 = 0x3D; // Interrupt Mask 1_0 (INT_LTCH1_0 group)
|
||||
static constexpr uint8_t TAS2780_INT_MASK2 = 0x40; // Interrupt Mask 2
|
||||
static constexpr uint8_t TAS2780_INT_MASK3 = 0x41; // Interrupt Mask 3
|
||||
static constexpr uint8_t TAS2780_INT_LTCH0 = 0x49; // Latched Interrupt Read-back 0, 1 and 1_0 follow
|
||||
static constexpr uint8_t TAS2780_INT_LTCH2 = 0x4F; // Latched Interrupt Read-back 2
|
||||
|
||||
static constexpr uint8_t TAS2780_INT_CLK_CFG = 0x5C; // Clock Setting and IRQZ
|
||||
static constexpr uint8_t TAS2780_INT_CLK_CFG_CLR_LATCH = (1 << 2); // Clear interrupt latches
|
||||
static constexpr uint8_t TAS2780_INT_CLK_CFG_MODE_MASK = 0x03; // Trigger mode field mask
|
||||
static constexpr uint8_t TAS2780_INT_CLK_CFG_MODE_LIVE = 0x00; // Trigger on any unmasked live interrupt
|
||||
static constexpr uint8_t TAS2780_PVDD_UVLO = 0x71; // UVLO Threshold
|
||||
static constexpr uint8_t TAS2780_PVDD_UVLO_2V76 = 0x03; // PVDD UVLO threshold = 2.76V
|
||||
|
||||
/* PAGE 0x01 */
|
||||
static constexpr uint8_t TAS2780_INIT_0 = 0x17; // Initialization
|
||||
static constexpr uint8_t TAS2780_INIT_0_VAL = 0xC8; // SARBurstMask=0, CMP_HYST_LP=1
|
||||
static constexpr uint8_t TAS2780_LSR = 0x19; // Modulation
|
||||
static constexpr uint8_t TAS2780_LSR_PWM_MODE = 0x00; // PWM modulation mode
|
||||
static constexpr uint8_t TAS2780_INIT_1 = 0x21; // Initialization
|
||||
static constexpr uint8_t TAS2780_INIT_1_VAL = 0x00; // Disable comparator hysteresis
|
||||
static constexpr uint8_t TAS2780_INIT_2 = 0x35; // Initialization
|
||||
static constexpr uint8_t TAS2780_INIT_2_VAL = 0x74; // Noise minimized
|
||||
|
||||
/* PAGE 0xFD */
|
||||
static constexpr uint8_t TAS2780_PAGE_FD_ACCESS = 0x0D; // Page 0xFD access unlock/lock register
|
||||
static constexpr uint8_t TAS2780_PAGE_FD_ACCESS_UNLOCK = 0x0D; // Unlock page 0xFD access
|
||||
static constexpr uint8_t TAS2780_PAGE_FD_ACCESS_LOCK = 0x00; // Lock page 0xFD access
|
||||
static constexpr uint8_t TAS2780_INIT_3 = 0x3E; // Initialization
|
||||
static constexpr uint8_t TAS2780_INIT_3_VAL = 0x4A; // Optimal Dmin
|
||||
|
||||
// CDS_MODE (2 bits) and VBAT1S_MODE (1 bit) per power mode 0..3, packed so nothing lands in RAM on ESP8266:
|
||||
// PWR_MODE0: 2/0, PWR_MODE1: 0/0, PWR_MODE2: 3/1, PWR_MODE3: 1/0
|
||||
static constexpr uint8_t POWER_MODE_CDS_MODES = 0x72;
|
||||
static constexpr uint8_t POWER_MODE_VBAT1S_MODES = 0x04;
|
||||
|
||||
// Latched interrupt bits per register (INT_LTCH0, 1, 1_0, 2, one byte each, low to high), split into faults
|
||||
// and informational events; bits outside both masks are undefined.
|
||||
static constexpr uint32_t TAS2780_INT_LTCH_ERROR_MASKS = 0x0FA058C7;
|
||||
static constexpr uint32_t TAS2780_INT_LTCH_INFO_MASKS = 0x00002138;
|
||||
|
||||
// An if chain rather than a switch: a switch table would land in rodata, which is RAM on ESP8266.
|
||||
static const LogString *fault_name(uint8_t reg, uint8_t bit) {
|
||||
const uint8_t key = (reg << 3) | bit;
|
||||
if (key == 0x00)
|
||||
return LOG_STR("Over temperature error");
|
||||
if (key == 0x01)
|
||||
return LOG_STR("Over current error");
|
||||
if (key == 0x02)
|
||||
return LOG_STR("TDM Clock Error");
|
||||
if (key == 0x03)
|
||||
return LOG_STR("Limiter active");
|
||||
if (key == 0x04)
|
||||
return LOG_STR("PVDD below limiter inflection point");
|
||||
if (key == 0x05)
|
||||
return LOG_STR("Limiter max attenuation");
|
||||
if (key == 0x06)
|
||||
return LOG_STR("BOP infinite hold");
|
||||
if (key == 0x07)
|
||||
return LOG_STR("BOP Mute");
|
||||
if (key == 0x08)
|
||||
return LOG_STR("Gain limiter active");
|
||||
if (key == 0x0B)
|
||||
return LOG_STR("Load Diagnostic mode fault status");
|
||||
if (key == 0x0D)
|
||||
return LOG_STR("Load diagnostic complete");
|
||||
if (key == 0x0E)
|
||||
return LOG_STR("OTP CRC error flag");
|
||||
if (key == 0x15)
|
||||
return LOG_STR("VBAT1S Under Voltage");
|
||||
if (key == 0x17)
|
||||
return LOG_STR("Internal PLL Clock Error");
|
||||
if (key == 0x18)
|
||||
return LOG_STR("PVDD UVLO");
|
||||
if (key == 0x19)
|
||||
return LOG_STR("Internal VBAT1S LDO Over Load");
|
||||
if (key == 0x1A)
|
||||
return LOG_STR("Internal VBAT1S LDO Over Voltage");
|
||||
if (key == 0x1B)
|
||||
return LOG_STR("Internal VBAT1S LDO Under Voltage");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void TAS2780::setup() {
|
||||
if (!this->init_()) {
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
this->write_mode_ctrl_(TAS2780_MODE_CTRL_MODE_SFTW_SHTDWN);
|
||||
}
|
||||
|
||||
bool TAS2780::select_page_(uint8_t page) {
|
||||
if (this->current_page_ == page)
|
||||
return true;
|
||||
if (!this->write_byte(TAS2780_PAGE_SELECT, page)) {
|
||||
this->current_page_ = 0xFF;
|
||||
return false;
|
||||
}
|
||||
this->current_page_ = page;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TAS2780::update_bits_(uint8_t reg, uint8_t mask, uint8_t value) {
|
||||
uint8_t current;
|
||||
if (!this->select_page_(TAS2780_PAGE_0) || !this->read_byte(reg, ¤t)) {
|
||||
ESP_LOGE(TAG, "Failed to read register 0x%02X", reg);
|
||||
return false;
|
||||
}
|
||||
return this->write_byte(reg, (current & ~mask) | (value & mask));
|
||||
}
|
||||
|
||||
bool TAS2780::init_() {
|
||||
// Software reset (must select page 0 first; reset invalidates page cache)
|
||||
if (!this->select_page_(TAS2780_PAGE_0)) {
|
||||
ESP_LOGE(TAG, "I2C write failed during init");
|
||||
return false;
|
||||
}
|
||||
this->current_page_ = 0xFF;
|
||||
this->applied_power_mode_ = 0xFF; // only a complete init makes the chip trustworthy again
|
||||
this->reg(TAS2780_SW_RESET) = TAS2780_SW_RESET_CMD;
|
||||
delay(1);
|
||||
|
||||
uint8_t chd1;
|
||||
if (!this->read_byte(TAS2780_DC_BLK1, &chd1)) {
|
||||
ESP_LOGE(TAG, "I2C read failed during init");
|
||||
return false;
|
||||
}
|
||||
if (chd1 != TAS2780_DC_BLK1_RESET_VAL) {
|
||||
ESP_LOGE(TAG, "Init failed (DC_BLK1=0x%02X, expected 0x%02X)", chd1, TAS2780_DC_BLK1_RESET_VAL);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!this->select_page_(TAS2780_PAGE_0)) {
|
||||
return false;
|
||||
}
|
||||
this->reg(TAS2780_TDM_CFG5) = TAS2780_TDM_CFG5_TX_VSNS_EN_SLOT4;
|
||||
this->reg(TAS2780_TDM_CFG6) = TAS2780_TDM_CFG6_TX_ISNS_EN_SLOT0;
|
||||
|
||||
if (!this->select_page_(TAS2780_PAGE_1)) {
|
||||
return false;
|
||||
}
|
||||
this->reg(TAS2780_LSR) = TAS2780_LSR_PWM_MODE;
|
||||
this->reg(TAS2780_INIT_0) = TAS2780_INIT_0_VAL;
|
||||
this->reg(TAS2780_INIT_1) = TAS2780_INIT_1_VAL;
|
||||
this->reg(TAS2780_INIT_2) = TAS2780_INIT_2_VAL;
|
||||
|
||||
if (!this->select_page_(TAS2780_PAGE_FD)) {
|
||||
return false;
|
||||
}
|
||||
this->reg(TAS2780_PAGE_FD_ACCESS) = TAS2780_PAGE_FD_ACCESS_UNLOCK;
|
||||
this->reg(TAS2780_INIT_3) = TAS2780_INIT_3_VAL;
|
||||
this->reg(TAS2780_PAGE_FD_ACCESS) = TAS2780_PAGE_FD_ACCESS_LOCK;
|
||||
|
||||
if (!this->select_page_(TAS2780_PAGE_0)) {
|
||||
return false;
|
||||
}
|
||||
if (!this->set_power_mode_(this->power_mode_))
|
||||
return false;
|
||||
|
||||
// When Y bridge is used (eg. PWR_MODE1) PVDD UVLO threshold needs to be set 2.5 V above VBAT1S level.
|
||||
// UVLO = 1.753V + val * 0.332V
|
||||
this->reg(TAS2780_PVDD_UVLO) = TAS2780_PVDD_UVLO_2V76;
|
||||
|
||||
// Mask all interrupt groups on the IRQZ pin, events are polled via update()
|
||||
this->reg(TAS2780_INT_MASK0) = TAS2780_INT_MASK_ALL;
|
||||
this->reg(TAS2780_INT_MASK1) = TAS2780_INT_MASK_ALL;
|
||||
this->reg(TAS2780_INT_MASK1_0) = TAS2780_INT_MASK_ALL;
|
||||
this->reg(TAS2780_INT_MASK2) = TAS2780_INT_MASK_ALL;
|
||||
this->reg(TAS2780_INT_MASK3) = TAS2780_INT_MASK_ALL;
|
||||
|
||||
if (!this->update_bits_(TAS2780_INT_CLK_CFG, TAS2780_INT_CLK_CFG_MODE_MASK, TAS2780_INT_CLK_CFG_MODE_LIVE))
|
||||
return false;
|
||||
|
||||
// Software reset sets DVC back to 0 dB (full volume)
|
||||
if (!this->apply_config())
|
||||
return false;
|
||||
this->applied_power_mode_ = this->power_mode_;
|
||||
return true;
|
||||
}
|
||||
|
||||
void TAS2780::activate() {
|
||||
if (this->is_failed())
|
||||
return;
|
||||
ESP_LOGD(TAG, "Activating (PWR_MODE:%u)", this->power_mode_);
|
||||
this->clear_latches_();
|
||||
if (this->power_mode_ != this->applied_power_mode_ && !this->reinit_())
|
||||
return;
|
||||
this->active_ = this->write_mode_ctrl_(this->active_mode_());
|
||||
}
|
||||
|
||||
void TAS2780::deactivate() {
|
||||
if (this->is_failed())
|
||||
return;
|
||||
ESP_LOGD(TAG, "Deactivating");
|
||||
this->active_ = false;
|
||||
this->write_mode_ctrl_(TAS2780_MODE_CTRL_MODE_SFTW_SHTDWN);
|
||||
}
|
||||
|
||||
void TAS2780::reset() {
|
||||
if (this->is_failed())
|
||||
return;
|
||||
if (this->reinit_() && this->active_)
|
||||
this->activate();
|
||||
}
|
||||
|
||||
bool TAS2780::reinit_() {
|
||||
if (!this->init_()) {
|
||||
ESP_LOGE(TAG, "Re-initialization failed");
|
||||
this->status_set_error(LOG_STR("Init failed"));
|
||||
return false;
|
||||
}
|
||||
this->status_clear_error();
|
||||
return true;
|
||||
}
|
||||
|
||||
void TAS2780::set_power_mode(uint8_t power_mode) {
|
||||
// Lambda-supplied values bypass schema validation; refuse before anything touches the chip
|
||||
if (power_mode >= 4) {
|
||||
ESP_LOGE(TAG, "Invalid power mode %u, must be 0-3", power_mode);
|
||||
return;
|
||||
}
|
||||
this->power_mode_ = power_mode;
|
||||
}
|
||||
|
||||
bool TAS2780::set_power_mode_(uint8_t power_mode) {
|
||||
uint8_t cds_mode = (POWER_MODE_CDS_MODES >> (power_mode * 2)) & 0x03;
|
||||
uint8_t vbat1s_mode = (POWER_MODE_VBAT1S_MODES >> power_mode) & 0x01;
|
||||
return this->update_bits_(TAS2780_CHNL_0, TAS2780_CHNL_0_CDS_MODE_MASK, cds_mode << TAS2780_CHNL_0_CDS_MODE_SHIFT) &&
|
||||
this->update_bits_(TAS2780_DC_BLK0, TAS2780_DC_BLK0_VBAT1S_MODE_MASK, vbat1s_mode ? 0xFF : 0);
|
||||
}
|
||||
|
||||
void TAS2780::clear_latches_() {
|
||||
this->update_bits_(TAS2780_INT_CLK_CFG, TAS2780_INT_CLK_CFG_CLR_LATCH, TAS2780_INT_CLK_CFG_CLR_LATCH);
|
||||
}
|
||||
|
||||
// Returns true if any latched interrupt flag is set
|
||||
bool TAS2780::log_error_states_() {
|
||||
uint8_t latched[4];
|
||||
if (!this->select_page_(TAS2780_PAGE_0) || !this->read_bytes(TAS2780_INT_LTCH0, latched, 3) ||
|
||||
!this->read_byte(TAS2780_INT_LTCH2, &latched[3])) {
|
||||
return false;
|
||||
}
|
||||
// LDMODE is a two-bit field in INT_LTCH1; report it once
|
||||
if (latched[1] & (1 << 4))
|
||||
latched[1] = (latched[1] & ~(1 << 4)) | (1 << 3);
|
||||
for (uint8_t reg = 0; reg < 4; reg++) {
|
||||
uint8_t errors = latched[reg] & (TAS2780_INT_LTCH_ERROR_MASKS >> (reg * 8));
|
||||
uint8_t infos = latched[reg] & (TAS2780_INT_LTCH_INFO_MASKS >> (reg * 8));
|
||||
for (uint8_t bit = 0; bit < 8; bit++) {
|
||||
if (errors & (1 << bit)) {
|
||||
ESP_LOGE(TAG, "%s", LOG_STR_ARG(fault_name(reg, bit)));
|
||||
} else if (infos & (1 << bit)) {
|
||||
ESP_LOGD(TAG, "%s", LOG_STR_ARG(fault_name(reg, bit)));
|
||||
}
|
||||
}
|
||||
}
|
||||
return (latched[0] | latched[1] | latched[2] | latched[3]) != 0;
|
||||
}
|
||||
|
||||
void TAS2780::update() {
|
||||
// Latches hold until cleared; without this the same events are logged on every update
|
||||
if (this->log_error_states_())
|
||||
this->clear_latches_();
|
||||
}
|
||||
|
||||
void TAS2780::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "Audio Amplifier:");
|
||||
LOG_I2C_DEVICE(this);
|
||||
LOG_UPDATE_INTERVAL(this);
|
||||
ESP_LOGCONFIG(TAG,
|
||||
" Power Mode: %u\n"
|
||||
" Amp Level: %u\n"
|
||||
" Volume Range: %.2f - %.2f\n"
|
||||
" Channel: %s",
|
||||
this->power_mode_, this->amp_level_, this->vol_range_min_, this->vol_range_max_,
|
||||
this->selected_channel_ == LEFT_CHANNEL ? LOG_STR_LITERAL("Left")
|
||||
: this->selected_channel_ == RIGHT_CHANNEL ? LOG_STR_LITERAL("Right")
|
||||
: LOG_STR_LITERAL("Mono Downmix"));
|
||||
}
|
||||
|
||||
bool TAS2780::write_mode_ctrl_(uint8_t mode) {
|
||||
return this->update_bits_(TAS2780_MODE_CTRL, TAS2780_MODE_CTRL_MODE_MASK, mode);
|
||||
}
|
||||
|
||||
uint8_t TAS2780::active_mode_() const {
|
||||
return this->is_muted_ ? TAS2780_MODE_CTRL_MODE_ACTIVE_MUTED : TAS2780_MODE_CTRL_MODE_ACTIVE;
|
||||
}
|
||||
|
||||
bool TAS2780::set_mute_(bool muted) {
|
||||
bool previous = this->is_muted_;
|
||||
this->is_muted_ = muted;
|
||||
uint8_t mode_ctrl;
|
||||
if (!this->select_page_(TAS2780_PAGE_0) || !this->read_byte(TAS2780_MODE_CTRL, &mode_ctrl)) {
|
||||
ESP_LOGE(TAG, "Failed to read MODE_CTRL");
|
||||
this->is_muted_ = previous;
|
||||
return false;
|
||||
}
|
||||
uint8_t current_mode = mode_ctrl & TAS2780_MODE_CTRL_MODE_MASK;
|
||||
// Only switch between active/muted if device is active; don't wake from shutdown
|
||||
if ((current_mode == TAS2780_MODE_CTRL_MODE_ACTIVE || current_mode == TAS2780_MODE_CTRL_MODE_ACTIVE_MUTED) &&
|
||||
!this->write_byte(TAS2780_MODE_CTRL, (mode_ctrl & ~TAS2780_MODE_CTRL_MODE_MASK) | this->active_mode_())) {
|
||||
ESP_LOGE(TAG, "Failed to write MODE_CTRL");
|
||||
this->is_muted_ = previous;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TAS2780::set_volume(float volume) {
|
||||
float previous = this->volume_;
|
||||
this->volume_ = clamp(volume, 0.0f, 1.0f);
|
||||
if (!this->write_volume_()) {
|
||||
this->volume_ = previous;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TAS2780::write_volume_() {
|
||||
// Lambda-supplied volume ranges are not bounded or ordered
|
||||
float range_min = std::min(this->vol_range_min_, this->vol_range_max_);
|
||||
float range_max = std::max(this->vol_range_min_, this->vol_range_max_);
|
||||
float volume = clamp(std::lerp(range_min, range_max, this->volume_), 0.0f, 1.0f);
|
||||
uint8_t dvc = remap<uint8_t, float>(volume, 0.0f, 1.0f, TAS2780_DVC_MAX_ATTEN, 0);
|
||||
ESP_LOGD(TAG, "Setting attenuation to: %u", dvc);
|
||||
return this->select_page_(TAS2780_PAGE_0) && this->write_byte(TAS2780_DVC, dvc);
|
||||
}
|
||||
|
||||
bool TAS2780::apply_config() { return this->apply_amp_and_channel_config_() && this->write_volume_(); }
|
||||
|
||||
bool TAS2780::apply_amp_and_channel_config_() {
|
||||
// Lambda-supplied values bypass schema validation
|
||||
if (this->amp_level_ > TAS2780_AMP_LEVEL_MAX) {
|
||||
ESP_LOGW(TAG, "Amp level %u out of range, using %u", this->amp_level_, TAS2780_AMP_LEVEL_MAX);
|
||||
this->amp_level_ = TAS2780_AMP_LEVEL_MAX;
|
||||
}
|
||||
ESP_LOGD(TAG, "Update amp to level idx: %u", this->amp_level_);
|
||||
if (!this->update_bits_(TAS2780_CHNL_0, TAS2780_CHNL_0_AMP_LEVEL_MASK,
|
||||
this->amp_level_ << TAS2780_CHNL_0_AMP_LEVEL_SHIFT)) {
|
||||
return false;
|
||||
}
|
||||
return this->update_bits_(
|
||||
TAS2780_TDM_CFG2, TAS2780_TDM_CFG2_RX_SCFG_MASK | TAS2780_TDM_CFG2_RX_WLEN_MASK | TAS2780_TDM_CFG2_RX_SLEN_MASK,
|
||||
(this->selected_channel_ << TAS2780_TDM_CFG2_RX_SCFG_SHIFT) | TAS2780_TDM_CFG2_RX_WLEN_32BIT |
|
||||
TAS2780_TDM_CFG2_RX_SLEN_32BIT);
|
||||
}
|
||||
|
||||
} // namespace esphome::tas2780
|
||||
@@ -0,0 +1,65 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/components/audio_dac/audio_dac.h"
|
||||
#include "esphome/components/i2c/i2c.h"
|
||||
#include "esphome/core/component.h"
|
||||
|
||||
namespace esphome::tas2780 {
|
||||
|
||||
// Values are the TDM_CFG2 RX_SCFG field.
|
||||
enum ChannelSelect : uint8_t { LEFT_CHANNEL = 1, RIGHT_CHANNEL = 2, MONO_DWN_MIX = 3 };
|
||||
|
||||
class TAS2780 : public audio_dac::AudioDac, public PollingComponent, public i2c::I2CDevice {
|
||||
public:
|
||||
void setup() override;
|
||||
void dump_config() override;
|
||||
float get_setup_priority() const override { return setup_priority::IO; }
|
||||
void update() override;
|
||||
|
||||
/// Software reset and re-initialization, leaving the amplifier on or off as last requested.
|
||||
void reset();
|
||||
/// Activate with the configured power mode; re-initializes first when the mode changed.
|
||||
void activate();
|
||||
void deactivate();
|
||||
/// Write the amp level, channel and volume range to the device.
|
||||
bool apply_config();
|
||||
|
||||
bool set_mute_off() override { return this->set_mute_(false); }
|
||||
bool set_mute_on() override { return this->set_mute_(true); }
|
||||
bool set_volume(float volume) override;
|
||||
|
||||
bool is_muted() override { return this->is_muted_; }
|
||||
float volume() override { return this->volume_; }
|
||||
|
||||
void set_amp_level(uint8_t amp_level) { this->amp_level_ = amp_level; }
|
||||
void set_power_mode(uint8_t power_mode);
|
||||
void set_vol_range_min(float min_val) { this->vol_range_min_ = min_val; }
|
||||
void set_vol_range_max(float max_val) { this->vol_range_max_ = max_val; }
|
||||
void set_selected_channel(ChannelSelect channel) { this->selected_channel_ = channel; }
|
||||
|
||||
protected:
|
||||
bool select_page_(uint8_t page);
|
||||
bool update_bits_(uint8_t reg, uint8_t mask, uint8_t value);
|
||||
bool init_();
|
||||
bool reinit_();
|
||||
bool set_power_mode_(uint8_t power_mode);
|
||||
bool apply_amp_and_channel_config_();
|
||||
bool write_mode_ctrl_(uint8_t mode);
|
||||
uint8_t active_mode_() const;
|
||||
bool set_mute_(bool muted);
|
||||
bool write_volume_();
|
||||
bool log_error_states_();
|
||||
void clear_latches_();
|
||||
|
||||
float volume_{0};
|
||||
float vol_range_min_{0.3f};
|
||||
float vol_range_max_{1.0f};
|
||||
uint8_t current_page_{0xFF};
|
||||
uint8_t power_mode_{2};
|
||||
uint8_t applied_power_mode_{0xFF};
|
||||
uint8_t amp_level_{8};
|
||||
ChannelSelect selected_channel_{MONO_DWN_MIX};
|
||||
bool active_{false};
|
||||
};
|
||||
|
||||
} // namespace esphome::tas2780
|
||||
@@ -9,7 +9,7 @@ static const char *const TAG = "tuya.light";
|
||||
void TuyaLight::setup() {
|
||||
if (this->color_temperature_id_.has_value()) {
|
||||
this->parent_->register_listener(*this->color_temperature_id_, [this](const TuyaDatapoint &datapoint) {
|
||||
if (this->state_->current_values != this->state_->remote_values) {
|
||||
if (this->state_->is_transitioning()) {
|
||||
ESP_LOGD(TAG, "Light is transitioning, datapoint change ignored");
|
||||
return;
|
||||
}
|
||||
@@ -27,7 +27,7 @@ void TuyaLight::setup() {
|
||||
}
|
||||
if (this->dimmer_id_.has_value()) {
|
||||
this->parent_->register_listener(*this->dimmer_id_, [this](const TuyaDatapoint &datapoint) {
|
||||
if (this->state_->current_values != this->state_->remote_values) {
|
||||
if (this->state_->is_transitioning()) {
|
||||
ESP_LOGD(TAG, "Light is transitioning, datapoint change ignored");
|
||||
return;
|
||||
}
|
||||
@@ -39,7 +39,7 @@ void TuyaLight::setup() {
|
||||
}
|
||||
if (switch_id_.has_value()) {
|
||||
this->parent_->register_listener(*this->switch_id_, [this](const TuyaDatapoint &datapoint) {
|
||||
if (this->state_->current_values != this->state_->remote_values) {
|
||||
if (this->state_->is_transitioning()) {
|
||||
ESP_LOGD(TAG, "Light is transitioning, datapoint change ignored");
|
||||
return;
|
||||
}
|
||||
@@ -51,7 +51,7 @@ void TuyaLight::setup() {
|
||||
}
|
||||
if (color_id_.has_value()) {
|
||||
this->parent_->register_listener(*this->color_id_, [this](const TuyaDatapoint &datapoint) {
|
||||
if (this->state_->current_values != this->state_->remote_values) {
|
||||
if (this->state_->is_transitioning()) {
|
||||
ESP_LOGD(TAG, "Light is transitioning, datapoint change ignored");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -5,6 +5,22 @@ import re
|
||||
from esphome import automation, pins
|
||||
import esphome.codegen as cg
|
||||
from esphome.components.const import CONF_DATA_BITS, CONF_PARITY, CONF_STOP_BITS
|
||||
from esphome.components.esp32 import (
|
||||
VARIANT_ESP32,
|
||||
VARIANT_ESP32C2,
|
||||
VARIANT_ESP32C3,
|
||||
VARIANT_ESP32C5,
|
||||
VARIANT_ESP32C6,
|
||||
VARIANT_ESP32C61,
|
||||
VARIANT_ESP32H2,
|
||||
VARIANT_ESP32H4,
|
||||
VARIANT_ESP32H21,
|
||||
VARIANT_ESP32P4,
|
||||
VARIANT_ESP32S2,
|
||||
VARIANT_ESP32S3,
|
||||
VARIANT_ESP32S31,
|
||||
variant_filtered_enum,
|
||||
)
|
||||
from esphome.config_helpers import (
|
||||
filter_source_files_from_defines,
|
||||
filter_source_files_from_platform,
|
||||
@@ -179,6 +195,33 @@ UART_PARITY_OPTIONS = {
|
||||
CONF_FLUSH_TIMEOUT = "flush_timeout"
|
||||
CONF_RX_FULL_THRESHOLD = "rx_full_threshold"
|
||||
CONF_RX_TIMEOUT = "rx_timeout"
|
||||
CONF_CLOCK_SOURCE = "clock_source"
|
||||
|
||||
UARTClockSource = cg.global_ns.enum("uart_sclk_t")
|
||||
UART_CLOCK_SOURCES = {
|
||||
"DEFAULT": UARTClockSource.UART_SCLK_DEFAULT,
|
||||
"APB": UARTClockSource.UART_SCLK_APB,
|
||||
"XTAL": UARTClockSource.UART_SCLK_XTAL,
|
||||
"RTC": UARTClockSource.UART_SCLK_RTC,
|
||||
"REF_TICK": UARTClockSource.UART_SCLK_REF_TICK,
|
||||
}
|
||||
|
||||
# Keep in sync with SOC_UART_SUPPORT_* in ESP-IDF's per-variant soc_caps.h.
|
||||
UART_CLOCK_SOURCES_BY_VARIANT = {
|
||||
VARIANT_ESP32: ["DEFAULT", "APB", "REF_TICK"],
|
||||
VARIANT_ESP32C2: ["DEFAULT", "XTAL", "RTC"],
|
||||
VARIANT_ESP32C3: ["DEFAULT", "APB", "XTAL", "RTC"],
|
||||
VARIANT_ESP32C5: ["DEFAULT", "XTAL", "RTC"],
|
||||
VARIANT_ESP32C6: ["DEFAULT", "XTAL", "RTC"],
|
||||
VARIANT_ESP32C61: ["DEFAULT", "XTAL", "RTC"],
|
||||
VARIANT_ESP32H2: ["DEFAULT", "XTAL", "RTC"],
|
||||
VARIANT_ESP32H4: ["DEFAULT", "XTAL", "RTC"],
|
||||
VARIANT_ESP32H21: ["DEFAULT", "XTAL", "RTC"],
|
||||
VARIANT_ESP32P4: ["DEFAULT", "XTAL", "RTC"],
|
||||
VARIANT_ESP32S2: ["DEFAULT", "APB", "REF_TICK"],
|
||||
VARIANT_ESP32S3: ["DEFAULT", "APB", "XTAL", "RTC"],
|
||||
VARIANT_ESP32S31: ["DEFAULT", "XTAL", "RTC"],
|
||||
}
|
||||
|
||||
UARTDirection = uart_ns.enum("UARTDirection")
|
||||
UART_DIRECTIONS = {
|
||||
@@ -263,6 +306,10 @@ CONFIG_SCHEMA = cv.All(
|
||||
cv.Optional(CONF_FLUSH_TIMEOUT): cv.All(
|
||||
cv.only_on_esp32, cv.positive_time_period_milliseconds
|
||||
),
|
||||
cv.Optional(CONF_CLOCK_SOURCE): cv.All(
|
||||
cv.only_on_esp32,
|
||||
variant_filtered_enum(UART_CLOCK_SOURCES_BY_VARIANT, upper=True),
|
||||
),
|
||||
cv.Optional(CONF_STOP_BITS, default=1): cv.one_of(1, 2, int=True),
|
||||
cv.Optional(CONF_DATA_BITS, default=8): cv.int_range(min=5, max=8),
|
||||
cv.Optional(CONF_PARITY, default="NONE"): cv.enum(
|
||||
@@ -344,6 +391,9 @@ async def to_code(config):
|
||||
cg.add(var.set_rx_timeout(config[CONF_RX_TIMEOUT]))
|
||||
if CONF_FLUSH_TIMEOUT in config:
|
||||
cg.add(var.set_flush_timeout(config[CONF_FLUSH_TIMEOUT]))
|
||||
# The member already defaults to UART_SCLK_DEFAULT, so only emit a real choice
|
||||
if (clock_source := config.get(CONF_CLOCK_SOURCE, "DEFAULT")) != "DEFAULT":
|
||||
cg.add(var.set_clock_source(UART_CLOCK_SOURCES[clock_source]))
|
||||
cg.add(var.set_stop_bits(config[CONF_STOP_BITS]))
|
||||
cg.add(var.set_data_bits(config[CONF_DATA_BITS]))
|
||||
cg.add(var.set_parity(config[CONF_PARITY]))
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "driver/gpio.h"
|
||||
#include "esp_private/gpio.h"
|
||||
#include "soc/gpio_num.h"
|
||||
#include "soc/soc_caps.h"
|
||||
#include "soc/uart_pins.h"
|
||||
|
||||
#ifdef USE_UART_WAKE_LOOP_ON_RX
|
||||
@@ -37,6 +38,32 @@ static constexpr bool is_default_uart0_pin(int8_t pin_num) {
|
||||
return pin_num == U0TXD_GPIO_NUM || pin_num == U0RXD_GPIO_NUM;
|
||||
}
|
||||
|
||||
// clock_source_ is stored in a byte; every uart_sclk_t value is a soc_module_clk_t below SOC_MOD_CLK_INVALID
|
||||
static_assert(SOC_MOD_CLK_INVALID <= UINT8_MAX, "uart_sclk_t no longer fits in uint8_t clock_source_");
|
||||
|
||||
static const LogString *clock_source_to_str(uart_sclk_t clock_source) {
|
||||
switch (clock_source) {
|
||||
#if SOC_UART_SUPPORT_APB_CLK
|
||||
case UART_SCLK_APB:
|
||||
return LOG_STR("APB");
|
||||
#endif
|
||||
#if SOC_UART_SUPPORT_XTAL_CLK
|
||||
case UART_SCLK_XTAL:
|
||||
return LOG_STR("XTAL");
|
||||
#endif
|
||||
#if SOC_UART_SUPPORT_RTC_CLK
|
||||
case UART_SCLK_RTC:
|
||||
return LOG_STR("RTC");
|
||||
#endif
|
||||
#if SOC_UART_SUPPORT_REF_TICK
|
||||
case UART_SCLK_REF_TICK:
|
||||
return LOG_STR("REF_TICK");
|
||||
#endif
|
||||
default:
|
||||
return clock_source == UART_SCLK_DEFAULT ? LOG_STR("DEFAULT") : LOG_STR("UNKNOWN");
|
||||
}
|
||||
}
|
||||
|
||||
uart_config_t IDFUARTComponent::get_config_() {
|
||||
uart_parity_t parity = UART_PARITY_DISABLE;
|
||||
if (this->parity_ == UART_CONFIG_PARITY_EVEN) {
|
||||
@@ -70,7 +97,7 @@ uart_config_t IDFUARTComponent::get_config_() {
|
||||
uart_config.parity = parity;
|
||||
uart_config.stop_bits = this->stop_bits_ == 1 ? UART_STOP_BITS_1 : UART_STOP_BITS_2;
|
||||
uart_config.flow_ctrl = UART_HW_FLOWCTRL_DISABLE;
|
||||
uart_config.source_clk = UART_SCLK_DEFAULT;
|
||||
uart_config.source_clk = static_cast<uart_sclk_t>(this->clock_source_);
|
||||
uart_config.rx_flow_ctrl_thresh = 122;
|
||||
|
||||
return uart_config;
|
||||
@@ -336,12 +363,14 @@ void IDFUARTComponent::dump_config() {
|
||||
" Baud Rate: %" PRIu32 " baud\n"
|
||||
" Data Bits: %u\n"
|
||||
" Parity: %s\n"
|
||||
" Stop bits: %u"
|
||||
" Stop bits: %u\n"
|
||||
" Clock Source: %s"
|
||||
#ifdef USE_UART_WAKE_LOOP_ON_RX
|
||||
"\n Wake on data RX: ENABLED"
|
||||
#endif
|
||||
,
|
||||
this->baud_rate_, this->data_bits_, LOG_STR_ARG(parity_to_str(this->parity_)), this->stop_bits_);
|
||||
this->baud_rate_, this->data_bits_, LOG_STR_ARG(parity_to_str(this->parity_)), this->stop_bits_,
|
||||
LOG_STR_ARG(clock_source_to_str(static_cast<uart_sclk_t>(this->clock_source_))));
|
||||
this->check_logger_conflict();
|
||||
}
|
||||
|
||||
|
||||
@@ -37,6 +37,8 @@ class IDFUARTComponent final : public UARTComponent, public Component {
|
||||
|
||||
void set_flush_timeout(uint32_t flush_timeout_ms) override { this->flush_timeout_ms_ = flush_timeout_ms; }
|
||||
|
||||
void set_clock_source(uart_sclk_t clock_source) { this->clock_source_ = static_cast<uint8_t>(clock_source); }
|
||||
|
||||
uint8_t get_hw_serial_number() { return this->uart_num_; }
|
||||
|
||||
/// Discard everything received so far: the peek cache and the driver's RX buffer.
|
||||
@@ -105,7 +107,8 @@ class IDFUARTComponent final : public UARTComponent, public Component {
|
||||
|
||||
bool has_peek_{false};
|
||||
uint8_t peek_byte_{0};
|
||||
uint32_t flush_timeout_ms_{0}; ///< 0 means wait indefinitely (portMAX_DELAY).
|
||||
uint8_t clock_source_{UART_SCLK_DEFAULT}; ///< uart_sclk_t stored in a byte; the IDF values are all small.
|
||||
uint32_t flush_timeout_ms_{0}; ///< 0 means wait indefinitely (portMAX_DELAY).
|
||||
|
||||
#ifdef USE_UART_WAKE_LOOP_ON_RX
|
||||
// ISR callback for UART RX data notification — wakes the main loop directly.
|
||||
|
||||
@@ -2,6 +2,7 @@ from typing import Any
|
||||
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import i2c, sensor
|
||||
from esphome.components.const import UNIT_COUNTS
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
CONF_ACTUAL_GAIN,
|
||||
@@ -31,7 +32,6 @@ from esphome.types import ConfigType
|
||||
CODEOWNERS = ["@latonita"]
|
||||
DEPENDENCIES = ["i2c"]
|
||||
|
||||
UNIT_COUNTS = "#"
|
||||
ICON_MULTIPLICATION = "mdi:multiplication"
|
||||
ICON_BRIGHTNESS_7 = "mdi:brightness-7"
|
||||
|
||||
|
||||
@@ -1015,7 +1015,7 @@ json::SerializationBuffer<> WebServer::light_json_(light::LightState *obj, JsonD
|
||||
json::JsonBuilder builder;
|
||||
JsonObject root = builder.root();
|
||||
|
||||
set_json_value(root, obj, "light", obj->remote_values.is_on() ? "ON" : "OFF", start_config);
|
||||
set_json_value(root, obj, "light", obj->get_reported_values().is_on() ? "ON" : "OFF", start_config);
|
||||
|
||||
light::LightJSONSchema::dump_json(*obj, root);
|
||||
if (start_config == DETAIL_ALL) {
|
||||
|
||||
@@ -775,6 +775,7 @@ async def to_code(config: ConfigType) -> None:
|
||||
cg.add_build_flag("-Wno-unused-variable")
|
||||
cg.add_build_flag("-Wno-unused-but-set-variable")
|
||||
cg.add_build_flag("-Wno-sign-compare")
|
||||
cg.add_build_flag("-Wno-unused-function")
|
||||
# C++20 deprecated ++/--, compound assignment, and chained assignment on
|
||||
# volatile lvalues; GCC warns via -Wvolatile, on by default at gnu++20.
|
||||
# C++23 (P2327R1) removed the deprecation for compound assignment, so the
|
||||
|
||||
@@ -89,6 +89,7 @@
|
||||
#define USE_RADIO_FREQUENCY
|
||||
#define USE_LIGHT
|
||||
#define USE_LIGHT_GAMMA_LUT
|
||||
#define USE_LIGHT_TRANSITION_PUBLISH_INTERVAL
|
||||
#define USE_LOCK
|
||||
#define USE_LOGGER
|
||||
#define USE_LOGGER_LEVEL_LISTENERS
|
||||
|
||||
@@ -22,6 +22,10 @@ namespace esphome {
|
||||
* pointer. When it is default constructed, it has empty string. You can freely copy or move around this struct, but
|
||||
* never free its pointer. str() function can be used to export the content as std::string. StringRef is adopted from
|
||||
* <https://github.com/nghttp2/nghttp2/blob/29cbf8b83ff78faf405d1086b16adc09a8772eca/src/template.h#L376>
|
||||
*
|
||||
* A StringRef may carry a null pointer while its length is zero (the generated api messages start their encode only
|
||||
* string fields that way). Every member treats that as the empty string: the iterators form an empty range, and
|
||||
* c_str() and byte() return the null pointer, so callers that print or copy through those must check empty() first.
|
||||
*/
|
||||
class StringRef {
|
||||
public:
|
||||
@@ -78,7 +82,7 @@ class StringRef {
|
||||
|
||||
/// True if the view begins with the given prefix (std::string::starts_with-like)
|
||||
bool starts_with(const StringRef &prefix) const {
|
||||
return len_ >= prefix.len_ && std::memcmp(base_, prefix.base_, prefix.len_) == 0;
|
||||
return len_ >= prefix.len_ && (prefix.len_ == 0 || std::memcmp(base_, prefix.base_, prefix.len_) == 0);
|
||||
}
|
||||
bool starts_with(const char *prefix) const { return this->starts_with(StringRef(prefix)); }
|
||||
bool starts_with(const std::string &prefix) const { return this->starts_with(StringRef(prefix)); }
|
||||
@@ -92,14 +96,15 @@ class StringRef {
|
||||
return actual;
|
||||
}
|
||||
|
||||
std::string str() const { return std::string(base_, len_); }
|
||||
std::string str() const { return std::string(base_, len_); } // fine for {nullptr, 0}: nothing is read
|
||||
const uint8_t *byte() const { return reinterpret_cast<const uint8_t *>(base_); }
|
||||
|
||||
operator std::string() const { return str(); }
|
||||
|
||||
/// Compare (compatible with std::string::compare)
|
||||
int compare(const StringRef &other) const {
|
||||
int result = std::memcmp(base_, other.base_, std::min(len_, other.len_));
|
||||
size_type common = std::min(len_, other.len_);
|
||||
int result = common == 0 ? 0 : std::memcmp(base_, other.base_, common);
|
||||
if (result != 0)
|
||||
return result;
|
||||
if (len_ < other.len_)
|
||||
@@ -222,6 +227,11 @@ inline std::string operator+(const std::string &lhs, const StringRef &rhs) {
|
||||
namespace internal {
|
||||
// NOLINTBEGIN(google-runtime-int)
|
||||
template<typename R, typename F> inline R parse_number(const StringRef &str, size_t *pos, F conv) {
|
||||
if (str.empty()) { // nothing to parse, and a null view must not reach the C library
|
||||
if (pos)
|
||||
*pos = 0;
|
||||
return R{};
|
||||
}
|
||||
char *end;
|
||||
R result = conv(str.c_str(), &end);
|
||||
// Set pos to 0 on conversion failure (when no characters consumed), otherwise index after number
|
||||
@@ -230,6 +240,11 @@ template<typename R, typename F> inline R parse_number(const StringRef &str, siz
|
||||
return result;
|
||||
}
|
||||
template<typename R, typename F> inline R parse_number(const StringRef &str, size_t *pos, int base, F conv) {
|
||||
if (str.empty()) { // nothing to parse, and a null view must not reach the C library
|
||||
if (pos)
|
||||
*pos = 0;
|
||||
return R{};
|
||||
}
|
||||
char *end;
|
||||
R result = conv(str.c_str(), &end, base);
|
||||
// Set pos to 0 on conversion failure (when no characters consumed), otherwise index after number
|
||||
@@ -258,7 +273,14 @@ inline double stod(const StringRef &str, size_t *pos = nullptr) {
|
||||
|
||||
#ifdef USE_JSON
|
||||
// NOLINTNEXTLINE(readability-identifier-naming)
|
||||
inline void convertToJson(const StringRef &src, JsonVariant dst) { dst.set(src.c_str()); }
|
||||
inline void convertToJson(const StringRef &src, JsonVariant dst) {
|
||||
// Bounded by the view length; a null, empty view becomes "" rather than JSON null
|
||||
if (src.empty()) {
|
||||
dst.set("");
|
||||
return;
|
||||
}
|
||||
dst.set(JsonString(src.c_str(), src.size()));
|
||||
}
|
||||
#endif // USE_JSON
|
||||
|
||||
} // namespace esphome
|
||||
|
||||
+1
-1
@@ -11,7 +11,7 @@ tzdata>=2026.4 # from time
|
||||
pyserial==3.5
|
||||
platformio==6.2.0
|
||||
esptool==5.4.0
|
||||
click==8.3.3
|
||||
click==8.5.0
|
||||
aioesphomeapi==46.5.0
|
||||
aiohappyeyeballs==2.7.1 # Happy Eyeballs for requests downloads; already pulled in by aioesphomeapi
|
||||
zeroconf==0.151.3
|
||||
|
||||
+286
-304
@@ -28,6 +28,11 @@ class WireType(IntEnum):
|
||||
END_GROUP = 4 # groups (deprecated)
|
||||
FIXED32 = 5 # fixed32, sfixed32, float
|
||||
|
||||
@property
|
||||
def cpp_name(self) -> str:
|
||||
"""The matching constant in proto.h."""
|
||||
return f"WIRE_TYPE_{self.name}"
|
||||
|
||||
|
||||
# Generate with
|
||||
# protoc --python_out=script/api_protobuf -I esphome/components/api/ api_options.proto
|
||||
@@ -126,9 +131,10 @@ def camel_to_snake(name: str) -> str:
|
||||
return re.sub("([a-z0-9])([A-Z])", r"\1_\2", s1).lower()
|
||||
|
||||
|
||||
def force_str(force: bool) -> str:
|
||||
"""Convert a boolean force value to string format for C++ code."""
|
||||
return str(force).lower()
|
||||
def _encode_call(func: str, *args: str, force: bool = False) -> str:
|
||||
"""Emit one ProtoEncode call; every helper takes the cursor and returns it advanced."""
|
||||
suffix = "_force" if force else ""
|
||||
return f"pos = ProtoEncode::{func}{suffix}({', '.join(('pos', *args))});"
|
||||
|
||||
|
||||
class TypeInfo(ABC):
|
||||
@@ -223,55 +229,39 @@ class TypeInfo(ABC):
|
||||
def class_member(self) -> str:
|
||||
return f"{self.cpp_type} {self.field_name}{{{self.default_value}}};"
|
||||
|
||||
@property
|
||||
def decode_varint_content(self) -> str:
|
||||
content = self.decode_varint
|
||||
if content is None:
|
||||
return None
|
||||
return f"case {self.number}: this->{self.field_name} = {content}; break;"
|
||||
def decode_case(self, body: str) -> str:
|
||||
"""Emit one decode_field() case, keyed on the field's wire tag."""
|
||||
return f"case proto_tag({self.number}, {self.wire_type.cpp_name}):\n" + indent(
|
||||
f"{body}\nbreak;"
|
||||
)
|
||||
|
||||
decode_varint = None
|
||||
# Expression that reads this field from `value`; None when the type is never decoded.
|
||||
decode_expr: str | None = None
|
||||
|
||||
def _decode_store(self, expr: str) -> str:
|
||||
return f"this->{self.field_name} = {expr};"
|
||||
|
||||
@property
|
||||
def decode_length_content(self) -> str:
|
||||
content = self.decode_length
|
||||
if content is None:
|
||||
return None
|
||||
return f"case {self.number}: this->{self.field_name} = {content}; break;"
|
||||
|
||||
decode_length = None
|
||||
|
||||
@property
|
||||
def decode_32bit_content(self) -> str:
|
||||
content = self.decode_32bit
|
||||
if content is None:
|
||||
return None
|
||||
return f"case {self.number}: this->{self.field_name} = {content}; break;"
|
||||
|
||||
decode_32bit = None
|
||||
|
||||
@property
|
||||
def decode_64bit_content(self) -> str:
|
||||
content = self.decode_64bit
|
||||
if content is None:
|
||||
return None
|
||||
return f"case {self.number}: this->{self.field_name} = {content}; break;"
|
||||
|
||||
decode_64bit = None
|
||||
def decode_content(self) -> str | None:
|
||||
"""The decode_field() case for this field, or None when it is never decoded."""
|
||||
expr = self.decode_expr
|
||||
return None if expr is None else self.decode_case(self._decode_store(expr))
|
||||
|
||||
# Mapping from encode_func to raw encode expression template.
|
||||
# When a forced field has a single-byte tag, the code generator emits
|
||||
# write_raw_byte(tag) + raw encode instead of the full encode_* method,
|
||||
# eliminating the zero-check branch and encode_field_raw indirection.
|
||||
# {value} is replaced with the actual field expression.
|
||||
RAW_ENCODE_MAP: dict[str, str] = {
|
||||
"encode_uint32": "ProtoEncode::encode_varint_raw(pos, {value});",
|
||||
"encode_uint64": "ProtoEncode::encode_varint_raw_64(pos, {value});",
|
||||
"encode_sint32": "ProtoEncode::encode_varint_raw_short(pos, encode_zigzag32({value}));",
|
||||
"encode_sint64": "ProtoEncode::encode_varint_raw_64(pos, encode_zigzag64({value}));",
|
||||
"encode_int64": "ProtoEncode::encode_varint_raw_64(pos, static_cast<uint64_t>({value}));",
|
||||
"encode_bool": "ProtoEncode::write_raw_byte(pos, {value} ? 0x01 : 0x00);",
|
||||
RAW_ENCODE_MAP: dict[str, tuple[str, str]] = {
|
||||
"encode_uint32": ("encode_varint_raw", "{value}"),
|
||||
"encode_uint64": ("encode_varint_raw_64", "{value}"),
|
||||
"encode_sint32": ("encode_varint_raw_short", "encode_zigzag32({value})"),
|
||||
"encode_sint64": ("encode_varint_raw_64", "encode_zigzag64({value})"),
|
||||
"encode_int64": ("encode_varint_raw_64", "static_cast<uint64_t>({value})"),
|
||||
"encode_bool": ("write_raw_byte", "{value} ? 0x01 : 0x00"),
|
||||
}
|
||||
# Fixed32 value expression for the shared tag+fixed32 writer; None for other wire types
|
||||
fixed32_value_template: str | None = None
|
||||
|
||||
def _encode_with_precomputed_tag(self, value_expr: str) -> str | None:
|
||||
"""Try to emit a precomputed-tag encode for a field.
|
||||
@@ -288,12 +278,17 @@ class TypeInfo(ABC):
|
||||
return None
|
||||
max_val = self.max_value
|
||||
# Only use RAW_ENCODE_MAP for forced fields or fields with max_value
|
||||
raw_expr = None
|
||||
raw = None
|
||||
if self.force or max_val is not None:
|
||||
raw_expr = self.RAW_ENCODE_MAP.get(self.encode_func)
|
||||
if raw_expr is None:
|
||||
raw = self.RAW_ENCODE_MAP.get(self.encode_func)
|
||||
if raw is None:
|
||||
return None
|
||||
body = f"ProtoEncode::write_raw_byte(pos, {tag});\n{raw_expr.format(value=value_expr)}"
|
||||
func, arg = raw
|
||||
body = (
|
||||
_encode_call("write_raw_byte", str(tag))
|
||||
+ "\n"
|
||||
+ _encode_call(func, arg.format(value=value_expr))
|
||||
)
|
||||
if self.force:
|
||||
return body
|
||||
# Non-forced with max_value: inline zero-check + raw encode
|
||||
@@ -314,23 +309,44 @@ class TypeInfo(ABC):
|
||||
return None
|
||||
# When max_len < 128, length varint is always 1 byte
|
||||
len_encode = (
|
||||
f"ProtoEncode::write_raw_byte(pos, static_cast<uint8_t>({len_expr}));"
|
||||
_encode_call("write_raw_byte", f"static_cast<uint8_t>({len_expr})")
|
||||
if max_len is not None and max_len < 128
|
||||
else f"ProtoEncode::encode_varint_raw(pos, {len_expr});"
|
||||
else _encode_call("encode_varint_raw", len_expr)
|
||||
)
|
||||
return "\n".join(
|
||||
(
|
||||
_encode_call("write_raw_byte", str(tag)),
|
||||
len_encode,
|
||||
_encode_call("encode_raw", data_expr, len_expr),
|
||||
)
|
||||
)
|
||||
|
||||
def _encode_fixed32_with_precomputed_tag(self, value: str) -> str | None:
|
||||
"""Single-byte tag fixed32 write, or None for other types and multi-byte tags."""
|
||||
tag = self.calculate_tag()
|
||||
if self.fixed32_value_template is None or tag >= 128:
|
||||
return None
|
||||
value_expr = self.fixed32_value_template.format(value=value)
|
||||
if self.force:
|
||||
return _encode_call("write_tag_and_fixed32", str(tag), value_expr)
|
||||
return (
|
||||
f"ProtoEncode::write_raw_byte(pos, {tag});\n"
|
||||
f"{len_encode}\n"
|
||||
f"ProtoEncode::encode_raw(pos, {data_expr}, {len_expr});"
|
||||
f"if (uint32_t raw = {value_expr}; raw != 0) [[likely]] {{\n"
|
||||
f" {_encode_call('write_tag_and_fixed32', str(tag), 'raw')}\n"
|
||||
"}"
|
||||
)
|
||||
|
||||
@property
|
||||
def encode_content(self) -> str:
|
||||
if result := self._encode_with_precomputed_tag(f"this->{self.field_name}"):
|
||||
value = f"this->{self.field_name}"
|
||||
if result := self._encode_with_precomputed_tag(value):
|
||||
return result
|
||||
if self.force:
|
||||
return f"ProtoEncode::{self.encode_func}(pos, {self.number}, this->{self.field_name}, true);"
|
||||
return f"ProtoEncode::{self.encode_func}(pos, {self.number}, this->{self.field_name});"
|
||||
if result := self._encode_fixed32_with_precomputed_tag(value):
|
||||
return result
|
||||
return _encode_call(self.encode_func, str(self.number), value, force=self.force)
|
||||
|
||||
def encode_element(self, number: int, element: str) -> str:
|
||||
"""Encode one element of a repeated field; elements are always written."""
|
||||
return _encode_call(self.encode_func, str(number), element, force=True)
|
||||
|
||||
encode_func = None
|
||||
|
||||
@@ -550,17 +566,17 @@ def create_field_type_info(
|
||||
# For messages that decode (SOURCE_CLIENT or SOURCE_BOTH), use pointer
|
||||
# for zero-copy access to the receive buffer
|
||||
if needs_decode:
|
||||
return PointerToBytesBufferType(field, None)
|
||||
return PointerToBytesBufferType(field, needs_decode)
|
||||
|
||||
# For SOURCE_SERVER (encode only), explicit annotation is still needed
|
||||
if get_field_opt(field, pb.pointer_to_buffer, False):
|
||||
return PointerToBytesBufferType(field, None)
|
||||
return PointerToBytesBufferType(field, needs_decode)
|
||||
|
||||
return BytesType(field, needs_decode, needs_encode)
|
||||
|
||||
# Special handling for string fields - use StringRef for zero-copy
|
||||
if field.type == 9:
|
||||
return PointerToStringBufferType(field, None)
|
||||
return PointerToStringBufferType(field, needs_decode)
|
||||
|
||||
validate_field_type(field.type, field.name)
|
||||
if field.type == 11:
|
||||
@@ -605,7 +621,6 @@ class DoubleType(FixedSizeTypeMixin, TypeInfo):
|
||||
# Unsupported but defined for completeness
|
||||
cpp_type = "double"
|
||||
default_value = "0.0"
|
||||
decode_64bit = "value.as_double()"
|
||||
encode_func = "encode_double"
|
||||
wire_type = WireType.FIXED64 # Uses wire type 1 according to protobuf spec
|
||||
|
||||
@@ -631,10 +646,12 @@ class DoubleType(FixedSizeTypeMixin, TypeInfo):
|
||||
class FloatType(FixedSizeTypeMixin, TypeInfo):
|
||||
cpp_type = "float"
|
||||
default_value = "0.0f"
|
||||
decode_32bit = "value.as_float()"
|
||||
decode_expr = "value.as_float()"
|
||||
encode_func = "encode_float"
|
||||
wire_type = WireType.FIXED32 # Uses wire type 5
|
||||
|
||||
fixed32_value_template = "float_to_raw({value})"
|
||||
|
||||
def dump(self, name: str) -> str:
|
||||
o = f'snprintf(buffer, sizeof(buffer), "%g", {name});\n'
|
||||
o += "out.append(buffer);"
|
||||
@@ -658,7 +675,7 @@ class Int64Type(VarintTypeMixin, TypeInfo):
|
||||
cpp_type = "int64_t"
|
||||
_varint_max_bits = 64
|
||||
default_value = "0"
|
||||
decode_varint = "static_cast<int64_t>(value)"
|
||||
decode_expr = "static_cast<int64_t>(value.as_varint())"
|
||||
encode_func = "encode_int64"
|
||||
wire_type = WireType.VARINT # Uses wire type 0
|
||||
|
||||
@@ -679,7 +696,7 @@ class UInt64Type(VarintTypeMixin, TypeInfo):
|
||||
cpp_type = "uint64_t"
|
||||
_varint_max_bits = 64
|
||||
default_value = "0"
|
||||
decode_varint = "value"
|
||||
decode_expr = "value.as_varint()"
|
||||
encode_func = "encode_uint64"
|
||||
wire_type = WireType.VARINT # Uses wire type 0
|
||||
|
||||
@@ -697,11 +714,11 @@ class UInt64Type(VarintTypeMixin, TypeInfo):
|
||||
return self._get_simple_size_calculation(name, force, "uint64")
|
||||
|
||||
@property
|
||||
def RAW_ENCODE_MAP(self) -> dict[str, str]: # noqa: N802
|
||||
def RAW_ENCODE_MAP(self) -> dict[str, tuple[str, str]]: # noqa: N802
|
||||
if self.mac_address:
|
||||
return {
|
||||
**TypeInfo.RAW_ENCODE_MAP,
|
||||
"encode_uint64": "ProtoEncode::encode_varint_raw_48bit(pos, {value});",
|
||||
"encode_uint64": ("encode_varint_raw_48bit", "{value}"),
|
||||
}
|
||||
return TypeInfo.RAW_ENCODE_MAP
|
||||
|
||||
@@ -714,7 +731,7 @@ class Int32Type(VarintTypeMixin, TypeInfo):
|
||||
cpp_type = "int32_t"
|
||||
_varint_max_bits = 64 # int32 is sign-extended to 64 bits in protobuf
|
||||
default_value = "0"
|
||||
decode_varint = "static_cast<int32_t>(value)"
|
||||
decode_expr = "static_cast<int32_t>(value.as_varint())"
|
||||
encode_func = "encode_int32"
|
||||
wire_type = WireType.VARINT # Uses wire type 0
|
||||
|
||||
@@ -734,7 +751,6 @@ class Int32Type(VarintTypeMixin, TypeInfo):
|
||||
class Fixed64Type(FixedSizeTypeMixin, TypeInfo):
|
||||
cpp_type = "uint64_t"
|
||||
default_value = "0"
|
||||
decode_64bit = "value.as_fixed64()"
|
||||
encode_func = "encode_fixed64"
|
||||
wire_type = WireType.FIXED64 # Uses wire type 1
|
||||
|
||||
@@ -760,7 +776,7 @@ class Fixed64Type(FixedSizeTypeMixin, TypeInfo):
|
||||
class Fixed32Type(FixedSizeTypeMixin, TypeInfo):
|
||||
cpp_type = "uint32_t"
|
||||
default_value = "0"
|
||||
decode_32bit = "value.as_fixed32()"
|
||||
decode_expr = "value.as_fixed32()"
|
||||
encode_func = "encode_fixed32"
|
||||
wire_type = WireType.FIXED32 # Uses wire type 5
|
||||
|
||||
@@ -769,15 +785,7 @@ class Fixed32Type(FixedSizeTypeMixin, TypeInfo):
|
||||
o += "out.append(buffer);"
|
||||
return o
|
||||
|
||||
@property
|
||||
def encode_content(self) -> str:
|
||||
tag = self.calculate_tag()
|
||||
if self.force and tag < 128:
|
||||
# Emit combined tag+value write: precomputed tag + direct memcpy
|
||||
return f"ProtoEncode::write_tag_and_fixed32(pos, {tag}, this->{self.field_name});"
|
||||
if self.force:
|
||||
return f"ProtoEncode::{self.encode_func}(pos, {self.number}, this->{self.field_name}, true);"
|
||||
return f"ProtoEncode::{self.encode_func}(pos, {self.number}, this->{self.field_name});"
|
||||
fixed32_value_template = "{value}"
|
||||
|
||||
def get_size_calculation(self, name: str, force: bool = False) -> str:
|
||||
field_id_size = self.calculate_field_id_size()
|
||||
@@ -797,7 +805,7 @@ class BoolType(VarintTypeMixin, TypeInfo):
|
||||
_varint_max_bits = 1
|
||||
cpp_type = "bool"
|
||||
default_value = "false"
|
||||
decode_varint = "value != 0"
|
||||
decode_expr = "value.as_bool()"
|
||||
encode_func = "encode_bool"
|
||||
wire_type = WireType.VARINT # Uses wire type 0
|
||||
|
||||
@@ -817,7 +825,7 @@ class StringType(TypeInfo):
|
||||
default_value = ""
|
||||
reference_type = "std::string &"
|
||||
const_reference_type = "const std::string &"
|
||||
decode_length = "value.as_string()"
|
||||
decode_expr = "value.as_string()"
|
||||
encode_func = "encode_string"
|
||||
wire_type = WireType.LENGTH_DELIMITED # Uses wire type 2
|
||||
|
||||
@@ -851,9 +859,12 @@ class StringType(TypeInfo):
|
||||
f"this->{self.field_name}_ref_.size()",
|
||||
):
|
||||
return result
|
||||
if self.force:
|
||||
return f"ProtoEncode::encode_string(pos, {self.number}, this->{self.field_name}_ref_, true);"
|
||||
return f"ProtoEncode::encode_string(pos, {self.number}, this->{self.field_name}_ref_);"
|
||||
return _encode_call(
|
||||
"encode_string",
|
||||
str(self.number),
|
||||
f"this->{self.field_name}_ref_",
|
||||
force=self.force,
|
||||
)
|
||||
|
||||
def dump(self, name):
|
||||
# If name is 'it', this is a repeated field element - always use string
|
||||
@@ -929,6 +940,9 @@ class MessageType(TypeInfo):
|
||||
def can_use_dump_field(cls) -> bool:
|
||||
return False
|
||||
|
||||
def encode_element(self, number: int, element: str) -> str:
|
||||
return _encode_call("encode_sub_message", "buffer", str(number), element)
|
||||
|
||||
@property
|
||||
def cpp_type(self) -> str:
|
||||
return self._field.type_name[1:]
|
||||
@@ -951,15 +965,9 @@ class MessageType(TypeInfo):
|
||||
@property
|
||||
def encode_content(self) -> str:
|
||||
# Sub-message encoding needs buffer for backpatch/sync
|
||||
return f"ProtoEncode::{self.encode_func}(pos, buffer, {self.number}, this->{self.field_name});"
|
||||
|
||||
@property
|
||||
def decode_length(self) -> str:
|
||||
# Override to return None for message types because we can't use template-based
|
||||
# decoding when the specific message type isn't known at compile time.
|
||||
# Instead, we use the non-template decode_to_message() method which allows
|
||||
# runtime polymorphism through virtual function calls.
|
||||
return None
|
||||
return _encode_call(
|
||||
self.encode_func, "buffer", str(self.number), f"this->{self.field_name}"
|
||||
)
|
||||
|
||||
@property
|
||||
def public_content(self) -> list[str]:
|
||||
@@ -976,19 +984,14 @@ class MessageType(TypeInfo):
|
||||
)
|
||||
|
||||
@property
|
||||
def decode_length_content(self) -> str:
|
||||
# Custom decode that doesn't use templates
|
||||
def decode_content(self) -> str:
|
||||
body = f"value.decode_to_message(this->{self.field_name});"
|
||||
if self._track_presence:
|
||||
# decode_to_message() cannot report failure, so setting the flag
|
||||
# afterwards only documents intent; a status-returning decode could
|
||||
# gate it for real without touching callers.
|
||||
return (
|
||||
f"case {self.number}:\n"
|
||||
f" value.decode_to_message(this->{self.field_name});\n"
|
||||
f" this->has_{self.name} = true;\n"
|
||||
f" break;"
|
||||
)
|
||||
return f"case {self.number}: value.decode_to_message(this->{self.field_name}); break;"
|
||||
body += f"\nthis->has_{self.name} = true;"
|
||||
return self.decode_case(body)
|
||||
|
||||
def dump(self, name: str) -> str:
|
||||
return f"{name}.dump_to(out);"
|
||||
@@ -1027,7 +1030,7 @@ class BytesType(TypeInfo):
|
||||
reference_type = "std::string &"
|
||||
const_reference_type = "const std::string &"
|
||||
encode_func = "encode_bytes"
|
||||
decode_length = "value.as_string()"
|
||||
decode_expr = "value.as_string()"
|
||||
wire_type = WireType.LENGTH_DELIMITED # Uses wire type 2
|
||||
|
||||
@property
|
||||
@@ -1058,9 +1061,13 @@ class BytesType(TypeInfo):
|
||||
f"this->{self.field_name}_ptr_", f"this->{self.field_name}_len_"
|
||||
):
|
||||
return result
|
||||
if self.force:
|
||||
return f"ProtoEncode::encode_bytes(pos, {self.number}, this->{self.field_name}_ptr_, this->{self.field_name}_len_, true);"
|
||||
return f"ProtoEncode::encode_bytes(pos, {self.number}, this->{self.field_name}_ptr_, this->{self.field_name}_len_);"
|
||||
return _encode_call(
|
||||
"encode_bytes",
|
||||
str(self.number),
|
||||
f"this->{self.field_name}_ptr_",
|
||||
f"this->{self.field_name}_len_",
|
||||
force=self.force,
|
||||
)
|
||||
|
||||
def dump(self, name: str) -> str:
|
||||
ptr_dump = f"format_hex_pretty(this->{self.field_name}_ptr_, this->{self.field_name}_len_)"
|
||||
@@ -1127,16 +1134,12 @@ class PointerToBufferTypeBase(TypeInfo):
|
||||
def can_use_dump_field(cls) -> bool:
|
||||
return False
|
||||
|
||||
# Only here to make needs_decode required: the null string default keys off it, so a call
|
||||
# site must not fall back on the base class default
|
||||
def __init__(
|
||||
self, field: descriptor.FieldDescriptorProto, size: int | None = None
|
||||
self, field: descriptor.FieldDescriptorProto, needs_decode: bool
|
||||
) -> None:
|
||||
super().__init__(field)
|
||||
self.array_size = 0
|
||||
|
||||
@property
|
||||
def decode_length(self) -> str | None:
|
||||
# This is handled in decode_length_content
|
||||
return None
|
||||
super().__init__(field, needs_decode)
|
||||
|
||||
@property
|
||||
def wire_type(self) -> WireType:
|
||||
@@ -1170,17 +1173,20 @@ class PointerToBytesBufferType(PointerToBufferTypeBase):
|
||||
f"this->{self.field_name}", f"this->{self.field_name}_len"
|
||||
):
|
||||
return result
|
||||
if self.force:
|
||||
return f"ProtoEncode::encode_bytes(pos, {self.number}, this->{self.field_name}, this->{self.field_name}_len, true);"
|
||||
return f"ProtoEncode::encode_bytes(pos, {self.number}, this->{self.field_name}, this->{self.field_name}_len);"
|
||||
return _encode_call(
|
||||
"encode_bytes",
|
||||
str(self.number),
|
||||
f"this->{self.field_name}",
|
||||
f"this->{self.field_name}_len",
|
||||
force=self.force,
|
||||
)
|
||||
|
||||
@property
|
||||
def decode_length_content(self) -> str | None:
|
||||
return f"""case {self.number}: {{
|
||||
this->{self.field_name} = value.data();
|
||||
this->{self.field_name}_len = value.size();
|
||||
break;
|
||||
}}"""
|
||||
def decode_content(self) -> str:
|
||||
return self.decode_case(
|
||||
f"this->{self.field_name} = value.data();\n"
|
||||
f"this->{self.field_name}_len = value.size();",
|
||||
)
|
||||
|
||||
def dump(self, name: str) -> str:
|
||||
return (
|
||||
@@ -1214,34 +1220,53 @@ class PointerToStringBufferType(PointerToBufferTypeBase):
|
||||
def can_use_dump_field(cls) -> bool:
|
||||
return True
|
||||
|
||||
@property
|
||||
def _starts_null(self) -> bool:
|
||||
"""A field that is only encoded, and skipped when empty, never has its pointer read
|
||||
before it is set, so it can default to a null StringRef and the message constructs as
|
||||
one zero fill. Any encode path that copies unconditionally must check this."""
|
||||
return not self._needs_decode and not self.force
|
||||
|
||||
@property
|
||||
def public_content(self) -> list[str]:
|
||||
if self._starts_null:
|
||||
return [
|
||||
f"StringRef {self.field_name}{{nullptr, 0}}; // null until set, encode only"
|
||||
]
|
||||
return [f"StringRef {self.field_name}{{}};"]
|
||||
|
||||
@property
|
||||
def encode_content(self) -> str:
|
||||
max_len = self.max_data_length
|
||||
if max_len is not None and max_len < 128 and self.force:
|
||||
assert not self._starts_null, (
|
||||
"unconditional copy of a field that may start null"
|
||||
)
|
||||
tag = self.calculate_tag()
|
||||
if tag < 128:
|
||||
return f"ProtoEncode::encode_short_string_force(pos, {tag}, this->{self.field_name});"
|
||||
return _encode_call(
|
||||
"encode_short_string_force", str(tag), f"this->{self.field_name}"
|
||||
)
|
||||
if result := self._encode_bytes_with_precomputed_tag(
|
||||
f"this->{self.field_name}.c_str()",
|
||||
f"this->{self.field_name}.size()",
|
||||
):
|
||||
assert not self._starts_null, (
|
||||
"unconditional copy of a field that may start null"
|
||||
)
|
||||
return result
|
||||
if self.force:
|
||||
return f"ProtoEncode::encode_string(pos, {self.number}, this->{self.field_name}, true);"
|
||||
return (
|
||||
f"ProtoEncode::encode_string(pos, {self.number}, this->{self.field_name});"
|
||||
return _encode_call(
|
||||
"encode_string",
|
||||
str(self.number),
|
||||
f"this->{self.field_name}",
|
||||
force=self.force,
|
||||
)
|
||||
|
||||
@property
|
||||
def decode_length_content(self) -> str | None:
|
||||
return f"""case {self.number}: {{
|
||||
this->{self.field_name} = StringRef(reinterpret_cast<const char *>(value.data()), value.size());
|
||||
break;
|
||||
}}"""
|
||||
def decode_content(self) -> str:
|
||||
return self.decode_case(
|
||||
f"this->{self.field_name} = StringRef(value.data(), value.size());",
|
||||
)
|
||||
|
||||
def dump(self, name: str) -> str:
|
||||
# Not used since we use dump_field, but required by abstract base class
|
||||
@@ -1310,14 +1335,13 @@ class PackedBufferTypeInfo(TypeInfo):
|
||||
]
|
||||
|
||||
@property
|
||||
def decode_length_content(self) -> str:
|
||||
def decode_content(self) -> str:
|
||||
"""Store pointer to buffer and calculate count of packed varints."""
|
||||
return f"""case {self.number}: {{
|
||||
this->{self.field_name}_data_ = value.data();
|
||||
this->{self.field_name}_length_ = value.size();
|
||||
this->{self.field_name}_count_ = count_packed_varints(value.data(), value.size());
|
||||
break;
|
||||
}}"""
|
||||
return self.decode_case(
|
||||
f"this->{self.field_name}_data_ = value.data();\n"
|
||||
f"this->{self.field_name}_length_ = value.size();\n"
|
||||
f"this->{self.field_name}_count_ = count_packed_varints(value.data(), value.size());",
|
||||
)
|
||||
|
||||
@property
|
||||
def encode_content(self) -> str:
|
||||
@@ -1402,17 +1426,11 @@ class FixedArrayBytesType(TypeInfo):
|
||||
]
|
||||
|
||||
@property
|
||||
def decode_length_content(self) -> str:
|
||||
o = f"case {self.number}: {{\n"
|
||||
o += " const std::string &data_str = value.as_string();\n"
|
||||
o += f" this->{self.field_name}_len = data_str.size();\n"
|
||||
o += f" if (this->{self.field_name}_len > {self.array_size}) {{\n"
|
||||
o += f" this->{self.field_name}_len = {self.array_size};\n"
|
||||
o += " }\n"
|
||||
o += f" memcpy(this->{self.field_name}, data_str.data(), this->{self.field_name}_len);\n"
|
||||
o += " break;\n"
|
||||
o += "}"
|
||||
return o
|
||||
def decode_content(self) -> str:
|
||||
return self.decode_case(
|
||||
f"this->{self.field_name}_len = std::min<size_t>(value.size(), {self.array_size});\n"
|
||||
f"memcpy(this->{self.field_name}, value.data(), this->{self.field_name}_len);",
|
||||
)
|
||||
|
||||
@property
|
||||
def encode_content(self) -> str:
|
||||
@@ -1421,9 +1439,13 @@ class FixedArrayBytesType(TypeInfo):
|
||||
f"this->{self.field_name}", f"this->{self.field_name}_len", max_len=max_len
|
||||
):
|
||||
return result
|
||||
if self.force:
|
||||
return f"ProtoEncode::encode_bytes(pos, {self.number}, this->{self.field_name}, this->{self.field_name}_len, true);"
|
||||
return f"ProtoEncode::encode_bytes(pos, {self.number}, this->{self.field_name}, this->{self.field_name}_len);"
|
||||
return _encode_call(
|
||||
"encode_bytes",
|
||||
str(self.number),
|
||||
f"this->{self.field_name}",
|
||||
f"this->{self.field_name}_len",
|
||||
force=self.force,
|
||||
)
|
||||
|
||||
def dump(self, name: str) -> str:
|
||||
return f"out.append(format_hex_pretty({name}, {name}_len));"
|
||||
@@ -1471,7 +1493,7 @@ class UInt32Type(VarintTypeMixin, TypeInfo):
|
||||
cpp_type = "uint32_t"
|
||||
_varint_max_bits = 32
|
||||
default_value = "0"
|
||||
decode_varint = "value"
|
||||
decode_expr = "value.as_varint()"
|
||||
encode_func = "encode_uint32"
|
||||
wire_type = WireType.VARINT # Uses wire type 0
|
||||
|
||||
@@ -1494,13 +1516,21 @@ class UInt32Type(VarintTypeMixin, TypeInfo):
|
||||
class EnumType(VarintTypeMixin, TypeInfo):
|
||||
_varint_max_bits = 32
|
||||
|
||||
def encode_element(self, number: int, element: str) -> str:
|
||||
return _encode_call(
|
||||
self.encode_func,
|
||||
str(number),
|
||||
f"static_cast<uint32_t>({element})",
|
||||
force=True,
|
||||
)
|
||||
|
||||
@property
|
||||
def cpp_type(self) -> str:
|
||||
return f"enums::{self._field.type_name[1:]}"
|
||||
|
||||
@property
|
||||
def decode_varint(self) -> str:
|
||||
return f"static_cast<{self.cpp_type}>(value)"
|
||||
def decode_expr(self) -> str:
|
||||
return f"static_cast<{self.cpp_type}>(value.as_varint())"
|
||||
|
||||
default_value = ""
|
||||
wire_type = WireType.VARINT # Uses wire type 0
|
||||
@@ -1520,9 +1550,9 @@ class EnumType(VarintTypeMixin, TypeInfo):
|
||||
@property
|
||||
def encode_content(self) -> str:
|
||||
value_expr = f"static_cast<uint32_t>(this->{self.field_name})"
|
||||
if self.force:
|
||||
return f"ProtoEncode::{self.encode_func}(pos, {self.number}, {value_expr}, true);"
|
||||
return f"ProtoEncode::{self.encode_func}(pos, {self.number}, {value_expr});"
|
||||
return _encode_call(
|
||||
self.encode_func, str(self.number), value_expr, force=self.force
|
||||
)
|
||||
|
||||
def dump(self, name: str) -> str:
|
||||
return f"out.append_p(proto_enum_to_string<{self.cpp_type}>({name}));"
|
||||
@@ -1547,7 +1577,7 @@ class EnumType(VarintTypeMixin, TypeInfo):
|
||||
class SFixed32Type(FixedSizeTypeMixin, TypeInfo):
|
||||
cpp_type = "int32_t"
|
||||
default_value = "0"
|
||||
decode_32bit = "value.as_sfixed32()"
|
||||
decode_expr = "value.as_sfixed32()"
|
||||
encode_func = "encode_sfixed32"
|
||||
wire_type = WireType.FIXED32 # Uses wire type 5
|
||||
|
||||
@@ -1573,7 +1603,6 @@ class SFixed32Type(FixedSizeTypeMixin, TypeInfo):
|
||||
class SFixed64Type(FixedSizeTypeMixin, TypeInfo):
|
||||
cpp_type = "int64_t"
|
||||
default_value = "0"
|
||||
decode_64bit = "value.as_sfixed64()"
|
||||
encode_func = "encode_sfixed64"
|
||||
wire_type = WireType.FIXED64 # Uses wire type 1
|
||||
|
||||
@@ -1600,7 +1629,7 @@ class SInt32Type(VarintTypeMixin, TypeInfo):
|
||||
cpp_type = "int32_t"
|
||||
_varint_max_bits = 32 # zigzag encoding keeps it 32-bit
|
||||
default_value = "0"
|
||||
decode_varint = "decode_zigzag32(static_cast<uint32_t>(value))"
|
||||
decode_expr = "decode_zigzag32(static_cast<uint32_t>(value.as_varint()))"
|
||||
encode_func = "encode_sint32"
|
||||
wire_type = WireType.VARINT # Uses wire type 0
|
||||
|
||||
@@ -1621,7 +1650,7 @@ class SInt64Type(VarintTypeMixin, TypeInfo):
|
||||
cpp_type = "int64_t"
|
||||
_varint_max_bits = 64
|
||||
default_value = "0"
|
||||
decode_varint = "decode_zigzag64(value)"
|
||||
decode_expr = "decode_zigzag64(value.as_varint())"
|
||||
encode_func = "encode_sint64"
|
||||
wire_type = WireType.VARINT # Uses wire type 0
|
||||
|
||||
@@ -1701,9 +1730,9 @@ def _generate_inline_encode_block(
|
||||
|
||||
lines = []
|
||||
lines.append(f"auto &sub_msg = {element};")
|
||||
lines.append(f"ProtoEncode::write_raw_byte(pos, {tag});")
|
||||
lines.append(_encode_call("write_raw_byte", str(tag)))
|
||||
lines.append("uint8_t *len_pos = pos;")
|
||||
lines.append("ProtoEncode::reserve_byte(pos);")
|
||||
lines.append(_encode_call("reserve_byte"))
|
||||
|
||||
# Generate inline field encoding for each sub-message field
|
||||
for field in sub_desc.field:
|
||||
@@ -1774,18 +1803,11 @@ class FixedArrayRepeatedType(TypeInfo):
|
||||
|
||||
def _encode_element(self, element: str) -> str:
|
||||
"""Helper to generate encode statement for a single element."""
|
||||
if isinstance(self._ti, EnumType):
|
||||
return f"ProtoEncode::{self._ti.encode_func}(pos, {self.number}, static_cast<uint32_t>({element}), true);"
|
||||
# Repeated message elements use encode_sub_message (force=true is default)
|
||||
if isinstance(self._ti, MessageType):
|
||||
if _is_inline_encode(self._ti.cpp_type):
|
||||
return _generate_inline_encode_block(
|
||||
self.number, self._ti.cpp_type, element
|
||||
)
|
||||
return f"ProtoEncode::encode_sub_message(pos, buffer, {self.number}, {element});"
|
||||
return (
|
||||
f"ProtoEncode::{self._ti.encode_func}(pos, {self.number}, {element}, true);"
|
||||
)
|
||||
if isinstance(self._ti, MessageType) and _is_inline_encode(self._ti.cpp_type):
|
||||
return _generate_inline_encode_block(
|
||||
self.number, self._ti.cpp_type, element
|
||||
)
|
||||
return self._ti.encode_element(self.number, element)
|
||||
|
||||
@property
|
||||
def cpp_type(self) -> str:
|
||||
@@ -2079,55 +2101,23 @@ class RepeatedTypeInfo(TypeInfo):
|
||||
return self._ti.wire_type
|
||||
|
||||
@property
|
||||
def decode_varint_content(self) -> str:
|
||||
# Pointer fields don't support decoding
|
||||
if self._use_pointer:
|
||||
return None
|
||||
content = self._ti.decode_varint
|
||||
if content is None:
|
||||
return None
|
||||
return (
|
||||
f"case {self.number}: this->{self.field_name}.push_back({content}); break;"
|
||||
)
|
||||
def decode_expr(self) -> str | None:
|
||||
return self._ti.decode_expr
|
||||
|
||||
def _decode_store(self, expr: str) -> str:
|
||||
return f"this->{self.field_name}.push_back({expr});"
|
||||
|
||||
@property
|
||||
def decode_length_content(self) -> str:
|
||||
def decode_content(self) -> str | None:
|
||||
# Pointer fields don't support decoding
|
||||
if self._use_pointer:
|
||||
return None
|
||||
content = self._ti.decode_length
|
||||
if content is None and isinstance(self._ti, MessageType):
|
||||
# Special handling for non-template message decoding
|
||||
return f"case {self.number}: this->{self.field_name}.emplace_back(); value.decode_to_message(this->{self.field_name}.back()); break;"
|
||||
if content is None:
|
||||
return None
|
||||
return (
|
||||
f"case {self.number}: this->{self.field_name}.push_back({content}); break;"
|
||||
)
|
||||
|
||||
@property
|
||||
def decode_32bit_content(self) -> str:
|
||||
# Pointer fields don't support decoding
|
||||
if self._use_pointer:
|
||||
return None
|
||||
content = self._ti.decode_32bit
|
||||
if content is None:
|
||||
return None
|
||||
return (
|
||||
f"case {self.number}: this->{self.field_name}.push_back({content}); break;"
|
||||
)
|
||||
|
||||
@property
|
||||
def decode_64bit_content(self) -> str:
|
||||
# Pointer fields don't support decoding
|
||||
if self._use_pointer:
|
||||
return None
|
||||
content = self._ti.decode_64bit
|
||||
if content is None:
|
||||
return None
|
||||
return (
|
||||
f"case {self.number}: this->{self.field_name}.push_back({content}); break;"
|
||||
)
|
||||
if isinstance(self._ti, MessageType):
|
||||
return self.decode_case(
|
||||
f"this->{self.field_name}.emplace_back();\n"
|
||||
f"value.decode_to_message(this->{self.field_name}.back());"
|
||||
)
|
||||
return super().decode_content
|
||||
|
||||
@property
|
||||
def _ti_is_bool(self) -> bool:
|
||||
@@ -2135,15 +2125,7 @@ class RepeatedTypeInfo(TypeInfo):
|
||||
return isinstance(self._ti, BoolType)
|
||||
|
||||
def _encode_element_call(self, element: str) -> str:
|
||||
"""Helper to generate encode call for a single element."""
|
||||
if isinstance(self._ti, EnumType):
|
||||
return f"ProtoEncode::{self._ti.encode_func}(pos, {self.number}, static_cast<uint32_t>({element}), true);"
|
||||
# Repeated message elements use encode_sub_message (force=true is default)
|
||||
if isinstance(self._ti, MessageType):
|
||||
return f"ProtoEncode::encode_sub_message(pos, buffer, {self.number}, {element});"
|
||||
return (
|
||||
f"ProtoEncode::{self._ti.encode_func}(pos, {self.number}, {element}, true);"
|
||||
)
|
||||
return self._ti.encode_element(self.number, element)
|
||||
|
||||
@property
|
||||
def encode_content(self) -> str:
|
||||
@@ -2152,7 +2134,7 @@ class RepeatedTypeInfo(TypeInfo):
|
||||
# Special handling for const char* elements (when container_no_template contains "const char")
|
||||
if "const char" in self._container_no_template:
|
||||
o = f"for (const char *it : *this->{self.field_name}) {{\n"
|
||||
o += f" ProtoEncode::{self._ti.encode_func}(pos, {self.number}, it, strlen(it), true);\n"
|
||||
o += f" {_encode_call(self._ti.encode_func, str(self.number), 'it', 'strlen(it)', force=True)}\n"
|
||||
else:
|
||||
o = f"for (const auto &it : *this->{self.field_name}) {{\n"
|
||||
o += f" {self._encode_element_call('it')}\n"
|
||||
@@ -2538,10 +2520,7 @@ def build_message_type(
|
||||
) -> tuple[str, str, str]:
|
||||
public_content: list[str] = []
|
||||
protected_content: list[str] = []
|
||||
decode_varint: list[str] = []
|
||||
decode_length: list[str] = []
|
||||
decode_32bit: list[str] = []
|
||||
decode_64bit: list[str] = []
|
||||
decode: list[str] = []
|
||||
encode: list[str] = []
|
||||
dump: list[str] = []
|
||||
size_calc: list[str] = []
|
||||
@@ -2557,8 +2536,8 @@ def build_message_type(
|
||||
|
||||
# Get source direction to determine if we need decode/encode methods
|
||||
source = message_source_map[desc.name]
|
||||
needs_decode = source in (SOURCE_BOTH, SOURCE_CLIENT)
|
||||
needs_encode = source in (SOURCE_BOTH, SOURCE_SERVER)
|
||||
needs_decode = message_needs_decode(source)
|
||||
needs_encode = message_needs_encode(source)
|
||||
|
||||
# Add MESSAGE_TYPE method if this is a service message
|
||||
if message_id is not None:
|
||||
@@ -2670,22 +2649,8 @@ def build_message_type(
|
||||
if field.options.HasExtension(pb.field_ifdef):
|
||||
field_ifdef = field.options.Extensions[pb.field_ifdef]
|
||||
|
||||
if ti.decode_varint_content:
|
||||
decode_varint.extend(
|
||||
wrap_with_ifdef(ti.decode_varint_content, field_ifdef)
|
||||
)
|
||||
if ti.decode_length_content:
|
||||
decode_length.extend(
|
||||
wrap_with_ifdef(ti.decode_length_content, field_ifdef)
|
||||
)
|
||||
if ti.decode_32bit_content:
|
||||
decode_32bit.extend(
|
||||
wrap_with_ifdef(ti.decode_32bit_content, field_ifdef)
|
||||
)
|
||||
if ti.decode_64bit_content:
|
||||
decode_64bit.extend(
|
||||
wrap_with_ifdef(ti.decode_64bit_content, field_ifdef)
|
||||
)
|
||||
if case := ti.decode_content:
|
||||
decode.extend(wrap_with_ifdef(case, field_ifdef))
|
||||
if ti.dump_content:
|
||||
# Check for field_ifdef option for dump as well
|
||||
field_ifdef = None
|
||||
@@ -2695,50 +2660,23 @@ def build_message_type(
|
||||
dump.extend(wrap_with_ifdef(ti.dump_content, field_ifdef))
|
||||
|
||||
cpp = ""
|
||||
if decode_varint:
|
||||
o = f"bool {desc.name}::decode_varint(uint32_t field_id, proto_varint_value_t value) {{\n"
|
||||
o += " switch (field_id) {\n"
|
||||
o += indent("\n".join(decode_varint), " ") + "\n"
|
||||
o += " default: return false;\n"
|
||||
if decode:
|
||||
o = f"void {desc.name}::decode_field(void *self, uint32_t tag, const uint8_t *data, proto_varint_value_t scalar) {{\n"
|
||||
o += f" auto &msg = *static_cast<{desc.name} *>(self);\n"
|
||||
o += " const ProtoFieldValue value(data, scalar);\n"
|
||||
o += " switch (tag) {\n"
|
||||
o += indent("\n".join(decode), " ").replace("this->", "msg.") + "\n"
|
||||
o += " }\n"
|
||||
o += " return true;\n"
|
||||
o += "}\n"
|
||||
cpp += o
|
||||
prot = "bool decode_varint(uint32_t field_id, proto_varint_value_t value) override;"
|
||||
protected_content.insert(0, prot)
|
||||
if decode_length:
|
||||
o = f"bool {desc.name}::decode_length(uint32_t field_id, ProtoLengthDelimited value) {{\n"
|
||||
o += " switch (field_id) {\n"
|
||||
o += indent("\n".join(decode_length), " ") + "\n"
|
||||
o += " default: return false;\n"
|
||||
o += " }\n"
|
||||
o += " return true;\n"
|
||||
o += "}\n"
|
||||
cpp += o
|
||||
prot = "bool decode_length(uint32_t field_id, ProtoLengthDelimited value) override;"
|
||||
protected_content.insert(0, prot)
|
||||
if decode_32bit:
|
||||
o = f"bool {desc.name}::decode_32bit(uint32_t field_id, Proto32Bit value) {{\n"
|
||||
o += " switch (field_id) {\n"
|
||||
o += indent("\n".join(decode_32bit), " ") + "\n"
|
||||
o += " default: return false;\n"
|
||||
o += " }\n"
|
||||
o += " return true;\n"
|
||||
o += "}\n"
|
||||
cpp += o
|
||||
prot = "bool decode_32bit(uint32_t field_id, Proto32Bit value) override;"
|
||||
protected_content.insert(0, prot)
|
||||
if decode_64bit:
|
||||
o = f"bool {desc.name}::decode_64bit(uint32_t field_id, Proto64Bit value) {{\n"
|
||||
o += " switch (field_id) {\n"
|
||||
o += indent("\n".join(decode_64bit), " ") + "\n"
|
||||
o += " default: return false;\n"
|
||||
o += " }\n"
|
||||
o += " return true;\n"
|
||||
o += "}\n"
|
||||
cpp += o
|
||||
prot = "bool decode_64bit(uint32_t field_id, Proto64Bit value) override;"
|
||||
prot = "static void decode_field(void *self, uint32_t tag, const uint8_t *data, proto_varint_value_t scalar);"
|
||||
protected_content.insert(0, prot)
|
||||
if not fixed_vector_fields:
|
||||
public_content.append(
|
||||
"void decode(const uint8_t *buffer, size_t length) {\n"
|
||||
" ProtoDecodableMessage::decode_fields(this, buffer, length, &decode_field);\n"
|
||||
"}"
|
||||
)
|
||||
|
||||
# Generate custom decode() override for messages with FixedVector fields
|
||||
if fixed_vector_fields:
|
||||
@@ -2748,8 +2686,8 @@ def build_message_type(
|
||||
for field_name, field_number in fixed_vector_fields:
|
||||
o += f" uint32_t count_{field_name} = ProtoDecodableMessage::count_repeated_field(buffer, length, {field_number});\n"
|
||||
o += f" this->{field_name}.init(count_{field_name});\n"
|
||||
# Call parent decode to populate the fields
|
||||
o += " ProtoDecodableMessage::decode(buffer, length);\n"
|
||||
# Then the shared loop fills them
|
||||
o += " ProtoDecodableMessage::decode_fields(this, buffer, length, &decode_field);\n"
|
||||
o += "}\n"
|
||||
cpp += o
|
||||
# Generate the decode() declaration in header (public method)
|
||||
@@ -2784,28 +2722,36 @@ def build_message_type(
|
||||
)
|
||||
for line in encode
|
||||
]
|
||||
o = f"{speed_attr}uint8_t *{desc.name}::encode(ProtoWriteBuffer &buffer PROTO_ENCODE_DEBUG_PARAM) const {{\n"
|
||||
o = f"{speed_attr}uint8_t *{desc.name}::encode_msg(const void *self, ProtoWriteBuffer &buffer PROTO_ENCODE_DEBUG_PARAM) {{\n"
|
||||
o += f" const auto &msg = *static_cast<const {desc.name} *>(self);\n"
|
||||
o += " uint8_t *__restrict__ pos = buffer.get_pos();\n"
|
||||
o += indent("\n".join(encode_debug)) + "\n"
|
||||
o += indent("\n".join(encode_debug)).replace("this->", "msg.") + "\n"
|
||||
o += " return pos;\n"
|
||||
o += "}\n"
|
||||
cpp += o
|
||||
prot = (
|
||||
"uint8_t *encode(ProtoWriteBuffer &buffer PROTO_ENCODE_DEBUG_PARAM) const;"
|
||||
public_content.append(
|
||||
"static uint8_t *encode_msg(const void *self, ProtoWriteBuffer &buffer PROTO_ENCODE_DEBUG_PARAM);"
|
||||
)
|
||||
public_content.append(
|
||||
"uint8_t *encode(ProtoWriteBuffer &buffer PROTO_ENCODE_DEBUG_PARAM) const {\n"
|
||||
" return encode_msg(this, buffer PROTO_ENCODE_DEBUG_ARG);\n"
|
||||
"}"
|
||||
)
|
||||
public_content.append(prot)
|
||||
# If no fields to encode or message doesn't need encoding, the default implementation in ProtoMessage will be used
|
||||
|
||||
# Add calculate_size method only if this message needs encoding and has fields
|
||||
if needs_encode and size_calc and not is_inline_only:
|
||||
o = f"{speed_attr}uint32_t {desc.name}::calculate_size() const {{\n"
|
||||
o = f"{speed_attr}uint32_t {desc.name}::calc_size_msg(const void *self) {{\n"
|
||||
o += f" const auto &msg = *static_cast<const {desc.name} *>(self);\n"
|
||||
o += " uint32_t size = 0;\n"
|
||||
o += indent("\n".join(size_calc)) + "\n"
|
||||
o += indent("\n".join(size_calc)).replace("this->", "msg.") + "\n"
|
||||
o += " return size;\n"
|
||||
o += "}\n"
|
||||
cpp += o
|
||||
prot = "uint32_t calculate_size() const;"
|
||||
public_content.append(prot)
|
||||
public_content.append("static uint32_t calc_size_msg(const void *self);")
|
||||
public_content.append(
|
||||
"uint32_t calculate_size() const { return calc_size_msg(this); }"
|
||||
)
|
||||
# If no fields to calculate size for or message doesn't need encoding, the default implementation in ProtoMessage will be used
|
||||
|
||||
# dump_to method declaration in header
|
||||
@@ -2900,6 +2846,23 @@ def get_field_opt(
|
||||
return field.options.Extensions[opt]
|
||||
|
||||
|
||||
def message_needs_decode(source: int) -> bool:
|
||||
return source in (SOURCE_BOTH, SOURCE_CLIENT)
|
||||
|
||||
|
||||
def message_needs_encode(source: int) -> bool:
|
||||
return source in (SOURCE_BOTH, SOURCE_SERVER)
|
||||
|
||||
|
||||
def is_decodable_class(desc: descriptor.DescriptorProto, source: int) -> bool:
|
||||
"""Whether the generated class derives from ProtoDecodableMessage: decoded, and either on a
|
||||
decodable base class or with at least one live field."""
|
||||
return message_needs_decode(source) and (
|
||||
get_base_class(desc) is not None
|
||||
or any(not field.options.deprecated for field in desc.field)
|
||||
)
|
||||
|
||||
|
||||
def get_base_class(desc: descriptor.DescriptorProto) -> str | None:
|
||||
"""Get the base_class option from a message descriptor."""
|
||||
if not desc.options.HasExtension(pb.base_class):
|
||||
@@ -3001,11 +2964,11 @@ def build_base_class(
|
||||
|
||||
# Determine if any message using this base class needs decoding/encoding
|
||||
needs_decode = any(
|
||||
message_source_map.get(msg.name, SOURCE_BOTH) in (SOURCE_BOTH, SOURCE_CLIENT)
|
||||
message_needs_decode(message_source_map.get(msg.name, SOURCE_BOTH))
|
||||
for msg in messages
|
||||
)
|
||||
needs_encode = any(
|
||||
message_source_map.get(msg.name, SOURCE_BOTH) in (SOURCE_BOTH, SOURCE_SERVER)
|
||||
message_needs_encode(message_source_map.get(msg.name, SOURCE_BOTH))
|
||||
for msg in messages
|
||||
)
|
||||
|
||||
@@ -3439,6 +3402,7 @@ static void dump_bytes_field(DumpBuffer &out, const char *field_name, const uint
|
||||
|
||||
# Generate message types with base class information
|
||||
# Simple grouping by ifdef
|
||||
decodable_messages: list[tuple[str, str | None]] = []
|
||||
current_ifdef = None
|
||||
|
||||
for m in mt:
|
||||
@@ -3455,6 +3419,8 @@ static void dump_bytes_field(DumpBuffer &out, const char *field_name, const uint
|
||||
continue
|
||||
|
||||
s, c, dc = build_message_type(m, base_class_fields, message_source_map)
|
||||
if is_decodable_class(m, message_source_map[m.name]):
|
||||
decodable_messages.append((m.name, message_ifdef_map.get(m.name)))
|
||||
msg_ifdef = message_ifdef_map.get(m.name)
|
||||
|
||||
# Handle ifdef changes
|
||||
@@ -3481,6 +3447,22 @@ static void dump_bytes_field(DumpBuffer &out, const char *field_name, const uint
|
||||
cpp += "#endif\n"
|
||||
dump_cpp += "#endif\n"
|
||||
|
||||
# decode() passes decode_field explicitly, so without the dump virtuals no decodable message
|
||||
# may carry a vtable; a build at any level below VERY_VERBOSE proves it
|
||||
cpp += "#ifndef HAS_PROTO_MESSAGE_DUMP\n"
|
||||
assert_ifdef = None
|
||||
for name, msg_ifdef in decodable_messages:
|
||||
if msg_ifdef != assert_ifdef:
|
||||
if assert_ifdef is not None:
|
||||
cpp += "#endif\n"
|
||||
if msg_ifdef is not None:
|
||||
cpp += _make_ifdef_line(msg_ifdef) + "\n"
|
||||
assert_ifdef = msg_ifdef
|
||||
cpp += f'static_assert(!std::is_polymorphic_v<{name}>, "decodable messages carry no vtable");\n'
|
||||
if assert_ifdef is not None:
|
||||
cpp += "#endif\n"
|
||||
cpp += "#endif\n"
|
||||
|
||||
content += """\
|
||||
|
||||
} // namespace esphome::api
|
||||
|
||||
+67
-7
@@ -529,21 +529,67 @@ def lint_conf_matches(fname, match):
|
||||
CONF_RE = r'^(CONF_[a-zA-Z0-9_]+)\s*=\s*[\'"].*?[\'"]\s*?$'
|
||||
with codecs.open("esphome/const.py", "r", encoding="utf-8") as const_f_handle:
|
||||
constants_content = const_f_handle.read()
|
||||
with codecs.open(
|
||||
"esphome/components/const/__init__.py", "r", encoding="utf-8"
|
||||
) as component_const_f_handle:
|
||||
component_constants_content = component_const_f_handle.read()
|
||||
|
||||
# The two canonical homes for shared constants: esphome/const.py (core, frozen) and
|
||||
# esphome/components/const/__init__.py (shared by components). A constant defined in
|
||||
# either must be imported from there rather than redefined in a component.
|
||||
CONST_HOMES = ["esphome/const.py", "esphome/components/const/__init__.py"]
|
||||
|
||||
CONSTANTS = [m.group(1) for m in re.finditer(CONF_RE, constants_content, re.MULTILINE)]
|
||||
COMPONENT_CONSTANTS = [
|
||||
m.group(1) for m in re.finditer(CONF_RE, component_constants_content, re.MULTILINE)
|
||||
]
|
||||
|
||||
CONSTANTS_USES = collections.defaultdict(list)
|
||||
|
||||
|
||||
@lint_re_check(CONF_RE, include=["*.py"], exclude=["esphome/const.py"])
|
||||
def _const_home_error(name, core_constants, component_constants):
|
||||
"""Return an error if the constant already lives in one of the canonical homes."""
|
||||
if name in core_constants:
|
||||
return (
|
||||
f"Constant {highlight(name)} has already been defined in const.py - "
|
||||
"please import the constant from const.py directly."
|
||||
)
|
||||
if name in component_constants:
|
||||
return (
|
||||
f"Constant {highlight(name)} has already been defined in "
|
||||
"esphome/components/const/__init__.py - please import the constant from "
|
||||
"esphome.components.const directly."
|
||||
)
|
||||
return None
|
||||
|
||||
|
||||
@lint_re_check(CONF_RE, include=["*.py"], exclude=CONST_HOMES)
|
||||
def lint_conf_from_const_py(fname, match):
|
||||
name = match.group(1)
|
||||
if name not in CONSTANTS:
|
||||
err = _const_home_error(name, CONSTANTS, COMPONENT_CONSTANTS)
|
||||
if err is None:
|
||||
CONSTANTS_USES[name].append(fname)
|
||||
return None
|
||||
return (
|
||||
f"Constant {highlight(name)} has already been defined in const.py - "
|
||||
"please import the constant from const.py directly."
|
||||
)
|
||||
return err
|
||||
|
||||
|
||||
UNIT_RE = r'^(UNIT_[a-zA-Z0-9_]+)\s*=\s*[\'"].*?[\'"]\s*?$'
|
||||
UNIT_CONSTANTS = [
|
||||
m.group(1) for m in re.finditer(UNIT_RE, constants_content, re.MULTILINE)
|
||||
]
|
||||
COMPONENT_UNIT_CONSTANTS = [
|
||||
m.group(1) for m in re.finditer(UNIT_RE, component_constants_content, re.MULTILINE)
|
||||
]
|
||||
|
||||
UNIT_CONSTANTS_USES = collections.defaultdict(list)
|
||||
|
||||
|
||||
@lint_re_check(UNIT_RE, include=["*.py"], exclude=CONST_HOMES)
|
||||
def lint_unit_from_const_py(fname, match):
|
||||
name = match.group(1)
|
||||
err = _const_home_error(name, UNIT_CONSTANTS, COMPONENT_UNIT_CONSTANTS)
|
||||
if err is None:
|
||||
UNIT_CONSTANTS_USES[name].append(fname)
|
||||
return err
|
||||
|
||||
|
||||
RAW_PIN_ACCESS_RE = (
|
||||
@@ -711,6 +757,20 @@ def lint_constants_usage():
|
||||
return errs
|
||||
|
||||
|
||||
@lint_post_check
|
||||
def lint_unit_constants_usage():
|
||||
errs = []
|
||||
for constant, uses in UNIT_CONSTANTS_USES.items():
|
||||
if len(uses) < 3:
|
||||
continue
|
||||
errs.append(
|
||||
f"Constant {highlight(constant)} is defined in {len(uses)} files. Please move all definitions of the "
|
||||
f"constant to esphome/components/const/__init__.py (Uses: {', '.join(str(u) for u in uses)}) in a separate PR. "
|
||||
"See https://developers.esphome.io/contributing/code/#python"
|
||||
)
|
||||
return errs
|
||||
|
||||
|
||||
# Maximum allowed CONF_ constants in esphome/const.py.
|
||||
# This file is frozen — new constants go in esphome/components/const/__init__.py.
|
||||
# Decrease this number when constants are moved out of const.py.
|
||||
|
||||
@@ -249,7 +249,7 @@ static APIBuffer build_infrared_rf_transmit_wire() {
|
||||
std::memcpy(bytes + len, packed, packed_len);
|
||||
len += packed_len;
|
||||
// field 6: modulation = 1 (non-zero so it's actually emitted and exercises
|
||||
// decode_varint for this field, matching the documented layout above).
|
||||
// decode_field for this field, matching the documented layout above).
|
||||
put_byte(0x30);
|
||||
put_varint(1);
|
||||
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
"""Tests for UART clock source validation and code generation."""
|
||||
|
||||
from collections.abc import Callable
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from esphome.components.esp32 import KEY_VARIANT, VARIANTS
|
||||
from esphome.components.uart import (
|
||||
CONF_CLOCK_SOURCE,
|
||||
CONFIG_SCHEMA,
|
||||
UART_CLOCK_SOURCES,
|
||||
UART_CLOCK_SOURCES_BY_VARIANT,
|
||||
)
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import PlatformFramework
|
||||
from tests.component_tests.types import SetCoreConfigCallable
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def clock_source_validator() -> cv.All:
|
||||
"""Find the clock validator without depending on pre-validator ordering."""
|
||||
schema = next(
|
||||
validator
|
||||
for validator in CONFIG_SCHEMA.validators
|
||||
if isinstance(validator, cv.Schema)
|
||||
)
|
||||
return schema.schema[cv.Optional(CONF_CLOCK_SOURCE)]
|
||||
|
||||
|
||||
def test_clock_source_table_covers_all_variants() -> None:
|
||||
"""New ESP32 variants must declare their supported UART clocks."""
|
||||
assert set(UART_CLOCK_SOURCES_BY_VARIANT) == set(VARIANTS)
|
||||
for sources in UART_CLOCK_SOURCES_BY_VARIANT.values():
|
||||
assert "DEFAULT" in sources
|
||||
assert set(sources) <= UART_CLOCK_SOURCES.keys()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("variant", "source", "supported"),
|
||||
[
|
||||
("ESP32", "REF_TICK", True),
|
||||
("ESP32", "XTAL", False),
|
||||
("ESP32S2", "REF_TICK", True),
|
||||
("ESP32S2", "RTC", False),
|
||||
("ESP32C3", "APB", True),
|
||||
("ESP32C3", "xtal", True),
|
||||
("ESP32C3", "REF_TICK", False),
|
||||
("ESP32S3", "RTC", True),
|
||||
("ESP32S3", "REF_TICK", False),
|
||||
("ESP32C2", "APB", False),
|
||||
("ESP32C6", "APB", False),
|
||||
("ESP32C6", "DEFAULT", True),
|
||||
("ESP32C5", "RTC", True),
|
||||
("ESP32C61", "XTAL", True),
|
||||
("ESP32H4", "RTC", True),
|
||||
("ESP32H21", "XTAL", True),
|
||||
("ESP32S31", "RTC", True),
|
||||
("ESP32H2", "XTAL", True),
|
||||
("ESP32P4", "RTC", True),
|
||||
("ESP32C3", "INVALID", False),
|
||||
],
|
||||
)
|
||||
def test_clock_source_validation(
|
||||
variant: str,
|
||||
source: str,
|
||||
supported: bool,
|
||||
set_core_config: SetCoreConfigCallable,
|
||||
clock_source_validator: cv.All,
|
||||
) -> None:
|
||||
"""Reject unsupported clocks and normalize accepted names."""
|
||||
set_core_config(
|
||||
PlatformFramework.ESP32_IDF,
|
||||
platform_data={KEY_VARIANT: variant},
|
||||
)
|
||||
if supported:
|
||||
assert clock_source_validator(source) == source.upper()
|
||||
else:
|
||||
with pytest.raises(cv.Invalid):
|
||||
clock_source_validator(source)
|
||||
|
||||
|
||||
def test_clock_source_requires_esp32(
|
||||
set_core_config: SetCoreConfigCallable,
|
||||
clock_source_validator: cv.All,
|
||||
) -> None:
|
||||
"""Reject the ESP32-only option before accessing the chip variant."""
|
||||
set_core_config(PlatformFramework.ESP8266_ARDUINO)
|
||||
with pytest.raises(cv.Invalid, match="ESP32"):
|
||||
clock_source_validator("DEFAULT")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("source", [None, "default", "xtal"])
|
||||
def test_clock_source_codegen(
|
||||
source: str | None,
|
||||
tmp_path: Path,
|
||||
generate_main: Callable[[str | Path], str],
|
||||
) -> None:
|
||||
"""Emit IDF constants directly and skip the setter when nothing changes."""
|
||||
config = tmp_path / "uart.yaml"
|
||||
config.write_text(
|
||||
"esphome:\n name: uart-clock-test\n"
|
||||
"esp32:\n variant: esp32c3\n framework:\n type: esp-idf\n"
|
||||
"uart:\n id: test_uart\n tx_pin: GPIO4\n baud_rate: 9600\n"
|
||||
+ (f" clock_source: {source}\n" if source else ""),
|
||||
encoding="utf-8",
|
||||
)
|
||||
main_cpp = generate_main(config)
|
||||
if source in (None, "default"):
|
||||
assert "set_clock_source(" not in main_cpp
|
||||
else:
|
||||
assert f"test_uart->set_clock_source(::UART_SCLK_{source.upper()});" in main_cpp
|
||||
@@ -59,7 +59,7 @@ static void verify_mac(uint64_t mac, size_t expected_bytes) {
|
||||
#ifdef ESPHOME_DEBUG_API
|
||||
uint8_t *proto_debug_end_ = api_buf.data() + api_buf.size();
|
||||
#endif
|
||||
ProtoEncode::encode_varint_raw_48bit(pos PROTO_ENCODE_DEBUG_ARG, mac);
|
||||
pos = ProtoEncode::encode_varint_raw_48bit(pos PROTO_ENCODE_DEBUG_ARG, mac);
|
||||
size_t new_len = pos - api_buf.data();
|
||||
|
||||
EXPECT_EQ(new_len, expected_bytes) << "mac=0x" << std::hex << mac << std::dec;
|
||||
|
||||
@@ -45,13 +45,13 @@ binary_sensor:
|
||||
state: !lambda "return x.value_or(false);"
|
||||
- if:
|
||||
condition:
|
||||
binary_sensor.is_on: publish_target
|
||||
binary_sensor.is_on: binary_sensor_publish_target
|
||||
then:
|
||||
- logger.log: "publish_target is on"
|
||||
- if:
|
||||
condition:
|
||||
binary_sensor.is_off:
|
||||
id: publish_target
|
||||
id: binary_sensor_publish_target
|
||||
then:
|
||||
- logger.log: "publish_target is off"
|
||||
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
|
||||
#include "esphome/core/string_ref.h"
|
||||
|
||||
#include <iterator>
|
||||
#include <string>
|
||||
|
||||
namespace esphome::core::testing {
|
||||
|
||||
TEST(StringRefStartsWith, ProperPrefixMatches) {
|
||||
@@ -59,4 +62,103 @@ TEST(StringRefStartsWith, RefOverloadComparesOnlyTheViewedLength) {
|
||||
EXPECT_TRUE(ref.starts_with(prefix));
|
||||
}
|
||||
|
||||
// The generated api messages start their encode only string fields as a null pointer with zero
|
||||
// length; every member must treat that exactly like the default constructed empty string.
|
||||
TEST(StringRefNullEmpty, BehavesAsEmptyString) {
|
||||
const StringRef null_empty{nullptr, 0};
|
||||
const StringRef empty;
|
||||
EXPECT_TRUE(null_empty.empty());
|
||||
EXPECT_EQ(null_empty.size(), 0u);
|
||||
EXPECT_EQ(null_empty.c_str(), nullptr);
|
||||
EXPECT_EQ(null_empty.byte(), nullptr);
|
||||
EXPECT_TRUE(null_empty == empty);
|
||||
EXPECT_FALSE(null_empty < empty);
|
||||
EXPECT_FALSE(empty < null_empty);
|
||||
EXPECT_TRUE(null_empty == ""); // NOLINT(readability-container-size-empty) - operator under test
|
||||
EXPECT_TRUE(null_empty == std::string()); // NOLINT(readability-container-size-empty) - operator under test
|
||||
EXPECT_EQ(null_empty.compare(empty), 0);
|
||||
EXPECT_EQ(null_empty.compare(""), 0);
|
||||
EXPECT_LT(null_empty.compare("a"), 0);
|
||||
EXPECT_TRUE(null_empty.starts_with(""));
|
||||
EXPECT_FALSE(null_empty.starts_with("a"));
|
||||
EXPECT_EQ(null_empty.str(), std::string());
|
||||
EXPECT_EQ(null_empty.substr(0), std::string());
|
||||
EXPECT_EQ(null_empty.find('a'), std::string::npos);
|
||||
EXPECT_EQ(null_empty.find("a"), std::string::npos);
|
||||
char buf[4] = "xyz";
|
||||
EXPECT_EQ(null_empty.copy(buf, sizeof(buf)), 0u);
|
||||
EXPECT_EQ(null_empty.begin(), null_empty.end());
|
||||
}
|
||||
|
||||
TEST(StringRefNullEmpty, ComparesAgainstText) {
|
||||
const StringRef null_empty{nullptr, 0};
|
||||
const StringRef text("abc", 3);
|
||||
EXPECT_FALSE(null_empty == text);
|
||||
EXPECT_FALSE(text == null_empty);
|
||||
EXPECT_LT(null_empty.compare(text), 0);
|
||||
EXPECT_GT(text.compare(null_empty), 0);
|
||||
EXPECT_TRUE(null_empty < text);
|
||||
EXPECT_FALSE(text < null_empty);
|
||||
EXPECT_TRUE(text.starts_with(null_empty));
|
||||
}
|
||||
|
||||
TEST(StringRefNullEmpty, TwoNullViewsAreEqual) {
|
||||
const StringRef a{nullptr, 0};
|
||||
const StringRef b{nullptr, 0};
|
||||
EXPECT_TRUE(a == b);
|
||||
EXPECT_FALSE(a < b);
|
||||
EXPECT_EQ(a.compare(b), 0);
|
||||
EXPECT_TRUE(a.starts_with(b));
|
||||
}
|
||||
|
||||
// Every iterator endpoint of a null view is the same null position: nothing is dereferenced and
|
||||
// no offset is applied to the null pointer, so the range is simply empty.
|
||||
TEST(StringRefNullEmpty, IteratorEndpointsFormAnEmptyRange) {
|
||||
const StringRef null_empty{nullptr, 0};
|
||||
EXPECT_EQ(null_empty.cbegin(), null_empty.cend());
|
||||
EXPECT_EQ(null_empty.rbegin(), null_empty.rend());
|
||||
EXPECT_EQ(null_empty.crbegin(), null_empty.crend());
|
||||
EXPECT_EQ(std::distance(null_empty.begin(), null_empty.end()), 0);
|
||||
size_t visited = 0;
|
||||
for (char c : null_empty) {
|
||||
(void) c;
|
||||
visited++;
|
||||
}
|
||||
EXPECT_EQ(visited, 0u);
|
||||
// NOLINTNEXTLINE(bugprone-string-constructor) - empty range under test
|
||||
EXPECT_EQ(std::string(null_empty.begin(), null_empty.end()), std::string());
|
||||
}
|
||||
|
||||
// The pointer and length constructor accepts an empty range at a null pointer; the copy into a
|
||||
// std::string reads nothing.
|
||||
TEST(StringRefNullEmpty, ConvertsToEmptyStdString) {
|
||||
const StringRef null_empty{nullptr, 0};
|
||||
const std::string copy = null_empty.str();
|
||||
EXPECT_TRUE(copy.empty());
|
||||
EXPECT_EQ(static_cast<std::string>(null_empty), std::string());
|
||||
EXPECT_EQ(null_empty.substr(0, 5), std::string());
|
||||
std::string target("keep");
|
||||
target += null_empty;
|
||||
EXPECT_EQ(target, "keep");
|
||||
}
|
||||
|
||||
// The number conversions hand the pointer to the C library; a null view must stop before that.
|
||||
TEST(StringRefNullEmpty, NumericConversionsReturnZero) {
|
||||
const StringRef null_empty{nullptr, 0};
|
||||
size_t pos = 99;
|
||||
EXPECT_EQ(stoi(null_empty, &pos), 0);
|
||||
EXPECT_EQ(pos, 0u);
|
||||
pos = 99;
|
||||
EXPECT_EQ(stol(null_empty, &pos, 16), 0L);
|
||||
EXPECT_EQ(pos, 0u);
|
||||
pos = 99;
|
||||
EXPECT_EQ(stof(null_empty, &pos), 0.0f);
|
||||
EXPECT_EQ(pos, 0u);
|
||||
pos = 99;
|
||||
EXPECT_EQ(stod(null_empty, &pos), 0.0);
|
||||
EXPECT_EQ(pos, 0u);
|
||||
EXPECT_EQ(stoi(null_empty), 0);
|
||||
EXPECT_EQ(stof(null_empty), 0.0f);
|
||||
}
|
||||
|
||||
} // namespace esphome::core::testing
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
import esphome.codegen as cg
|
||||
from tests.testing_helpers import ComponentManifestOverride
|
||||
|
||||
|
||||
def override_manifest(manifest: ComponentManifestOverride) -> None:
|
||||
# Enables light_json_schema.cpp without USE_MQTT, which pulls mqtt code into core/util.cpp
|
||||
async def to_code_testing(config):
|
||||
cg.add_define("USE_WEBSERVER")
|
||||
# api_connection.cpp reports the port whenever USE_WEBSERVER is set
|
||||
cg.add_define("USE_WEBSERVER_PORT", 80)
|
||||
|
||||
manifest.to_code = to_code_testing
|
||||
manifest.dependencies = manifest.dependencies + ["json"]
|
||||
@@ -187,6 +187,7 @@ light:
|
||||
name: Monochromatic Light
|
||||
output: test_ledc_1
|
||||
gamma_correct: 2.8
|
||||
transition_state_publish_interval: 200ms
|
||||
default_transition_length: 2s
|
||||
effects:
|
||||
- strobe:
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "esphome/components/json/json_util.h"
|
||||
#include "esphome/components/light/light_call.h"
|
||||
#include "esphome/components/light/light_json_schema.h"
|
||||
#include "esphome/components/light/light_output.h"
|
||||
#include "esphome/components/light/light_state.h"
|
||||
|
||||
namespace esphome::light::testing {
|
||||
|
||||
namespace {
|
||||
|
||||
// An `rgbw` light with `color_interlock: true`
|
||||
class InterlockedRgbwOutput : public LightOutput {
|
||||
public:
|
||||
LightTraits get_traits() override {
|
||||
LightTraits traits;
|
||||
traits.set_supported_color_modes({ColorMode::RGB, ColorMode::WHITE});
|
||||
return traits;
|
||||
}
|
||||
void write_state(LightState *state) override {}
|
||||
};
|
||||
|
||||
class TestableLightCall : public LightCall {
|
||||
public:
|
||||
using LightCall::LightCall;
|
||||
using LightCall::validate_;
|
||||
};
|
||||
|
||||
LightColorValues parse(LightState &state, const char *payload) {
|
||||
TestableLightCall call(&state);
|
||||
json::parse_json(payload, [&](JsonObject root) {
|
||||
LightJSONSchema::parse_json(state, call, root);
|
||||
return true;
|
||||
});
|
||||
return call.validate_();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
// HA's MQTT JSON schema selects the white color mode with a top-level `white` key
|
||||
TEST(LightJSONSchema, TopLevelWhiteSelectsWhiteMode) {
|
||||
InterlockedRgbwOutput output;
|
||||
LightState state(&output);
|
||||
state.remote_values.set_color_mode(ColorMode::RGB);
|
||||
|
||||
auto values = parse(state, R"({"state":"ON","white":128})");
|
||||
|
||||
EXPECT_EQ(values.get_color_mode(), ColorMode::WHITE);
|
||||
EXPECT_FLOAT_EQ(values.get_brightness(), 128.0f / 255.0f);
|
||||
EXPECT_FLOAT_EQ(values.get_white(), 1.0f);
|
||||
}
|
||||
|
||||
TEST(LightJSONSchema, TopLevelWhiteOverridesBrightness) {
|
||||
InterlockedRgbwOutput output;
|
||||
LightState state(&output);
|
||||
|
||||
auto values = parse(state, R"({"state":"ON","brightness":255,"white":128})");
|
||||
|
||||
EXPECT_EQ(values.get_color_mode(), ColorMode::WHITE);
|
||||
EXPECT_FLOAT_EQ(values.get_brightness(), 128.0f / 255.0f);
|
||||
}
|
||||
|
||||
TEST(LightJSONSchema, ColorWStillSetsWhite) {
|
||||
InterlockedRgbwOutput output;
|
||||
LightState state(&output);
|
||||
|
||||
auto values = parse(state, R"({"state":"ON","color":{"w":255}})");
|
||||
|
||||
EXPECT_EQ(values.get_color_mode(), ColorMode::WHITE);
|
||||
EXPECT_FLOAT_EQ(values.get_white(), 1.0f);
|
||||
}
|
||||
|
||||
} // namespace esphome::light::testing
|
||||
@@ -0,0 +1,8 @@
|
||||
# Waveform commands from a lambda with no waveform sensor configured, so
|
||||
# USE_NEXTION_WAVEFORM is not defined.
|
||||
display:
|
||||
- platform: nextion
|
||||
id: main_lcd
|
||||
lambda: |-
|
||||
id(main_lcd).open_waveform_channel(37, 0, 10);
|
||||
id(main_lcd).add_waveform_data(37, 0, 42);
|
||||
@@ -0,0 +1,3 @@
|
||||
packages:
|
||||
uart: !include ../../test_build_components/common/uart/esp32-idf.yaml
|
||||
waveform_lambda: !include common_waveform_lambda.yaml
|
||||
@@ -275,3 +275,6 @@ button:
|
||||
- remote_transmitter.digital_write: true
|
||||
- remote_transmitter.digital_write:
|
||||
value: false
|
||||
- remote_transmitter.digital_write:
|
||||
transmitter_id: xmitr
|
||||
value: !lambda return id(remote_transmitter_test_number).state > 127;
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
audio_dac:
|
||||
- platform: tas2780
|
||||
id: tas2780_amp
|
||||
address: 0x38
|
||||
i2c_id: i2c_bus
|
||||
amp_level: 8
|
||||
power_mode: 2
|
||||
vol_range_min: 0.3
|
||||
vol_range_max: 1.0
|
||||
channel: mono
|
||||
|
||||
esphome:
|
||||
on_boot:
|
||||
- tas2780.activate:
|
||||
id: tas2780_amp
|
||||
- tas2780.activate:
|
||||
id: tas2780_amp
|
||||
power_mode: 1
|
||||
- tas2780.activate:
|
||||
id: tas2780_amp
|
||||
power_mode: !lambda "return id(tas2780_amp).volume() > 0.5f ? 3 : 1;"
|
||||
- tas2780.update_config:
|
||||
id: tas2780_amp
|
||||
amp_level: 10
|
||||
vol_range_min: 0.2
|
||||
vol_range_max: 0.9
|
||||
channel: left
|
||||
- tas2780.deactivate:
|
||||
id: tas2780_amp
|
||||
- tas2780.reset:
|
||||
id: tas2780_amp
|
||||
@@ -0,0 +1,3 @@
|
||||
packages:
|
||||
i2c: !include ../../test_build_components/common/i2c/esp32-idf.yaml
|
||||
tas2780: !include common.yaml
|
||||
@@ -0,0 +1,3 @@
|
||||
packages:
|
||||
i2c: !include ../../test_build_components/common/i2c/esp8266-ard.yaml
|
||||
tas2780: !include common.yaml
|
||||
@@ -0,0 +1,3 @@
|
||||
packages:
|
||||
i2c: !include ../../test_build_components/common/i2c/rp2040-ard.yaml
|
||||
tas2780: !include common.yaml
|
||||
@@ -24,5 +24,6 @@ uart:
|
||||
tx_pin: 18
|
||||
rx_pin: 19
|
||||
baud_rate: 115200
|
||||
clock_source: XTAL
|
||||
debug:
|
||||
debug_prefix: "[UART1] "
|
||||
|
||||
@@ -28,6 +28,7 @@ uart:
|
||||
tx_pin: 21
|
||||
rx_pin: 22
|
||||
baud_rate: 115200
|
||||
clock_source: REF_TICK
|
||||
debug:
|
||||
debug_prefix: "[UART1] "
|
||||
- id: uart_debug_custom
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
esphome:
|
||||
name: api-decode-wire-types-test
|
||||
host:
|
||||
api:
|
||||
logger:
|
||||
level: DEBUG
|
||||
|
||||
switch:
|
||||
- platform: template
|
||||
name: "Wire Switch"
|
||||
optimistic: true
|
||||
|
||||
output:
|
||||
- platform: template
|
||||
id: wire_dim
|
||||
type: float
|
||||
write_action:
|
||||
- lambda: ""
|
||||
|
||||
light:
|
||||
- platform: monochromatic
|
||||
name: "Wire Light"
|
||||
output: wire_dim
|
||||
default_transition_length: 0s
|
||||
effects:
|
||||
- pulse:
|
||||
name: Pulse
|
||||
|
||||
text:
|
||||
- platform: template
|
||||
name: "Wire Text"
|
||||
optimistic: true
|
||||
mode: text
|
||||
min_length: 0
|
||||
max_length: 255
|
||||
|
||||
number:
|
||||
- platform: template
|
||||
name: "Wire Number"
|
||||
optimistic: true
|
||||
min_value: -1000
|
||||
max_value: 1000
|
||||
step: 0.5
|
||||
@@ -0,0 +1,11 @@
|
||||
esphome:
|
||||
name: api-empty-message-test
|
||||
host:
|
||||
api:
|
||||
logger:
|
||||
level: DEBUG
|
||||
|
||||
switch:
|
||||
- platform: template
|
||||
name: "Empty Message Switch"
|
||||
optimistic: true
|
||||
@@ -0,0 +1,58 @@
|
||||
esphome:
|
||||
name: api-encode-boundaries-test
|
||||
# Top-level area fills DeviceInfoResponse.suggested_area (field 16, a two-byte tag)
|
||||
area:
|
||||
id: kitchen_area
|
||||
name: Kitchen
|
||||
on_boot:
|
||||
- sensor.template.publish:
|
||||
id: zero_then_value
|
||||
state: 0.0
|
||||
|
||||
host:
|
||||
api:
|
||||
logger:
|
||||
level: DEBUG
|
||||
|
||||
sensor:
|
||||
- platform: template
|
||||
name: "Zero Then Value"
|
||||
id: zero_then_value
|
||||
# Negative int32 takes the ten byte varint path
|
||||
accuracy_decimals: -2
|
||||
update_interval: never
|
||||
|
||||
text_sensor:
|
||||
- platform: template
|
||||
name: "Long Text"
|
||||
id: long_text
|
||||
update_interval: never
|
||||
|
||||
number:
|
||||
- platform: template
|
||||
name: "Negative Number"
|
||||
optimistic: true
|
||||
min_value: -1000
|
||||
max_value: 1000
|
||||
step: 0.5
|
||||
initial_value: -123.5
|
||||
|
||||
select:
|
||||
- platform: template
|
||||
name: "Long Option Select"
|
||||
optimistic: true
|
||||
options:
|
||||
- short
|
||||
- "option-with-a-name-long-enough-that-its-length-prefix-needs-two-varint-bytes-when-the-list-entities-response-is-encoded-xxxxxxxxxx"
|
||||
initial_option: short
|
||||
|
||||
button:
|
||||
- platform: template
|
||||
name: "Publish Values"
|
||||
on_press:
|
||||
- sensor.template.publish:
|
||||
id: zero_then_value
|
||||
state: 12.5
|
||||
- text_sensor.template.publish:
|
||||
id: long_text
|
||||
state: !lambda return std::string(200, 'y');
|
||||
@@ -0,0 +1,38 @@
|
||||
esphome:
|
||||
name: ltspi-save
|
||||
host:
|
||||
api:
|
||||
logger:
|
||||
|
||||
preferences:
|
||||
flash_write_interval: 0s # Flush to disk on every loop
|
||||
|
||||
output:
|
||||
- platform: template
|
||||
id: mono_output
|
||||
type: float
|
||||
write_action:
|
||||
- logger.log:
|
||||
format: "mono_output: %.2f"
|
||||
args: [state]
|
||||
|
||||
light:
|
||||
- platform: monochromatic
|
||||
id: test_mono_light
|
||||
name: "Test Mono Light"
|
||||
output: mono_output
|
||||
restore_mode: RESTORE_DEFAULT_OFF
|
||||
default_transition_length: 1s
|
||||
transition_state_publish_interval: 0.2s
|
||||
|
||||
button:
|
||||
- platform: template
|
||||
id: run_persistence_transition
|
||||
name: "Run Persistence Transition"
|
||||
on_press:
|
||||
- lambda: |-
|
||||
auto call = id(test_mono_light).turn_on();
|
||||
call.set_brightness(1.0f);
|
||||
call.set_transition_length(1000);
|
||||
call.set_save(true);
|
||||
call.perform();
|
||||
@@ -0,0 +1,121 @@
|
||||
esphome:
|
||||
name: ltspi
|
||||
host:
|
||||
api:
|
||||
logger:
|
||||
|
||||
output:
|
||||
# Monochromatic output
|
||||
- platform: template
|
||||
id: mono_output
|
||||
type: float
|
||||
write_action:
|
||||
- logger.log:
|
||||
format: "mono_output: %.2f"
|
||||
args: [state]
|
||||
# RGB outputs
|
||||
- platform: template
|
||||
id: rgb_red
|
||||
type: float
|
||||
write_action:
|
||||
- logger.log:
|
||||
format: "rgb_red: %.2f"
|
||||
args: [state]
|
||||
- platform: template
|
||||
id: rgb_green
|
||||
type: float
|
||||
write_action:
|
||||
- logger.log:
|
||||
format: "rgb_green: %.2f"
|
||||
args: [state]
|
||||
- platform: template
|
||||
id: rgb_blue
|
||||
type: float
|
||||
write_action:
|
||||
- logger.log:
|
||||
format: "rgb_blue: %.2f"
|
||||
args: [state]
|
||||
# CWWW outputs (using rgbww light for CT + cold/warm white coverage)
|
||||
- platform: template
|
||||
id: cwww_red
|
||||
type: float
|
||||
write_action:
|
||||
- logger.log:
|
||||
format: "cwww_red: %.2f"
|
||||
args: [state]
|
||||
- platform: template
|
||||
id: cwww_green
|
||||
type: float
|
||||
write_action:
|
||||
- logger.log:
|
||||
format: "cwww_green: %.2f"
|
||||
args: [state]
|
||||
- platform: template
|
||||
id: cwww_blue
|
||||
type: float
|
||||
write_action:
|
||||
- logger.log:
|
||||
format: "cwww_blue: %.2f"
|
||||
args: [state]
|
||||
- platform: template
|
||||
id: cwww_cold_white
|
||||
type: float
|
||||
write_action:
|
||||
- logger.log:
|
||||
format: "cwww_cold_white: %.2f"
|
||||
args: [state]
|
||||
- platform: template
|
||||
id: cwww_warm_white
|
||||
type: float
|
||||
write_action:
|
||||
- logger.log:
|
||||
format: "cwww_warm_white: %.2f"
|
||||
args: [state]
|
||||
- platform: template
|
||||
id: legacy_output
|
||||
type: float
|
||||
write_action:
|
||||
- logger.log:
|
||||
format: "legacy_output: %.2f"
|
||||
args: [state]
|
||||
|
||||
light:
|
||||
# Monochromatic light (brightness only)
|
||||
- platform: monochromatic
|
||||
id: test_mono_light
|
||||
name: "Test Mono Light"
|
||||
output: mono_output
|
||||
default_transition_length: 1s
|
||||
transition_state_publish_interval: 0.2s
|
||||
|
||||
# RGB light
|
||||
- platform: rgb
|
||||
id: test_rgb_light
|
||||
name: "Test RGB Light"
|
||||
red: rgb_red
|
||||
green: rgb_green
|
||||
blue: rgb_blue
|
||||
default_transition_length: 1s
|
||||
transition_state_publish_interval: 0.2s
|
||||
|
||||
# CWWW/CT-capable light
|
||||
- platform: rgbww
|
||||
id: test_cwww_light
|
||||
name: "Test CWWW Light"
|
||||
red: cwww_red
|
||||
green: cwww_green
|
||||
blue: cwww_blue
|
||||
cold_white: cwww_cold_white
|
||||
warm_white: cwww_warm_white
|
||||
cold_white_color_temperature: 6500 K
|
||||
warm_white_color_temperature: 2000 K
|
||||
constant_brightness: true
|
||||
default_transition_length: 1s
|
||||
transition_state_publish_interval: 0.2s
|
||||
|
||||
# No interval: publishes only when a call starts
|
||||
- platform: monochromatic
|
||||
id: test_legacy_light
|
||||
name: "Test Legacy Light"
|
||||
output: legacy_output
|
||||
default_transition_length: 1s
|
||||
@@ -125,11 +125,12 @@ class RawApiClient:
|
||||
await self.read_until_frame(MESSAGE_TYPE_OF[api_pb2.HelloResponse])
|
||||
|
||||
async def send_message(self, msg: message.Message) -> None:
|
||||
await self.send_raw(MESSAGE_TYPE_OF[type(msg)], msg.SerializeToString())
|
||||
|
||||
async def send_raw(self, msg_type: int, payload: bytes) -> None:
|
||||
"""Send a frame with a hand built payload, for shapes protobuf will not serialize."""
|
||||
loop = asyncio.get_running_loop()
|
||||
await loop.sock_sendall(
|
||||
self._sock,
|
||||
encode_frame(MESSAGE_TYPE_OF[type(msg)], msg.SerializeToString()),
|
||||
)
|
||||
await loop.sock_sendall(self._sock, encode_frame(msg_type, payload))
|
||||
|
||||
async def read_until_frame(self, msg_type: int, timeout: float = 10.0) -> None:
|
||||
"""Read until at least one frame of msg_type has been received."""
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from collections.abc import Callable
|
||||
from collections.abc import Awaitable, Callable
|
||||
import logging
|
||||
from typing import TypeVar
|
||||
|
||||
@@ -57,6 +57,58 @@ async def wait_for_state(
|
||||
return await asyncio.wait_for(future, timeout=timeout)
|
||||
|
||||
|
||||
class StateWaiter:
|
||||
"""Route one state subscription to any number of predicate waits."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self._waiters: list[
|
||||
tuple[Callable[[EntityState], bool], asyncio.Future[EntityState]]
|
||||
] = []
|
||||
|
||||
def on_state(self, state: EntityState) -> None:
|
||||
for predicate, future in self._waiters:
|
||||
if future.done():
|
||||
continue
|
||||
try:
|
||||
matched = predicate(state)
|
||||
except Exception as exc: # noqa: BLE001 the wait re-raises it, the callback must not die
|
||||
future.set_exception(exc)
|
||||
continue
|
||||
if matched:
|
||||
future.set_result(state)
|
||||
|
||||
def expect(
|
||||
self,
|
||||
predicate: Callable[[EntityState], bool],
|
||||
timeout: float = 5.0,
|
||||
label: str | None = None,
|
||||
) -> Awaitable[EntityState]:
|
||||
"""Arm a wait for the next state matching ``predicate`` and return the awaitable for it.
|
||||
|
||||
The wait is armed here, at call time, so it can be created before the action that produces
|
||||
the state and awaited afterwards; states seen before this call never match.
|
||||
"""
|
||||
entry = (predicate, asyncio.get_running_loop().create_future())
|
||||
self._waiters.append(entry)
|
||||
return self._wait(entry, timeout, label)
|
||||
|
||||
async def _wait(
|
||||
self,
|
||||
entry: tuple[Callable[[EntityState], bool], asyncio.Future[EntityState]],
|
||||
timeout: float,
|
||||
label: str | None,
|
||||
) -> EntityState:
|
||||
try:
|
||||
async with asyncio.timeout(timeout):
|
||||
return await entry[1]
|
||||
except TimeoutError:
|
||||
raise TimeoutError(
|
||||
f"no state matched {label or entry[0]} within {timeout}s"
|
||||
) from None
|
||||
finally:
|
||||
self._waiters.remove(entry)
|
||||
|
||||
|
||||
def find_entity[T: EntityInfo](
|
||||
entities: list[EntityInfo],
|
||||
object_id_substring: str,
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
"""decode_field() must take fields that match their declared wire type, drop the ones that do
|
||||
not, skip unknown fields, and handle two byte tags, varints and length prefixes."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable
|
||||
import struct
|
||||
|
||||
from aioesphomeapi import (
|
||||
EntityState,
|
||||
LightState,
|
||||
NumberState,
|
||||
SwitchState,
|
||||
TextState,
|
||||
api_pb2,
|
||||
)
|
||||
import pytest
|
||||
|
||||
from .raw_api_client import MESSAGE_TYPE_OF, RawApiClient, encode_varint
|
||||
from .state_utils import InitialStateHelper, StateWaiter, require_entity
|
||||
from .types import APIClientConnectedFactory, RunCompiledFunction
|
||||
|
||||
SWITCH_COMMAND = MESSAGE_TYPE_OF[api_pb2.SwitchCommandRequest]
|
||||
WIRE_VARINT, WIRE_LENGTH, WIRE_FIXED32 = 0, 2, 5
|
||||
|
||||
|
||||
def tag(field: int, wire_type: int) -> bytes:
|
||||
return encode_varint((field << 3) | wire_type)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_api_decode_wire_types(
|
||||
yaml_config: str,
|
||||
run_compiled: RunCompiledFunction,
|
||||
api_client_connected: APIClientConnectedFactory,
|
||||
unused_tcp_port: int,
|
||||
) -> None:
|
||||
async with (
|
||||
run_compiled(yaml_config),
|
||||
api_client_connected() as client,
|
||||
RawApiClient(unused_tcp_port) as raw,
|
||||
):
|
||||
entities, _ = await client.list_entities_services()
|
||||
switch = require_entity(entities, "wire_switch")
|
||||
light = require_entity(entities, "wire_light")
|
||||
text = require_entity(entities, "wire_text")
|
||||
number = require_entity(entities, "wire_number")
|
||||
key = tag(1, WIRE_FIXED32) + struct.pack("<I", switch.key)
|
||||
on, off = tag(2, WIRE_VARINT) + b"\x01", tag(2, WIRE_VARINT) + b"\x00"
|
||||
|
||||
switch_states: list[bool] = []
|
||||
waiter = StateWaiter()
|
||||
|
||||
def on_state(state: EntityState) -> None:
|
||||
if isinstance(state, SwitchState) and state.key == switch.key:
|
||||
switch_states.append(state.state)
|
||||
waiter.on_state(state)
|
||||
|
||||
def switch_is(value: bool) -> Callable[[EntityState], bool]:
|
||||
return lambda s: (
|
||||
isinstance(s, SwitchState) and s.key == switch.key and s.state is value
|
||||
)
|
||||
|
||||
def number_is(value: float) -> Callable[[EntityState], bool]:
|
||||
return lambda s: (
|
||||
isinstance(s, NumberState) and s.key == number.key and s.state == value
|
||||
)
|
||||
|
||||
initial = InitialStateHelper(entities)
|
||||
client.subscribe_states(initial.on_state_wrapper(on_state))
|
||||
await initial.wait_for_initial_states()
|
||||
await raw.connect()
|
||||
|
||||
# A well formed command: fixed32 key, varint state
|
||||
await raw.send_raw(SWITCH_COMMAND, key + on)
|
||||
await waiter.expect(switch_is(True))
|
||||
await raw.send_raw(SWITCH_COMMAND, key + off)
|
||||
await waiter.expect(switch_is(False))
|
||||
|
||||
# The same field with the wrong wire type is dropped, and a varint key never matches an
|
||||
# entity; each of these would turn the switch on if the payload were read as a varint
|
||||
seen = len(switch_states)
|
||||
await raw.send_raw(SWITCH_COMMAND, key + tag(2, WIRE_LENGTH) + b"\x01\x01")
|
||||
await raw.send_raw(
|
||||
SWITCH_COMMAND, key + tag(2, WIRE_FIXED32) + b"\x01\x00\x00\x00"
|
||||
)
|
||||
await raw.send_raw(
|
||||
SWITCH_COMMAND, tag(1, WIRE_VARINT) + encode_varint(switch.key) + on
|
||||
)
|
||||
# Ordered on the raw socket itself: this frame cannot be parsed before the bad ones, so
|
||||
# the only switch state since the marker must be the one it produces
|
||||
await raw.send_raw(SWITCH_COMMAND, key + on)
|
||||
await waiter.expect(switch_is(True), label="switch on after wrong wire types")
|
||||
assert switch_states[seen:] == [True]
|
||||
await raw.send_raw(SWITCH_COMMAND, key + off)
|
||||
await waiter.expect(switch_is(False))
|
||||
|
||||
# Truncated bodies stop the decode loop without taking the connection down: a tag with its
|
||||
# continuation bit set and nothing after it, a length prefix past the end of the payload,
|
||||
# and a fixed32 with two of its four bytes
|
||||
seen = len(switch_states)
|
||||
await raw.send_raw(SWITCH_COMMAND, key + b"\x80")
|
||||
await raw.send_raw(SWITCH_COMMAND, key + tag(2, WIRE_LENGTH) + b"\x7f" + b"ab")
|
||||
await raw.send_raw(SWITCH_COMMAND, tag(1, WIRE_FIXED32) + b"\x01\x02")
|
||||
await raw.send_raw(SWITCH_COMMAND, key + on)
|
||||
await waiter.expect(switch_is(True), label="switch on after truncated frames")
|
||||
assert switch_states[seen:] == [True]
|
||||
await raw.send_raw(SWITCH_COMMAND, key + off)
|
||||
await waiter.expect(switch_is(False))
|
||||
|
||||
# A negative number goes through the fixed32 float path of a normal client
|
||||
client.number_command(number.key, -77.5)
|
||||
await waiter.expect(number_is(-77.5))
|
||||
|
||||
# An unknown field ahead of the known ones is skipped; field 200 needs a two byte tag
|
||||
await raw.send_raw(
|
||||
SWITCH_COMMAND, tag(200, WIRE_VARINT) + encode_varint(300) + key + on
|
||||
)
|
||||
await waiter.expect(switch_is(True))
|
||||
|
||||
# Two byte tags (effect fields 18 and 19) and a two byte varint (300 ms transition)
|
||||
client.light_command(
|
||||
light.key, state=True, brightness=0.5, transition_length=0.3, effect="Pulse"
|
||||
)
|
||||
await waiter.expect(
|
||||
lambda s: (
|
||||
isinstance(s, LightState) and s.key == light.key and s.effect == "Pulse"
|
||||
)
|
||||
)
|
||||
client.light_command(light.key, effect="None", state=False)
|
||||
await waiter.expect(
|
||||
lambda s: isinstance(s, LightState) and s.key == light.key and not s.state
|
||||
)
|
||||
|
||||
# A string whose length prefix needs two varint bytes
|
||||
long_text = "w" * 200
|
||||
client.text_command(text.key, long_text)
|
||||
await waiter.expect(
|
||||
lambda s: (
|
||||
isinstance(s, TextState) and s.key == text.key and s.state == long_text
|
||||
)
|
||||
)
|
||||
@@ -0,0 +1,37 @@
|
||||
"""Messages without fields go through the shared ProtoMessage entry points on both directions."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from aioesphomeapi import api_pb2
|
||||
import pytest
|
||||
|
||||
from .raw_api_client import MESSAGE_TYPE_OF, RawApiClient
|
||||
from .types import RunCompiledFunction
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_api_empty_message_roundtrip(
|
||||
yaml_config: str,
|
||||
run_compiled: RunCompiledFunction,
|
||||
unused_tcp_port: int,
|
||||
) -> None:
|
||||
async with run_compiled(yaml_config), RawApiClient(unused_tcp_port) as client:
|
||||
await client.connect()
|
||||
|
||||
# Field free request and reply on the plain send path
|
||||
await client.send_message(api_pb2.PingRequest())
|
||||
await client.read_until_frame(MESSAGE_TYPE_OF[api_pb2.PingResponse])
|
||||
|
||||
# Field free request answered by a message with fields, and a list that ends with
|
||||
# the field free ListEntitiesDoneResponse through the batching path
|
||||
await client.send_message(api_pb2.DeviceInfoRequest())
|
||||
await client.read_until_frame(MESSAGE_TYPE_OF[api_pb2.DeviceInfoResponse])
|
||||
await client.send_message(api_pb2.ListEntitiesRequest())
|
||||
await client.read_until_frame(MESSAGE_TYPE_OF[api_pb2.ListEntitiesDoneResponse])
|
||||
assert (
|
||||
client.frame_counts[MESSAGE_TYPE_OF[api_pb2.ListEntitiesSwitchResponse]]
|
||||
== 1
|
||||
)
|
||||
|
||||
await client.send_message(api_pb2.DisconnectRequest())
|
||||
await client.read_until_frame(MESSAGE_TYPE_OF[api_pb2.DisconnectResponse])
|
||||
@@ -0,0 +1,76 @@
|
||||
"""Encode paths at their branch boundaries: zero skipped float, fixed32 state, negative int32,
|
||||
length prefixes of two varint bytes and two byte field tags."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
|
||||
from aioesphomeapi import (
|
||||
NumberState,
|
||||
SelectInfo,
|
||||
SensorInfo,
|
||||
SensorState,
|
||||
TextSensorState,
|
||||
)
|
||||
import pytest
|
||||
|
||||
from .state_utils import InitialStateHelper, StateWaiter, require_entity
|
||||
from .types import APIClientConnectedFactory, RunCompiledFunction
|
||||
|
||||
LONG_OPTION = (
|
||||
"option-with-a-name-long-enough-that-its-length-prefix-needs-two-varint-bytes-"
|
||||
"when-the-list-entities-response-is-encoded-xxxxxxxxxx"
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_api_encode_boundaries(
|
||||
yaml_config: str,
|
||||
run_compiled: RunCompiledFunction,
|
||||
api_client_connected: APIClientConnectedFactory,
|
||||
) -> None:
|
||||
async with run_compiled(yaml_config), api_client_connected() as client:
|
||||
device_info, (entities, _) = await asyncio.gather(
|
||||
client.device_info(), client.list_entities_services()
|
||||
)
|
||||
assert device_info.suggested_area == "Kitchen"
|
||||
|
||||
sensor = require_entity(entities, "zero_then_value", SensorInfo)
|
||||
assert sensor.accuracy_decimals == -2
|
||||
select = require_entity(entities, "long_option_select", SelectInfo)
|
||||
assert len(LONG_OPTION) >= 128
|
||||
assert select.options == ["short", LONG_OPTION]
|
||||
text = require_entity(entities, "long_text")
|
||||
number = require_entity(entities, "negative_number")
|
||||
button = require_entity(entities, "publish_values")
|
||||
|
||||
initial = InitialStateHelper(entities)
|
||||
waiter = StateWaiter()
|
||||
client.subscribe_states(initial.on_state_wrapper(waiter.on_state))
|
||||
await initial.wait_for_initial_states()
|
||||
|
||||
# A float of exactly zero is skipped on the wire and must still read as 0.0, not missing
|
||||
first = initial.initial_states[sensor.key]
|
||||
assert isinstance(first, SensorState)
|
||||
assert first.state == 0.0 and not first.missing_state
|
||||
first_number = initial.initial_states[number.key]
|
||||
assert isinstance(first_number, NumberState)
|
||||
assert first_number.state == -123.5
|
||||
|
||||
# Arm both waits before the press so no ordering of the replies can slip past them
|
||||
sensor_seen = waiter.expect(
|
||||
lambda s: (
|
||||
isinstance(s, SensorState) and s.key == sensor.key and s.state == 12.5
|
||||
),
|
||||
label="sensor 12.5",
|
||||
)
|
||||
text_seen = waiter.expect(
|
||||
lambda s: (
|
||||
isinstance(s, TextSensorState)
|
||||
and s.key == text.key
|
||||
and s.state == "y" * 200
|
||||
),
|
||||
label="text 200 x y",
|
||||
)
|
||||
client.button_command(button.key)
|
||||
await asyncio.gather(sensor_seen, text_seen)
|
||||
@@ -0,0 +1,328 @@
|
||||
"""Integration tests for the light transition_state_publish_interval option."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from collections.abc import Callable
|
||||
from itertools import pairwise
|
||||
|
||||
from aioesphomeapi import (
|
||||
APIClient,
|
||||
ButtonInfo,
|
||||
EntityInfo,
|
||||
EntityState,
|
||||
LightInfo,
|
||||
LightState,
|
||||
)
|
||||
import pytest
|
||||
|
||||
from .state_utils import InitialStateHelper, require_entity, wait_for_state
|
||||
from .types import APIClientConnectedFactory, RunCompiledFunction
|
||||
|
||||
Timeline = list[tuple[float, LightState]]
|
||||
DonePredicate = Callable[[float, LightState], bool]
|
||||
|
||||
|
||||
class _Recorder:
|
||||
"""Records the states one light publishes while an action runs.
|
||||
|
||||
``run`` fires ``action`` and returns the (elapsed, state) timeline once a
|
||||
published state satisfies ``done``, after ``settle`` more seconds so late
|
||||
publishes still land in the timeline.
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self._loop = asyncio.get_running_loop()
|
||||
self._event = asyncio.Event()
|
||||
self._key = 0
|
||||
self._start = 0.0
|
||||
self._done: DonePredicate | None = None
|
||||
self.timeline: Timeline = []
|
||||
|
||||
def on_state(self, state: EntityState) -> None:
|
||||
if (
|
||||
self._done is None
|
||||
or not isinstance(state, LightState)
|
||||
or state.key != self._key
|
||||
):
|
||||
return
|
||||
elapsed = self._loop.time() - self._start
|
||||
self.timeline.append((elapsed, state))
|
||||
if self._done(elapsed, state):
|
||||
self._event.set()
|
||||
|
||||
async def run(
|
||||
self,
|
||||
key: int,
|
||||
action: Callable[[], None],
|
||||
done: DonePredicate,
|
||||
settle: float = 0.0,
|
||||
) -> Timeline:
|
||||
self.timeline = []
|
||||
self._key = key
|
||||
self._done = done
|
||||
self._event.clear()
|
||||
self._start = self._loop.time()
|
||||
action()
|
||||
async with asyncio.timeout(5):
|
||||
await self._event.wait()
|
||||
if settle:
|
||||
await asyncio.sleep(settle)
|
||||
self._done = None
|
||||
return self.timeline
|
||||
|
||||
|
||||
async def _subscribe(client: APIClient) -> tuple[list[EntityInfo], _Recorder]:
|
||||
"""List entities and attach a recorder once the initial states have arrived."""
|
||||
entities, _ = await client.list_entities_services()
|
||||
helper = InitialStateHelper(entities)
|
||||
recorder = _Recorder()
|
||||
client.subscribe_states(helper.on_state_wrapper(recorder.on_state))
|
||||
await helper.wait_for_initial_states()
|
||||
return entities, recorder
|
||||
|
||||
|
||||
def _visible_brightness(state: LightState) -> float:
|
||||
"""Brightness as a remote sees it: an off light counts as zero."""
|
||||
return state.brightness if state.state else 0.0
|
||||
|
||||
|
||||
def _brightness_is(value: float) -> DonePredicate:
|
||||
return lambda _elapsed, state: (
|
||||
_visible_brightness(state) == pytest.approx(value, abs=0.01)
|
||||
)
|
||||
|
||||
|
||||
def _brightness_values(timeline: Timeline) -> list[float]:
|
||||
return [_visible_brightness(state) for _, state in timeline]
|
||||
|
||||
|
||||
def _assert_ramp(timeline: Timeline, target: float) -> None:
|
||||
"""Several states were published and the last one is ``target`` after ~1 s."""
|
||||
values = _brightness_values(timeline)
|
||||
assert len(values) >= 3, values
|
||||
assert values[-1] == pytest.approx(target, abs=0.05), values
|
||||
assert timeline[-1][0] >= 0.8, timeline[-1][0]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.shared_yaml("light_transition_state_publish_interval")
|
||||
async def test_light_without_interval_publishes_target_once(
|
||||
yaml_config: str,
|
||||
run_compiled: RunCompiledFunction,
|
||||
api_client_connected: APIClientConnectedFactory,
|
||||
) -> None:
|
||||
"""A light without the option publishes the target once, up front."""
|
||||
async with run_compiled(yaml_config), api_client_connected() as client:
|
||||
entities, recorder = await _subscribe(client)
|
||||
legacy = require_entity(entities, "test_legacy_light", LightInfo)
|
||||
|
||||
timeline = await recorder.run(
|
||||
legacy.key,
|
||||
lambda: client.light_command(
|
||||
key=legacy.key, state=True, brightness=0.8, transition_length=1.0
|
||||
),
|
||||
_brightness_is(0.8),
|
||||
settle=1.3,
|
||||
)
|
||||
|
||||
assert _brightness_values(timeline) == [pytest.approx(0.8)]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.shared_yaml("light_transition_state_publish_interval")
|
||||
async def test_transition_interval_nonzero_emits_intermediate_updates(
|
||||
yaml_config: str,
|
||||
run_compiled: RunCompiledFunction,
|
||||
api_client_connected: APIClientConnectedFactory,
|
||||
) -> None:
|
||||
"""Turning on over 1 s with a 200 ms interval publishes a rising ramp."""
|
||||
async with run_compiled(yaml_config), api_client_connected() as client:
|
||||
entities, recorder = await _subscribe(client)
|
||||
mono = require_entity(entities, "test_mono_light", LightInfo)
|
||||
|
||||
timeline = await recorder.run(
|
||||
mono.key,
|
||||
lambda: client.light_command(
|
||||
key=mono.key, state=True, brightness=1.0, transition_length=1.0
|
||||
),
|
||||
_brightness_is(1.0),
|
||||
)
|
||||
|
||||
values = _brightness_values(timeline)
|
||||
assert len(values) >= 5, values
|
||||
assert values[0] == pytest.approx(0.0, abs=0.1), values
|
||||
assert values[-1] == pytest.approx(1.0, abs=0.05), values
|
||||
assert len([v for v in values if 0.1 < v < 0.9]) >= 2, values
|
||||
assert all(b >= a - 0.1 for a, b in pairwise(values)), values
|
||||
assert timeline[-1][0] >= 0.8, timeline[-1][0]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.shared_yaml("light_transition_state_publish_interval")
|
||||
async def test_light_transition_state_publish_interval(
|
||||
yaml_config: str,
|
||||
run_compiled: RunCompiledFunction,
|
||||
api_client_connected: APIClientConnectedFactory,
|
||||
) -> None:
|
||||
"""Default-length, RGB and color temperature transitions publish on the interval."""
|
||||
async with run_compiled(yaml_config), api_client_connected() as client:
|
||||
entities, recorder = await _subscribe(client)
|
||||
mono = require_entity(entities, "test_mono_light", LightInfo)
|
||||
rgb = require_entity(entities, "test_rgb_light", LightInfo)
|
||||
cwww = require_entity(entities, "test_cwww_light", LightInfo)
|
||||
|
||||
# No transition_length: default_transition_length (1 s) applies
|
||||
timeline = await recorder.run(
|
||||
mono.key,
|
||||
lambda: client.light_command(key=mono.key, state=True, brightness=1.0),
|
||||
_brightness_is(1.0),
|
||||
)
|
||||
_assert_ramp(timeline, 1.0)
|
||||
|
||||
timeline = await recorder.run(
|
||||
rgb.key,
|
||||
lambda: client.light_command(
|
||||
key=rgb.key,
|
||||
state=True,
|
||||
brightness=1.0,
|
||||
rgb=(1.0, 0.0, 0.0),
|
||||
transition_length=1.0,
|
||||
),
|
||||
_brightness_is(1.0),
|
||||
)
|
||||
_assert_ramp(timeline, 1.0)
|
||||
|
||||
# Start at the cold end instantly so the fade to 300 mireds has a gradient
|
||||
await recorder.run(
|
||||
cwww.key,
|
||||
lambda: client.light_command(
|
||||
key=cwww.key,
|
||||
state=True,
|
||||
brightness=1.0,
|
||||
color_temperature=153.0,
|
||||
transition_length=0.0,
|
||||
),
|
||||
lambda _t, s: (
|
||||
s.state and s.color_temperature == pytest.approx(153.0, abs=1.0)
|
||||
),
|
||||
)
|
||||
timeline = await recorder.run(
|
||||
cwww.key,
|
||||
lambda: client.light_command(
|
||||
key=cwww.key,
|
||||
state=True,
|
||||
brightness=1.0,
|
||||
color_temperature=300.0,
|
||||
transition_length=1.0,
|
||||
),
|
||||
lambda _t, s: s.color_temperature == pytest.approx(300.0, abs=1.0),
|
||||
)
|
||||
ct_values = [state.color_temperature for _, state in timeline]
|
||||
assert len(ct_values) >= 3, ct_values
|
||||
assert min(ct_values) < max(ct_values), ct_values
|
||||
assert timeline[-1][0] >= 0.8, timeline[-1][0]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.shared_yaml("light_transition_state_publish_interval")
|
||||
async def test_flash_interval_emits_intermediate_updates(
|
||||
yaml_config: str,
|
||||
run_compiled: RunCompiledFunction,
|
||||
api_client_connected: APIClientConnectedFactory,
|
||||
) -> None:
|
||||
"""A flash publishes its value on the interval and ends back where it started."""
|
||||
async with run_compiled(yaml_config), api_client_connected() as client:
|
||||
entities, recorder = await _subscribe(client)
|
||||
mono = require_entity(entities, "test_mono_light", LightInfo)
|
||||
|
||||
await recorder.run(
|
||||
mono.key,
|
||||
lambda: client.light_command(
|
||||
key=mono.key, state=True, brightness=0.4, transition_length=0.0
|
||||
),
|
||||
_brightness_is(0.4),
|
||||
)
|
||||
timeline = await recorder.run(
|
||||
mono.key,
|
||||
lambda: client.light_command(
|
||||
key=mono.key, brightness=1.0, flash_length=1.0
|
||||
),
|
||||
lambda t, s: (
|
||||
t > 0.5 and _visible_brightness(s) == pytest.approx(0.4, abs=0.01)
|
||||
),
|
||||
)
|
||||
|
||||
values = _brightness_values(timeline)
|
||||
assert values.count(pytest.approx(1.0, abs=0.01)) >= 3, values
|
||||
assert values[-1] == pytest.approx(0.4, abs=0.05), values
|
||||
assert timeline[-1][0] >= 0.8, timeline[-1][0]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.shared_yaml("light_transition_interval_save")
|
||||
async def test_transition_interval_persistence_semantics(
|
||||
yaml_config: str,
|
||||
run_compiled: RunCompiledFunction,
|
||||
api_client_connected: APIClientConnectedFactory,
|
||||
) -> None:
|
||||
"""A save=true interval transition saves its target, not a sample, and restores it."""
|
||||
async with run_compiled(yaml_config), api_client_connected() as client:
|
||||
entities, recorder = await _subscribe(client)
|
||||
mono = require_entity(entities, "test_mono_light", LightInfo)
|
||||
button = require_entity(entities, "run_persistence_transition", ButtonInfo)
|
||||
|
||||
timeline = await recorder.run(
|
||||
mono.key,
|
||||
lambda: client.button_command(button.key),
|
||||
_brightness_is(1.0),
|
||||
)
|
||||
_assert_ramp(timeline, 1.0)
|
||||
|
||||
# The restored light fades up from off over its default transition, so wait for the end
|
||||
async with run_compiled(yaml_config), api_client_connected() as client:
|
||||
entities, _ = await client.list_entities_services()
|
||||
mono = require_entity(entities, "test_mono_light", LightInfo)
|
||||
await wait_for_state(
|
||||
client,
|
||||
lambda s: (
|
||||
isinstance(s, LightState)
|
||||
and s.key == mono.key
|
||||
and _visible_brightness(s) == pytest.approx(1.0, abs=0.01)
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.shared_yaml("light_transition_state_publish_interval")
|
||||
async def test_partial_call_during_interval_transition_keeps_target(
|
||||
yaml_config: str,
|
||||
run_compiled: RunCompiledFunction,
|
||||
api_client_connected: APIClientConnectedFactory,
|
||||
) -> None:
|
||||
"""A colour temperature change mid-fade keeps the fade's brightness target."""
|
||||
async with run_compiled(yaml_config), api_client_connected() as client:
|
||||
entities, recorder = await _subscribe(client)
|
||||
cwww = require_entity(entities, "test_cwww_light", LightInfo)
|
||||
|
||||
# Let the fade from off run for a couple of samples before changing only the colour
|
||||
await recorder.run(
|
||||
cwww.key,
|
||||
lambda: client.light_command(
|
||||
key=cwww.key,
|
||||
state=True,
|
||||
brightness=1.0,
|
||||
color_temperature=153.0,
|
||||
transition_length=1.0,
|
||||
),
|
||||
lambda t, _s: t >= 0.3,
|
||||
)
|
||||
timeline = await recorder.run(
|
||||
cwww.key,
|
||||
lambda: client.light_command(
|
||||
key=cwww.key, color_temperature=300.0, transition_length=1.0
|
||||
),
|
||||
lambda _t, s: s.color_temperature == pytest.approx(300.0, abs=1.0),
|
||||
)
|
||||
|
||||
assert _brightness_values(timeline)[-1] == pytest.approx(1.0, abs=0.01)
|
||||
@@ -248,6 +248,41 @@ def test_convert_keys_bare_callable_dotted_qualname() -> None:
|
||||
assert set(converted["schema"]["config_vars"]) == {"string"}
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def language_schema_dir(tmp_path_factory: pytest.TempPathFactory) -> Path:
|
||||
"""Run the full language-schema build once and return the output directory.
|
||||
|
||||
The build must run in a fresh interpreter: ``build_language_schema.py``
|
||||
enables schema extraction *before* importing any esphome component, and the
|
||||
extraction hooks are no-ops if the components were already imported (as they
|
||||
are inside the pytest session). Running it as a subprocess mirrors how CI
|
||||
generates the schema and keeps these tests isolated from import order.
|
||||
"""
|
||||
out_dir = tmp_path_factory.mktemp("language_schema")
|
||||
subprocess.run(
|
||||
[sys.executable, str(SCRIPT_PATH), "--output-path", str(out_dir)],
|
||||
check=True,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
return out_dir
|
||||
|
||||
|
||||
def test_uart_clock_source_preserves_variant_metadata(
|
||||
language_schema_dir: Path,
|
||||
) -> None:
|
||||
"""UART clock choices retain chip restrictions in the editor schema."""
|
||||
uart_schema = json.loads((language_schema_dir / "uart.json").read_text())
|
||||
clock_source = uart_schema["uart"]["schemas"]["CONFIG_SCHEMA"]["schema"][
|
||||
"config_vars"
|
||||
]["clock_source"]
|
||||
|
||||
assert clock_source["type"] == "enum"
|
||||
assert clock_source["values"]["REF_TICK"] == {"variants": ["ESP32", "ESP32S2"]}
|
||||
assert "ESP32C6" in clock_source["values"]["XTAL"]["variants"]
|
||||
assert "ESP32C6" not in clock_source["values"]["APB"]["variants"]
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Regression tests for the lvgl schema dump.
|
||||
#
|
||||
@@ -261,23 +296,8 @@ def test_convert_keys_bare_callable_dotted_qualname() -> None:
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def lvgl_schema(tmp_path_factory: pytest.TempPathFactory) -> dict:
|
||||
"""Run the full language-schema build once and return parsed lvgl.json.
|
||||
|
||||
The build must run in a fresh interpreter: ``build_language_schema.py``
|
||||
enables schema extraction *before* importing any esphome component, and the
|
||||
extraction hooks are no-ops if the components were already imported (as they
|
||||
are inside the pytest session). Running it as a subprocess mirrors how CI
|
||||
generates the schema and keeps this test isolated from import order.
|
||||
"""
|
||||
out_dir = tmp_path_factory.mktemp("language_schema")
|
||||
subprocess.run(
|
||||
[sys.executable, str(SCRIPT_PATH), "--output-path", str(out_dir)],
|
||||
check=True,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
return json.loads((out_dir / "lvgl.json").read_text())
|
||||
def lvgl_schema(language_schema_dir: Path) -> dict:
|
||||
return json.loads((language_schema_dir / "lvgl.json").read_text())
|
||||
|
||||
|
||||
def _lvgl_config_vars(lvgl_schema: dict) -> dict:
|
||||
|
||||
@@ -151,6 +151,82 @@ def test_nolint_on_control_line_suppresses() -> None:
|
||||
assert not _lint("if (x) // NOLINT\n ESP_LOGD(t);\n")
|
||||
|
||||
|
||||
# --- rule: UNIT_ constants must not be redefined (mirror of the CONF_ check) ---
|
||||
|
||||
# Real UNIT_ constants that live in each canonical home.
|
||||
UNIT_IN_CONST_PY = ci_custom.UNIT_CONSTANTS[0]
|
||||
UNIT_IN_COMPONENT_CONST = ci_custom.COMPONENT_UNIT_CONSTANTS[0]
|
||||
|
||||
|
||||
def _unit_def(fname: str, content: str) -> list:
|
||||
return ci_custom.lint_unit_from_const_py(fname, content)
|
||||
|
||||
|
||||
def test_unit_already_in_const_py_is_flagged() -> None:
|
||||
errs = _unit_def("esphome/components/x/sensor.py", f'{UNIT_IN_CONST_PY} = "x"\n')
|
||||
assert errs
|
||||
assert "const.py" in errs[0][2]
|
||||
|
||||
|
||||
def test_unit_already_in_component_const_is_flagged() -> None:
|
||||
errs = _unit_def(
|
||||
"esphome/components/x/sensor.py", f'{UNIT_IN_COMPONENT_CONST} = "x"\n'
|
||||
)
|
||||
assert errs
|
||||
assert "esphome.components.const" in errs[0][2]
|
||||
|
||||
|
||||
def test_unit_not_in_const_py_is_tracked_not_flagged() -> None:
|
||||
ci_custom.UNIT_CONSTANTS_USES.clear()
|
||||
assert _unit_def("a.py", 'UNIT_FOO_BAR = "fb"\n') == []
|
||||
assert ci_custom.UNIT_CONSTANTS_USES["UNIT_FOO_BAR"] == ["a.py"]
|
||||
|
||||
|
||||
def test_unit_defined_in_three_files_is_flagged() -> None:
|
||||
ci_custom.UNIT_CONSTANTS_USES.clear()
|
||||
for fname in ("a.py", "b.py", "c.py"):
|
||||
_unit_def(fname, 'UNIT_FOO_BAR = "fb"\n')
|
||||
errs = ci_custom.lint_unit_constants_usage()
|
||||
assert any("UNIT_FOO_BAR" in e and "3 files" in e for e in errs)
|
||||
|
||||
|
||||
def test_unit_defined_in_two_files_is_not_flagged() -> None:
|
||||
ci_custom.UNIT_CONSTANTS_USES.clear()
|
||||
for fname in ("a.py", "b.py"):
|
||||
_unit_def(fname, 'UNIT_FOO_BAR = "fb"\n')
|
||||
assert ci_custom.lint_unit_constants_usage() == []
|
||||
|
||||
|
||||
# --- same rule for CONF_, now also recognising the components/const home ---
|
||||
|
||||
CONF_IN_CONST_PY = ci_custom.CONSTANTS[0]
|
||||
CONF_IN_COMPONENT_CONST = ci_custom.COMPONENT_CONSTANTS[0]
|
||||
|
||||
|
||||
def _conf_def(fname: str, content: str) -> list:
|
||||
return ci_custom.lint_conf_from_const_py(fname, content)
|
||||
|
||||
|
||||
def test_conf_already_in_const_py_is_flagged() -> None:
|
||||
errs = _conf_def("esphome/components/x/sensor.py", f'{CONF_IN_CONST_PY} = "x"\n')
|
||||
assert errs
|
||||
assert "const.py" in errs[0][2]
|
||||
|
||||
|
||||
def test_conf_already_in_component_const_is_flagged() -> None:
|
||||
errs = _conf_def(
|
||||
"esphome/components/x/sensor.py", f'{CONF_IN_COMPONENT_CONST} = "x"\n'
|
||||
)
|
||||
assert errs
|
||||
assert "esphome.components.const" in errs[0][2]
|
||||
|
||||
|
||||
def test_conf_not_in_a_const_home_is_tracked_not_flagged() -> None:
|
||||
ci_custom.CONSTANTS_USES.pop("CONF_FOO_BAR", None)
|
||||
assert _conf_def("a.py", 'CONF_FOO_BAR = "foo_bar"\n') == []
|
||||
assert ci_custom.CONSTANTS_USES["CONF_FOO_BAR"] == ["a.py"]
|
||||
|
||||
|
||||
# --- ESP_LOG call scanner and bare-literal-ternary lint ---
|
||||
|
||||
|
||||
|
||||
@@ -194,17 +194,17 @@ def test_superseded_device_info_fields_still_declared_in_header() -> None:
|
||||
|
||||
def test_superseded_device_info_fields_still_encoded_and_sized() -> None:
|
||||
"""Each superseded field must still be touched by DeviceInfoResponse's
|
||||
generated encode() and calculate_size(), i.e. it is still put on the wire.
|
||||
generated encode_msg() and calc_size_msg(), i.e. it is still put on the wire.
|
||||
"""
|
||||
encode_body = _extract_function_body(CPP_TEXT, "DeviceInfoResponse::encode")
|
||||
size_body = _extract_function_body(CPP_TEXT, "DeviceInfoResponse::calculate_size")
|
||||
encode_body = _extract_function_body(CPP_TEXT, "DeviceInfoResponse::encode_msg")
|
||||
size_body = _extract_function_body(CPP_TEXT, "DeviceInfoResponse::calc_size_msg")
|
||||
for field_name in SUPERSEDED_FIELDS:
|
||||
assert f"this->{field_name}" in encode_body, (
|
||||
f"DeviceInfoResponse::encode() no longer references {field_name}. "
|
||||
assert f"msg.{field_name}" in encode_body, (
|
||||
f"DeviceInfoResponse::encode_msg() no longer references {field_name}. "
|
||||
f"{DEPRECATED_FIELD_TRAP}"
|
||||
)
|
||||
assert f"this->{field_name}" in size_body, (
|
||||
f"DeviceInfoResponse::calculate_size() no longer references "
|
||||
assert f"msg.{field_name}" in size_body, (
|
||||
f"DeviceInfoResponse::calc_size_msg() no longer references "
|
||||
f"{field_name}. {DEPRECATED_FIELD_TRAP}"
|
||||
)
|
||||
|
||||
@@ -380,3 +380,13 @@ def test_api_version_minor_is_at_least_15() -> None:
|
||||
"clients to see api_version >= 1.15 in HelloResponse before they will "
|
||||
"ever request it."
|
||||
)
|
||||
|
||||
|
||||
def test_generated_encode_calls_keep_the_cursor() -> None:
|
||||
"""No generated ProtoEncode call may drop the returned cursor."""
|
||||
dropped = [
|
||||
line
|
||||
for line in CPP_TEXT.splitlines()
|
||||
if "ProtoEncode::" in line and "pos = ProtoEncode::" not in line
|
||||
]
|
||||
assert not dropped, dropped[:5]
|
||||
|
||||
@@ -15,9 +15,15 @@ import pytest
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).parents[4] / "script" / "api_protobuf"))
|
||||
|
||||
import aioesphomeapi.api_options_pb2 as pb # noqa: E402
|
||||
from api_protobuf import ( # noqa: E402
|
||||
MAX_MESSAGE_ID,
|
||||
SOURCE_BOTH,
|
||||
SOURCE_CLIENT,
|
||||
SOURCE_SERVER,
|
||||
_make_ifdef_line,
|
||||
build_message_type,
|
||||
create_field_type_info,
|
||||
get_varint64_ifdef,
|
||||
validate_message_id,
|
||||
)
|
||||
@@ -34,16 +40,26 @@ def _file_with_messages(
|
||||
file_desc = descriptor_pb2.FileDescriptorProto(name="test.proto")
|
||||
for name, field_type, deprecated in messages:
|
||||
msg = file_desc.message_type.add(name=name)
|
||||
field = msg.field.add(name="value", number=1, type=field_type)
|
||||
field = msg.field.add()
|
||||
field.CopyFrom(_field(field_type))
|
||||
field.options.deprecated = deprecated
|
||||
return file_desc
|
||||
|
||||
|
||||
UINT64 = descriptor_pb2.FieldDescriptorProto.TYPE_UINT64
|
||||
MESSAGE = descriptor_pb2.FieldDescriptorProto.TYPE_MESSAGE
|
||||
DOUBLE = descriptor_pb2.FieldDescriptorProto.TYPE_DOUBLE
|
||||
INT64 = descriptor_pb2.FieldDescriptorProto.TYPE_INT64
|
||||
SINT64 = descriptor_pb2.FieldDescriptorProto.TYPE_SINT64
|
||||
UINT32 = descriptor_pb2.FieldDescriptorProto.TYPE_UINT32
|
||||
INT32 = descriptor_pb2.FieldDescriptorProto.TYPE_INT32
|
||||
SINT32 = descriptor_pb2.FieldDescriptorProto.TYPE_SINT32
|
||||
FIXED64 = descriptor_pb2.FieldDescriptorProto.TYPE_FIXED64
|
||||
FIXED32 = descriptor_pb2.FieldDescriptorProto.TYPE_FIXED32
|
||||
FLOAT = descriptor_pb2.FieldDescriptorProto.TYPE_FLOAT
|
||||
BOOL = descriptor_pb2.FieldDescriptorProto.TYPE_BOOL
|
||||
STRING = descriptor_pb2.FieldDescriptorProto.TYPE_STRING
|
||||
BYTES = descriptor_pb2.FieldDescriptorProto.TYPE_BYTES
|
||||
|
||||
|
||||
def test_no_varint64_fields() -> None:
|
||||
@@ -107,3 +123,240 @@ def test_message_id_at_maximum_is_accepted() -> None:
|
||||
def test_message_id_above_maximum_is_rejected() -> None:
|
||||
with pytest.raises(ValueError, match="exceeds the plaintext"):
|
||||
validate_message_id(MAX_MESSAGE_ID + 1, "TooBigMessage")
|
||||
|
||||
|
||||
def _field(
|
||||
field_type: int, number: int = 1, *, force: bool = False, repeated: bool = False
|
||||
) -> descriptor_pb2.FieldDescriptorProto:
|
||||
field = descriptor_pb2.FieldDescriptorProto(
|
||||
name="value", number=number, type=field_type
|
||||
)
|
||||
if repeated:
|
||||
field.label = descriptor_pb2.FieldDescriptorProto.LABEL_REPEATED
|
||||
if force:
|
||||
field.options.Extensions[pb.force] = True
|
||||
return field
|
||||
|
||||
|
||||
def _encode_field(
|
||||
field_type: int, number: int = 1, force: bool = False, repeated: bool = False
|
||||
) -> str:
|
||||
"""Return the encode statement the generator emits for one encode-only field."""
|
||||
field = _field(field_type, number, force=force, repeated=repeated)
|
||||
return create_field_type_info(
|
||||
field, needs_decode=False, needs_encode=True
|
||||
).encode_content
|
||||
|
||||
|
||||
SCALAR_TYPES = [
|
||||
BOOL,
|
||||
UINT32,
|
||||
INT32,
|
||||
UINT64,
|
||||
INT64,
|
||||
SINT32,
|
||||
FLOAT,
|
||||
FIXED32,
|
||||
STRING,
|
||||
BYTES,
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("field_type", SCALAR_TYPES)
|
||||
def test_forced_fields_use_the_force_overload_or_raw_writes(field_type: int) -> None:
|
||||
content = _encode_field(field_type, force=True)
|
||||
assert (
|
||||
"_force(" in content
|
||||
or "write_raw_byte(" in content
|
||||
or "write_tag_and_fixed32(" in content
|
||||
), content
|
||||
|
||||
|
||||
@pytest.mark.parametrize("field_type", [FLOAT, FIXED32])
|
||||
def test_single_byte_tag_fixed32_shares_the_outlined_writer(field_type: int) -> None:
|
||||
unconditional = _encode_field(field_type, force=True)
|
||||
assert unconditional.count("write_tag_and_fixed32(pos, 13,") == 1, unconditional
|
||||
guarded = _encode_field(field_type, force=False)
|
||||
assert guarded.startswith("if ("), guarded
|
||||
assert "[[likely]]" in guarded
|
||||
assert "write_tag_and_fixed32(pos, 13," in guarded
|
||||
|
||||
|
||||
@pytest.mark.parametrize("field_type", [FLOAT, FIXED32])
|
||||
def test_multi_byte_tag_fixed32_falls_back_to_the_generic_helper(
|
||||
field_type: int,
|
||||
) -> None:
|
||||
content = _encode_field(field_type, number=16)
|
||||
assert "write_tag_and_fixed32" not in content, content
|
||||
assert content.startswith("pos = ProtoEncode::encode_"), content
|
||||
|
||||
|
||||
def _decode_case(field_type: int, number: int, *, repeated: bool = False) -> str:
|
||||
"""Return the decode_field() case the generator emits for one decoded field."""
|
||||
field = _field(field_type, number, repeated=repeated)
|
||||
if field_type == MESSAGE:
|
||||
field.type_name = ".Sub"
|
||||
return create_field_type_info(
|
||||
field, needs_decode=True, needs_encode=False
|
||||
).decode_content
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("needs_decode", "force", "member"),
|
||||
[
|
||||
(False, False, "StringRef value{nullptr, 0}; // null until set, encode only"),
|
||||
(True, False, "StringRef value{};"),
|
||||
(False, True, "StringRef value{};"),
|
||||
],
|
||||
)
|
||||
def test_string_fields_default_to_null_only_when_never_read(
|
||||
needs_decode: bool, force: bool, member: str
|
||||
) -> None:
|
||||
"""Only a string that is neither decoded nor force encoded may start as a null StringRef."""
|
||||
ti = create_field_type_info(
|
||||
_field(STRING, force=force), needs_decode=needs_decode, needs_encode=True
|
||||
)
|
||||
assert ti.public_content == [member]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("field_type", "number", "wire_type", "accessor"),
|
||||
[
|
||||
(UINT32, 2, "WIRE_TYPE_VARINT", "value.as_varint()"),
|
||||
(BOOL, 3, "WIRE_TYPE_VARINT", "value.as_bool()"),
|
||||
(STRING, 1, "WIRE_TYPE_LENGTH_DELIMITED", "value.data()"),
|
||||
(FLOAT, 4, "WIRE_TYPE_FIXED32", "value.as_float()"),
|
||||
(FIXED32, 5, "WIRE_TYPE_FIXED32", "value.as_fixed32()"),
|
||||
],
|
||||
)
|
||||
def test_decode_cases_carry_field_number_and_wire_type(
|
||||
field_type: int, number: int, wire_type: str, accessor: str
|
||||
) -> None:
|
||||
"""Each decoded field yields one case keyed on its number and declared wire type."""
|
||||
case = _decode_case(field_type, number)
|
||||
lines = case.splitlines()
|
||||
assert lines[0] == f"case proto_tag({number}, {wire_type}):", case
|
||||
assert accessor in lines[1], case
|
||||
assert lines[-1].strip() == "break;", case
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("field_type", "repeated", "wire_type", "store"),
|
||||
[
|
||||
(UINT32, True, "WIRE_TYPE_VARINT", "this->value.push_back(value.as_varint());"),
|
||||
(
|
||||
STRING,
|
||||
True,
|
||||
"WIRE_TYPE_LENGTH_DELIMITED",
|
||||
"this->value.push_back(value.as_string());",
|
||||
),
|
||||
(
|
||||
MESSAGE,
|
||||
False,
|
||||
"WIRE_TYPE_LENGTH_DELIMITED",
|
||||
"value.decode_to_message(this->value);",
|
||||
),
|
||||
(
|
||||
MESSAGE,
|
||||
True,
|
||||
"WIRE_TYPE_LENGTH_DELIMITED",
|
||||
"value.decode_to_message(this->value.back());",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_repeated_and_message_fields_decode_through_the_same_case_shape(
|
||||
field_type: int, repeated: bool, wire_type: str, store: str
|
||||
) -> None:
|
||||
"""Repeated and sub message fields land in the one switch with their own store."""
|
||||
case = _decode_case(field_type, 7, repeated=repeated)
|
||||
lines = case.splitlines()
|
||||
assert lines[0] == f"case proto_tag(7, {wire_type}):", case
|
||||
assert store in case, case
|
||||
if field_type == MESSAGE and repeated:
|
||||
assert "this->value.emplace_back();" in case, case
|
||||
assert lines[-1].strip() == "break;", case
|
||||
|
||||
|
||||
def test_a_fixed64_field_fails_at_generation_time() -> None:
|
||||
"""The decode loop has no 64 bit wire type path, so such a field must never reach it silently."""
|
||||
desc = descriptor_pb2.DescriptorProto(name="Wide")
|
||||
desc.field.add(name="ratio", number=1, type=DOUBLE)
|
||||
with pytest.raises(
|
||||
ValueError, match="64-bit type 'double' .*ratio.* not supported"
|
||||
):
|
||||
build_message_type(desc, {}, {"Wide": SOURCE_CLIENT})
|
||||
|
||||
|
||||
def test_message_decodes_through_one_static_decode_field() -> None:
|
||||
"""All wire types of a decoded message land in one static decode_field() switch that the
|
||||
inline decode() hands to the shared loop."""
|
||||
desc = descriptor_pb2.DescriptorProto(name="Mixed")
|
||||
desc.field.add(name="name", number=1, type=STRING)
|
||||
desc.field.add(name="count", number=2, type=UINT32)
|
||||
desc.field.add(name="level", number=3, type=FLOAT)
|
||||
header, cpp, _ = build_message_type(desc, {}, {"Mixed": SOURCE_CLIENT})
|
||||
decl = "static void decode_field(void *self, uint32_t tag, const uint8_t *data, proto_varint_value_t scalar);"
|
||||
assert header.count(decl) == 1
|
||||
assert (
|
||||
cpp.count(
|
||||
"void Mixed::decode_field(void *self, uint32_t tag, const uint8_t *data, proto_varint_value_t scalar) {"
|
||||
)
|
||||
== 1
|
||||
)
|
||||
assert "switch (tag) {" in cpp
|
||||
assert "const ProtoFieldValue value(data, scalar);" in cpp
|
||||
for number, wire_type in (
|
||||
(1, "WIRE_TYPE_LENGTH_DELIMITED"),
|
||||
(2, "WIRE_TYPE_VARINT"),
|
||||
(3, "WIRE_TYPE_FIXED32"),
|
||||
):
|
||||
assert f"case proto_tag({number}, {wire_type}):" in cpp, cpp
|
||||
# The static body works on the cast message, never on this
|
||||
assert "auto &msg = *static_cast<Mixed *>(self);" in cpp
|
||||
assert "this->" not in cpp
|
||||
assert (
|
||||
header.count(
|
||||
"ProtoDecodableMessage::decode_fields(this, buffer, length, &decode_field);"
|
||||
)
|
||||
== 1
|
||||
)
|
||||
|
||||
|
||||
def test_fixed_vector_message_keeps_its_own_decode() -> None:
|
||||
"""A message that sizes a FixedVector first decodes through its own decode(), not the inline one."""
|
||||
desc = descriptor_pb2.DescriptorProto(name="Sized")
|
||||
field = desc.field.add(name="values", number=1, type=UINT32)
|
||||
field.label = descriptor_pb2.FieldDescriptorProto.LABEL_REPEATED
|
||||
field.options.Extensions[pb.fixed_vector] = True
|
||||
header, cpp, _ = build_message_type(desc, {}, {"Sized": SOURCE_CLIENT})
|
||||
assert (
|
||||
"ProtoDecodableMessage::decode_fields(this, buffer, length, &decode_field);"
|
||||
not in header
|
||||
)
|
||||
assert header.count("void decode(const uint8_t *buffer, size_t length);") == 1
|
||||
assert "void Sized::decode(const uint8_t *buffer, size_t length) {" in cpp
|
||||
assert "ProtoDecodableMessage::count_repeated_field(buffer, length, 1)" in cpp
|
||||
assert (
|
||||
cpp.count(
|
||||
"ProtoDecodableMessage::decode_fields(this, buffer, length, &decode_field);"
|
||||
)
|
||||
== 1
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("source", [SOURCE_CLIENT, SOURCE_SERVER, SOURCE_BOTH])
|
||||
def test_repeated_strings_generate_in_every_direction(source: int) -> None:
|
||||
"""A repeated string builds its element type from the type map, so the null
|
||||
default plumbing on the pointer string type never sees it."""
|
||||
desc = descriptor_pb2.DescriptorProto(name="Names")
|
||||
desc.field.add(
|
||||
name="names",
|
||||
number=1,
|
||||
type=STRING,
|
||||
label=descriptor_pb2.FieldDescriptorProto.LABEL_REPEATED,
|
||||
)
|
||||
header, cpp, _ = build_message_type(desc, {}, {"Names": source})
|
||||
assert "std::vector<std::string> names{};" in header
|
||||
assert "nullptr" not in header
|
||||
if source != SOURCE_SERVER:
|
||||
assert "case proto_tag(1, WIRE_TYPE_LENGTH_DELIMITED):" in cpp
|
||||
|
||||
@@ -610,12 +610,13 @@ async def _run_entry(
|
||||
config: dict[str, object],
|
||||
args: list[tuple[object, str]] | None,
|
||||
platform: str,
|
||||
id_key: str = CONF_ID,
|
||||
) -> RegistryEntry:
|
||||
"""Run a registered builder with the given config, trigger args and platform."""
|
||||
CORE.data[KEY_CORE] = {KEY_TARGET_PLATFORM: platform}
|
||||
args = args or []
|
||||
template_arg = cg.TemplateArguments(*(t for t, _ in args))
|
||||
await entry.fun({CONF_ID: PARENT_ID, **config}, ID("obj_1"), template_arg, args)
|
||||
await entry.fun({id_key: PARENT_ID, **config}, ID("obj_1"), template_arg, args)
|
||||
return entry
|
||||
|
||||
|
||||
@@ -626,11 +627,12 @@ async def _run_apply_action(
|
||||
args: list[tuple[object, str]] | None = None,
|
||||
call: str | None = None,
|
||||
platform: str = "esp32",
|
||||
id_key: str = CONF_ID,
|
||||
) -> RegistryEntry:
|
||||
"""Register an apply action and run its builder with the given config."""
|
||||
actions, _ = registries
|
||||
register_apply_action("my.apply", None, *fields, call=call)
|
||||
return await _run_entry(actions["my.apply"], config, args, platform)
|
||||
register_apply_action("my.apply", None, *fields, call=call, id_key=id_key)
|
||||
return await _run_entry(actions["my.apply"], config, args, platform, id_key)
|
||||
|
||||
|
||||
async def _run_apply_condition(
|
||||
@@ -639,11 +641,12 @@ async def _run_apply_condition(
|
||||
config: dict[str, object],
|
||||
args: list[tuple[object, str]] | None = None,
|
||||
platform: str = "esp32",
|
||||
id_key: str = CONF_ID,
|
||||
) -> RegistryEntry:
|
||||
"""Register an apply condition and run its builder with the given config."""
|
||||
_, conditions = registries
|
||||
register_apply_condition("my.check", None, check)
|
||||
return await _run_entry(conditions["my.check"], config, args, platform)
|
||||
register_apply_condition("my.check", None, check, id_key=id_key)
|
||||
return await _run_entry(conditions["my.check"], config, args, platform, id_key)
|
||||
|
||||
|
||||
def _apply_lambda(mock_cg: MockCodegen) -> str:
|
||||
@@ -663,6 +666,17 @@ async def test_register_apply_action_entry(
|
||||
assert str(template_arg) == "<int32_t>"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_apply_custom_id_key(
|
||||
registries: tuple[Registry, Registry], mock_cg: MockCodegen
|
||||
) -> None:
|
||||
await _run_apply_action(registries, (), {}, id_key="transmitter_id")
|
||||
mock_cg.get_variable.assert_awaited_once_with(PARENT_ID)
|
||||
mock_cg.get_variable.reset_mock()
|
||||
await _run_apply_condition(registries, "is_on()", {}, id_key="transmitter_id")
|
||||
mock_cg.get_variable.assert_awaited_once_with(PARENT_ID)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_apply_constants(
|
||||
registries: tuple[Registry, Registry], mock_cg: MockCodegen
|
||||
@@ -806,6 +820,10 @@ def test_apply_registration_checks(registries: tuple[Registry, Registry]) -> Non
|
||||
register_apply_condition(
|
||||
"my.bad_is", schema, ApplyCall("kd == {}", (("kd", cg.float_),))
|
||||
)
|
||||
with pytest.raises(ValueError, match="'parent_id' is not in the schema"):
|
||||
register_apply_action("my.bad_id", schema, id_key="parent_id")
|
||||
with pytest.raises(ValueError, match="'parent_id' is not in the schema"):
|
||||
register_apply_condition("my.bad_is_id", schema, "is_on()", id_key="parent_id")
|
||||
either = cv.Any(schema, cv.Schema({cv.Optional("kd"): cv.float_}))
|
||||
register_apply_action("my.any", either, ApplyField("kd", "set_kd", cg.float_))
|
||||
for wrapped in (
|
||||
@@ -818,7 +836,12 @@ def test_apply_registration_checks(registries: tuple[Registry, Registry]) -> Non
|
||||
register_apply_action(
|
||||
"my.bad", wrapped, ApplyField("kd", "set_kd", cg.float_)
|
||||
)
|
||||
nested = cv.Schema({cv.Optional("v"): cv.Schema({cv.Optional("dir"): cv.int_})})
|
||||
nested = cv.Schema(
|
||||
{
|
||||
cv.Required(CONF_ID): cv.string,
|
||||
cv.Optional("v"): cv.Schema({cv.Optional("dir"): cv.int_}),
|
||||
}
|
||||
)
|
||||
register_apply_action(
|
||||
"my.nested", nested, ApplyField(("v", "dir"), "set_dir", cg.int_)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user