mirror of
https://github.com/esphome/esphome.git
synced 2025-07-29 14:46:40 +00:00
[api] Fix missing ifdef guards for field_ifdef fields in protobuf base classes (#9693)
This commit is contained in:
parent
727e8ca376
commit
5b3d61b4a6
@ -301,8 +301,10 @@ class APIConnection : public APIServerConnection {
|
|||||||
if (entity->has_own_name())
|
if (entity->has_own_name())
|
||||||
msg.name = entity->get_name();
|
msg.name = entity->get_name();
|
||||||
|
|
||||||
// Set common EntityBase properties
|
// Set common EntityBase properties
|
||||||
|
#ifdef USE_ENTITY_ICON
|
||||||
msg.icon = entity->get_icon();
|
msg.icon = entity->get_icon();
|
||||||
|
#endif
|
||||||
msg.disabled_by_default = entity->is_disabled_by_default();
|
msg.disabled_by_default = entity->is_disabled_by_default();
|
||||||
msg.entity_category = static_cast<enums::EntityCategory>(entity->get_entity_category());
|
msg.entity_category = static_cast<enums::EntityCategory>(entity->get_entity_category());
|
||||||
#ifdef USE_DEVICES
|
#ifdef USE_DEVICES
|
||||||
|
@ -292,9 +292,13 @@ class InfoResponseProtoMessage : public ProtoMessage {
|
|||||||
uint32_t key{0};
|
uint32_t key{0};
|
||||||
std::string name{};
|
std::string name{};
|
||||||
bool disabled_by_default{false};
|
bool disabled_by_default{false};
|
||||||
|
#ifdef USE_ENTITY_ICON
|
||||||
std::string icon{};
|
std::string icon{};
|
||||||
|
#endif
|
||||||
enums::EntityCategory entity_category{};
|
enums::EntityCategory entity_category{};
|
||||||
|
#ifdef USE_DEVICES
|
||||||
uint32_t device_id{0};
|
uint32_t device_id{0};
|
||||||
|
#endif
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
};
|
};
|
||||||
@ -303,7 +307,9 @@ class StateResponseProtoMessage : public ProtoMessage {
|
|||||||
public:
|
public:
|
||||||
~StateResponseProtoMessage() override = default;
|
~StateResponseProtoMessage() override = default;
|
||||||
uint32_t key{0};
|
uint32_t key{0};
|
||||||
|
#ifdef USE_DEVICES
|
||||||
uint32_t device_id{0};
|
uint32_t device_id{0};
|
||||||
|
#endif
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
};
|
};
|
||||||
@ -312,7 +318,9 @@ class CommandProtoMessage : public ProtoDecodableMessage {
|
|||||||
public:
|
public:
|
||||||
~CommandProtoMessage() override = default;
|
~CommandProtoMessage() override = default;
|
||||||
uint32_t key{0};
|
uint32_t key{0};
|
||||||
|
#ifdef USE_DEVICES
|
||||||
uint32_t device_id{0};
|
uint32_t device_id{0};
|
||||||
|
#endif
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
};
|
};
|
||||||
|
@ -1491,6 +1491,28 @@ def find_common_fields(
|
|||||||
return common_fields
|
return common_fields
|
||||||
|
|
||||||
|
|
||||||
|
def get_common_field_ifdef(
|
||||||
|
field_name: str, messages: list[descriptor.DescriptorProto]
|
||||||
|
) -> str | None:
|
||||||
|
"""Get the field_ifdef option if it's consistent across all messages.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
field_name: Name of the field to check
|
||||||
|
messages: List of messages that contain this field
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The field_ifdef string if all messages have the same value, None otherwise
|
||||||
|
"""
|
||||||
|
field_ifdefs = {
|
||||||
|
get_field_opt(field, pb.field_ifdef)
|
||||||
|
for msg in messages
|
||||||
|
if (field := next((f for f in msg.field if f.name == field_name), None))
|
||||||
|
}
|
||||||
|
|
||||||
|
# Return the ifdef only if all messages agree on the same value
|
||||||
|
return field_ifdefs.pop() if len(field_ifdefs) == 1 else None
|
||||||
|
|
||||||
|
|
||||||
def build_base_class(
|
def build_base_class(
|
||||||
base_class_name: str,
|
base_class_name: str,
|
||||||
common_fields: list[descriptor.FieldDescriptorProto],
|
common_fields: list[descriptor.FieldDescriptorProto],
|
||||||
@ -1506,9 +1528,14 @@ def build_base_class(
|
|||||||
for field in common_fields:
|
for field in common_fields:
|
||||||
ti = create_field_type_info(field)
|
ti = create_field_type_info(field)
|
||||||
|
|
||||||
|
# Get field_ifdef if it's consistent across all messages
|
||||||
|
field_ifdef = get_common_field_ifdef(field.name, messages)
|
||||||
|
|
||||||
# Only add field declarations, not encode/decode logic
|
# Only add field declarations, not encode/decode logic
|
||||||
protected_content.extend(ti.protected_content)
|
if ti.protected_content:
|
||||||
public_content.extend(ti.public_content)
|
protected_content.extend(wrap_with_ifdef(ti.protected_content, field_ifdef))
|
||||||
|
if ti.public_content:
|
||||||
|
public_content.extend(wrap_with_ifdef(ti.public_content, field_ifdef))
|
||||||
|
|
||||||
# Determine if any message using this base class needs decoding
|
# Determine if any message using this base class needs decoding
|
||||||
needs_decode = any(
|
needs_decode = any(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user