1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-22 19:53:46 +01:00

no more magic 3

This commit is contained in:
J. Nick Koston
2025-10-15 15:47:26 -10:00
parent 5e5620fb49
commit b39976ce35

View File

@@ -11,6 +11,7 @@ from typing import Any
import aioesphomeapi.api_options_pb2 as pb import aioesphomeapi.api_options_pb2 as pb
import google.protobuf.descriptor_pb2 as descriptor import google.protobuf.descriptor_pb2 as descriptor
from google.protobuf.descriptor_pb2 import FieldDescriptorProto
class WireType(IntEnum): class WireType(IntEnum):
@@ -148,7 +149,7 @@ class TypeInfo(ABC):
@property @property
def repeated(self) -> bool: def repeated(self) -> bool:
"""Check if the field is repeated.""" """Check if the field is repeated."""
return self._field.label == 3 return self._field.label == FieldDescriptorProto.LABEL_REPEATED
@property @property
def wire_type(self) -> WireType: def wire_type(self) -> WireType:
@@ -337,7 +338,7 @@ def create_field_type_info(
needs_encode: bool = True, needs_encode: bool = True,
) -> TypeInfo: ) -> TypeInfo:
"""Create the appropriate TypeInfo instance for a field, handling repeated fields and custom options.""" """Create the appropriate TypeInfo instance for a field, handling repeated fields and custom options."""
if field.label == 3: # repeated if field.label == FieldDescriptorProto.LABEL_REPEATED:
# Check if this repeated field has fixed_array_with_length_define option # Check if this repeated field has fixed_array_with_length_define option
if ( if (
fixed_size := get_field_opt(field, pb.fixed_array_with_length_define) fixed_size := get_field_opt(field, pb.fixed_array_with_length_define)
@@ -1890,7 +1891,7 @@ def build_message_type(
# Validate that fixed_array_size is only used in encode-only messages # Validate that fixed_array_size is only used in encode-only messages
if ( if (
needs_decode needs_decode
and field.label == 3 and field.label == FieldDescriptorProto.LABEL_REPEATED
and get_field_opt(field, pb.fixed_array_size) is not None and get_field_opt(field, pb.fixed_array_size) is not None
): ):
raise ValueError( raise ValueError(
@@ -1903,7 +1904,7 @@ def build_message_type(
# Validate that fixed_array_with_length_define is only used in encode-only messages # Validate that fixed_array_with_length_define is only used in encode-only messages
if ( if (
needs_decode needs_decode
and field.label == 3 and field.label == FieldDescriptorProto.LABEL_REPEATED
and get_field_opt(field, pb.fixed_array_with_length_define) is not None and get_field_opt(field, pb.fixed_array_with_length_define) is not None
): ):
raise ValueError( raise ValueError(
@@ -1916,7 +1917,7 @@ def build_message_type(
# Collect fixed_vector repeated fields for custom decode generation # Collect fixed_vector repeated fields for custom decode generation
if ( if (
needs_decode needs_decode
and field.label == 3 and field.label == FieldDescriptorProto.LABEL_REPEATED
and get_field_opt(field, pb.fixed_vector, False) and get_field_opt(field, pb.fixed_vector, False)
): ):
fixed_vector_fields.append((field.name, field.number)) fixed_vector_fields.append((field.name, field.number))