I thought this is the function to set up entity_id, but after restarting HA, the entity_id of entities created by my custom components is different from the ones return by this function. why this happens? Thanks.
Visible entity ids are created by names and users are allowed to modify it.
What you are referring is internal id of the entities and they are not modifiable. Unique ids are stored in internal files of home assistant (core.entity_registry)
If you set a name property then that will get used as entity_id, or else an entity_id property (which you will want to have passed through generate_entity_id from homeassistant.helpers.entity to make sure it s unique).