[api] Auto-derive max_value for enum fields in protobuf codegen

The protobuf code generator already parses all enum definitions and
knows every enum's maximum value. Use this to automatically apply the
max_value < 128 optimization to all enum fields, eliminating the need
for manual annotation.

Since every enum in api.proto has max < 128, all 58 enum field size
calculations now use constant arithmetic instead of calling varint
size functions.

Builds on #15424 which introduced the max_value optimization.
This commit is contained in:
J. Nick Koston
2026-04-05 18:16:14 -10:00
parent 7644f17cf6
commit 0fc3664441
2 changed files with 78 additions and 58 deletions
+58 -58
View File
@@ -79,7 +79,7 @@ void SerialProxyInfo::encode(ProtoWriteBuffer &buffer) const {
uint32_t SerialProxyInfo::calculate_size() const {
uint32_t size = 0;
size += ProtoSize::calc_length(1, this->name.size());
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->port_type));
size += this->port_type ? 2 : 0;
return size;
}
#endif
@@ -232,7 +232,7 @@ uint32_t ListEntitiesBinarySensorResponse::calculate_size() const {
#ifdef USE_ENTITY_ICON
size += ProtoSize::calc_length(1, this->icon.size());
#endif
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->entity_category));
size += this->entity_category ? 2 : 0;
#ifdef USE_DEVICES
size += ProtoSize::calc_uint32(1, this->device_id);
#endif
@@ -289,7 +289,7 @@ uint32_t ListEntitiesCoverResponse::calculate_size() const {
#ifdef USE_ENTITY_ICON
size += ProtoSize::calc_length(1, this->icon.size());
#endif
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->entity_category));
size += this->entity_category ? 2 : 0;
size += ProtoSize::calc_bool(1, this->supports_stop);
#ifdef USE_DEVICES
size += ProtoSize::calc_uint32(1, this->device_id);
@@ -310,7 +310,7 @@ uint32_t CoverStateResponse::calculate_size() const {
size += 5;
size += ProtoSize::calc_float(1, this->position);
size += ProtoSize::calc_float(1, this->tilt);
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->current_operation));
size += this->current_operation ? 2 : 0;
#ifdef USE_DEVICES
size += ProtoSize::calc_uint32(1, this->device_id);
#endif
@@ -388,7 +388,7 @@ uint32_t ListEntitiesFanResponse::calculate_size() const {
#ifdef USE_ENTITY_ICON
size += ProtoSize::calc_length(1, this->icon.size());
#endif
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->entity_category));
size += this->entity_category ? 2 : 0;
if (!this->supported_preset_modes->empty()) {
for (const char *it : *this->supported_preset_modes) {
size += ProtoSize::calc_length_force(1, strlen(it));
@@ -415,7 +415,7 @@ uint32_t FanStateResponse::calculate_size() const {
size += 5;
size += ProtoSize::calc_bool(1, this->state);
size += ProtoSize::calc_bool(1, this->oscillating);
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->direction));
size += this->direction ? 2 : 0;
size += ProtoSize::calc_int32(1, this->speed_level);
size += ProtoSize::calc_length(1, this->preset_mode.size());
#ifdef USE_DEVICES
@@ -513,7 +513,7 @@ uint32_t ListEntitiesLightResponse::calculate_size() const {
size += ProtoSize::calc_length(1, this->name.size());
if (!this->supported_color_modes->empty()) {
for (const auto &it : *this->supported_color_modes) {
size += ProtoSize::calc_uint32_force(1, static_cast<uint32_t>(it));
size += 2;
}
}
size += ProtoSize::calc_float(1, this->min_mireds);
@@ -527,7 +527,7 @@ uint32_t ListEntitiesLightResponse::calculate_size() const {
#ifdef USE_ENTITY_ICON
size += ProtoSize::calc_length(1, this->icon.size());
#endif
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->entity_category));
size += this->entity_category ? 2 : 0;
#ifdef USE_DEVICES
size += ProtoSize::calc_uint32(2, this->device_id);
#endif
@@ -556,7 +556,7 @@ uint32_t LightStateResponse::calculate_size() const {
size += 5;
size += ProtoSize::calc_bool(1, this->state);
size += ProtoSize::calc_float(1, this->brightness);
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->color_mode));
size += this->color_mode ? 2 : 0;
size += ProtoSize::calc_float(1, this->color_brightness);
size += ProtoSize::calc_float(1, this->red);
size += ProtoSize::calc_float(1, this->green);
@@ -711,9 +711,9 @@ uint32_t ListEntitiesSensorResponse::calculate_size() const {
size += ProtoSize::calc_int32(1, this->accuracy_decimals);
size += ProtoSize::calc_bool(1, this->force_update);
size += ProtoSize::calc_length(1, this->device_class.size());
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->state_class));
size += this->state_class ? 2 : 0;
size += ProtoSize::calc_bool(1, this->disabled_by_default);
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->entity_category));
size += this->entity_category ? 2 : 0;
#ifdef USE_DEVICES
size += ProtoSize::calc_uint32(1, this->device_id);
#endif
@@ -764,7 +764,7 @@ uint32_t ListEntitiesSwitchResponse::calculate_size() const {
#endif
size += ProtoSize::calc_bool(1, this->assumed_state);
size += ProtoSize::calc_bool(1, this->disabled_by_default);
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->entity_category));
size += this->entity_category ? 2 : 0;
size += ProtoSize::calc_length(1, this->device_class.size());
#ifdef USE_DEVICES
size += ProtoSize::calc_uint32(1, this->device_id);
@@ -837,7 +837,7 @@ uint32_t ListEntitiesTextSensorResponse::calculate_size() const {
size += ProtoSize::calc_length(1, this->icon.size());
#endif
size += ProtoSize::calc_bool(1, this->disabled_by_default);
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->entity_category));
size += this->entity_category ? 2 : 0;
size += ProtoSize::calc_length(1, this->device_class.size());
#ifdef USE_DEVICES
size += ProtoSize::calc_uint32(1, this->device_id);
@@ -882,7 +882,7 @@ void SubscribeLogsResponse::encode(ProtoWriteBuffer &buffer) const {
}
uint32_t SubscribeLogsResponse::calculate_size() const {
uint32_t size = 0;
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->level));
size += this->level ? 2 : 0;
size += ProtoSize::calc_length(1, this->message_len_);
return size;
}
@@ -1119,7 +1119,7 @@ void ListEntitiesServicesArgument::encode(ProtoWriteBuffer &buffer) const {
uint32_t ListEntitiesServicesArgument::calculate_size() const {
uint32_t size = 0;
size += ProtoSize::calc_length(1, this->name.size());
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->type));
size += this->type ? 2 : 0;
return size;
}
void ListEntitiesServicesResponse::encode(ProtoWriteBuffer &buffer) const {
@@ -1139,7 +1139,7 @@ uint32_t ListEntitiesServicesResponse::calculate_size() const {
size += ProtoSize::calc_message_force(1, it.calculate_size());
}
}
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->supports_response));
size += this->supports_response ? 2 : 0;
return size;
}
bool ExecuteServiceArgument::decode_varint(uint32_t field_id, proto_varint_value_t value) {
@@ -1289,7 +1289,7 @@ uint32_t ListEntitiesCameraResponse::calculate_size() const {
#ifdef USE_ENTITY_ICON
size += ProtoSize::calc_length(1, this->icon.size());
#endif
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->entity_category));
size += this->entity_category ? 2 : 0;
#ifdef USE_DEVICES
size += ProtoSize::calc_uint32(1, this->device_id);
#endif
@@ -1380,7 +1380,7 @@ uint32_t ListEntitiesClimateResponse::calculate_size() const {
size += ProtoSize::calc_bool(1, this->supports_two_point_target_temperature);
if (!this->supported_modes->empty()) {
for (const auto &it : *this->supported_modes) {
size += ProtoSize::calc_uint32_force(1, static_cast<uint32_t>(it));
size += 2;
}
}
size += ProtoSize::calc_float(1, this->visual_min_temperature);
@@ -1389,12 +1389,12 @@ uint32_t ListEntitiesClimateResponse::calculate_size() const {
size += ProtoSize::calc_bool(1, this->supports_action);
if (!this->supported_fan_modes->empty()) {
for (const auto &it : *this->supported_fan_modes) {
size += ProtoSize::calc_uint32_force(1, static_cast<uint32_t>(it));
size += 2;
}
}
if (!this->supported_swing_modes->empty()) {
for (const auto &it : *this->supported_swing_modes) {
size += ProtoSize::calc_uint32_force(1, static_cast<uint32_t>(it));
size += 2;
}
}
if (!this->supported_custom_fan_modes->empty()) {
@@ -1404,7 +1404,7 @@ uint32_t ListEntitiesClimateResponse::calculate_size() const {
}
if (!this->supported_presets->empty()) {
for (const auto &it : *this->supported_presets) {
size += ProtoSize::calc_uint32_force(2, static_cast<uint32_t>(it));
size += 3;
}
}
if (!this->supported_custom_presets->empty()) {
@@ -1416,7 +1416,7 @@ uint32_t ListEntitiesClimateResponse::calculate_size() const {
#ifdef USE_ENTITY_ICON
size += ProtoSize::calc_length(2, this->icon.size());
#endif
size += ProtoSize::calc_uint32(2, static_cast<uint32_t>(this->entity_category));
size += this->entity_category ? 3 : 0;
size += ProtoSize::calc_float(2, this->visual_current_temperature_step);
size += ProtoSize::calc_bool(2, this->supports_current_humidity);
size += ProtoSize::calc_bool(2, this->supports_target_humidity);
@@ -1450,16 +1450,16 @@ void ClimateStateResponse::encode(ProtoWriteBuffer &buffer) const {
uint32_t ClimateStateResponse::calculate_size() const {
uint32_t size = 0;
size += 5;
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->mode));
size += this->mode ? 2 : 0;
size += ProtoSize::calc_float(1, this->current_temperature);
size += ProtoSize::calc_float(1, this->target_temperature);
size += ProtoSize::calc_float(1, this->target_temperature_low);
size += ProtoSize::calc_float(1, this->target_temperature_high);
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->action));
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->fan_mode));
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->swing_mode));
size += this->action ? 2 : 0;
size += this->fan_mode ? 2 : 0;
size += this->swing_mode ? 2 : 0;
size += ProtoSize::calc_length(1, this->custom_fan_mode.size());
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->preset));
size += this->preset ? 2 : 0;
size += ProtoSize::calc_length(1, this->custom_preset.size());
size += ProtoSize::calc_float(1, this->current_humidity);
size += ProtoSize::calc_float(1, this->target_humidity);
@@ -1590,7 +1590,7 @@ uint32_t ListEntitiesWaterHeaterResponse::calculate_size() const {
size += ProtoSize::calc_length(1, this->icon.size());
#endif
size += ProtoSize::calc_bool(1, this->disabled_by_default);
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->entity_category));
size += this->entity_category ? 2 : 0;
#ifdef USE_DEVICES
size += ProtoSize::calc_uint32(1, this->device_id);
#endif
@@ -1599,7 +1599,7 @@ uint32_t ListEntitiesWaterHeaterResponse::calculate_size() const {
size += ProtoSize::calc_float(1, this->target_temperature_step);
if (!this->supported_modes->empty()) {
for (const auto &it : *this->supported_modes) {
size += ProtoSize::calc_uint32_force(1, static_cast<uint32_t>(it));
size += 2;
}
}
size += ProtoSize::calc_uint32(1, this->supported_features);
@@ -1622,7 +1622,7 @@ uint32_t WaterHeaterStateResponse::calculate_size() const {
size += 5;
size += ProtoSize::calc_float(1, this->current_temperature);
size += ProtoSize::calc_float(1, this->target_temperature);
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->mode));
size += this->mode ? 2 : 0;
#ifdef USE_DEVICES
size += ProtoSize::calc_uint32(1, this->device_id);
#endif
@@ -1704,9 +1704,9 @@ uint32_t ListEntitiesNumberResponse::calculate_size() const {
size += ProtoSize::calc_float(1, this->max_value);
size += ProtoSize::calc_float(1, this->step);
size += ProtoSize::calc_bool(1, this->disabled_by_default);
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->entity_category));
size += this->entity_category ? 2 : 0;
size += ProtoSize::calc_length(1, this->unit_of_measurement.size());
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->mode));
size += this->mode ? 2 : 0;
size += ProtoSize::calc_length(1, this->device_class.size());
#ifdef USE_DEVICES
size += ProtoSize::calc_uint32(1, this->device_id);
@@ -1788,7 +1788,7 @@ uint32_t ListEntitiesSelectResponse::calculate_size() const {
}
}
size += ProtoSize::calc_bool(1, this->disabled_by_default);
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->entity_category));
size += this->entity_category ? 2 : 0;
#ifdef USE_DEVICES
size += ProtoSize::calc_uint32(1, this->device_id);
#endif
@@ -1881,7 +1881,7 @@ uint32_t ListEntitiesSirenResponse::calculate_size() const {
}
size += ProtoSize::calc_bool(1, this->supports_duration);
size += ProtoSize::calc_bool(1, this->supports_volume);
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->entity_category));
size += this->entity_category ? 2 : 0;
#ifdef USE_DEVICES
size += ProtoSize::calc_uint32(1, this->device_id);
#endif
@@ -1985,7 +1985,7 @@ uint32_t ListEntitiesLockResponse::calculate_size() const {
size += ProtoSize::calc_length(1, this->icon.size());
#endif
size += ProtoSize::calc_bool(1, this->disabled_by_default);
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->entity_category));
size += this->entity_category ? 2 : 0;
size += ProtoSize::calc_bool(1, this->assumed_state);
size += ProtoSize::calc_bool(1, this->supports_open);
size += ProtoSize::calc_bool(1, this->requires_code);
@@ -2005,7 +2005,7 @@ void LockStateResponse::encode(ProtoWriteBuffer &buffer) const {
uint32_t LockStateResponse::calculate_size() const {
uint32_t size = 0;
size += 5;
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->state));
size += this->state ? 2 : 0;
#ifdef USE_DEVICES
size += ProtoSize::calc_uint32(1, this->device_id);
#endif
@@ -2075,7 +2075,7 @@ uint32_t ListEntitiesButtonResponse::calculate_size() const {
size += ProtoSize::calc_length(1, this->icon.size());
#endif
size += ProtoSize::calc_bool(1, this->disabled_by_default);
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->entity_category));
size += this->entity_category ? 2 : 0;
size += ProtoSize::calc_length(1, this->device_class.size());
#ifdef USE_DEVICES
size += ProtoSize::calc_uint32(1, this->device_id);
@@ -2118,7 +2118,7 @@ uint32_t MediaPlayerSupportedFormat::calculate_size() const {
size += ProtoSize::calc_length(1, this->format.size());
size += ProtoSize::calc_uint32(1, this->sample_rate);
size += ProtoSize::calc_uint32(1, this->num_channels);
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->purpose));
size += this->purpose ? 2 : 0;
size += ProtoSize::calc_uint32(1, this->sample_bytes);
return size;
}
@@ -2149,7 +2149,7 @@ uint32_t ListEntitiesMediaPlayerResponse::calculate_size() const {
size += ProtoSize::calc_length(1, this->icon.size());
#endif
size += ProtoSize::calc_bool(1, this->disabled_by_default);
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->entity_category));
size += this->entity_category ? 2 : 0;
size += ProtoSize::calc_bool(1, this->supports_pause);
if (!this->supported_formats.empty()) {
for (const auto &it : this->supported_formats) {
@@ -2174,7 +2174,7 @@ void MediaPlayerStateResponse::encode(ProtoWriteBuffer &buffer) const {
uint32_t MediaPlayerStateResponse::calculate_size() const {
uint32_t size = 0;
size += 5;
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->state));
size += this->state ? 2 : 0;
size += ProtoSize::calc_float(1, this->volume);
size += ProtoSize::calc_bool(1, this->muted);
#ifdef USE_DEVICES
@@ -2631,9 +2631,9 @@ void BluetoothScannerStateResponse::encode(ProtoWriteBuffer &buffer) const {
}
uint32_t BluetoothScannerStateResponse::calculate_size() const {
uint32_t size = 0;
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->state));
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->mode));
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->configured_mode));
size += this->state ? 2 : 0;
size += this->mode ? 2 : 0;
size += this->configured_mode ? 2 : 0;
return size;
}
bool BluetoothScannerSetModeRequest::decode_varint(uint32_t field_id, proto_varint_value_t value) {
@@ -2969,7 +2969,7 @@ uint32_t ListEntitiesAlarmControlPanelResponse::calculate_size() const {
size += ProtoSize::calc_length(1, this->icon.size());
#endif
size += ProtoSize::calc_bool(1, this->disabled_by_default);
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->entity_category));
size += this->entity_category ? 2 : 0;
size += ProtoSize::calc_uint32(1, this->supported_features);
size += ProtoSize::calc_bool(1, this->requires_code);
size += ProtoSize::calc_bool(1, this->requires_code_to_arm);
@@ -2988,7 +2988,7 @@ void AlarmControlPanelStateResponse::encode(ProtoWriteBuffer &buffer) const {
uint32_t AlarmControlPanelStateResponse::calculate_size() const {
uint32_t size = 0;
size += 5;
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->state));
size += this->state ? 2 : 0;
#ifdef USE_DEVICES
size += ProtoSize::calc_uint32(1, this->device_id);
#endif
@@ -3058,11 +3058,11 @@ uint32_t ListEntitiesTextResponse::calculate_size() const {
size += ProtoSize::calc_length(1, this->icon.size());
#endif
size += ProtoSize::calc_bool(1, this->disabled_by_default);
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->entity_category));
size += this->entity_category ? 2 : 0;
size += ProtoSize::calc_uint32(1, this->min_length);
size += ProtoSize::calc_uint32(1, this->max_length);
size += ProtoSize::calc_length(1, this->pattern.size());
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->mode));
size += this->mode ? 2 : 0;
#ifdef USE_DEVICES
size += ProtoSize::calc_uint32(1, this->device_id);
#endif
@@ -3143,7 +3143,7 @@ uint32_t ListEntitiesDateResponse::calculate_size() const {
size += ProtoSize::calc_length(1, this->icon.size());
#endif
size += ProtoSize::calc_bool(1, this->disabled_by_default);
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->entity_category));
size += this->entity_category ? 2 : 0;
#ifdef USE_DEVICES
size += ProtoSize::calc_uint32(1, this->device_id);
#endif
@@ -3226,7 +3226,7 @@ uint32_t ListEntitiesTimeResponse::calculate_size() const {
size += ProtoSize::calc_length(1, this->icon.size());
#endif
size += ProtoSize::calc_bool(1, this->disabled_by_default);
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->entity_category));
size += this->entity_category ? 2 : 0;
#ifdef USE_DEVICES
size += ProtoSize::calc_uint32(1, this->device_id);
#endif
@@ -3313,7 +3313,7 @@ uint32_t ListEntitiesEventResponse::calculate_size() const {
size += ProtoSize::calc_length(1, this->icon.size());
#endif
size += ProtoSize::calc_bool(1, this->disabled_by_default);
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->entity_category));
size += this->entity_category ? 2 : 0;
size += ProtoSize::calc_length(1, this->device_class.size());
if (!this->event_types->empty()) {
for (const char *it : *this->event_types) {
@@ -3369,7 +3369,7 @@ uint32_t ListEntitiesValveResponse::calculate_size() const {
size += ProtoSize::calc_length(1, this->icon.size());
#endif
size += ProtoSize::calc_bool(1, this->disabled_by_default);
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->entity_category));
size += this->entity_category ? 2 : 0;
size += ProtoSize::calc_length(1, this->device_class.size());
size += ProtoSize::calc_bool(1, this->assumed_state);
size += ProtoSize::calc_bool(1, this->supports_position);
@@ -3391,7 +3391,7 @@ uint32_t ValveStateResponse::calculate_size() const {
uint32_t size = 0;
size += 5;
size += ProtoSize::calc_float(1, this->position);
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->current_operation));
size += this->current_operation ? 2 : 0;
#ifdef USE_DEVICES
size += ProtoSize::calc_uint32(1, this->device_id);
#endif
@@ -3452,7 +3452,7 @@ uint32_t ListEntitiesDateTimeResponse::calculate_size() const {
size += ProtoSize::calc_length(1, this->icon.size());
#endif
size += ProtoSize::calc_bool(1, this->disabled_by_default);
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->entity_category));
size += this->entity_category ? 2 : 0;
#ifdef USE_DEVICES
size += ProtoSize::calc_uint32(1, this->device_id);
#endif
@@ -3526,7 +3526,7 @@ uint32_t ListEntitiesUpdateResponse::calculate_size() const {
size += ProtoSize::calc_length(1, this->icon.size());
#endif
size += ProtoSize::calc_bool(1, this->disabled_by_default);
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->entity_category));
size += this->entity_category ? 2 : 0;
size += ProtoSize::calc_length(1, this->device_class.size());
#ifdef USE_DEVICES
size += ProtoSize::calc_uint32(1, this->device_id);
@@ -3638,7 +3638,7 @@ void ZWaveProxyRequest::encode(ProtoWriteBuffer &buffer) const {
}
uint32_t ZWaveProxyRequest::calculate_size() const {
uint32_t size = 0;
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->type));
size += this->type ? 2 : 0;
size += ProtoSize::calc_length(1, this->data_len);
return size;
}
@@ -3668,7 +3668,7 @@ uint32_t ListEntitiesInfraredResponse::calculate_size() const {
size += ProtoSize::calc_length(1, this->icon.size());
#endif
size += ProtoSize::calc_bool(1, this->disabled_by_default);
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->entity_category));
size += this->entity_category ? 2 : 0;
#ifdef USE_DEVICES
size += ProtoSize::calc_uint32(1, this->device_id);
#endif
@@ -3855,8 +3855,8 @@ void SerialProxyRequestResponse::encode(ProtoWriteBuffer &buffer) const {
uint32_t SerialProxyRequestResponse::calculate_size() const {
uint32_t size = 0;
size += ProtoSize::calc_uint32(1, this->instance);
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->type));
size += ProtoSize::calc_uint32(1, static_cast<uint32_t>(this->status));
size += this->type ? 2 : 0;
size += this->status ? 2 : 0;
size += ProtoSize::calc_length(1, this->error_message.size());
return size;
}
+20
View File
@@ -56,6 +56,10 @@ FILE_HEADER = """// This file was automatically generated with a tool.
// See script/api_protobuf/api_protobuf.py
"""
# Populated by main() before any TypeInfo creation.
# Maps enum type name (e.g. ".BluetoothDeviceRequestType") to max enum value.
_enum_max_values: dict[str, int] = {}
def indent_list(text: str, padding: str = " ") -> list[str]:
"""Indent each line of the given text with the specified padding."""
@@ -1312,6 +1316,14 @@ class EnumType(TypeInfo):
default_value = ""
wire_type = WireType.VARINT # Uses wire type 0
@property
def max_value(self) -> int | None:
"""Get max_value from explicit annotation or auto-derive from enum definition."""
explicit = super().max_value
if explicit is not None:
return explicit
return _enum_max_values.get(self._field.type_name)
@property
def encode_func(self) -> str:
return "encode_uint32"
@@ -1334,6 +1346,9 @@ class EnumType(TypeInfo):
return f"static_cast<{self.cpp_type}>({value})"
def get_size_calculation(self, name: str, force: bool = False) -> str:
max_val = self.max_value
if max_val is not None and max_val < 128:
return self._get_single_byte_varint_size(name, force)
return self._get_simple_size_calculation(
name, force, "uint32", f"static_cast<uint32_t>({name})"
)
@@ -2771,6 +2786,11 @@ def main() -> None:
file = d.file[0]
# Build enum max value map so EnumType can auto-derive max_value
for enum in file.enum_type:
if not enum.options.deprecated and enum.value:
_enum_max_values[f".{enum.name}"] = max(v.number for v in enum.value)
# Build dynamic ifdef mappings early so we can emit USE_API_VARINT64 before includes
enum_ifdef_map, message_ifdef_map, message_source_map, used_messages = (
build_type_usage_map(file)